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 
38 typedef uint8_t OSThreadState;
39 
41 typedef uint32_t OSThreadRequest;
42 
44 typedef uint8_t OSThreadAttributes;
45 
46 typedef int (*OSThreadEntryPointFn)(int argc, const char **argv);
47 typedef void (*OSThreadCleanupCallbackFn)(OSThread *thread, void *stack);
48 typedef void (*OSThreadDeallocatorFn)(OSThread *thread, void *stack);
49 
51 {
53 
56 
59 
62 
65 };
66 
68 {
72 };
73 
75 {
78 
81 
84 
86  OS_THREAD_ATTRIB_AFFINITY_ANY = ((1 << 0) | (1 << 1) | (1 << 2)),
87 
90 
93 };
94 
95 #define OS_CONTEXT_TAG 0x4F53436F6E747874ull
96 
97 struct OSContext
98 {
100  uint64_t tag;
101 
102  uint32_t gpr[32];
103  uint32_t cr;
104  uint32_t lr;
105  uint32_t ctr;
106  uint32_t xer;
107  uint32_t srr0;
108  uint32_t srr1;
109  UNKNOWN(0x14);
110  uint32_t fpscr;
111  double fpr[32];
112  uint16_t spinLockCount;
113  uint16_t state;
114  uint32_t gqr[8];
115  UNKNOWN(4);
116  double psf[32];
117  uint64_t coretime[3];
118  uint64_t starttime;
119  uint32_t error;
120  UNKNOWN(4);
121  uint32_t pmc1;
122  uint32_t pmc2;
123  uint32_t pmc3;
124  uint32_t pmc4;
125  uint32_t mmcr0;
126  uint32_t mmcr1;
127 };
128 CHECK_OFFSET(OSContext, 0x00, tag);
129 CHECK_OFFSET(OSContext, 0x08, gpr);
130 CHECK_OFFSET(OSContext, 0x88, cr);
131 CHECK_OFFSET(OSContext, 0x8c, lr);
132 CHECK_OFFSET(OSContext, 0x90, ctr);
133 CHECK_OFFSET(OSContext, 0x94, xer);
134 CHECK_OFFSET(OSContext, 0x98, srr0);
135 CHECK_OFFSET(OSContext, 0x9c, srr1);
136 CHECK_OFFSET(OSContext, 0xb4, fpscr);
137 CHECK_OFFSET(OSContext, 0xb8, fpr);
138 CHECK_OFFSET(OSContext, 0x1b8, spinLockCount);
139 CHECK_OFFSET(OSContext, 0x1ba, state);
140 CHECK_OFFSET(OSContext, 0x1bc, gqr);
141 CHECK_OFFSET(OSContext, 0x1e0, psf);
142 CHECK_OFFSET(OSContext, 0x2e0, coretime);
143 CHECK_OFFSET(OSContext, 0x2f8, starttime);
144 CHECK_OFFSET(OSContext, 0x300, error);
145 CHECK_OFFSET(OSContext, 0x308, pmc1);
146 CHECK_OFFSET(OSContext, 0x30c, pmc2);
147 CHECK_OFFSET(OSContext, 0x310, pmc3);
148 CHECK_OFFSET(OSContext, 0x314, pmc4);
149 CHECK_OFFSET(OSContext, 0x318, mmcr0);
150 CHECK_OFFSET(OSContext, 0x31c, mmcr1);
151 CHECK_SIZE(OSContext, 0x320);
152 
154 {
157  void *parent;
158  UNKNOWN(4);
159 };
160 CHECK_OFFSET(OSMutexQueue, 0x0, head);
161 CHECK_OFFSET(OSMutexQueue, 0x4, tail);
162 CHECK_OFFSET(OSMutexQueue, 0x8, parent);
163 CHECK_SIZE(OSMutexQueue, 0x10);
164 
166 {
169 };
170 CHECK_OFFSET(OSFastMutexQueue, 0x00, head);
171 CHECK_OFFSET(OSFastMutexQueue, 0x04, tail);
172 CHECK_SIZE(OSFastMutexQueue, 0x08);
173 
174 #define OS_THREAD_TAG 0x74487244u
175 #pragma pack(push, 1)
176 struct OSThread
177 {
179 
181  uint32_t tag;
182 
184  OSThreadState state;
185 
187  OSThreadAttributes attr;
188 
190  uint16_t id;
191 
193  int32_t suspendCounter;
194 
196  int32_t priority;
197 
199  int32_t basePriority;
200 
202  int32_t exitValue;
203 
204  UNKNOWN(0x35C - 0x338);
205 
208 
211 
214 
217 
220 
223 
225  void *stackStart;
226 
228  void *stackEnd;
229 
232 
233  UNKNOWN(0x57c - 0x3a0);
234 
236  uint32_t specific[0x10];
237 
238  UNKNOWN(0x5c0 - 0x5bc);
239 
241  const char *name;
242 
243  UNKNOWN(0x4);
244 
247 
250 
253 
256 
258  OSThreadRequest requestFlag;
259 
261  int32_t needSuspend;
262 
264  int32_t suspendResult;
265 
268 
269  UNKNOWN(0x69c - 0x5f4);
270 };
271 #pragma pack(pop)
272 CHECK_OFFSET(OSThread, 0x320, tag);
273 CHECK_OFFSET(OSThread, 0x324, state);
274 CHECK_OFFSET(OSThread, 0x325, attr);
275 CHECK_OFFSET(OSThread, 0x326, id);
276 CHECK_OFFSET(OSThread, 0x328, suspendCounter);
277 CHECK_OFFSET(OSThread, 0x32c, priority);
278 CHECK_OFFSET(OSThread, 0x330, basePriority);
279 CHECK_OFFSET(OSThread, 0x334, exitValue);
280 CHECK_OFFSET(OSThread, 0x35c, queue);
281 CHECK_OFFSET(OSThread, 0x360, link);
282 CHECK_OFFSET(OSThread, 0x368, joinQueue);
283 CHECK_OFFSET(OSThread, 0x378, mutex);
284 CHECK_OFFSET(OSThread, 0x37c, mutexQueue);
285 CHECK_OFFSET(OSThread, 0x38c, activeLink);
286 CHECK_OFFSET(OSThread, 0x394, stackStart);
287 CHECK_OFFSET(OSThread, 0x398, stackEnd);
288 CHECK_OFFSET(OSThread, 0x39c, entryPoint);
289 CHECK_OFFSET(OSThread, 0x57c, specific);
290 CHECK_OFFSET(OSThread, 0x5c0, name);
291 CHECK_OFFSET(OSThread, 0x5c8, userStackPointer);
292 CHECK_OFFSET(OSThread, 0x5cc, cleanupCallback);
293 CHECK_OFFSET(OSThread, 0x5d0, deallocator);
294 CHECK_OFFSET(OSThread, 0x5d4, cancelState);
295 CHECK_OFFSET(OSThread, 0x5d8, requestFlag);
296 CHECK_OFFSET(OSThread, 0x5dc, needSuspend);
297 CHECK_OFFSET(OSThread, 0x5e0, suspendResult);
298 CHECK_OFFSET(OSThread, 0x5e4, suspendQueue);
299 CHECK_SIZE(OSThread, 0x69c);
300 
301 
308 void
309 OSCancelThread(OSThread *thread);
310 
311 
315 int32_t
317 
318 
322 int32_t
324 
325 
329 void
331 
332 
336 void
337 OSContinueThread(OSThread *thread);
338 
339 
352 BOOL
353 OSCreateThread(OSThread *thread,
354  OSThreadEntryPointFn entry,
355  int32_t argc,
356  char *argv,
357  void *stack,
358  uint32_t stackSize,
359  int32_t priority,
360  OSThreadAttributes attributes);
361 
362 
366 void
367 OSDetachThread(OSThread *thread);
368 
369 
375 void
376 OSExitThread(int32_t result);
377 
378 
382 void
384  OSThreadLink *link);
385 
386 
390 OSThread *
392 
393 
401 OSThread *
402 OSGetDefaultThread(uint32_t coreID);
403 
404 
408 uint32_t
410 
411 
415 uint32_t
417 
418 
422 const char *
423 OSGetThreadName(OSThread *thread);
424 
425 
429 int32_t
431 
432 
436 uint32_t
437 OSGetThreadSpecific(uint32_t id);
438 
439 
443 BOOL
445 
446 
450 BOOL
452 
453 
463 BOOL
464 OSJoinThread(OSThread *thread,
465  int *threadResult);
466 
467 
476 int32_t
477 OSResumeThread(OSThread *thread);
478 
479 
485 BOOL
486 OSRunThread(OSThread *thread,
487  OSThreadEntryPointFn entry,
488  int argc,
489  const char **argv);
490 
491 
495 BOOL
497  uint32_t affinity);
498 
499 
506 BOOL
508 
509 
515  OSThreadCleanupCallbackFn callback);
516 
517 
523  OSThreadDeallocatorFn deallocator);
524 
525 
529 void
530 OSSetThreadName(OSThread *thread,
531  const char *name);
532 
533 
537 BOOL
539  int32_t priority);
540 
541 
548 BOOL
550  uint32_t quantum);
551 
557 void
558 OSSetThreadSpecific(uint32_t id,
559  uint32_t value);
560 
561 
565 BOOL
567 
568 
574 void
576 
577 
581 void
582 OSSleepTicks(OSTime ticks);
583 
584 
593 uint32_t
594 OSSuspendThread(OSThread *thread);
595 
596 
610 void
612 
613 
619 void
621 
622 
629 void
630 OSYieldThread();
631 
632 
633 #ifdef __cplusplus
634 }
635 #endif
636 
OSThreadCleanupCallbackFn cleanupCallback
Called just before thread is terminated, set with OSSetThreadCleanupCallback.
Definition: thread.h:249
OSThreadDeallocatorFn deallocator
Called just after a thread is terminated, set with OSSetThreadDeallocator.
Definition: thread.h:252
uint32_t mmcr0
Definition: thread.h:125
uint32_t gpr[32]
Definition: thread.h:102
void OSSetThreadSpecific(uint32_t id, uint32_t value)
uint32_t srr1
Definition: thread.h:108
int32_t priority
Actual priority of thread.
Definition: thread.h:196
OSThreadLink link
Link used for thread queue.
Definition: thread.h:210
uint32_t gqr[8]
Definition: thread.h:114
OSThreadQueue * queue
Queue the thread is currently waiting on.
Definition: thread.h:207
OSContext context
Definition: thread.h:178
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:111
int32_t suspendCounter
Suspend count (increased by OSSuspendThread).
Definition: thread.h:193
uint64_t tag
Should always be set to the value OS_CONTEXT_TAG.
Definition: thread.h:100
OSThread * OSGetDefaultThread(uint32_t coreID)
int32_t basePriority
Base priority of thread, 0 is highest priority, 31 is lowest priority.
Definition: thread.h:199
uint8_t OSThreadAttributes
A bitfield of enum OS_THREAD_ATTRIB.
Definition: thread.h:44
OSThread * OSGetCurrentThread()
int32_t OSGetThreadPriority(OSThread *thread)
void OSCancelThread(OSThread *thread)
uint32_t pmc1
Definition: thread.h:121
uint32_t OSThreadRequest
A value from enum OS_THREAD_REQUEST.
Definition: thread.h:41
const char * name
Thread name, accessed with OSSetThreadName and OSGetThreadName.
Definition: thread.h:241
uint16_t id
Unique thread ID.
Definition: thread.h:190
Thread is running.
Definition: thread.h:58
void OSSleepTicks(OSTime ticks)
void * stackStart
Stack start (top, highest address)
Definition: thread.h:225
void(* OSThreadDeallocatorFn)(OSThread *thread, void *stack)
Definition: thread.h:48
BOOL OSIsThreadSuspended(OSThread *thread)
Start the thread detached.
Definition: thread.h:89
void OSYieldThread()
const char * OSGetThreadName(OSThread *thread)
uint32_t pmc3
Definition: thread.h:123
OS_THREAD_STATE
Definition: thread.h:50
int32_t exitValue
Exit value.
Definition: thread.h:202
OSThreadLink activeLink
Link for global active thread queue.
Definition: thread.h:222
OSMutex * tail
Definition: thread.h:156
BOOL OSSetThreadRunQuantum(OSThread *thread, uint32_t quantum)
OSThreadAttributes attr
Bitfield of OS_THREAD_ATTRIB.
Definition: thread.h:187
uint32_t ctr
Definition: thread.h:105
uint32_t OSSuspendThread(OSThread *thread)
OSThreadState state
Bitfield of OS_THREAD_STATE.
Definition: thread.h:184
void OSDetachThread(OSThread *thread)
Allow the thread to run any CPU.
Definition: thread.h:86
uint8_t OSThreadState
A value from enum OS_THREAD_STATE.
Definition: thread.h:38
BOOL OSSetThreadCancelState(BOOL state)
int(* OSThreadEntryPointFn)(int argc, const char **argv)
Definition: thread.h:46
BOOL OSSetThreadPriority(OSThread *thread, int32_t priority)
int32_t OSResumeThread(OSThread *thread)
int32_t needSuspend
Pending suspend request count.
Definition: thread.h:261
OSMutex * mutex
Mutex this thread is waiting to lock.
Definition: thread.h:216
OS_THREAD_ATTRIB
Definition: thread.h:74
OS_THREAD_REQUEST
Definition: thread.h:67
uint32_t srr0
Definition: thread.h:107
uint32_t OSGetStackPointer()
BOOL cancelState
If TRUE then a thread can be cancelled or suspended, set with OSSetThreadCancelState.
Definition: thread.h:255
void OSGetActiveThreadLink(OSThread *thread, OSThreadLink *link)
Allow the thread to run on CPU2.
Definition: thread.h:83
int32_t OSCheckActiveThreads()
void * stackEnd
Stack end (bottom, lowest address)
Definition: thread.h:228
void * parent
Definition: thread.h:157
uint32_t cr
Definition: thread.h:103
Allow the thread to run on CPU0.
Definition: thread.h:77
BOOL OSSetThreadAffinity(OSThread *thread, uint32_t affinity)
uint16_t spinLockCount
Definition: thread.h:112
BOOL OSJoinThread(OSThread *thread, int *threadResult)
uint32_t tag
Should always be set to the value OS_THREAD_TAG.
Definition: thread.h:181
void OSWakeupThread(OSThreadQueue *queue)
uint32_t pmc2
Definition: thread.h:122
void OSContinueThread(OSThread *thread)
OSThreadRequest requestFlag
Current thread request, used for cancelleing and suspending the thread.
Definition: thread.h:258
OSFastMutex * head
Definition: thread.h:167
void OSSetThreadName(OSThread *thread, const char *name)
uint32_t specific[0x10]
Thread specific values, accessed with OSSetThreadSpecific and OSGetThreadSpecific.
Definition: thread.h:236
OSThreadEntryPointFn entryPoint
Thread entry point.
Definition: thread.h:231
uint32_t fpscr
Definition: thread.h:110
OSThreadQueue suspendQueue
Queue of threads waiting for a thread to be suspended.
Definition: thread.h:267
BOOL OSSetThreadStackUsage(OSThread *thread)
int BOOL
Definition: wut_types.h:5
uint32_t lr
Definition: thread.h:104
Thread is about to terminate.
Definition: thread.h:64
Definition: mutex.h:35
void * userStackPointer
The stack pointer passed in OSCreateThread.
Definition: thread.h:246
uint64_t starttime
Definition: thread.h:118
Thread is waiting, i.e. on a mutex.
Definition: thread.h:61
OSMutex * head
Definition: thread.h:155
uint64_t coretime[3]
Definition: thread.h:117
int32_t suspendResult
Result of thread suspend.
Definition: thread.h:264
uint32_t xer
Definition: thread.h:106
uint32_t error
Definition: thread.h:119
uint32_t OSGetThreadAffinity(OSThread *thread)
OSThreadQueue joinQueue
Queue of threads waiting to join this thread.
Definition: thread.h:213
void OSSleepThread(OSThreadQueue *queue)
BOOL OSRunThread(OSThread *thread, OSThreadEntryPointFn entry, int argc, const char **argv)
double psf[32]
Definition: thread.h:116
void OSTestThreadCancel()
void OSExitThread(int32_t result)
uint32_t mmcr1
Definition: thread.h:126
OSThreadDeallocatorFn OSSetThreadDeallocator(OSThread *thread, OSThreadDeallocatorFn deallocator)
OSThreadCleanupCallbackFn OSSetThreadCleanupCallback(OSThread *thread, OSThreadCleanupCallbackFn callback)
OSMutexQueue mutexQueue
Queue of mutexes this thread owns.
Definition: thread.h:219
Enables tracking of stack usage.
Definition: thread.h:92
uint16_t state
Definition: thread.h:113
void(* OSThreadCleanupCallbackFn)(OSThread *thread, void *stack)
Definition: thread.h:47
OSFastMutex * tail
Definition: thread.h:168
BOOL OSIsThreadTerminated(OSThread *thread)
Thread is ready to run.
Definition: thread.h:55
uint32_t pmc4
Definition: thread.h:124
uint32_t OSGetThreadSpecific(uint32_t id)
void OSClearThreadStackUsage(OSThread *thread)
Allow the thread to run on CPU1.
Definition: thread.h:80