Subversion Repositories HelenOS

Rev

Rev 3343 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2001-2004 Jakub Jermar
1 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>
1104 jermar 31
#include <atomic.h>
1 jermar 32
#include <print.h>
33
#include <proc/thread.h>
3862 rimsky 34
#include <arch/asm.h>
1 jermar 35
#include <arch/types.h>
36
#include <arch/context.h>
37
 
38
#include <synch/waitq.h>
39
#include <synch/semaphore.h>
40
#include <synch/synch.h>
623 jermar 41
#include <synch/spinlock.h>
1 jermar 42
 
43
static semaphore_t sem;
44
 
2024 decky 45
SPINLOCK_INITIALIZE(sem_lock);
1 jermar 46
 
47
static waitq_t can_start;
48
 
2022 decky 49
static uint32_t seed = 0xdeadbeef;
1 jermar 50
 
2022 decky 51
static uint32_t random(uint32_t max)
1 jermar 52
{
1780 jermar 53
    uint32_t rc;
1 jermar 54
 
2024 decky 55
    spinlock_lock(&sem_lock);  
1 jermar 56
    rc = seed % max;
57
    seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
2024 decky 58
    spinlock_unlock(&sem_lock);
1 jermar 59
    return rc;
60
}
61
 
2022 decky 62
static void consumer(void *arg)
1 jermar 63
{
64
    int rc, to;
1658 vana 65
 
66
    thread_detach(THREAD);
67
 
1 jermar 68
    waitq_sleep(&can_start);
69
 
70
    to = random(20000);
3069 decky 71
    printf("cpu%u, tid %" PRIu64 " down+ (%d)\n", CPU->id, THREAD->tid, to);
1 jermar 72
    rc = semaphore_down_timeout(&sem, to);
73
    if (SYNCH_FAILED(rc)) {
3069 decky 74
        printf("cpu%u, tid %" PRIu64 " down!\n", CPU->id, THREAD->tid);
1 jermar 75
        return;
76
    }
77
 
3069 decky 78
    printf("cpu%u, tid %" PRIu64 " down=\n", CPU->id, THREAD->tid);
1 jermar 79
    thread_usleep(random(30000));
80
 
81
    semaphore_up(&sem);
3069 decky 82
    printf("cpu%u, tid %" PRIu64 " up\n", CPU->id, THREAD->tid);
1 jermar 83
}
84
 
2050 decky 85
char * test_semaphore2(bool quiet)
1 jermar 86
{
1780 jermar 87
    uint32_t i, k;
1 jermar 88
 
89
    waitq_initialize(&can_start);
90
    semaphore_initialize(&sem, 5);
91
 
2028 decky 92
    thread_t *thrd;
1 jermar 93
 
2028 decky 94
    k = random(7) + 1;
3069 decky 95
    printf("Creating %" PRIu32 " consumers\n", k);
2028 decky 96
    for (i = 0; i < k; i++) {
2042 decky 97
        thrd = thread_create(consumer, NULL, TASK, 0, "consumer", false);
2028 decky 98
        if (thrd)
99
            thread_ready(thrd);
100
        else
101
            printf("Error creating thread\n");
102
    }
103
 
104
    thread_usleep(20000);
105
    waitq_wakeup(&can_start, WAKEUP_ALL);
106
 
107
    return NULL;
1 jermar 108
}