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 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 typedef struct OSContext OSContext;
31 typedef struct OSFastMutex OSFastMutex;
33 typedef struct OSMutex OSMutex;
34 typedef struct OSMutexQueue OSMutexQueue;
35 typedef struct OSThread OSThread;
36 
37 typedef uint8_t OSThreadState;
38 typedef uint32_t OSThreadRequest;
39 typedef uint8_t OSThreadAttributes;
40 
41 typedef int (*OSThreadEntryPointFn)(int argc, const char **argv);
42 typedef void (*OSThreadCleanupCallbackFn)(OSThread *thread, void *stack);
43 typedef void (*OSThreadDeallocatorFn)(OSThread *thread, void *stack);
44 
45 enum OSThreadState
46 {
48 
51 
54 
57 
60 };
61 
62 enum OSThreadRequest
63 {
67 };
68 
69 enum OSThreadAttributes
70 {
73 
76 
79 
81  OS_THREAD_ATTRIB_AFFINITY_ANY = ((1 << 0) | (1 << 1) | (1 << 2)),
82 
85 
88 };
89 
90 #define OS_CONTEXT_TAG 0x4F53436F6E747874ull
91 
92 struct OSContext
93 {
95  uint64_t tag;
96 
97  uint32_t gpr[32];
98  uint32_t cr;
99  uint32_t lr;
100  uint32_t ctr;
101  uint32_t xer;
102  uint32_t srr0;
103  uint32_t srr1;
104  UNKNOWN(0x14);
105  uint32_t fpscr;
106  double fpr[32];
107  uint16_t spinLockCount;
108  uint16_t state;
109  uint32_t gqr[8];
110  UNKNOWN(4);
111  double psf[32];
112  uint64_t coretime[3];
113  uint64_t starttime;
114  uint32_t error;
115  UNKNOWN(4);
116  uint32_t pmc1;
117  uint32_t pmc2;
118  uint32_t pmc3;
119  uint32_t pmc4;
120  uint32_t mmcr0;
121  uint32_t mmcr1;
122 };
123 CHECK_OFFSET(OSContext, 0x00, tag);
124 CHECK_OFFSET(OSContext, 0x08, gpr);
125 CHECK_OFFSET(OSContext, 0x88, cr);
126 CHECK_OFFSET(OSContext, 0x8c, lr);
127 CHECK_OFFSET(OSContext, 0x90, ctr);
128 CHECK_OFFSET(OSContext, 0x94, xer);
129 CHECK_OFFSET(OSContext, 0x98, srr0);
130 CHECK_OFFSET(OSContext, 0x9c, srr1);
131 CHECK_OFFSET(OSContext, 0xb4, fpscr);
132 CHECK_OFFSET(OSContext, 0xb8, fpr);
133 CHECK_OFFSET(OSContext, 0x1b8, spinLockCount);
134 CHECK_OFFSET(OSContext, 0x1ba, state);
135 CHECK_OFFSET(OSContext, 0x1bc, gqr);
136 CHECK_OFFSET(OSContext, 0x1e0, psf);
137 CHECK_OFFSET(OSContext, 0x2e0, coretime);
138 CHECK_OFFSET(OSContext, 0x2f8, starttime);
139 CHECK_OFFSET(OSContext, 0x300, error);
140 CHECK_OFFSET(OSContext, 0x308, pmc1);
141 CHECK_OFFSET(OSContext, 0x30c, pmc2);
142 CHECK_OFFSET(OSContext, 0x310, pmc3);
143 CHECK_OFFSET(OSContext, 0x314, pmc4);
144 CHECK_OFFSET(OSContext, 0x318, mmcr0);
145 CHECK_OFFSET(OSContext, 0x31c, mmcr1);
146 CHECK_SIZE(OSContext, 0x320);
147 
149 {
152  void *parent;
153  UNKNOWN(4);
154 };
155 CHECK_OFFSET(OSMutexQueue, 0x0, head);
156 CHECK_OFFSET(OSMutexQueue, 0x4, tail);
157 CHECK_OFFSET(OSMutexQueue, 0x8, parent);
158 CHECK_SIZE(OSMutexQueue, 0x10);
159 
161 {
164 };
165 CHECK_OFFSET(OSFastMutexQueue, 0x00, head);
166 CHECK_OFFSET(OSFastMutexQueue, 0x04, tail);
167 CHECK_SIZE(OSFastMutexQueue, 0x08);
168 
169 #define OS_THREAD_TAG 0x74487244u
170 
171 struct OSThread
172 {
174  uint32_t tag;
175  OSThreadState state;
176  OSThreadAttributes attr;
177  uint16_t id;
178 
180  int32_t suspendCounter;
181 
183  int32_t priority;
184 
186  int32_t basePriority;
187 
189  int32_t exitValue;
190 
191  UNKNOWN(0x35C - 0x338);
192 
195 
198 
201 
204 
207 
210 
212  void *stackStart;
213 
215  void *stackEnd;
216 
219 
220  UNKNOWN(0x57c - 0x3a0);
221 
223  uint32_t specific[0x10];
224 
225  UNKNOWN(0x5c0 - 0x5bc);
226 
228  const char *name;
229 
230  UNKNOWN(0x4);
231 
234 
237 
240 
243 
245  OSThreadRequest requestFlag;
246 
248  int32_t needSuspend;
249 
251  int32_t suspendResult;
252 
255  UNKNOWN(0x69c - 0x5f4);
256 };
257 CHECK_OFFSET(OSThread, 0x320, tag);
258 CHECK_OFFSET(OSThread, 0x324, state);
259 CHECK_OFFSET(OSThread, 0x325, attr);
260 CHECK_OFFSET(OSThread, 0x326, id);
261 CHECK_OFFSET(OSThread, 0x328, suspendCounter);
262 CHECK_OFFSET(OSThread, 0x32c, priority);
263 CHECK_OFFSET(OSThread, 0x330, basePriority);
264 CHECK_OFFSET(OSThread, 0x334, exitValue);
265 CHECK_OFFSET(OSThread, 0x35c, queue);
266 CHECK_OFFSET(OSThread, 0x360, link);
267 CHECK_OFFSET(OSThread, 0x368, joinQueue);
268 CHECK_OFFSET(OSThread, 0x378, mutex);
269 CHECK_OFFSET(OSThread, 0x37c, mutexQueue);
270 CHECK_OFFSET(OSThread, 0x38c, activeLink);
271 CHECK_OFFSET(OSThread, 0x394, stackStart);
272 CHECK_OFFSET(OSThread, 0x398, stackEnd);
273 CHECK_OFFSET(OSThread, 0x39c, entryPoint);
274 CHECK_OFFSET(OSThread, 0x57c, specific);
275 CHECK_OFFSET(OSThread, 0x5c0, name);
276 CHECK_OFFSET(OSThread, 0x5c8, userStackPointer);
277 CHECK_OFFSET(OSThread, 0x5cc, cleanupCallback);
278 CHECK_OFFSET(OSThread, 0x5d0, deallocator);
279 CHECK_OFFSET(OSThread, 0x5d4, cancelState);
280 CHECK_OFFSET(OSThread, 0x5d8, requestFlag);
281 CHECK_OFFSET(OSThread, 0x5dc, needSuspend);
282 CHECK_OFFSET(OSThread, 0x5e0, suspendResult);
283 CHECK_OFFSET(OSThread, 0x5e4, suspendQueue);
284 CHECK_SIZE(OSThread, 0x69c);
285 
286 
293 void
294 OSCancelThread(OSThread *thread);
295 
296 
300 int32_t
302 
303 
307 int32_t
309 
310 
314 void
316 
317 
321 void
322 OSContinueThread(OSThread *thread);
323 
324 
337 BOOL
338 OSCreateThread(OSThread *thread,
339  OSThreadEntryPointFn entry,
340  int32_t argc,
341  char *argv,
342  void *stack,
343  uint32_t stackSize,
344  int32_t priority,
345  OSThreadAttributes attributes);
346 
347 
351 void
352 OSDetachThread(OSThread *thread);
353 
354 
360 void
361 OSExitThread(int32_t result);
362 
363 
367 void
369  OSThreadLink *link);
370 
371 
375 OSThread *
377 
378 
386 OSThread *
387 OSGetDefaultThread(uint32_t coreID);
388 
389 
393 uint32_t
395 
396 
400 uint32_t
402 
403 
407 const char *
408 OSGetThreadName(OSThread *thread);
409 
410 
414 int32_t
416 
417 
421 uint32_t
422 OSGetThreadSpecific(uint32_t id);
423 
424 
428 BOOL
430 
431 
435 BOOL
437 
438 
448 BOOL
449 OSJoinThread(OSThread *thread,
450  int *threadResult);
451 
452 
461 int32_t
462 OSResumeThread(OSThread *thread);
463 
464 
470 BOOL
471 OSRunThread(OSThread *thread,
472  OSThreadEntryPointFn entry,
473  int argc,
474  const char **argv);
475 
476 
480 BOOL
482  uint32_t affinity);
483 
484 
491 BOOL
493 
494 
500  OSThreadCleanupCallbackFn callback);
501 
502 
508  OSThreadDeallocatorFn deallocator);
509 
510 
514 void
515 OSSetThreadName(OSThread *thread,
516  const char *name);
517 
518 
522 BOOL
524  int32_t priority);
525 
526 
533 BOOL
535  uint32_t quantum);
536 
542 void
543 OSSetThreadSpecific(uint32_t id,
544  uint32_t value);
545 
546 
550 BOOL
552 
553 
559 void
561 
562 
566 void
567 OSSleepTicks(OSTime ticks);
568 
569 
578 uint32_t
579 OSSuspendThread(OSThread *thread);
580 
581 
595 void
597 
598 
604 void
606 
607 
614 void
615 OSYieldThread();
616 
617 
618 #ifdef __cplusplus
619 }
620 #endif
621 
OSThreadCleanupCallbackFn cleanupCallback
Called just before thread is terminated, set with OSSetThreadCleanupCallback.
Definition: thread.h:236
OSThreadRequest
Definition: thread.h:62
OSThreadDeallocatorFn deallocator
Called just after a thread is terminated, set with OSSetThreadDeallocator.
Definition: thread.h:239
uint32_t mmcr0
Definition: thread.h:120
Allow the thread to run on CPU1.
Definition: thread.h:75
uint32_t gpr[32]
Definition: thread.h:97
void OSSetThreadSpecific(uint32_t id, uint32_t value)
uint32_t srr1
Definition: thread.h:103
int32_t priority
Actual priority of thread.
Definition: thread.h:183
OSThreadLink link
Link used for thread queue.
Definition: thread.h:197
uint32_t gqr[8]
Definition: thread.h:109
OSThreadQueue * queue
Queue the thread is currently waiting on.
Definition: thread.h:194
OSContext context
Definition: thread.h:173
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:106
int32_t suspendCounter
Suspend count (increased by OSSuspendThread).
Definition: thread.h:180
uint64_t tag
Should always be set to the value OS_CONTEXT_TAG.
Definition: thread.h:95
OSThread * OSGetDefaultThread(uint32_t coreID)
int32_t basePriority
Base priority of thread, 0 is highest priority, 31 is lowest priority.
Definition: thread.h:186
OSThread * OSGetCurrentThread()
int32_t OSGetThreadPriority(OSThread *thread)
void OSCancelThread(OSThread *thread)
uint32_t pmc1
Definition: thread.h:116
Allow the thread to run any CPU.
Definition: thread.h:81
const char * name
Thread name, accessed with OSSetThreadName and OSGetThreadName.
Definition: thread.h:228
uint16_t id
Definition: thread.h:177
Thread is waiting, i.e. on a mutex.
Definition: thread.h:56
void OSSleepTicks(OSTime ticks)
void * stackStart
Stack start (top, highest address)
Definition: thread.h:212
void(* OSThreadDeallocatorFn)(OSThread *thread, void *stack)
Definition: thread.h:43
Enables tracking of stack usage.
Definition: thread.h:87
Allow the thread to run on CPU0.
Definition: thread.h:72
BOOL OSIsThreadSuspended(OSThread *thread)
void OSYieldThread()
const char * OSGetThreadName(OSThread *thread)
uint32_t pmc3
Definition: thread.h:118
int32_t exitValue
Exit value.
Definition: thread.h:189
OSThreadLink activeLink
Link for global active thread queue.
Definition: thread.h:209
OSMutex * tail
Definition: thread.h:151
BOOL OSSetThreadRunQuantum(OSThread *thread, uint32_t quantum)
OSThreadAttributes attr
Definition: thread.h:176
uint32_t ctr
Definition: thread.h:100
uint32_t OSSuspendThread(OSThread *thread)
OSThreadState state
Definition: thread.h:175
void OSDetachThread(OSThread *thread)
BOOL OSSetThreadCancelState(BOOL state)
int(* OSThreadEntryPointFn)(int argc, const char **argv)
Definition: thread.h:41
BOOL OSSetThreadPriority(OSThread *thread, int32_t priority)
int32_t OSResumeThread(OSThread *thread)
int32_t needSuspend
Pending suspend request count.
Definition: thread.h:248
OSMutex * mutex
Mutex this thread is waiting to lock.
Definition: thread.h:203
uint32_t srr0
Definition: thread.h:102
uint32_t OSGetStackPointer()
BOOL cancelState
If TRUE then a thread can be cancelled or suspended, set with OSSetThreadCancelState.
Definition: thread.h:242
void OSGetActiveThreadLink(OSThread *thread, OSThreadLink *link)
int32_t OSCheckActiveThreads()
void * stackEnd
Stack end (bottom, lowest address)
Definition: thread.h:215
void * parent
Definition: thread.h:152
uint32_t cr
Definition: thread.h:98
OSThreadState
Definition: thread.h:45
BOOL OSSetThreadAffinity(OSThread *thread, uint32_t affinity)
uint16_t spinLockCount
Definition: thread.h:107
BOOL OSJoinThread(OSThread *thread, int *threadResult)
uint32_t tag
Definition: thread.h:174
OSThreadAttributes
Definition: thread.h:69
Allow the thread to run on CPU2.
Definition: thread.h:78
void OSWakeupThread(OSThreadQueue *queue)
Thread is ready to run.
Definition: thread.h:50
uint32_t pmc2
Definition: thread.h:117
void OSContinueThread(OSThread *thread)
OSThreadRequest requestFlag
Current thread request, used for cancelleing and suspending the thread.
Definition: thread.h:245
OSFastMutex * head
Definition: thread.h:162
void OSSetThreadName(OSThread *thread, const char *name)
Start the thread detached.
Definition: thread.h:84
uint32_t specific[0x10]
Thread specific values, accessed with OSSetThreadSpecific and OSGetThreadSpecific.
Definition: thread.h:223
OSThreadEntryPointFn entryPoint
Thread entry point.
Definition: thread.h:218
uint32_t fpscr
Definition: thread.h:105
OSThreadQueue suspendQueue
Queue of threads waiting for a thread to be suspended.
Definition: thread.h:254
BOOL OSSetThreadStackUsage(OSThread *thread)
int BOOL
Definition: wut_types.h:4
uint32_t lr
Definition: thread.h:99
Definition: mutex.h:36
void * userStackPointer
The stack pointer passed in OSCreateThread.
Definition: thread.h:233
uint64_t starttime
Definition: thread.h:113
OSMutex * head
Definition: thread.h:150
uint64_t coretime[3]
Definition: thread.h:112
int32_t suspendResult
Result of thread suspend.
Definition: thread.h:251
uint32_t xer
Definition: thread.h:101
uint32_t error
Definition: thread.h:114
uint32_t OSGetThreadAffinity(OSThread *thread)
OSThreadQueue joinQueue
Queue of threads waiting to join this thread.
Definition: thread.h:200
void OSSleepThread(OSThreadQueue *queue)
BOOL OSRunThread(OSThread *thread, OSThreadEntryPointFn entry, int argc, const char **argv)
double psf[32]
Definition: thread.h:111
void OSTestThreadCancel()
void OSExitThread(int32_t result)
uint32_t mmcr1
Definition: thread.h:121
Thread is running.
Definition: thread.h:53
OSThreadDeallocatorFn OSSetThreadDeallocator(OSThread *thread, OSThreadDeallocatorFn deallocator)
OSThreadCleanupCallbackFn OSSetThreadCleanupCallback(OSThread *thread, OSThreadCleanupCallbackFn callback)
Thread is about to terminate.
Definition: thread.h:59
OSMutexQueue mutexQueue
Queue of mutexes this thread owns.
Definition: thread.h:206
uint16_t state
Definition: thread.h:108
void(* OSThreadCleanupCallbackFn)(OSThread *thread, void *stack)
Definition: thread.h:42
OSFastMutex * tail
Definition: thread.h:163
BOOL OSIsThreadTerminated(OSThread *thread)
uint32_t pmc4
Definition: thread.h:119
uint32_t OSGetThreadSpecific(uint32_t id)
void OSClearThreadStackUsage(OSThread *thread)