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

Last change on this file since 8996582 was 111b9b9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Reimplement waitq using thread_wait/wakeup

This adds a few functions to the thread API which can be
summarized as "stop running until woken up by others".
The ordering and context-switching concerns are thus yeeted
to this abstraction and waitq only deals with maintaining
the queues. Overall, this makes the control flow in waitq
much easier to navigate.

  • Property mode set to 100644
File size: 3.3 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 <synch/waitq.h>
34#include <synch/semaphore.h>
35
36#define AT_ONCE 3
37#define PRODUCERS 50
38#define CONSUMERS 50
39
40static semaphore_t sem;
41
42static waitq_t can_start;
43static atomic_size_t items_produced;
44static atomic_size_t items_consumed;
45
46static void producer(void *arg)
47{
48 waitq_sleep(&can_start);
49
50 semaphore_down(&sem);
51 atomic_inc(&items_produced);
52 thread_usleep(250);
53 semaphore_up(&sem);
54}
55
56static void consumer(void *arg)
57{
58 waitq_sleep(&can_start);
59
60 semaphore_down(&sem);
61 atomic_inc(&items_consumed);
62 thread_usleep(500);
63 semaphore_up(&sem);
64}
65
66const char *test_semaphore1(void)
67{
68 int i, j, k;
69 size_t consumers;
70 size_t producers;
71
72 waitq_initialize(&can_start);
73 semaphore_initialize(&sem, AT_ONCE);
74
75 for (i = 1; i <= 3; i++) {
76 thread_t *thrd;
77
78 atomic_store(&items_produced, 0);
79 atomic_store(&items_consumed, 0);
80
81 consumers = i * CONSUMERS;
82 producers = (4 - i) * PRODUCERS;
83
84 TPRINTF("Creating %zu consumers and %zu producers...",
85 consumers, producers);
86
87 for (j = 0; j < (CONSUMERS + PRODUCERS) / 2; j++) {
88 for (k = 0; k < i; k++) {
89 thrd = thread_create(consumer, NULL, TASK,
90 THREAD_FLAG_NONE, "consumer");
91 if (thrd)
92 thread_ready(thrd);
93 else
94 TPRINTF("could not create consumer %d\n", i);
95 }
96 for (k = 0; k < (4 - i); k++) {
97 thrd = thread_create(producer, NULL, TASK,
98 THREAD_FLAG_NONE, "producer");
99 if (thrd)
100 thread_ready(thrd);
101 else
102 TPRINTF("could not create producer %d\n", i);
103 }
104 }
105
106 TPRINTF("ok\n");
107
108 thread_sleep(1);
109 waitq_wake_all(&can_start);
110
111 while ((items_consumed != consumers) || (items_produced != producers)) {
112 TPRINTF("%zu consumers remaining, %zu producers remaining\n",
113 consumers - items_consumed, producers - items_produced);
114 thread_sleep(1);
115 }
116 }
117
118 return NULL;
119}
Note: See TracBrowser for help on using the repository browser.