WUT  0.1
Wii U Toolchain
thread.h
Go to the documentation of this file.
1 #pragma once
2 #include <wut.h>
3 #include "time.h"
4 #include "threadqueue.h"
5 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 typedef struct OSContext OSContext;
17 typedef struct OSFastMutex OSFastMutex;
19 typedef struct OSMutex OSMutex;
20 typedef struct OSMutexQueue OSMutexQueue;
21 typedef struct OSThread OSThread;
22 
23 typedef uint8_t OSThreadState;
24 typedef uint32_t OSThreadRequest;
25 typedef uint8_t OSThreadAttributes;
26 
27 typedef int (*OSThreadEntryPointFn)(int argc, const char **argv);
28 typedef void (*OSThreadCleanupCallbackFn)(OSThread *thread, void *stack);
29 typedef void (*OSThreadDeallocatorFn)(OSThread *thread, void *stack);
30 
31 enum OSThreadState
32 {
38 };
39 
40 enum OSThreadRequest
41 {
45 };
46 
47 enum OSThreadAttributes
48 {
54 };
55 
56 #define OS_CONTEXT_TAG 0x4F53436F6E747874ull
57 
58 struct OSContext
59 {
60  uint64_t tag;
61  uint32_t gpr[32];
62  uint32_t cr;
63  uint32_t lr;
64  uint32_t ctr;
65  uint32_t xer;
66  uint32_t srr0;
67  uint32_t srr1;
68  UNKNOWN(0x14);
69  uint32_t fpscr;
70  double fpr[32];
71  uint16_t spinLockCount;
72  uint16_t state;
73  uint32_t gqr[8];
74  UNKNOWN(4);
75  double psf[32];
76  uint64_t coretime[3];
77  uint64_t starttime;
78  uint32_t error;
79  UNKNOWN(4);
80  uint32_t pmc1;
81  uint32_t pmc2;
82  uint32_t pmc3;
83  uint32_t pmc4;
84  uint32_t mmcr0;
85  uint32_t mmcr1;
86 };
87 CHECK_OFFSET(OSContext, 0x00, tag);
88 CHECK_OFFSET(OSContext, 0x08, gpr);
89 CHECK_OFFSET(OSContext, 0x88, cr);
90 CHECK_OFFSET(OSContext, 0x8c, lr);
91 CHECK_OFFSET(OSContext, 0x90, ctr);
92 CHECK_OFFSET(OSContext, 0x94, xer);
93 CHECK_OFFSET(OSContext, 0x98, srr0);
94 CHECK_OFFSET(OSContext, 0x9c, srr1);
95 CHECK_OFFSET(OSContext, 0xb4, fpscr);
96 CHECK_OFFSET(OSContext, 0xb8, fpr);
97 CHECK_OFFSET(OSContext, 0x1b8, spinLockCount);
98 CHECK_OFFSET(OSContext, 0x1ba, state);
99 CHECK_OFFSET(OSContext, 0x1bc, gqr);
100 CHECK_OFFSET(OSContext, 0x1e0, psf);
101 CHECK_OFFSET(OSContext, 0x2e0, coretime);
102 CHECK_OFFSET(OSContext, 0x2f8, starttime);
103 CHECK_OFFSET(OSContext, 0x300, error);
104 CHECK_OFFSET(OSContext, 0x308, pmc1);
105 CHECK_OFFSET(OSContext, 0x30c, pmc2);
106 CHECK_OFFSET(OSContext, 0x310, pmc3);
107 CHECK_OFFSET(OSContext, 0x314, pmc4);
108 CHECK_OFFSET(OSContext, 0x318, mmcr0);
109 CHECK_OFFSET(OSContext, 0x31c, mmcr1);
110 CHECK_SIZE(OSContext, 0x320);
111 
113 {
116  void *parent;
117  UNKNOWN(4);
118 };
119 CHECK_OFFSET(OSMutexQueue, 0x0, head);
120 CHECK_OFFSET(OSMutexQueue, 0x4, tail);
121 CHECK_OFFSET(OSMutexQueue, 0x8, parent);
122 CHECK_SIZE(OSMutexQueue, 0x10);
123 
125 {
128 };
129 CHECK_OFFSET(OSFastMutexQueue, 0x00, head);
130 CHECK_OFFSET(OSFastMutexQueue, 0x04, tail);
131 CHECK_SIZE(OSFastMutexQueue, 0x08);
132 
133 #define OS_THREAD_TAG 0x74487244u
134 
135 #pragma pack(push, 1)
136 struct OSThread
137 {
139  uint32_t tag;
140  OSThreadState state;
141  OSThreadAttributes attr;
142  uint16_t id;
143  int32_t suspendCounter;
144  int32_t priority; // Actual priority of thread
145  int32_t basePriority; // Base priority of thread
146  int32_t exitValue; // Value from OSExitThread
147  UNKNOWN(0x35C - 0x338);
148  OSThreadQueue *queue; // Queue the thread is on
149  OSThreadLink link; // Thread queue link
150  OSThreadQueue joinQueue; // Queue of threads waiting to join this
151  OSMutex *mutex; // Mutex we are waiting to lock
152  OSMutexQueue mutexQueue; // Mutexes owned by this thread
153  OSThreadLink activeLink; // Link on active thread queue
154  void *stackStart; // Stack starting value (top, highest address)
155  void *stackEnd; // Stack end value (bottom, lowest address)
156  OSThreadEntryPointFn entryPoint; // Entry point from OSCreateThread
157  UNKNOWN(0x57c - 0x3a0);
158  uint32_t specific[0x10]; // OSSetThreadSpecific / OSGetThreadSpecific
159  UNKNOWN(0x5c0 - 0x5bc);
160  const char *name; // Thread name
161  UNKNOWN(0x4);
162  void *userStackPointer; // The stack specified in OSCreateThread
163  OSThreadCleanupCallbackFn cleanupCallback; // Set with OSSetThreadCleanupCallback
164  OSThreadDeallocatorFn deallocator; // Set with OSSetThreadDeallocator
165  uint32_t cancelState; // Is listening to requestFlag enabled
166  OSThreadRequest requestFlag; // Request flag for cancel or suspend
167  int32_t needSuspend; // How many pending suspends we have
168  int32_t suspendResult; // Result of suspend
169  OSThreadQueue suspendQueue; // Queue of threads waiting for suspend to finish
170  UNKNOWN(0x69c - 0x5f4);
171 };
172 #pragma pack(pop)
173 CHECK_OFFSET(OSThread, 0x320, tag);
174 CHECK_OFFSET(OSThread, 0x324, state);
175 CHECK_OFFSET(OSThread, 0x325, attr);
176 CHECK_OFFSET(OSThread, 0x326, id);
177 CHECK_OFFSET(OSThread, 0x328, suspendCounter);
178 CHECK_OFFSET(OSThread, 0x32c, priority);
179 CHECK_OFFSET(OSThread, 0x330, basePriority);
180 CHECK_OFFSET(OSThread, 0x334, exitValue);
181 CHECK_OFFSET(OSThread, 0x35c, queue);
182 CHECK_OFFSET(OSThread, 0x360, link);
183 CHECK_OFFSET(OSThread, 0x368, joinQueue);
184 CHECK_OFFSET(OSThread, 0x378, mutex);
185 CHECK_OFFSET(OSThread, 0x37c, mutexQueue);
186 CHECK_OFFSET(OSThread, 0x38c, activeLink);
187 CHECK_OFFSET(OSThread, 0x394, stackStart);
188 CHECK_OFFSET(OSThread, 0x398, stackEnd);
189 CHECK_OFFSET(OSThread, 0x39c, entryPoint);
190 CHECK_OFFSET(OSThread, 0x57c, specific);
191 CHECK_OFFSET(OSThread, 0x5c0, name);
192 CHECK_OFFSET(OSThread, 0x5c8, userStackPointer);
193 CHECK_OFFSET(OSThread, 0x5cc, cleanupCallback);
194 CHECK_OFFSET(OSThread, 0x5d0, deallocator);
195 CHECK_OFFSET(OSThread, 0x5d4, cancelState);
196 CHECK_OFFSET(OSThread, 0x5d8, requestFlag);
197 CHECK_OFFSET(OSThread, 0x5dc, needSuspend);
198 CHECK_OFFSET(OSThread, 0x5e0, suspendResult);
199 CHECK_OFFSET(OSThread, 0x5e4, suspendQueue);
200 CHECK_SIZE(OSThread, 0x69c);
201 
202 void
203 OSCancelThread(OSThread *thread);
204 
205 int32_t
207 
208 int32_t
210 
211 void
213 
214 void
215 OSContinueThread(OSThread *thread);
216 
217 BOOL
218 OSCreateThread(OSThread *thread,
219  OSThreadEntryPointFn entry,
220  int32_t argc,
221  char *argv,
222  void *stack,
223  uint32_t stackSize,
224  int32_t priority,
225  OSThreadAttributes attributes);
226 
227 void
228 OSDetachThread(OSThread *thread);
229 
230 void
231 OSExitThread(int32_t result);
232 
233 void
235  OSThreadLink *link);
236 
237 OSThread *
239 
240 OSThread *
241 OSGetDefaultThread(uint32_t coreID);
242 
243 uint32_t
245 
246 uint32_t
248 
249 const char *
250 OSGetThreadName(OSThread *thread);
251 
252 int32_t
254 
255 uint32_t
256 OSGetThreadSpecific(uint32_t id);
257 
258 BOOL
260 
261 BOOL
263 
264 BOOL
265 OSJoinThread(OSThread *thread,
266  int *threadResult);
267 
268 void
270 
271 int32_t
272 OSResumeThread(OSThread *thread);
273 
274 BOOL
275 OSRunThread(OSThread *thread,
276  OSThreadEntryPointFn entry,
277  int argc,
278  const char **argv);
279 
280 BOOL
282  uint32_t affinity);
283 
284 BOOL
286 
289  OSThreadCleanupCallbackFn callback);
290 
293  OSThreadDeallocatorFn deallocator);
294 
295 void
296 OSSetThreadName(OSThread *thread,
297  const char *name);
298 
299 BOOL
301  int32_t priority);
302 
303 BOOL
305  uint32_t quantum);
306 
307 void
308 OSSetThreadSpecific(uint32_t id,
309  uint32_t value);
310 
311 BOOL
313 
314 void
316 
317 void
318 OSSleepTicks(OSTime ticks);
319 
320 uint32_t
321 OSSuspendThread(OSThread *thread);
322 
323 void
325 
326 void
328 
329 void
330 OSYieldThread();
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 
OSThreadCleanupCallbackFn cleanupCallback
Definition: thread.h:163
OSThreadRequest
Definition: thread.h:40
OSThreadDeallocatorFn deallocator
Definition: thread.h:164
uint32_t mmcr0
Definition: thread.h:84
uint32_t gpr[32]
Definition: thread.h:61
void OSSetThreadSpecific(uint32_t id, uint32_t value)
uint32_t srr1
Definition: thread.h:67
int32_t priority
Definition: thread.h:144
OSThreadLink link
Definition: thread.h:149
uint32_t gqr[8]
Definition: thread.h:73
OSThreadQueue * queue
Definition: thread.h:148
OSContext context
Definition: thread.h:138
BOOL OSCreateThread(OSThread *thread, OSThreadEntryPointFn entry, int32_t argc, char *argv, void *stack, uint32_t stackSize, int32_t priority, OSThreadAttributes attributes)
int64_t OSTime
Definition: time.h:17
int32_t OSCheckThreadStackUsage(OSThread *thread)
double fpr[32]
Definition: thread.h:70
int32_t suspendCounter
Definition: thread.h:143
uint64_t tag
Definition: thread.h:60
OSThread * OSGetDefaultThread(uint32_t coreID)
int32_t basePriority
Definition: thread.h:145
OSThread * OSGetCurrentThread()
int32_t OSGetThreadPriority(OSThread *thread)
void OSCancelThread(OSThread *thread)
uint32_t pmc1
Definition: thread.h:80
const char * name
Definition: thread.h:160
uint16_t id
Definition: thread.h:142
void OSSleepTicks(OSTime ticks)
void * stackStart
Definition: thread.h:154
void(* OSThreadDeallocatorFn)(OSThread *thread, void *stack)
Definition: thread.h:29
BOOL OSIsThreadSuspended(OSThread *thread)
void OSYieldThread()
const char * OSGetThreadName(OSThread *thread)
uint32_t pmc3
Definition: thread.h:82
int32_t exitValue
Definition: thread.h:146
OSThreadLink activeLink
Definition: thread.h:153
OSMutex * tail
Definition: thread.h:115
BOOL OSSetThreadRunQuantum(OSThread *thread, uint32_t quantum)
OSThreadAttributes attr
Definition: thread.h:141
uint32_t ctr
Definition: thread.h:64
uint32_t OSSuspendThread(OSThread *thread)
OSThreadState state
Definition: thread.h:140
void OSDetachThread(OSThread *thread)
BOOL OSSetThreadCancelState(BOOL state)
int(* OSThreadEntryPointFn)(int argc, const char **argv)
Definition: thread.h:27
BOOL OSSetThreadPriority(OSThread *thread, int32_t priority)
int32_t OSResumeThread(OSThread *thread)
int32_t needSuspend
Definition: thread.h:167
OSMutex * mutex
Definition: thread.h:151
uint32_t srr0
Definition: thread.h:66
uint32_t OSGetStackPointer()
void OSGetActiveThreadLink(OSThread *thread, OSThreadLink *link)
int32_t OSCheckActiveThreads()
void OSPrintCurrentThreadState()
void * stackEnd
Definition: thread.h:155
void * parent
Definition: thread.h:116
uint32_t cr
Definition: thread.h:62
OSThreadState
Definition: thread.h:31
BOOL OSSetThreadAffinity(OSThread *thread, uint32_t affinity)
uint16_t spinLockCount
Definition: thread.h:71
BOOL OSJoinThread(OSThread *thread, int *threadResult)
uint32_t tag
Definition: thread.h:139
OSThreadAttributes
Definition: thread.h:47
void OSWakeupThread(OSThreadQueue *queue)
uint32_t pmc2
Definition: thread.h:81
void OSContinueThread(OSThread *thread)
OSThreadRequest requestFlag
Definition: thread.h:166
OSFastMutex * head
Definition: thread.h:126
void OSSetThreadName(OSThread *thread, const char *name)
uint32_t specific[0x10]
Definition: thread.h:158
OSThreadEntryPointFn entryPoint
Definition: thread.h:156
uint32_t fpscr
Definition: thread.h:69
OSThreadQueue suspendQueue
Definition: thread.h:169
BOOL OSSetThreadStackUsage(OSThread *thread)
int BOOL
Definition: wut_types.h:4
uint32_t lr
Definition: thread.h:63
Definition: mutex.h:32
uint32_t cancelState
Definition: thread.h:165
void * userStackPointer
Definition: thread.h:162
uint64_t starttime
Definition: thread.h:77
OSMutex * head
Definition: thread.h:114
uint64_t coretime[3]
Definition: thread.h:76
int32_t suspendResult
Definition: thread.h:168
uint32_t xer
Definition: thread.h:65
uint32_t error
Definition: thread.h:78
uint32_t OSGetThreadAffinity(OSThread *thread)
OSThreadQueue joinQueue
Definition: thread.h:150
void OSSleepThread(OSThreadQueue *queue)
BOOL OSRunThread(OSThread *thread, OSThreadEntryPointFn entry, int argc, const char **argv)
double psf[32]
Definition: thread.h:75
void OSTestThreadCancel()
void OSExitThread(int32_t result)
uint32_t mmcr1
Definition: thread.h:85
OSThreadDeallocatorFn OSSetThreadDeallocator(OSThread *thread, OSThreadDeallocatorFn deallocator)
OSThreadCleanupCallbackFn OSSetThreadCleanupCallback(OSThread *thread, OSThreadCleanupCallbackFn callback)
OSMutexQueue mutexQueue
Definition: thread.h:152
uint16_t state
Definition: thread.h:72
void(* OSThreadCleanupCallbackFn)(OSThread *thread, void *stack)
Definition: thread.h:28
OSFastMutex * tail
Definition: thread.h:127
BOOL OSIsThreadTerminated(OSThread *thread)
uint32_t pmc4
Definition: thread.h:83
uint32_t OSGetThreadSpecific(uint32_t id)
void OSClearThreadStackUsage(OSThread *thread)