Rev 1 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1 | Rev 15 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <test.h> |
29 | #include <test.h> |
30 | #include <arch.h> |
30 | #include <arch.h> |
31 | #include <arch/atomic.h> |
31 | #include <arch/atomic.h> |
32 | #include <print.h> |
32 | #include <print.h> |
33 | #include <proc/thread.h> |
33 | #include <proc/thread.h> |
34 | #include <arch/types.h> |
34 | #include <arch/types.h> |
35 | #include <arch/context.h> |
35 | #include <arch/context.h> |
36 | 36 | ||
37 | #include <synch/waitq.h> |
37 | #include <synch/waitq.h> |
38 | #include <synch/semaphore.h> |
38 | #include <synch/semaphore.h> |
39 | #include <synch/synch.h> |
39 | #include <synch/synch.h> |
40 | 40 | ||
41 | static semaphore_t sem; |
41 | static semaphore_t sem; |
42 | 42 | ||
43 | static spinlock_t lock; |
43 | static spinlock_t lock; |
44 | 44 | ||
45 | static waitq_t can_start; |
45 | static waitq_t can_start; |
46 | 46 | ||
47 | __u32 seed = 0xdeadbeaf; |
47 | __u32 seed = 0xdeadbeaf; |
48 | 48 | ||
49 | static __u32 random(__u32 max); |
49 | static __u32 random(__u32 max); |
50 | 50 | ||
51 | static void consumer(void *arg); |
51 | static void consumer(void *arg); |
52 | static void failed(void); |
52 | static void failed(void); |
53 | 53 | ||
54 | __u32 random(__u32 max) |
54 | __u32 random(__u32 max) |
55 | { |
55 | { |
56 | __u32 rc; |
56 | __u32 rc; |
57 | 57 | ||
58 | spinlock_lock(&lock); |
58 | spinlock_lock(&lock); |
59 | rc = seed % max; |
59 | rc = seed % max; |
60 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
60 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
61 | spinlock_unlock(&lock); |
61 | spinlock_unlock(&lock); |
62 | return rc; |
62 | return rc; |
63 | } |
63 | } |
64 | 64 | ||
65 | 65 | ||
66 | void consumer(void *arg) |
66 | void consumer(void *arg) |
67 | { |
67 | { |
68 | int rc, to; |
68 | int rc, to; |
69 | waitq_sleep(&can_start); |
69 | waitq_sleep(&can_start); |
70 | 70 | ||
71 | to = random(20000); |
71 | to = random(20000); |
72 | printf("cpu%d, tid %d down+ (%d)\n", the->cpu->id, the->thread->tid, to); |
72 | printf("cpu%d, tid %d down+ (%d)\n", CPU->id, THREAD->tid, to); |
73 | rc = semaphore_down_timeout(&sem, to); |
73 | rc = semaphore_down_timeout(&sem, to); |
74 | if (SYNCH_FAILED(rc)) { |
74 | if (SYNCH_FAILED(rc)) { |
75 | printf("cpu%d, tid %d down!\n", the->cpu->id, the->thread->tid); |
75 | printf("cpu%d, tid %d down!\n", CPU->id, THREAD->tid); |
76 | return; |
76 | return; |
77 | } |
77 | } |
78 | 78 | ||
79 | printf("cpu%d, tid %d down=\n", the->cpu->id, the->thread->tid); |
79 | printf("cpu%d, tid %d down=\n", CPU->id, THREAD->tid); |
80 | thread_usleep(random(30000)); |
80 | thread_usleep(random(30000)); |
81 | 81 | ||
82 | semaphore_up(&sem); |
82 | semaphore_up(&sem); |
83 | printf("cpu%d, tid %d up\n", the->cpu->id, the->thread->tid); |
83 | printf("cpu%d, tid %d up\n", CPU->id, THREAD->tid); |
84 | } |
84 | } |
85 | 85 | ||
86 | void failed(void) |
86 | void failed(void) |
87 | { |
87 | { |
88 | printf("Test failed prematurely.\n"); |
88 | printf("Test failed prematurely.\n"); |
89 | thread_exit(); |
89 | thread_exit(); |
90 | } |
90 | } |
91 | 91 | ||
92 | void test(void) |
92 | void test(void) |
93 | { |
93 | { |
94 | context_t ctx; |
94 | context_t ctx; |
95 | __u32 i, k; |
95 | __u32 i, k; |
96 | 96 | ||
97 | printf("Semaphore test #2\n"); |
97 | printf("Semaphore test #2\n"); |
98 | 98 | ||
99 | waitq_initialize(&can_start); |
99 | waitq_initialize(&can_start); |
100 | semaphore_initialize(&sem, 5); |
100 | semaphore_initialize(&sem, 5); |
101 | 101 | ||
102 | 102 | ||
103 | 103 | ||
104 | for (; ;) { |
104 | for (; ;) { |
105 | thread_t *thrd; |
105 | thread_t *thrd; |
106 | 106 | ||
107 | k = random(7) + 1; |
107 | k = random(7) + 1; |
108 | printf("Creating %d consumers\n", k); |
108 | printf("Creating %d consumers\n", k); |
109 | for (i=0; i<k; i++) { |
109 | for (i=0; i<k; i++) { |
110 | thrd = thread_create(consumer, NULL, the->task, 0); |
110 | thrd = thread_create(consumer, NULL, TASK, 0); |
111 | if (thrd) |
111 | if (thrd) |
112 | thread_ready(thrd); |
112 | thread_ready(thrd); |
113 | else |
113 | else |
114 | failed(); |
114 | failed(); |
115 | } |
115 | } |
116 | 116 | ||
117 | thread_usleep(20000); |
117 | thread_usleep(20000); |
118 | waitq_wakeup(&can_start, WAKEUP_ALL); |
118 | waitq_wakeup(&can_start, WAKEUP_ALL); |
119 | } |
119 | } |
120 | 120 | ||
121 | } |
121 | } |
122 | 122 |