-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathjv_thread.h
76 lines (67 loc) · 1.74 KB
/
jv_thread.h
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
#ifndef JV_THREAD_H
#define JV_THREAD_H
#ifdef WIN32
#ifndef __MINGW32__
#include <windows.h>
#include <winnt.h>
#include <errno.h>
/* Copied from Heimdal: pthread-like mutexes for WIN32 -- see lib/base/heimbase.h in Heimdal */
typedef struct pthread_mutex {
HANDLE h;
} pthread_mutex_t;
#define PTHREAD_MUTEX_INITIALIZER { INVALID_HANDLE_VALUE }
static inline int
pthread_mutex_init(pthread_mutex_t *m)
{
m->h = CreateSemaphore(NULL, 1, 1, NULL);
if (m->h == INVALID_HANDLE_VALUE)
return EAGAIN;
return 0;
}
static inline int
pthread_mutex_lock(pthread_mutex_t *m)
{
HANDLE h, new_h;
int created = 0;
h = InterlockedCompareExchangePointer(&m->h, m->h, m->h);
if (h == INVALID_HANDLE_VALUE || h == NULL) {
created = 1;
new_h = CreateSemaphore(NULL, 0, 1, NULL);
if (new_h == INVALID_HANDLE_VALUE)
return EAGAIN;
if (InterlockedCompareExchangePointer(&m->h, new_h, h) != h) {
created = 0;
CloseHandle(new_h);
}
}
if (!created)
WaitForSingleObject(m->h, INFINITE);
return 0;
}
static inline int
pthread_mutex_unlock(pthread_mutex_t *m)
{
if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)
return EPERM;
return 0;
}
static inline int
pthread_mutex_destroy(pthread_mutex_t *m)
{
HANDLE h;
h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);
if (h != INVALID_HANDLE_VALUE)
CloseHandle(h);
return 0;
}
typedef unsigned long pthread_key_t;
int pthread_key_create(pthread_key_t *, void (*)(void *));
int pthread_setspecific(pthread_key_t, void *);
void *pthread_getspecific(pthread_key_t);
#else
#include <pthread.h>
#endif
#else
#include <pthread.h>
#endif
#endif /* JV_THREAD_H */