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