forked from AZO234/NP2kai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
np2_thread.c
382 lines (348 loc) · 9.47 KB
/
np2_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* === NP2 threading library === (c) 2019 AZO */
#ifdef SUPPORT_NP2_THREAD
#include <np2_thread.h>
#include <string.h>
/* --- thread --- */
/* for caller */
void NP2_Thread_Create(NP2_Thread_t* pth, void *(*thread)(void *), void* param) {
#if defined(NP2_THREAD_WIN)
*pth = (NP2_Thread_t)_beginthread((void (__cdecl *)(void *))thread, 0, param);
#elif defined(NP2_THREAD_POSIX)
pthread_create(pth, NULL, thread, param);
#elif defined(NP2_SDL)
#if SDL_MAJOR_VERSION == 1
*(SDL_Thread**)pth = SDL_CreateThread(thread, param);
#else
*(SDL_Thread**)pth = SDL_CreateThread((SDL_ThreadFunction)thread, NULL, param);
#endif
#elif defined(__LIBRETRO__)
*pth = sthread_create((void (*)(void*))thread, param);
#endif
}
/* for caller */
void NP2_Thread_Destroy(NP2_Thread_t* pth) {
NP2_Thread_Detach(pth);
#if defined(NP2_THREAD_POSIX)
pth = NULL;
#else
*pth = NULL;
#endif
}
/* for callee */
int NP2_Thread_Exit(void* retval) {
#if defined(NP2_THREAD_WIN)
(void)retval;
_endthread();
return 0;
#elif defined(NP2_THREAD_POSIX)
pthread_exit(retval);
return 0;
#elif defined(NP2_SDL)
return (intptr_t)retval;
#elif defined(__LIBRETRO__)
(void)retval;
return 0;
#endif
}
/* for caller */
void NP2_Thread_Wait(NP2_Thread_t* pth, void **retval) {
#if defined(NP2_THREAD_WIN)
(void)retval;
if(*pth)
WaitForSingleObject(*pth, INFINITE);
#elif defined(NP2_THREAD_POSIX)
if(pth)
pthread_join(*pth, retval);
pth = NULL;
#elif defined(NP2_SDL)
if(pth)
SDL_WaitThread((SDL_Thread*)*pth, (int*)retval);
pth = NULL;
#elif defined(__LIBRETRO__)
(void)retval;
if(*pth)
sthread_join(*pth);
*pth = NULL;
#endif
}
/* for caller */
void NP2_Thread_Detach(NP2_Thread_t* pth) {
#if defined(NP2_THREAD_WIN)
if(*pth)
CloseHandle(*pth);
*pth = NULL;
#elif defined(NP2_THREAD_POSIX)
if(pth)
pthread_detach(*pth);
pth = NULL;
#elif defined(NP2_SDL)
if(*pth)
#if SDL_MAJOR_VERSION == 1
SDL_KillThread((SDL_Thread*)*pth);
#else
SDL_DetachThread((SDL_Thread*)*pth);
#endif
*pth = NULL;
#elif defined(__LIBRETRO__)
if(*pth)
sthread_detach(*pth);
*pth = NULL;
#endif
}
/* --- semaphore --- */
/* for caller */
void NP2_Semaphore_Create(NP2_Semaphore_t* psem, const unsigned int initcount) {
#if defined(NP2_THREAD_WIN)
*psem = CreateSemaphore(NULL, initcount, initcount, NULL);
#elif defined(NP2_THREAD_POSIX)
sem_init(psem, 0, initcount);
#elif defined(NP2_SDL)
*(SDL_sem**)psem = SDL_CreateSemaphore(initcount);
#elif defined(__LIBRETRO__)
*psem = ssem_new(initcount);
#endif
}
/* for caller */
void NP2_Semaphore_Destroy(NP2_Semaphore_t* psem) {
#if defined(NP2_THREAD_WIN)
if(*psem)
CloseHandle(*psem);
*psem = NULL;
#elif defined(NP2_THREAD_POSIX)
if(psem)
sem_destroy(psem);
psem = NULL;
#elif defined(NP2_SDL)
if(*psem)
SDL_DestroySemaphore((SDL_sem*)*psem);
*psem = NULL;
#elif defined(__LIBRETRO__)
if(*psem)
ssem_free(*psem);
*psem = NULL;
#endif
}
/* for caller/callee */
void NP2_Semaphore_Wait(NP2_Semaphore_t* psem) {
#if defined(NP2_THREAD_WIN)
WaitForSingleObject(*psem, INFINITE);
#elif defined(NP2_THREAD_POSIX)
sem_wait(psem);
#elif defined(NP2_SDL)
SDL_SemWait((SDL_sem*)*psem);
#elif defined(__LIBRETRO__)
ssem_wait(*psem);
#endif
}
/* for caller/callee */
void NP2_Semaphore_Release(NP2_Semaphore_t* psem) {
#if defined(NP2_THREAD_WIN)
if(psem)
ReleaseSemaphore(*psem, 1, NULL);
#elif defined(NP2_THREAD_POSIX)
if(psem)
sem_post(psem);
#elif defined(NP2_SDL)
if(*psem)
SDL_SemPost((SDL_sem*)*psem);
#elif defined(__LIBRETRO__)
if(*psem)
ssem_signal(*psem);
#endif
}
/* --- wait queue --- */
/* for caller (ring) */
void NP2_WaitQueue_Ring_Create(NP2_WaitQueue_t* pque, unsigned int maxcount) {
if(!pque || maxcount == 0) {
return;
}
memset(pque, 0, sizeof(NP2_WaitQueue_t));
pque->ring.type = NP2_WAITQUEUE_TYPE_RING;
pque->ring.params = malloc(sizeof(void*) * maxcount);
pque->ring.maxcount = maxcount;
}
/* for caller (ringint) */
void NP2_WaitQueue_RingInt_Create(NP2_WaitQueue_t* pque, unsigned int maxcount) {
if(!pque || maxcount == 0) {
return;
}
memset(pque, 0, sizeof(NP2_WaitQueue_t));
pque->ring.type = NP2_WAITQUEUE_TYPE_RINGINT;
pque->ring.params = malloc(sizeof(int) * maxcount);
pque->ring.maxcount = maxcount;
}
/* for caller (list) */
void NP2_WaitQueue_List_Create(NP2_WaitQueue_t* pque) {
if(pque != NULL) {
memset(pque, 0, sizeof(NP2_WaitQueue_t));
pque->list.type = NP2_WAITQUEUE_TYPE_LIST;
}
}
/* for caller (ring,ringint,list) */
void NP2_WaitQueue_Destroy(NP2_WaitQueue_t* pque) {
NP2_WaitQueue_List_Item_t* item;
unsigned int i;
if(pque) {
switch(pque->ring.type) {
case NP2_WAITQUEUE_TYPE_RING:
for(i = 0; i < pque->ring.maxcount; i++) {
if(((void**)(pque->ring.params))[i])
free(((void**)(pque->ring.params))[i]);
}
case NP2_WAITQUEUE_TYPE_RINGINT:
free(pque->ring.params);
break;
case NP2_WAITQUEUE_TYPE_LIST:
while(pque->list.first) {
item = pque->list.first;
if(item->param)
free(item->param);
pque->list.first = pque->list.first->next;
free(item);
}
break;
}
memset(pque, 0, sizeof(NP2_WaitQueue_t));
}
}
/* for caller (ringint) */
void NP2_WaitQueue_RingInt_Append(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, const int param) {
NP2_WaitQueue_List_Item_t* item;
if(pque && psem) {
if(pque->ring.type == NP2_WAITQUEUE_TYPE_RINGINT) {
NP2_Semaphore_Wait(psem);
((int*)(pque->ring.params))[pque->ring.queued] = param;
if(pque->ring.queued + 1 >= pque->ring.maxcount) {
pque->ring.queued = 0;
} else {
pque->ring.queued++;
}
NP2_Semaphore_Release(psem);
if(pque->ring.queued == pque->ring.current) {
// TRACEOUT("NP2_WaitQueue_Append: Error Queue is overlow.\n");
}
}
}
}
/* for caller (ring,list) */
void NP2_WaitQueue_Append(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, void* param) {
NP2_WaitQueue_List_Item_t* item;
if(pque && psem) {
if(pque->ring.type == NP2_WAITQUEUE_TYPE_RING) {
NP2_Semaphore_Wait(psem);
((void**)(pque->ring.params))[pque->ring.queued] = param;
if(pque->ring.queued + 1 >= pque->ring.maxcount) {
pque->ring.queued = 0;
} else {
pque->ring.queued++;
}
NP2_Semaphore_Release(psem);
if(pque->ring.queued == pque->ring.current) {
// TRACEOUT("NP2_WaitQueue_Append: Error Queue is overlow.\n");
}
} else {
item = (NP2_WaitQueue_List_Item_t*)malloc(sizeof(NP2_WaitQueue_List_t));
item->next = NULL;
item->param = param;
NP2_Semaphore_Wait(psem);
if(pque->list.first == NULL) {
pque->list.first = item;
} else {
pque->list.last->next = item;
}
pque->list.last = item;
NP2_Semaphore_Release(psem);
}
}
}
/* for callee (ringint) */
void NP2_WaitQueue_RingInt_Shift(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, int* param) {
NP2_WaitQueue_List_Item_t* item;
if(pque && psem && param) {
if(pque->ring.type == NP2_WAITQUEUE_TYPE_RINGINT) {
NP2_Semaphore_Wait(psem);
if(pque->ring.queued == pque->ring.current) {
*param = 0;
} else {
*param = ((int*)(pque->ring.params))[pque->ring.current];
if(pque->ring.current + 1 >= pque->ring.maxcount) {
pque->ring.current = 0;
} else {
pque->ring.current++;
}
}
NP2_Semaphore_Release(psem);
}
}
}
/* for callee (ring,list) */
void NP2_WaitQueue_Shift(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, void** param) {
NP2_WaitQueue_List_Item_t* item;
if(pque && psem && param) {
if(pque->ring.type == NP2_WAITQUEUE_TYPE_RING) {
NP2_Semaphore_Wait(psem);
if(pque->ring.queued == pque->ring.current) {
*param = NULL;
} else {
*param = ((void**)(pque->ring.params))[pque->ring.current];
((void**)(pque->ring.params))[pque->ring.current] = NULL;
if(pque->ring.current + 1 >= pque->ring.maxcount) {
pque->ring.current = 0;
} else {
pque->ring.current++;
}
}
NP2_Semaphore_Release(psem);
} else {
NP2_Semaphore_Wait(psem);
*param = pque->list.first->param;
item = pque->list.first;
pque->list.first = pque->list.first->next;
NP2_Semaphore_Release(psem);
free(item);
}
}
}
/* for callee (ringint) */
void NP2_WaitQueue_RingInt_Shift_Wait(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, int* param) {
void* item;
if(pque && psem && param) {
do {
NP2_Semaphore_Wait(psem);
if(pque->ring.type == NP2_WAITQUEUE_TYPE_RINGINT) {
if(pque->ring.queued == pque->ring.current) {
item = NULL;
} else {
item = (void*)1;
}
}
NP2_Semaphore_Release(psem);
if(!item)
NP2_Sleep_ms(1);
} while(!item);
NP2_WaitQueue_RingInt_Shift(pque, psem, param);
}
}
/* for callee (ring,list) */
void NP2_WaitQueue_Shift_Wait(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, void** param) {
void* item;
if(pque && psem && param) {
do {
NP2_Semaphore_Wait(psem);
if(pque->ring.type == NP2_WAITQUEUE_TYPE_RING) {
if(pque->ring.queued == pque->ring.current) {
item = NULL;
} else {
item = (void*)1;
}
} else {
item = pque->list.first;
}
NP2_Semaphore_Release(psem);
if(!item)
NP2_Sleep_ms(1);
} while(!item);
NP2_WaitQueue_Shift(pque, psem, param);
}
}
#endif /* SUPPORT_NP2_THREAD */