OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
gsPlatformThread.h
1
3#ifndef __GSPLATFORMTHREAD_H__
4#define __GSPLATFORMTHREAD_H__
5
6
7#include "gsPlatform.h"
8
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14
17// Thread types
18#if defined(_WIN32)
19 typedef CRITICAL_SECTION GSICriticalSection;
20 typedef HANDLE GSISemaphoreID;
21 typedef HANDLE GSIThreadID;
22 typedef DWORD (WINAPI *GSThreadFunc)(void *arg);
23
24#elif defined(_PS2)
25 typedef int GSIThreadID;
26 typedef int GSISemaphoreID;
27 typedef struct
28 {
29 // A critical section is a re-entrant semaphore
30 GSISemaphoreID mSemaphore;
31 GSIThreadID mOwnerThread;
32 gsi_u32 mEntryCount; // track re-entry
33 gsi_u32 mPad; // make 16bytes
34 } GSICriticalSection;
35 typedef void (*GSThreadFunc)(void *arg);
36
37#elif defined(_NITRO)
38 typedef OSMutex GSICriticalSection;
39 typedef struct
40 {
41 OSMutex mLock;
42 gsi_i32 mValue;
43 gsi_i32 mMax;
44 } GSISemaphoreID;
45 typedef struct
46 {
47 OSThread mThread;
48 void * mStack;
49 } GSIThreadID;
50 typedef void (*GSThreadFunc)(void *arg);
51
52#elif defined(_REVOLUTION)
53 typedef OSMutex GSICriticalSection;
54 typedef OSSemaphore GSISemaphoreID;
55 typedef struct
56 {
57 OSThread mThread;
58 void * mStack;
59 } GSIThreadID;
60 typedef void *(*GSThreadFunc)(void *arg);
61
62#elif defined(_PSP)
63 // Todo: Test PSP thread code, then remove this define
64 #define GSI_NO_THREADS
65 typedef int GSIThreadID;
66 typedef int GSISemaphoreID;
67 typedef struct
68 {
69 // A critical section is a re-entrant semaphore
70 GSISemaphoreID mSemaphore;
71 GSIThreadID mOwnerThread;
72 gsi_u32 mEntryCount; // track re-entry
73 gsi_u32 mPad; // make 16bytes
74 } GSICriticalSection;
75 typedef void (*GSThreadFunc)(void *arg);
76
77#elif defined(_PS3)
78 // Todo: Test PS3 ppu thread code, then remove this define
79 #define GSI_NO_THREADS
80 typedef int GSIThreadID;
81 typedef int GSISemaphoreID;
82 typedef struct
83 {
84 // A critical section is a re-entrant semaphore
85 GSISemaphoreID mSemaphore;
86 GSIThreadID mOwnerThread;
87 gsi_u32 mEntryCount; // track re-entry
88 gsi_u32 mPad; // make 16bytes
89 } GSICriticalSection;
90 typedef void (*GSThreadFunc)(void *arg);
91
92#elif defined(_UNIX) // Linux, Mac OS X, openbsd, etc.
93 #if defined(__OpenBSD__)
94 #include <pthread.h>
95 #endif
96
97 typedef pthread_mutex_t GSICriticalSection;
98 typedef struct
99 {
100 pthread_mutex_t mLock;
101 gsi_i32 mValue;
102 gsi_i32 mMax;
103 } GSISemaphoreID;
104 typedef struct
105 {
106 pthread_t thread;
107 pthread_attr_t attr;
108 } GSIThreadID;
109 typedef void (*GSThreadFunc)(void *arg);
110
111#else
112 #define GSI_NO_THREADS
113#endif
114
115#if defined(WIN32)
116 #define GSI_INFINITE INFINITE
117#else
118 #define GSI_INFINITE (gsi_u32)(-1)
119#endif
120
121
122#if !defined(GSI_NO_THREADS)
123 // The increment/read operations must not be preempted
124 #if defined(_WIN32)
125 #define gsiInterlockedIncrement(a) InterlockedIncrement((long*)a)
126 #define gsiInterlockedDecrement(a) InterlockedDecrement((long*)a)
127 #elif defined(_PS2)
128 gsi_u32 gsiInterlockedIncrement(gsi_u32* num);
129 gsi_u32 gsiInterlockedDecrement(gsi_u32* num);
130 #elif defined(_PS3)
131 // TODO - threading in PS3 uses pthreads, just like Linux
132 #elif defined(_NITRO)
133 gsi_u32 gsiInterlockedIncrement(gsi_u32* num);
134 gsi_u32 gsiInterlockedDecrement(gsi_u32* num);
135 #elif defined(_REVOLUTION)
136 gsi_u32 gsiInterlockedIncrement(gsi_u32* num);
137 gsi_u32 gsiInterlockedDecrement(gsi_u32* num);
138 #elif defined(_UNIX)
139 gsi_u32 gsiInterlockedIncrement(gsi_u32* num);
140 gsi_u32 gsiInterlockedDecrement(gsi_u32* num);
141 #endif
142
143#else
144 // Don't worry about concurrancy when GSI_NO_THREADS is defined
145 #define gsiInterlockedIncrement(a) (++(*a))
146 #define gsiInterlockedDecrement(a) (--(*a))
147#endif
148
149
150
153#if !defined(GSI_NO_THREADS)
154 int gsiStartThread(GSThreadFunc aThreadFunc, gsi_u32 theStackSize, void *arg, GSIThreadID* theThreadIdOut);
155 void gsiCancelThread(GSIThreadID theThreadID);
156 void gsiExitThread(GSIThreadID theThreadID);
157 void gsiCleanupThread(GSIThreadID theThreadID);
158
159 // Thread Synchronization - Startup/Shutdown
160 gsi_u32 gsiHasThreadShutdown(GSIThreadID theThreadID);
161
162 // Thread Synchronization - Critical Section
163 void gsiInitializeCriticalSection(GSICriticalSection *theCrit);
164 void gsiEnterCriticalSection(GSICriticalSection *theCrit);
165 void gsiLeaveCriticalSection(GSICriticalSection *theCrit);
166 void gsiDeleteCriticalSection(GSICriticalSection *theCrit);
167
168 // Thread Synchronization - Semaphore
169 GSISemaphoreID gsiCreateSemaphore(gsi_i32 theInitialCount, gsi_i32 theMaxCount, char* theName);
170 gsi_u32 gsiWaitForSemaphore(GSISemaphoreID theSemaphore, gsi_u32 theTimeoutMs);
171 void gsiReleaseSemaphore(GSISemaphoreID theSemaphore, gsi_i32 theReleaseCount);
172 void gsiCloseSemaphore(GSISemaphoreID theSemaphore);
173
174#else
175 // NO THREADS - stub everything to unused
176 #define gsiStartThread(a, b, c, d) (-1) // must return something
177 #define gsiCancelThread(a)
178 #define gsiExitThread(a)
179 #define gsiCleanupThread(a)
180
181 #define gsiHasThreadShutdown(a) (1) // must return something
182
183 #define gsiInitializeCriticalSection(a)
184 #define gsiEnterCriticalSection(a)
185 #define gsiLeaveCriticalSection(a)
186 #define gsiDeleteCriticalSection(a)
187
188 #define gsiCreateSemaphore(a,b,c) (-1)
189 #define gsiWaitForSemaphore(a,b) (0)
190 #define gsiReleaseSemaphore(a,b)
191 #define gsiCloseSemaphore(a)
192
193#endif // GSI_NO_THREADS
194
197#ifdef __cplusplus
198}
199#endif
200
201#endif // __GSPLATFORMTHREAD_H__