(c) versus (C)
[helenos.git] / kernel / test / synch / semaphore2.c
blob070ed18fcea301882c0f0a496d4f05056aea16ee
1 /*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <test.h>
30 #include <arch.h>
31 #include <atomic.h>
32 #include <print.h>
33 #include <proc/thread.h>
34 #include <arch/types.h>
35 #include <arch/context.h>
37 #include <synch/waitq.h>
38 #include <synch/semaphore.h>
39 #include <synch/synch.h>
40 #include <synch/spinlock.h>
42 static semaphore_t sem;
44 SPINLOCK_INITIALIZE(sem_lock);
46 static waitq_t can_start;
48 static uint32_t seed = 0xdeadbeef;
50 static uint32_t random(uint32_t max)
52 uint32_t rc;
54 spinlock_lock(&sem_lock);
55 rc = seed % max;
56 seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
57 spinlock_unlock(&sem_lock);
58 return rc;
61 static void consumer(void *arg)
63 int rc, to;
65 thread_detach(THREAD);
67 waitq_sleep(&can_start);
69 to = random(20000);
70 printf("cpu%d, tid %d down+ (%d)\n", CPU->id, THREAD->tid, to);
71 rc = semaphore_down_timeout(&sem, to);
72 if (SYNCH_FAILED(rc)) {
73 printf("cpu%d, tid %d down!\n", CPU->id, THREAD->tid);
74 return;
77 printf("cpu%d, tid %d down=\n", CPU->id, THREAD->tid);
78 thread_usleep(random(30000));
80 semaphore_up(&sem);
81 printf("cpu%d, tid %d up\n", CPU->id, THREAD->tid);
84 char * test_semaphore2(bool quiet)
86 uint32_t i, k;
88 waitq_initialize(&can_start);
89 semaphore_initialize(&sem, 5);
91 thread_t *thrd;
93 k = random(7) + 1;
94 printf("Creating %d consumers\n", k);
95 for (i = 0; i < k; i++) {
96 thrd = thread_create(consumer, NULL, TASK, 0, "consumer", false);
97 if (thrd)
98 thread_ready(thrd);
99 else
100 printf("Error creating thread\n");
103 thread_usleep(20000);
104 waitq_wakeup(&can_start, WAKEUP_ALL);
106 return NULL;