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