-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththread.c
149 lines (127 loc) · 2.59 KB
/
thread.c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* thread.c
*
* Multithreading routines
*
* (C)1999 Stefano Busti
*
*/
#include "thread.h"
int thr_create(thr_t *thread, int detached,
thr_startfunc_ptr_t startfunc, void *arg)
{
#if defined(PTHREADS)
int res;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, detached
? PTHREAD_CREATE_DETACHED
: PTHREAD_CREATE_JOINABLE);
res = pthread_create(thread, &attr, startfunc, arg);
pthread_attr_destroy(&attr);
return res;
#elif defined(WINTHREADS)
DWORD ThreadID;
HANDLE hThread = CreateThread(NULL, 0, startfunc, arg, 0, &ThreadID);
if (hThread == NULL)
return GetLastError();
*thread = hThread;
return 0;
#endif
}
int thr_cancel(thr_t thread)
{
#if defined(PTHREADS)
return pthread_cancel(thread);
#elif defined(WINTHREADS)
BOOL res = TerminateThread(thread, 0);
if (!res)
return GetLastError();
return 0;
#endif
}
void thr_exit(thr_exitcode_t retval)
{
#if defined(PTHREADS)
pthread_exit(retval);
#elif defined(WINTHREADS)
ExitThread(retval);
#endif
}
thr_t thr_self(void)
{
#if defined(PTHREADS)
return pthread_self();
#elif defined(WINTHREADS)
return GetCurrentThread();
#endif
}
int thr_equal(thr_t thread1, thr_t thread2)
{
#if defined(PTHREADS)
return pthread_equal(thread1, thread2);
#elif defined(WINTHREADS)
return thread1 == thread2;
#endif
}
int thr_join(thr_t thread, thr_exitcode_t *retval)
{
#if defined(PTHREADS)
return pthread_join(thread, retval);
#elif defined(WINTHREADS)
return -1;
#endif
}
int thr_detach(thr_t thread)
{
#if defined(PTHREADS)
return pthread_detach(thread);
#elif defined(WINTHREADS)
return 0;
#endif
}
/*
* Mutex functions
*/
thr_mutex_t thr_mutex_create(void)
{
#if defined(PTHREADS)
thr_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
return mutex;
#elif defined(WINTHREADS)
return CreateMutex(NULL, FALSE, NULL);
#endif
}
int thr_mutex_destroy(thr_mutex_t *mutex)
{
#if defined(PTHREADS)
return pthread_mutex_destroy(mutex);
#elif defined(WINTHREADS)
return CloseHandle(*mutex) ? 0 : -1;
#endif
}
int thr_mutex_lock(thr_mutex_t *mutex)
{
#if defined(PTHREADS)
return pthread_mutex_lock(mutex);
#elif defined(WINTHREADS)
return WaitForSingleObject(*mutex, INFINITE) == WAIT_TIMEOUT ? -1 : 0;
#endif
}
int thr_mutex_trylock(thr_mutex_t *mutex)
{
#if defined(PTHREADS)
return pthread_mutex_trylock(mutex);
#elif defined(WINTHREADS)
return WaitForSingleObject(*mutex, 0) == WAIT_TIMEOUT ? -1 : 0;
#endif
}
int thr_mutex_unlock(thr_mutex_t *mutex)
{
#if defined(PTHREADS)
return pthread_mutex_unlock(mutex);
#elif defined(WINTHREADS)
return ReleaseMutex(*mutex) ? 0 : -1;
#endif
}