Rev 1104 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1104 | Rev 1658 | ||
---|---|---|---|
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 <atomic.h> |
31 | #include <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 | #include <synch/spinlock.h> |
40 | #include <synch/spinlock.h> |
41 | 41 | ||
42 | static semaphore_t sem; |
42 | static semaphore_t sem; |
43 | 43 | ||
44 | SPINLOCK_INITIALIZE(lock); |
44 | SPINLOCK_INITIALIZE(lock); |
45 | 45 | ||
46 | static waitq_t can_start; |
46 | static waitq_t can_start; |
47 | 47 | ||
48 | __u32 seed = 0xdeadbeef; |
48 | __u32 seed = 0xdeadbeef; |
49 | 49 | ||
50 | static __u32 random(__u32 max); |
50 | static __u32 random(__u32 max); |
51 | 51 | ||
52 | static void consumer(void *arg); |
52 | static void consumer(void *arg); |
53 | static void failed(void); |
53 | static void failed(void); |
54 | 54 | ||
55 | __u32 random(__u32 max) |
55 | __u32 random(__u32 max) |
56 | { |
56 | { |
57 | __u32 rc; |
57 | __u32 rc; |
58 | 58 | ||
59 | spinlock_lock(&lock); |
59 | spinlock_lock(&lock); |
60 | rc = seed % max; |
60 | rc = seed % max; |
61 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
61 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
62 | spinlock_unlock(&lock); |
62 | spinlock_unlock(&lock); |
63 | return rc; |
63 | return rc; |
64 | } |
64 | } |
65 | 65 | ||
66 | 66 | ||
67 | void consumer(void *arg) |
67 | void consumer(void *arg) |
68 | { |
68 | { |
69 | int rc, to; |
69 | int rc, to; |
- | 70 | ||
- | 71 | thread_detach(THREAD); |
|
- | 72 | ||
70 | waitq_sleep(&can_start); |
73 | waitq_sleep(&can_start); |
71 | 74 | ||
72 | to = random(20000); |
75 | to = random(20000); |
73 | printf("cpu%d, tid %d down+ (%d)\n", CPU->id, THREAD->tid, to); |
76 | printf("cpu%d, tid %d down+ (%d)\n", CPU->id, THREAD->tid, to); |
74 | rc = semaphore_down_timeout(&sem, to); |
77 | rc = semaphore_down_timeout(&sem, to); |
75 | if (SYNCH_FAILED(rc)) { |
78 | if (SYNCH_FAILED(rc)) { |
76 | printf("cpu%d, tid %d down!\n", CPU->id, THREAD->tid); |
79 | printf("cpu%d, tid %d down!\n", CPU->id, THREAD->tid); |
77 | return; |
80 | return; |
78 | } |
81 | } |
79 | 82 | ||
80 | printf("cpu%d, tid %d down=\n", CPU->id, THREAD->tid); |
83 | printf("cpu%d, tid %d down=\n", CPU->id, THREAD->tid); |
81 | thread_usleep(random(30000)); |
84 | thread_usleep(random(30000)); |
82 | 85 | ||
83 | semaphore_up(&sem); |
86 | semaphore_up(&sem); |
84 | printf("cpu%d, tid %d up\n", CPU->id, THREAD->tid); |
87 | printf("cpu%d, tid %d up\n", CPU->id, THREAD->tid); |
85 | } |
88 | } |
86 | 89 | ||
87 | void failed(void) |
90 | void failed(void) |
88 | { |
91 | { |
89 | printf("Test failed prematurely.\n"); |
92 | printf("Test failed prematurely.\n"); |
90 | thread_exit(); |
93 | thread_exit(); |
91 | } |
94 | } |
92 | 95 | ||
93 | void test(void) |
96 | void test(void) |
94 | { |
97 | { |
95 | __u32 i, k; |
98 | __u32 i, k; |
96 | 99 | ||
97 | printf("Semaphore test #2\n"); |
100 | printf("Semaphore test #2\n"); |
98 | 101 | ||
99 | waitq_initialize(&can_start); |
102 | waitq_initialize(&can_start); |
100 | semaphore_initialize(&sem, 5); |
103 | semaphore_initialize(&sem, 5); |
101 | 104 | ||
102 | 105 | ||
103 | 106 | ||
104 | for (; ;) { |
107 | for (; ;) { |
105 | thread_t *thrd; |
108 | thread_t *thrd; |
106 | 109 | ||
107 | k = random(7) + 1; |
110 | k = random(7) + 1; |
108 | printf("Creating %d consumers\n", k); |
111 | printf("Creating %d consumers\n", k); |
109 | for (i=0; i<k; i++) { |
112 | for (i=0; i<k; i++) { |
110 | thrd = thread_create(consumer, NULL, TASK, 0, "consumer"); |
113 | thrd = thread_create(consumer, NULL, TASK, 0, "consumer"); |
111 | if (thrd) |
114 | if (thrd) |
112 | thread_ready(thrd); |
115 | thread_ready(thrd); |
113 | else |
116 | else |
114 | failed(); |
117 | failed(); |
115 | } |
118 | } |
116 | 119 | ||
117 | thread_usleep(20000); |
120 | thread_usleep(20000); |
118 | waitq_wakeup(&can_start, WAKEUP_ALL); |
121 | waitq_wakeup(&can_start, WAKEUP_ALL); |
119 | } |
122 | } |
120 | 123 | ||
121 | } |
124 | } |
122 | 125 |