forked from AZO234/NP2kai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
np2_thread.h
144 lines (122 loc) · 3.93 KB
/
np2_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
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
/* === NP2 threading library === (c) 2019 AZO */
#ifndef _NP2_THREAD_H_
#define _NP2_THREAD_H_
#ifdef SUPPORT_NP2_THREAD
#include <stdlib.h>
#if defined(NP2_THREAD_WIN)
#include <windows.h>
#include <process.h>
#elif defined(NP2_THREAD_POSIX)
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#elif defined(NP2_THREAD_SDL2)
#if defined(USE_SDL_CONFIG)
#include "SDL.h"
#else
#include <SDL.h>
#endif
#elif defined(NP2_THREAD_LR)
#include <rthreads/rthreads.h>
#include <retro_timers.h>
#include "sdl2/libretro/rsemaphore.h"
#endif
#if defined(NP2_THREAD_WIN)
typedef HANDLE NP2_Thread_t;
#elif defined(NP2_THREAD_POSIX)
typedef pthread_t NP2_Thread_t;
#elif defined(NP2_THREAD_SDL2)
typedef SDL_Thread* NP2_Thread_t;
#elif defined(NP2_THREAD_LR)
typedef sthread_t* NP2_Thread_t;
#endif
#ifndef TRACEOUT
#define TRACEOUT(s)
#endif
/* --- thread --- */
/* for caller */
void NP2_Thread_Create(NP2_Thread_t* pth, void*(*thread)(void*), void* param);
/* for caller */
void NP2_Thread_Destroy(NP2_Thread_t* pth);
/* for callee */
int NP2_Thread_Exit(void* retval);
/* for caller */
void NP2_Thread_Wait(NP2_Thread_t* pth, void** retval);
/* for caller */
void NP2_Thread_Detach(NP2_Thread_t* pth);
/* --- semaphore --- */
#if defined(NP2_THREAD_WIN)
typedef HANDLE NP2_Semaphore_t;
#elif defined(NP2_THREAD_POSIX)
typedef sem_t NP2_Semaphore_t;
#elif defined(NP2_THREAD_SDL2)
typedef SDL_sem* NP2_Semaphore_t;
#elif defined(NP2_THREAD_LR)
typedef ssem_t* NP2_Semaphore_t;
#endif
/* for caller */
void NP2_Semaphore_Create(NP2_Semaphore_t* psem, const unsigned int initcount);
/* for caller */
void NP2_Semaphore_Destroy(NP2_Semaphore_t* psem);
/* for caller/callee */
void NP2_Semaphore_Wait(NP2_Semaphore_t* psem);
/* for caller/callee */
void NP2_Semaphore_Release(NP2_Semaphore_t* psem);
/* --- wait queue --- */
typedef enum {
NP2_WAITQUEUE_TYPE_RING = 0,
NP2_WAITQUEUE_TYPE_RINGINT,
NP2_WAITQUEUE_TYPE_LIST,
} NP2_WaitQueue_Type_t;
typedef struct NP2_WaitQueue_List_Item_t_ {
struct NP2_WaitQueue_List_Item_t_* next;
void* param;
} NP2_WaitQueue_List_Item_t;
typedef struct NP2_WaitQueue_Ring_t_ {
NP2_WaitQueue_Type_t type;
void* params;
unsigned int maxcount;
unsigned int current;
unsigned int queued;
} NP2_WaitQueue_Ring_t;
typedef struct NP2_WaitQueue_List_t_ {
NP2_WaitQueue_Type_t type;
NP2_WaitQueue_List_Item_t* first;
NP2_WaitQueue_List_Item_t* last;
} NP2_WaitQueue_List_t;
typedef union NP2_WaitQueue_t_ {
NP2_WaitQueue_Ring_t ring;
NP2_WaitQueue_List_t list;
} NP2_WaitQueue_t;
/* for caller (ring) */
void NP2_WaitQueue_Ring_Create(NP2_WaitQueue_t* pque, unsigned int maxcount);
/* for caller (ringint) */
void NP2_WaitQueue_RingInt_Create(NP2_WaitQueue_t* pque, unsigned int maxcount);
/* for caller (list) */
void NP2_WaitQueue_List_Create(NP2_WaitQueue_t* pque);
/* for caller (ring,ringint,list) */
void NP2_WaitQueue_Destroy(NP2_WaitQueue_t* pque);
/* for caller (ringint) */
void NP2_WaitQueue_RingInt_Append(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, const int param);
/* for caller (ring,list) */
void NP2_WaitQueue_Append(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, void* param);
/* for callee (ringint) */
void NP2_WaitQueue_RingInt_Shift(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, int* param);
/* for callee (ring,list) */
void NP2_WaitQueue_Shift(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, void** param);
/* for callee (ringint) */
void NP2_WaitQueue_RingInt_Shift_Wait(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, int* param);
/* for callee (ring,list) */
void NP2_WaitQueue_Shift_Wait(NP2_WaitQueue_t* pque, NP2_Semaphore_t* psem, void** param);
/* --- sleep ms --- */
#if defined(NP2_THREAD_WIN)
#define NP2_Sleep_ms(ms) Sleep(ms);
#elif defined(NP2_THREAD_POSIX)
#define NP2_Sleep_ms(ms) usleep(ms * 1000);
#elif defined(NP2_THREAD_SDL2)
#define NP2_Sleep_ms(ms) SDL_Delay(ms);
#elif defined(NP2_THREAD_LR)
#define NP2_Sleep_ms(ms) retro_sleep(ms);
#endif
#endif /* SUPPORT_NP2_THREAD */
#endif /* _NP2_THREAD_H_ */