summaryrefslogtreecommitdiff
path: root/testing/embedding/thread-test.h
blob: 33a7e480cd4b7d438ea4024b4dfc311ee24058c1 (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
/************************************************************/
#ifndef _MSC_VER
/************************************************************/


#include <pthread.h>
#include <semaphore.h>


/************************************************************/
#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;

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;
}

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

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

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

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
/************************************************************/