source: mainline/kernel/test/synch/semaphore1.c@ e3272101

Last change on this file since e3272101 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: 3.3 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]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>
[23684b7]31#include <atomic.h>
[f761f1eb]32#include <proc/thread.h>
33#include <synch/waitq.h>
34#include <synch/semaphore.h>
35
[cb01e1e]36#define AT_ONCE 3
37#define PRODUCERS 50
38#define CONSUMERS 50
[f761f1eb]39
40static semaphore_t sem;
41
42static waitq_t can_start;
[59e07c91]43static atomic_t items_produced;
44static atomic_t items_consumed;
[f761f1eb]45
[70b6de1]46static void producer(void *arg)
[f761f1eb]47{
[cb01e1e]48 thread_detach(THREAD);
[a35b458]49
[f761f1eb]50 waitq_sleep(&can_start);
[a35b458]51
[f761f1eb]52 semaphore_down(&sem);
53 atomic_inc(&items_produced);
54 thread_usleep(250);
55 semaphore_up(&sem);
56}
57
[70b6de1]58static void consumer(void *arg)
[f761f1eb]59{
[cb01e1e]60 thread_detach(THREAD);
[a35b458]61
[f761f1eb]62 waitq_sleep(&can_start);
[a35b458]63
[f761f1eb]64 semaphore_down(&sem);
65 atomic_inc(&items_consumed);
66 thread_usleep(500);
67 semaphore_up(&sem);
68}
69
[a000878c]70const char *test_semaphore1(void)
[f761f1eb]71{
72 int i, j, k;
[3cfe2b8]73 size_t consumers;
74 size_t producers;
[a35b458]75
[f761f1eb]76 waitq_initialize(&can_start);
77 semaphore_initialize(&sem, AT_ONCE);
[a35b458]78
[96348adc]79 for (i = 1; i <= 3; i++) {
[f761f1eb]80 thread_t *thrd;
[a35b458]81
[e3306d04]82 atomic_store(&items_produced, 0);
83 atomic_store(&items_consumed, 0);
[a35b458]84
[f761f1eb]85 consumers = i * CONSUMERS;
[96348adc]86 producers = (4 - i) * PRODUCERS;
[a35b458]87
[077842c]88 TPRINTF("Creating %zu consumers and %zu producers...",
[7e752b2]89 consumers, producers);
[a35b458]90
[96348adc]91 for (j = 0; j < (CONSUMERS + PRODUCERS) / 2; j++) {
92 for (k = 0; k < i; k++) {
[6eef3c4]93 thrd = thread_create(consumer, NULL, TASK,
94 THREAD_FLAG_NONE, "consumer");
[f761f1eb]95 if (thrd)
96 thread_ready(thrd);
97 else
[cb01e1e]98 TPRINTF("could not create consumer %d\n", i);
[f761f1eb]99 }
[96348adc]100 for (k = 0; k < (4 - i); k++) {
[6eef3c4]101 thrd = thread_create(producer, NULL, TASK,
102 THREAD_FLAG_NONE, "producer");
[f761f1eb]103 if (thrd)
104 thread_ready(thrd);
105 else
[cb01e1e]106 TPRINTF("could not create producer %d\n", i);
[f761f1eb]107 }
108 }
[a35b458]109
[cb01e1e]110 TPRINTF("ok\n");
[a35b458]111
[f761f1eb]112 thread_sleep(1);
113 waitq_wakeup(&can_start, WAKEUP_ALL);
[a35b458]114
[e9d2905]115 while ((items_consumed != consumers) || (items_produced != producers)) {
[077842c]116 TPRINTF("%zu consumers remaining, %zu producers remaining\n",
[e9d2905]117 consumers - items_consumed, producers - items_produced);
[f761f1eb]118 thread_sleep(1);
119 }
120 }
[a35b458]121
[96348adc]122 return NULL;
[f761f1eb]123}
Note: See TracBrowser for help on using the repository browser.