source: mainline/kernel/test/synch/semaphore2.c@ 394e22f

Last change on this file since 394e22f was bab75df6, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Let kernel code get printf via the standard stdio header. Clean up unused includes.

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29#include <test.h>
30#include <arch.h>
31#include <atomic.h>
32#include <proc/thread.h>
33#include <typedefs.h>
34#include <arch/context.h>
35#include <synch/waitq.h>
36#include <synch/semaphore.h>
37#include <synch/spinlock.h>
38
39static semaphore_t sem;
40
41SPINLOCK_INITIALIZE(sem_lock);
42
43static waitq_t can_start;
44
45static uint32_t seed = 0xdeadbeef;
46
47static uint32_t random(uint32_t max)
48{
49 uint32_t rc;
50
51 spinlock_lock(&sem_lock);
52 rc = seed % max;
53 seed = (((seed << 2) ^ (seed >> 2)) * 487) + rc;
54 spinlock_unlock(&sem_lock);
55 return rc;
56}
57
58static void consumer(void *arg)
59{
60 errno_t rc;
61 int to;
62
63 thread_detach(THREAD);
64
65 waitq_sleep(&can_start);
66
67 to = random(20000);
68 TPRINTF("cpu%u, tid %" PRIu64 " down+ (%d)\n", CPU->id, THREAD->tid, to);
69 rc = semaphore_down_timeout(&sem, to);
70 if (rc != EOK) {
71 TPRINTF("cpu%u, tid %" PRIu64 " down!\n", CPU->id, THREAD->tid);
72 return;
73 }
74
75 TPRINTF("cpu%u, tid %" PRIu64 " down=\n", CPU->id, THREAD->tid);
76 thread_usleep(random(30000));
77
78 semaphore_up(&sem);
79 TPRINTF("cpu%u, tid %" PRIu64 " up\n", CPU->id, THREAD->tid);
80}
81
82const char *test_semaphore2(void)
83{
84 uint32_t i, k;
85
86 waitq_initialize(&can_start);
87 semaphore_initialize(&sem, 5);
88
89 thread_t *thrd;
90
91 k = random(7) + 1;
92 TPRINTF("Creating %" PRIu32 " consumers\n", k);
93 for (i = 0; i < k; i++) {
94 thrd = thread_create(consumer, NULL, TASK,
95 THREAD_FLAG_NONE, "consumer");
96 if (thrd)
97 thread_ready(thrd);
98 else
99 TPRINTF("Error creating thread\n");
100 }
101
102 thread_usleep(20000);
103 waitq_wakeup(&can_start, WAKEUP_ALL);
104
105 return NULL;
106}
Note: See TracBrowser for help on using the repository browser.