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

Last change on this file was 0f4f1b2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 18 months ago

Add (and use) functions thread_start() and thread_detach()

Mostly cosmetic, with thread_start() replacing calls to thread_ready(),
but not consuming the passed reference, and thread_detach() being
synonym for thread_put(). Makes the code's function more obvious.

Also modify some threaded tests to use thread_join() for waiting,
instead of counting threads with atomics or semaphores.

  • Property mode set to 100644
File size: 3.4 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_start(thrd);
93 thread_detach(thrd);
94 } else {
95 TPRINTF("could not create consumer %d\n", i);
96 }
97 }
98 for (k = 0; k < (4 - i); k++) {
99 thrd = thread_create(producer, NULL, TASK,
100 THREAD_FLAG_NONE, "producer");
101 if (thrd) {
102 thread_start(thrd);
103 thread_detach(thrd);
104 } else {
105 TPRINTF("could not create producer %d\n", i);
106 }
107 }
108 }
109
110 TPRINTF("ok\n");
111
112 thread_sleep(1);
113 waitq_wake_all(&can_start);
114
115 while ((items_consumed != consumers) || (items_produced != producers)) {
116 TPRINTF("%zu consumers remaining, %zu producers remaining\n",
117 consumers - items_consumed, producers - items_produced);
118 thread_sleep(1);
119 }
120 }
121
122 return NULL;
123}
Note: See TracBrowser for help on using the repository browser.