wut  v1.7.0
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 "alarm.h"
5 #include "context.h"
6 #include "time.h"
7 #include "threadqueue.h"
8 #include "exception.h"
9 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 typedef struct OSFastMutex OSFastMutex;
37 typedef struct OSFastMutexQueue OSFastMutexQueue;
38 typedef struct OSMutex OSMutex;
39 typedef struct OSMutexQueue OSMutexQueue;
40 typedef struct OSThread OSThread;
41 typedef struct OSTLSSection OSTLSSection;
43 
45 typedef uint8_t OSThreadState;
46 
48 typedef uint32_t OSThreadRequest;
49 
51 typedef uint8_t OSThreadAttributes;
52 
54 typedef uint32_t OSThreadType;
55 
56 typedef int (*OSThreadEntryPointFn)(int argc, const char **argv);
57 typedef void (*OSThreadCleanupCallbackFn)(OSThread *thread, void *stack);
58 typedef void (*OSThreadDeallocatorFn)(OSThread *thread, void *stack);
59 
60 typedef enum OSThreadSpecificID
61 {
81 
83 {
85 
88 
91 
94 
97 };
98 
100 {
104 };
105 
107 {
110 
113 
116 
118  OS_THREAD_ATTRIB_AFFINITY_ANY = ((1 << 0) | (1 << 1) | (1 << 2)),
119 
122 
125 
126  OS_THREAD_ATTRIB_UNKNOWN = 1 << 7
127 };
128 
133 };
134 
136 {
137  WUT_UNKNOWN_BYTES(0x68);
138  void *eh_globals;
139  void *eh_mem_manage[9];
140  void *eh_store_globals[6];
141  void *eh_store_globals_tdeh[76];
142 };
143 WUT_CHECK_OFFSET(OSThreadGHSExceptionHandling, 0x68, eh_globals);
144 WUT_CHECK_OFFSET(OSThreadGHSExceptionHandling, 0x6c, eh_mem_manage);
145 WUT_CHECK_OFFSET(OSThreadGHSExceptionHandling, 0x90, eh_store_globals);
146 WUT_CHECK_OFFSET(OSThreadGHSExceptionHandling, 0xa8, eh_store_globals_tdeh);
147 WUT_CHECK_SIZE(OSThreadGHSExceptionHandling, 0x1d8);
148 
150 {
153  void *parent;
154  WUT_UNKNOWN_BYTES(4);
155 };
156 WUT_CHECK_OFFSET(OSMutexQueue, 0x0, head);
157 WUT_CHECK_OFFSET(OSMutexQueue, 0x4, tail);
158 WUT_CHECK_OFFSET(OSMutexQueue, 0x8, parent);
159 WUT_CHECK_SIZE(OSMutexQueue, 0x10);
160 
162 {
165 };
166 WUT_CHECK_OFFSET(OSFastMutexQueue, 0x00, head);
167 WUT_CHECK_OFFSET(OSFastMutexQueue, 0x04, tail);
168 WUT_CHECK_SIZE(OSFastMutexQueue, 0x08);
169 
171 {
172  void* data;
173  WUT_UNKNOWN_BYTES(4);
174 };
175 WUT_CHECK_OFFSET(OSTLSSection, 0x00, data);
176 WUT_CHECK_SIZE(OSTLSSection, 0x08);
177 
178 #define OS_THREAD_TAG 0x74487244u
179 #pragma pack(push, 1)
180 struct WUT_ALIGNAS(8) OSThread
181 {
183 
185  uint32_t tag;
186 
189 
192 
194  uint16_t id;
195 
197  int32_t suspendCounter;
198 
200  int32_t priority;
201 
203  int32_t basePriority;
204 
206  int32_t exitValue;
207 
209  OSThreadQueue *coreRunQueue[3];
210  OSThreadLink coreRunQueueLink[3];
211 
214 
217 
220 
223 
226 
229 
231  void *stackStart;
232 
234  void *stackEnd;
235 
238 
241 
243 
245  void *specific[0x10];
246 
248 
250  const char *name;
251 
253 
256 
259 
262 
265 
268 
270  int32_t needSuspend;
271 
273  int32_t suspendResult;
274 
277 
278  WUT_UNKNOWN_BYTES(0x4);
279 
282 
285 
287  uint64_t wakeCount;
288 
293 
295  OSExceptionCallbackFn dsiCallback[3];
297  OSExceptionCallbackFn isiCallback[3];
299  OSExceptionCallbackFn programCallback[3];
301  OSExceptionCallbackFn perfMonCallback[3];
302 
305 
307  uint16_t tlsSectionCount;
308 
309  WUT_UNKNOWN_BYTES(0x2);
310 
313 
316 
319 
322 
324  OSExceptionCallbackFn alignCallback[3];
325 
327  uint32_t reserved[5];
328 };
329 #pragma pack(pop)
330 WUT_CHECK_OFFSET(OSThread, 0x320, tag);
331 WUT_CHECK_OFFSET(OSThread, 0x324, state);
332 WUT_CHECK_OFFSET(OSThread, 0x325, attr);
333 WUT_CHECK_OFFSET(OSThread, 0x326, id);
334 WUT_CHECK_OFFSET(OSThread, 0x328, suspendCounter);
335 WUT_CHECK_OFFSET(OSThread, 0x32c, priority);
336 WUT_CHECK_OFFSET(OSThread, 0x330, basePriority);
337 WUT_CHECK_OFFSET(OSThread, 0x334, exitValue);
338 WUT_CHECK_OFFSET(OSThread, 0x338, coreRunQueue);
339 WUT_CHECK_OFFSET(OSThread, 0x344, coreRunQueueLink);
340 WUT_CHECK_OFFSET(OSThread, 0x35c, queue);
341 WUT_CHECK_OFFSET(OSThread, 0x360, link);
342 WUT_CHECK_OFFSET(OSThread, 0x368, joinQueue);
343 WUT_CHECK_OFFSET(OSThread, 0x378, mutex);
344 WUT_CHECK_OFFSET(OSThread, 0x37c, mutexQueue);
345 WUT_CHECK_OFFSET(OSThread, 0x38c, activeLink);
346 WUT_CHECK_OFFSET(OSThread, 0x394, stackStart);
347 WUT_CHECK_OFFSET(OSThread, 0x398, stackEnd);
348 WUT_CHECK_OFFSET(OSThread, 0x39c, entryPoint);
349 WUT_CHECK_OFFSET(OSThread, 0x3a0, ghsExceptionHandling);
350 WUT_CHECK_OFFSET(OSThread, 0x57c, specific);
351 WUT_CHECK_OFFSET(OSThread, 0x5bc, type);
352 WUT_CHECK_OFFSET(OSThread, 0x5c0, name);
353 WUT_CHECK_OFFSET(OSThread, 0x5c4, waitEventTimeoutAlarm);
354 WUT_CHECK_OFFSET(OSThread, 0x5c8, userStackPointer);
355 WUT_CHECK_OFFSET(OSThread, 0x5cc, cleanupCallback);
356 WUT_CHECK_OFFSET(OSThread, 0x5d0, deallocator);
357 WUT_CHECK_OFFSET(OSThread, 0x5d4, cancelState);
358 WUT_CHECK_OFFSET(OSThread, 0x5d8, requestFlag);
359 WUT_CHECK_OFFSET(OSThread, 0x5dc, needSuspend);
360 WUT_CHECK_OFFSET(OSThread, 0x5e0, suspendResult);
361 WUT_CHECK_OFFSET(OSThread, 0x5e4, suspendQueue);
362 WUT_CHECK_OFFSET(OSThread, 0x5f8, runQuantumTicks);
363 WUT_CHECK_OFFSET(OSThread, 0x600, coreTimeConsumedNs);
364 WUT_CHECK_OFFSET(OSThread, 0x608, wakeCount);
365 WUT_CHECK_OFFSET(OSThread, 0x610, unk0x610);
366 WUT_CHECK_OFFSET(OSThread, 0x618, unk0x618);
367 WUT_CHECK_OFFSET(OSThread, 0x620, unk0x620);
368 WUT_CHECK_OFFSET(OSThread, 0x628, unk0x628);
369 WUT_CHECK_OFFSET(OSThread, 0x630, dsiCallback);
370 WUT_CHECK_OFFSET(OSThread, 0x63c, isiCallback);
371 WUT_CHECK_OFFSET(OSThread, 0x648, programCallback);
372 WUT_CHECK_OFFSET(OSThread, 0x654, perfMonCallback);
373 WUT_CHECK_OFFSET(OSThread, 0x664, tlsSectionCount);
374 WUT_CHECK_OFFSET(OSThread, 0x668, tlsSections);
375 WUT_CHECK_OFFSET(OSThread, 0x66c, fastMutex);
376 WUT_CHECK_OFFSET(OSThread, 0x670, contendedFastMutexes);
377 WUT_CHECK_OFFSET(OSThread, 0x678, fastMutexQueue);
378 WUT_CHECK_OFFSET(OSThread, 0x680, alignCallback);
379 WUT_CHECK_OFFSET(OSThread, 0x68c, reserved);
380 WUT_CHECK_SIZE(OSThread, 0x6a0);
381 
388 void
390 
391 
395 int32_t
397 
398 
402 int32_t
404 
405 
409 void
411 
412 
416 void
418 
419 
432 BOOL
434  OSThreadEntryPointFn entry,
435  int32_t argc,
436  char *argv,
437  void *stack,
438  uint32_t stackSize,
439  int32_t priority,
440  OSThreadAttributes attributes);
441 
442 
446 void
448 
449 
455 void
456 OSExitThread(int32_t result);
457 
458 
462 void
464  OSThreadLink *link);
465 
466 
470 OSThread *
472 
473 
481 OSThread *
482 OSGetDefaultThread(uint32_t coreID);
483 
484 
488 uint32_t
490 
491 
495 uint32_t
497 
498 
502 const char *
504 
505 
509 int32_t
511 
512 
516 void *
518 
519 
523 BOOL
525 
526 
530 BOOL
532 
533 
543 BOOL
545  int *threadResult);
546 
547 
556 int32_t
558 
559 
565 BOOL
567  OSThreadEntryPointFn entry,
568  int argc,
569  const char **argv);
570 
571 
575 BOOL
577  uint32_t affinity);
578 
579 
586 BOOL
588 
589 
595  OSThreadCleanupCallbackFn callback);
596 
597 
603  OSThreadDeallocatorFn deallocator);
604 
605 
609 void
611  const char *name);
612 
613 
617 BOOL
619  int32_t priority);
620 
621 
628 BOOL
630  uint32_t quantum);
631 
637 void
639  void *value);
640 
641 
645 BOOL
647 
648 
654 void
656 
657 
661 void
663 
664 
673 uint32_t
675 
676 void
678 
692 void
694 
695 
701 void
703 
704 
711 void
713 
714 
715 #ifdef __cplusplus
716 }
717 #endif
718 
Definition: alarm.h:63
BOOL(* OSExceptionCallbackFn)(OSContext *context)
Definition: exception.h:15
Definition: mutex.h:36
OSThreadLink activeLink
Link for global active thread queue.
Definition: thread.h:228
OSFastMutexQueue contendedFastMutexes
The fast mutexes we are currently contended on.
Definition: thread.h:318
OSMutex * head
Definition: thread.h:151
uint64_t coreTimeConsumedNs
The total amount of core time consumed by this thread (Does not include time while Running)
Definition: thread.h:284
OSThreadAttributes attr
Bitfield of OS_THREAD_ATTRIB.
Definition: thread.h:191
OSFastMutex * tail
Definition: thread.h:164
OSThreadState state
Bitfield of OS_THREAD_STATE.
Definition: thread.h:188
OSMutexQueue mutexQueue
Queue of mutexes this thread owns.
Definition: thread.h:225
OSTime unk0x620
Definition: thread.h:291
uint32_t tag
Should always be set to the value OS_THREAD_TAG.
Definition: thread.h:185
OSFastMutex * fastMutex
The fast mutex we are currently waiting for.
Definition: thread.h:315
void * userStackPointer
The stack pointer passed in OSCreateThread.
Definition: thread.h:255
OSTLSSection * tlsSections
TLS Sections.
Definition: thread.h:309
OSThreadDeallocatorFn deallocator
Called just after a thread is terminated, set with OSSetThreadDeallocator.
Definition: thread.h:261
BOOL alarmCancelled
Definition: thread.h:242
OSThreadGHSExceptionHandling ghsExceptionHandling
GHS Exception handling thread-specifics.
Definition: thread.h:240
int32_t needSuspend
Pending suspend request count.
Definition: thread.h:270
int32_t priority
Actual priority of thread.
Definition: thread.h:200
OSTime unk0x618
Definition: thread.h:290
uint16_t id
Unique thread ID.
Definition: thread.h:194
OSThreadEntryPointFn entryPoint
Thread entry point.
Definition: thread.h:237
OSAlarm * waitEventTimeoutAlarm
Definition: thread.h:252
uint64_t wakeCount
The number of times this thread has been awoken.
Definition: thread.h:287
void * parent
Definition: thread.h:153
OSMutex * tail
Definition: thread.h:152
int32_t basePriority
Base priority of thread, 0 is highest priority, 31 is lowest priority.
Definition: thread.h:203
void * data
Definition: thread.h:172
OSMutex * mutex
Mutex this thread is waiting to lock.
Definition: thread.h:222
const char * name
Thread name, accessed with OSSetThreadName and OSGetThreadName.
Definition: thread.h:250
OSTime unk0x610
Definition: thread.h:289
uint16_t tlsSectionCount
Number of TLS sections.
Definition: thread.h:307
OSThreadRequest requestFlag
Current thread request, used for cancelleing and suspending the thread.
Definition: thread.h:267
OSTime unk0x628
Definition: thread.h:292
BOOL cancelState
If TRUE then a thread can be cancelled or suspended, set with OSSetThreadCancelState.
Definition: thread.h:264
int32_t exitValue
Exit value.
Definition: thread.h:206
OSThreadQueue * queue
Queue the thread is currently waiting on.
Definition: thread.h:213
OSContext context
Definition: thread.h:182
int32_t suspendCounter
Suspend count (increased by OSSuspendThread).
Definition: thread.h:197
OSThreadQueue suspendQueue
Queue of threads waiting for a thread to be suspended.
Definition: thread.h:276
int32_t suspendResult
Result of thread suspend.
Definition: thread.h:273
int64_t runQuantumTicks
How many ticks the thread should run for before suspension.
Definition: thread.h:278
void * stackStart
Stack start (top, highest address)
Definition: thread.h:231
OSThreadCleanupCallbackFn cleanupCallback
Called just before thread is terminated, set with OSSetThreadCleanupCallback.
Definition: thread.h:258
BOOL stackSyncObjAllowed
Checks for synchronization objects placed on stack in debug mode when set to true.
Definition: thread.h:304
OSFastMutex * head
Definition: thread.h:163
OSThreadType type
Definition: thread.h:247
OSThreadQueue joinQueue
Queue of threads waiting to join this thread.
Definition: thread.h:219
OSFastMutexQueue fastMutexQueue
The fast mutexes we currently own locks on.
Definition: thread.h:321
OSThreadLink link
Link used for thread queue.
Definition: thread.h:216
void * stackEnd
Stack end (bottom, lowest address)
Definition: thread.h:234
BOOL OSIsThreadSuspended(OSThread *thread)
Returns TRUE if a thread is suspended.
void __OSSuspendThreadNolock(OSThread *thread)
int32_t OSCheckActiveThreads()
Returns the count of active threads.
BOOL OSIsThreadTerminated(OSThread *thread)
Returns TRUE if a thread is terminated.
uint8_t OSThreadState
A value from enum OS_THREAD_STATE.
Definition: thread.h:45
OS_THREAD_STATE
Definition: thread.h:83
void OSWakeupThread(OSThreadQueue *queue)
Wake up all threads in queue.
uint32_t OSThreadRequest
A value from enum OS_THREAD_REQUEST.
Definition: thread.h:48
int32_t OSCheckThreadStackUsage(OSThread *thread)
Get the maximum amount of stack the thread has used.
const char * OSGetThreadName(OSThread *thread)
Get a thread's name.
OSThread * OSGetCurrentThread()
Return pointer to OSThread object for the current thread.
BOOL OSSetThreadStackUsage(OSThread *thread)
Set thread stack usage tracking.
void OSTestThreadCancel()
Check to see if the current thread should be cancelled or suspended.
OSThreadSpecificID
Definition: thread.h:61
void OSSleepThread(OSThreadQueue *queue)
Sleep the current thread and add it to a thread queue.
uint32_t OSGetStackPointer()
Return current stack pointer, value of r1 register.
uint8_t OSThreadAttributes
A bitfield of enum OS_THREAD_ATTRIB.
Definition: thread.h:51
void OSExitThread(int32_t result)
Exit the current thread with a exit code.
void OSCancelThread(OSThread *thread)
Cancels a thread.
void OSGetActiveThreadLink(OSThread *thread, OSThreadLink *link)
Get the next and previous thread in the thread's active queue.
BOOL OSCreateThread(OSThread *thread, OSThreadEntryPointFn entry, int32_t argc, char *argv, void *stack, uint32_t stackSize, int32_t priority, OSThreadAttributes attributes)
Create a new thread.
BOOL OSSetThreadCancelState(BOOL state)
Set a thread's cancellation state.
void OSDetachThread(OSThread *thread)
Detach thread.
uint32_t OSGetThreadAffinity(OSThread *thread)
Get a thread's affinity.
BOOL OSSetThreadAffinity(OSThread *thread, uint32_t affinity)
Set a thread's affinity.
void OSContinueThread(OSThread *thread)
Clears a thread's suspend counter and resumes it.
void(* OSThreadCleanupCallbackFn)(OSThread *thread, void *stack)
Definition: thread.h:57
int32_t OSResumeThread(OSThread *thread)
Resumes a thread.
void * OSGetThreadSpecific(OSThreadSpecificID id)
Get a thread's specific value set by OSSetThreadSpecific.
OS_THREAD_ATTRIB
Definition: thread.h:107
uint32_t OSSuspendThread(OSThread *thread)
Suspend a thread.
int32_t OSGetThreadPriority(OSThread *thread)
Get a thread's base priority.
BOOL OSRunThread(OSThread *thread, OSThreadEntryPointFn entry, int argc, const char **argv)
Run a function on an already created thread.
void OSYieldThread()
Yield execution to waiting threads with same priority.
int(* OSThreadEntryPointFn)(int argc, const char **argv)
Definition: thread.h:56
OSThreadDeallocatorFn OSSetThreadDeallocator(OSThread *thread, OSThreadDeallocatorFn deallocator)
Set the callback to be called just after a thread is terminated.
BOOL OSJoinThread(OSThread *thread, int *threadResult)
Wait until thread is terminated.
BOOL OSSetThreadRunQuantum(OSThread *thread, uint32_t quantum)
Set a thread's run quantum.
OS_THREAD_REQUEST
Definition: thread.h:100
void(* OSThreadDeallocatorFn)(OSThread *thread, void *stack)
Definition: thread.h:58
OSThread * OSGetDefaultThread(uint32_t coreID)
Returns the default thread for a specific core.
uint32_t OSThreadType
A bitfield of enum OS_THREAD_TYPE.
Definition: thread.h:54
void OSSetThreadSpecific(OSThreadSpecificID id, void *value)
Set a thread specific value.
OSThreadCleanupCallbackFn OSSetThreadCleanupCallback(OSThread *thread, OSThreadCleanupCallbackFn callback)
Set the callback to be called just before a thread is terminated.
OS_THREAD_TYPE
Definition: thread.h:129
void OSSleepTicks(OSTime ticks)
Sleep the current thread for a period of time.
void OSClearThreadStackUsage(OSThread *thread)
Disable tracking of thread stack usage.
void OSSetThreadName(OSThread *thread, const char *name)
Set a thread's name.
BOOL OSSetThreadPriority(OSThread *thread, int32_t priority)
Set a thread's priority.
@ OS_THREAD_STATE_NONE
Definition: thread.h:84
@ OS_THREAD_STATE_MORIBUND
Thread is about to terminate.
Definition: thread.h:96
@ OS_THREAD_STATE_WAITING
Thread is waiting, i.e. on a mutex.
Definition: thread.h:93
@ OS_THREAD_STATE_READY
Thread is ready to run.
Definition: thread.h:87
@ OS_THREAD_STATE_RUNNING
Thread is running.
Definition: thread.h:90
@ OS_THREAD_SPECIFIC_0
These can be used by applications.
Definition: thread.h:63
@ OS_THREAD_SPECIFIC_1
Definition: thread.h:64
@ OS_THREAD_SPECIFIC_3
Definition: thread.h:66
@ OS_THREAD_SPECIFIC_11
Definition: thread.h:74
@ OS_THREAD_SPECIFIC_10
Definition: thread.h:73
@ OS_THREAD_SPECIFIC_13
Definition: thread.h:76
@ OS_THREAD_SPECIFIC_8
Definition: thread.h:71
@ OS_THREAD_SPECIFIC_5
Definition: thread.h:68
@ OS_THREAD_SPECIFIC_2
Definition: thread.h:65
@ OS_THREAD_SPECIFIC_4
Definition: thread.h:67
@ OS_THREAD_SPECIFIC_WUT_RESERVED_1
Definition: thread.h:79
@ OS_THREAD_SPECIFIC_WUT_RESERVED_0
These are reserved to wut for internal use.
Definition: thread.h:78
@ OS_THREAD_SPECIFIC_7
Definition: thread.h:70
@ OS_THREAD_SPECIFIC_9
Definition: thread.h:72
@ OS_THREAD_SPECIFIC_6
Definition: thread.h:69
@ OS_THREAD_SPECIFIC_12
Definition: thread.h:75
@ OS_THREAD_ATTRIB_AFFINITY_CPU2
Allow the thread to run on CPU2.
Definition: thread.h:115
@ OS_THREAD_ATTRIB_AFFINITY_CPU0
Allow the thread to run on CPU0.
Definition: thread.h:109
@ OS_THREAD_ATTRIB_DETACHED
Start the thread detached.
Definition: thread.h:121
@ OS_THREAD_ATTRIB_STACK_USAGE
Enables tracking of stack usage.
Definition: thread.h:124
@ OS_THREAD_ATTRIB_AFFINITY_ANY
Allow the thread to run any CPU.
Definition: thread.h:118
@ OS_THREAD_ATTRIB_UNKNOWN
Definition: thread.h:126
@ OS_THREAD_ATTRIB_AFFINITY_CPU1
Allow the thread to run on CPU1.
Definition: thread.h:112
@ OS_THREAD_REQUEST_NONE
Definition: thread.h:101
@ OS_THREAD_REQUEST_SUSPEND
Definition: thread.h:102
@ OS_THREAD_REQUEST_CANCEL
Definition: thread.h:103
@ OS_THREAD_TYPE_APP
Definition: thread.h:132
@ OS_THREAD_TYPE_IO
Definition: thread.h:131
@ OS_THREAD_TYPE_DRIVER
Definition: thread.h:130
int64_t OSTime
Definition: time.h:18
int32_t BOOL
Definition: wut_types.h:7