source: mainline/kernel/test/synch/semaphore2.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: 2.9 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>
[d99c1d2]33#include <typedefs.h>
[f761f1eb]34#include <arch/context.h>
35#include <synch/waitq.h>
36#include <synch/semaphore.h>
[dc747e3]37#include <synch/spinlock.h>
[f761f1eb]38
39static semaphore_t sem;
40
[dff0a94]41SPINLOCK_INITIALIZE(sem_lock);
[f761f1eb]42
43static waitq_t can_start;
44
[70b6de1]45static uint32_t seed = 0xdeadbeef;
[f761f1eb]46
[70b6de1]47static uint32_t random(uint32_t max)
[f761f1eb]48{
[7f1c620]49 uint32_t rc;
[a35b458]50
[cb01e1e]51 spinlock_lock(&sem_lock);
[f761f1eb]52 rc = seed % max;
[53634f9]53 seed = (((seed << 2) ^ (seed >> 2)) * 487) + rc;
[dff0a94]54 spinlock_unlock(&sem_lock);
[f761f1eb]55 return rc;
56}
57
[70b6de1]58static void consumer(void *arg)
[f761f1eb]59{
[b7fd2a0]60 errno_t rc;
[7f11dc6]61 int to;
[a35b458]62
[f761f1eb]63 waitq_sleep(&can_start);
[a35b458]64
[f761f1eb]65 to = random(20000);
[cb01e1e]66 TPRINTF("cpu%u, tid %" PRIu64 " down+ (%d)\n", CPU->id, THREAD->tid, to);
[f761f1eb]67 rc = semaphore_down_timeout(&sem, to);
[897fd8f1]68 if (rc != EOK) {
[cb01e1e]69 TPRINTF("cpu%u, tid %" PRIu64 " down!\n", CPU->id, THREAD->tid);
[f761f1eb]70 return;
71 }
[a35b458]72
[1b20da0]73 TPRINTF("cpu%u, tid %" PRIu64 " down=\n", CPU->id, THREAD->tid);
[f761f1eb]74 thread_usleep(random(30000));
[a35b458]75
[f761f1eb]76 semaphore_up(&sem);
[cb01e1e]77 TPRINTF("cpu%u, tid %" PRIu64 " up\n", CPU->id, THREAD->tid);
[f761f1eb]78}
79
[a000878c]80const char *test_semaphore2(void)
[f761f1eb]81{
[7f1c620]82 uint32_t i, k;
[a35b458]83
[f761f1eb]84 waitq_initialize(&can_start);
85 semaphore_initialize(&sem, 5);
[a35b458]86
[96348adc]87 thread_t *thrd;
[a35b458]88
[96348adc]89 k = random(7) + 1;
[cb01e1e]90 TPRINTF("Creating %" PRIu32 " consumers\n", k);
[96348adc]91 for (i = 0; i < k; i++) {
[6eef3c4]92 thrd = thread_create(consumer, NULL, TASK,
93 THREAD_FLAG_NONE, "consumer");
[0f4f1b2]94 if (thrd) {
95 thread_start(thrd);
96 thread_detach(thrd);
97 } else {
[cb01e1e]98 TPRINTF("Error creating thread\n");
[0f4f1b2]99 }
[96348adc]100 }
[a35b458]101
[96348adc]102 thread_usleep(20000);
[111b9b9]103 waitq_wake_all(&can_start);
[a35b458]104
[96348adc]105 return NULL;
[f761f1eb]106}
Note: See TracBrowser for help on using the repository browser.