-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmingw_threads.c
151 lines (121 loc) · 2.78 KB
/
mingw_threads.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
150
151
/*
Copyright (C) 2020- TheTrustedComputer
*/
#if (defined(__MINGW64__) || defined(__MINGW32__))
#include <stdio.h>
#include <stdlib.h>
#include "mingw_threads.h"
int thrd_create(thrd_t *_thr, thrd_start_t *_func, void *_arg)
{
thrd_start_wrapper_arg_t *wrapper;
if (!(wrapper = malloc(sizeof(*wrapper))))
{
return thrd_nomem;
}
wrapper->func = _func;
wrapper->arg = _arg;
switch (pthread_create(_thr, NULL, thrd_start_wrapper, wrapper))
{
case 0:
return thrd_success;
case EAGAIN:
return thrd_nomem;
default:
return thrd_error;
}
}
int thrd_join(thrd_t _thr, int *_res)
{
void *res;
int joinErr;
joinErr = pthread_join(_thr, &res);
if (_res && res)
{
*_res = *(int*)(res);
}
return joinErr ? thrd_error : thrd_success;
}
int thrd_sleep(const struct timespec *_DUR, struct timespec *_rem)
{
return nanosleep(_DUR, _rem);
}
void thrd_yield(void)
{
sched_yield();
}
void *thrd_start_wrapper(void *_arg)
{
thrd_start_wrapper_arg_t *wrapper;
int return_stat;
wrapper = _arg;
return_stat = wrapper->func(wrapper->arg);
free(wrapper);
return (void*)(return_stat);
}
int mtx_init(mtx_t *_mutex, int _type)
{
int mtxErr;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
switch (_type)
{
case mtx_plain:
case mtx_timed:
if (_type & mtx_recursive)
{
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
}
else
{
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_TIMED_NP);
}
break;
default:
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_TIMED_NP);
}
mtxErr = pthread_mutex_init(_mutex, &attr);
pthread_mutexattr_destroy(&attr);
return mtxErr ? thrd_error : thrd_success;
}
void mtx_destroy(mtx_t *_mutex)
{
pthread_mutex_destroy(_mutex);
}
int mtx_lock(mtx_t *_mutex)
{
return pthread_mutex_lock(_mutex) ? thrd_error : thrd_success;
}
int mtx_unlock(mtx_t *_mutex)
{
return pthread_mutex_unlock(_mutex) ? thrd_error : thrd_success;
}
int cnd_init(cnd_t *_cond)
{
switch (pthread_cond_init(_cond, NULL))
{
case 0:
return thrd_success;
case ENOMEM:
case EAGAIN:
return thrd_nomem;
default:
return thrd_error;
}
}
void cnd_destroy(cnd_t *_cond)
{
pthread_cond_destroy(_cond);
}
int cnd_signal(cnd_t *_cond)
{
return pthread_cond_signal(_cond) ? thrd_error : thrd_success;
}
int cnd_broadcast(cnd_t *_cond)
{
return pthread_cond_broadcast(_cond) ? thrd_error : thrd_success;
}
int cnd_wait(cnd_t *_cond, mtx_t *_mutex)
{
return pthread_cond_wait(_cond, _mutex) ? thrd_error : thrd_success;
}
#endif