summaryrefslogtreecommitdiff
path: root/testing/embedding/thread-test.h
blob: f66cf70f9c90e1412445b45b767a93c49d4a95f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/************************************************************/
#ifndef _MSC_VER
/************************************************************/


#include <pthread.h>

/* don't include <semaphore.h>, it is not available on OS/X */

typedef struct {
    pthread_mutex_t mutex1;
    pthread_cond_t cond1;
    unsigned int value;
} sem_t;

static int sem_init(sem_t *sem, int pshared, unsigned int value)
{
    assert(pshared == 0);
    sem->value = value;
    return (pthread_mutex_init(&sem->mutex1, NULL) ||
            pthread_cond_init(&sem->cond1, NULL));
}

static int sem_post(sem_t *sem)
{
    pthread_mutex_lock(&sem->mutex1);
    sem->value += 1;
    pthread_cond_signal(&sem->cond1);
    pthread_mutex_unlock(&sem->mutex1);
    return 0;
}

static int sem_wait(sem_t *sem)
{
    pthread_mutex_lock(&sem->mutex1);
    while (sem->value == 0)
        pthread_cond_wait(&sem->cond1, &sem->mutex1);
    sem->value -= 1;
    pthread_mutex_unlock(&sem->mutex1);
    return 0;
}


/************************************************************/
#else
/************************************************************/


/* Very quick and dirty, just what I need for these tests.
   Don't use directly in any real code! 
*/

#include <Windows.h>
#include <assert.h>

typedef HANDLE sem_t;
typedef HANDLE pthread_t;

static int sem_init(sem_t *sem, int pshared, unsigned int value)
{
    assert(pshared == 0);
    assert(value == 0);
    *sem = CreateSemaphore(NULL, 0, 999, NULL);
    return *sem ? 0 : -1;
}

static int sem_post(sem_t *sem)
{
    return ReleaseSemaphore(*sem, 1, NULL) ? 0 : -1;
}

static int sem_wait(sem_t *sem)
{
    WaitForSingleObject(*sem, INFINITE);
    return 0;
}

static DWORD WINAPI myThreadProc(LPVOID lpParameter)
{
    void *(* start_routine)(void *) = (void *(*)(void *))lpParameter;
    start_routine(NULL);
    return 0;
}

static int pthread_create(pthread_t *thread, void *attr,
                          void *start_routine(void *), void *arg)
{
    assert(arg == NULL);
    *thread = CreateThread(NULL, 0, myThreadProc, start_routine, 0, NULL);
    return *thread ? 0 : -1;
}


/************************************************************/
#endif
/************************************************************/