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
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;
[31e15be]43static atomic_size_t items_produced;
44static atomic_size_t items_consumed;
[f761f1eb]45
[70b6de1]46static void producer(void *arg)
[f761f1eb]47{
48 waitq_sleep(&can_start);
[a35b458]49
[f761f1eb]50 semaphore_down(&sem);
51 atomic_inc(&items_produced);
52 thread_usleep(250);
53 semaphore_up(&sem);
54}
55
[70b6de1]56static void consumer(void *arg)
[f761f1eb]57{
58 waitq_sleep(&can_start);
[a35b458]59
[f761f1eb]60 semaphore_down(&sem);
61 atomic_inc(&items_consumed);
62 thread_usleep(500);
63 semaphore_up(&sem);
64}
65
[a000878c]66const char *test_semaphore1(void)
[f761f1eb]67{
68 int i, j, k;
[3cfe2b8]69 size_t consumers;
70 size_t producers;
[a35b458]71
[f761f1eb]72 waitq_initialize(&can_start);
73 semaphore_initialize(&sem, AT_ONCE);
[a35b458]74
[96348adc]75 for (i = 1; i <= 3; i++) {
[f761f1eb]76 thread_t *thrd;
[a35b458]77
[e3306d04]78 atomic_store(&items_produced, 0);
79 atomic_store(&items_consumed, 0);
[a35b458]80
[f761f1eb]81 consumers = i * CONSUMERS;
[96348adc]82 producers = (4 - i) * PRODUCERS;
[a35b458]83
[077842c]84 TPRINTF("Creating %zu consumers and %zu producers...",
[7e752b2]85 consumers, producers);
[a35b458]86
[96348adc]87 for (j = 0; j < (CONSUMERS + PRODUCERS) / 2; j++) {
88 for (k = 0; k < i; k++) {
[6eef3c4]89 thrd = thread_create(consumer, NULL, TASK,
90 THREAD_FLAG_NONE, "consumer");
[0f4f1b2]91 if (thrd) {
92 thread_start(thrd);
93 thread_detach(thrd);
94 } else {
[cb01e1e]95 TPRINTF("could not create consumer %d\n", i);
[0f4f1b2]96 }
[f761f1eb]97 }
[96348adc]98 for (k = 0; k < (4 - i); k++) {
[6eef3c4]99 thrd = thread_create(producer, NULL, TASK,
100 THREAD_FLAG_NONE, "producer");
[0f4f1b2]101 if (thrd) {
102 thread_start(thrd);
103 thread_detach(thrd);
104 } else {
[cb01e1e]105 TPRINTF("could not create producer %d\n", i);
[0f4f1b2]106 }
[f761f1eb]107 }
108 }
[a35b458]109
[cb01e1e]110 TPRINTF("ok\n");
[a35b458]111
[f761f1eb]112 thread_sleep(1);
[111b9b9]113 waitq_wake_all(&can_start);
[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.