Rev 2024 | Rev 2042 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2024 | Rev 2028 | ||
---|---|---|---|
Line 45... | Line 45... | ||
45 | 45 | ||
46 | static waitq_t can_start; |
46 | static waitq_t can_start; |
47 | 47 | ||
48 | static uint32_t seed = 0xdeadbeef; |
48 | static uint32_t seed = 0xdeadbeef; |
49 | 49 | ||
50 | static uint32_t random(uint32_t max); |
- | |
51 | - | ||
52 | static void consumer(void *arg); |
- | |
53 | static void failed(void); |
- | |
54 | - | ||
55 | static uint32_t random(uint32_t max) |
50 | static uint32_t random(uint32_t max) |
56 | { |
51 | { |
57 | uint32_t rc; |
52 | uint32_t rc; |
58 | 53 | ||
59 | spinlock_lock(&sem_lock); |
54 | spinlock_lock(&sem_lock); |
Line 61... | Line 56... | ||
61 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
56 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
62 | spinlock_unlock(&sem_lock); |
57 | spinlock_unlock(&sem_lock); |
63 | return rc; |
58 | return rc; |
64 | } |
59 | } |
65 | 60 | ||
66 | - | ||
67 | static void consumer(void *arg) |
61 | static void consumer(void *arg) |
68 | { |
62 | { |
69 | int rc, to; |
63 | int rc, to; |
70 | 64 | ||
71 | thread_detach(THREAD); |
65 | thread_detach(THREAD); |
Line 85... | Line 79... | ||
85 | 79 | ||
86 | semaphore_up(&sem); |
80 | semaphore_up(&sem); |
87 | printf("cpu%d, tid %d up\n", CPU->id, THREAD->tid); |
81 | printf("cpu%d, tid %d up\n", CPU->id, THREAD->tid); |
88 | } |
82 | } |
89 | 83 | ||
90 | static void failed(void) |
- | |
91 | { |
- | |
92 | printf("Test failed prematurely.\n"); |
- | |
93 | thread_exit(); |
- | |
94 | } |
- | |
95 | - | ||
96 | void test_semaphore2(void) |
84 | char * test_semaphore2(void) |
97 | { |
85 | { |
98 | uint32_t i, k; |
86 | uint32_t i, k; |
99 | 87 | ||
100 | printf("Semaphore test #2\n"); |
- | |
101 | - | ||
102 | waitq_initialize(&can_start); |
88 | waitq_initialize(&can_start); |
103 | semaphore_initialize(&sem, 5); |
89 | semaphore_initialize(&sem, 5); |
104 | 90 | ||
105 | - | ||
- | 91 | thread_t *thrd; |
|
106 | 92 | ||
107 | for (; ;) { |
- | |
108 | thread_t *thrd; |
- | |
109 | - | ||
110 | k = random(7) + 1; |
93 | k = random(7) + 1; |
111 | printf("Creating %d consumers\n", k); |
94 | printf("Creating %d consumers\n", k); |
112 | for (i=0; i<k; i++) { |
95 | for (i = 0; i < k; i++) { |
113 | thrd = thread_create(consumer, NULL, TASK, 0, "consumer"); |
96 | thrd = thread_create(consumer, NULL, TASK, 0, "consumer"); |
114 | if (thrd) |
97 | if (thrd) |
115 | thread_ready(thrd); |
98 | thread_ready(thrd); |
116 | else |
99 | else |
117 | failed(); |
100 | printf("Error creating thread\n"); |
118 | } |
101 | } |
119 | 102 | ||
120 | thread_usleep(20000); |
103 | thread_usleep(20000); |
121 | waitq_wakeup(&can_start, WAKEUP_ALL); |
104 | waitq_wakeup(&can_start, WAKEUP_ALL); |
122 | } |
- | |
123 | 105 | ||
- | 106 | return NULL; |
|
124 | } |
107 | } |