wut  1.0.0-beta9
Wii U Toolchain
thread.h
Go to the documentation of this file.
1 #pragma once
2 #include <wut.h>
3 #include "context.h"
4 #include "time.h"
5 #include "threadqueue.h"
6 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 typedef struct OSFastMutex OSFastMutex;
35 typedef struct OSMutex OSMutex;
36 typedef struct OSMutexQueue OSMutexQueue;
37 typedef struct OSThread OSThread;
38 
40 typedef uint8_t OSThreadState;
41 
43 typedef uint32_t OSThreadRequest;
44 
46 typedef uint8_t OSThreadAttributes;
47 
48 typedef int (*OSThreadEntryPointFn)(int argc, const char **argv);
49 typedef void (*OSThreadCleanupCallbackFn)(OSThread *thread, void *stack);
50 typedef void (*OSThreadDeallocatorFn)(OSThread *thread, void *stack);
51 
53 {
55 
58 
61 
64 
67 };
68 
70 {
74 };
75 
77 {
80 
83 
86 
88  OS_THREAD_ATTRIB_AFFINITY_ANY = ((1 << 0) | (1 << 1) | (1 << 2)),
89 
92 
95 };
96 
98 {
101  void *parent;
102  WUT_UNKNOWN_BYTES(4);
103 };
104 WUT_CHECK_OFFSET(OSMutexQueue, 0x0, head);
105 WUT_CHECK_OFFSET(OSMutexQueue, 0x4, tail);
106 WUT_CHECK_OFFSET(OSMutexQueue, 0x8, parent);
107 WUT_CHECK_SIZE(OSMutexQueue, 0x10);
108 
110 {
113 };
114 WUT_CHECK_OFFSET(OSFastMutexQueue, 0x00, head);
115 WUT_CHECK_OFFSET(OSFastMutexQueue, 0x04, tail);
116 WUT_CHECK_SIZE(OSFastMutexQueue, 0x08);
117 
118 #define OS_THREAD_TAG 0x74487244u
119 #pragma pack(push, 1)
121 {
122  OSContext context;
123 
125  uint32_t tag;
126 
128  OSThreadState state;
129 
131  OSThreadAttributes attr;
132 
134  uint16_t id;
135 
137  int32_t suspendCounter;
138 
140  int32_t priority;
141 
143  int32_t basePriority;
144 
146  int32_t exitValue;
147 
148  WUT_UNKNOWN_BYTES(0x35C - 0x338);
149 
151  OSThreadQueue *queue;
152 
154  OSThreadLink link;
155 
157  OSThreadQueue joinQueue;
158 
160  OSMutex *mutex;
161 
163  OSMutexQueue mutexQueue;
164 
166  OSThreadLink activeLink;
167 
169  void *stackStart;
170 
172  void *stackEnd;
173 
175  OSThreadEntryPointFn entryPoint;
176 
177  WUT_UNKNOWN_BYTES(0x57c - 0x3a0);
178 
180  void *specific[0x10];
181 
182  WUT_UNKNOWN_BYTES(0x5c0 - 0x5bc);
183 
185  const char *name;
186 
187  WUT_UNKNOWN_BYTES(0x4);
188 
190  void *userStackPointer;
191 
193  OSThreadCleanupCallbackFn cleanupCallback;
194 
196  OSThreadDeallocatorFn deallocator;
197 
199  BOOL cancelState;
200 
202  OSThreadRequest requestFlag;
203 
205  int32_t needSuspend;
206 
208  int32_t suspendResult;
209 
211  OSThreadQueue suspendQueue;
212 
213  WUT_UNKNOWN_BYTES(0x6a0 - 0x5f4);
214 };
215 #pragma pack(pop)
216 WUT_CHECK_OFFSET(OSThread, 0x320, tag);
217 WUT_CHECK_OFFSET(OSThread, 0x324, state);
218 WUT_CHECK_OFFSET(OSThread, 0x325, attr);
219 WUT_CHECK_OFFSET(OSThread, 0x326, id);
220 WUT_CHECK_OFFSET(OSThread, 0x328, suspendCounter);
221 WUT_CHECK_OFFSET(OSThread, 0x32c, priority);
222 WUT_CHECK_OFFSET(OSThread, 0x330, basePriority);
223 WUT_CHECK_OFFSET(OSThread, 0x334, exitValue);
224 WUT_CHECK_OFFSET(OSThread, 0x35c, queue);
225 WUT_CHECK_OFFSET(OSThread, 0x360, link);
226 WUT_CHECK_OFFSET(OSThread, 0x368, joinQueue);
227 WUT_CHECK_OFFSET(OSThread, 0x378, mutex);
228 WUT_CHECK_OFFSET(OSThread, 0x37c, mutexQueue);
229 WUT_CHECK_OFFSET(OSThread, 0x38c, activeLink);
230 WUT_CHECK_OFFSET(OSThread, 0x394, stackStart);
231 WUT_CHECK_OFFSET(OSThread, 0x398, stackEnd);
232 WUT_CHECK_OFFSET(OSThread, 0x39c, entryPoint);
233 WUT_CHECK_OFFSET(OSThread, 0x57c, specific);
234 WUT_CHECK_OFFSET(OSThread, 0x5c0, name);
235 WUT_CHECK_OFFSET(OSThread, 0x5c8, userStackPointer);
236 WUT_CHECK_OFFSET(OSThread, 0x5cc, cleanupCallback);
237 WUT_CHECK_OFFSET(OSThread, 0x5d0, deallocator);
238 WUT_CHECK_OFFSET(OSThread, 0x5d4, cancelState);
239 WUT_CHECK_OFFSET(OSThread, 0x5d8, requestFlag);
240 WUT_CHECK_OFFSET(OSThread, 0x5dc, needSuspend);
241 WUT_CHECK_OFFSET(OSThread, 0x5e0, suspendResult);
242 WUT_CHECK_OFFSET(OSThread, 0x5e4, suspendQueue);
243 WUT_CHECK_SIZE(OSThread, 0x6a0);
244 
245 
252 void
253 OSCancelThread(OSThread *thread);
254 
255 
259 int32_t
261 
262 
266 int32_t
268 
269 
273 void
275 
276 
280 void
281 OSContinueThread(OSThread *thread);
282 
283 
296 BOOL
297 OSCreateThread(OSThread *thread,
298  OSThreadEntryPointFn entry,
299  int32_t argc,
300  char *argv,
301  void *stack,
302  uint32_t stackSize,
303  int32_t priority,
304  OSThreadAttributes attributes);
305 
306 
310 void
311 OSDetachThread(OSThread *thread);
312 
313 
319 void
320 OSExitThread(int32_t result);
321 
322 
326 void
328  OSThreadLink *link);
329 
330 
334 OSThread *
336 
337 
345 OSThread *
346 OSGetDefaultThread(uint32_t coreID);
347 
348 
352 uint32_t
354 
355 
359 uint32_t
361 
362 
366 const char *
367 OSGetThreadName(OSThread *thread);
368 
369 
373 int32_t
375 
376 
380 void *
381 OSGetThreadSpecific(uint32_t id);
382 
383 
387 BOOL
389 
390 
394 BOOL
396 
397 
407 BOOL
408 OSJoinThread(OSThread *thread,
409  int *threadResult);
410 
411 
420 int32_t
421 OSResumeThread(OSThread *thread);
422 
423 
429 BOOL
430 OSRunThread(OSThread *thread,
431  OSThreadEntryPointFn entry,
432  int argc,
433  const char **argv);
434 
435 
439 BOOL
441  uint32_t affinity);
442 
443 
450 BOOL
452 
453 
459  OSThreadCleanupCallbackFn callback);
460 
461 
467  OSThreadDeallocatorFn deallocator);
468 
469 
473 void
474 OSSetThreadName(OSThread *thread,
475  const char *name);
476 
477 
481 BOOL
483  int32_t priority);
484 
485 
492 BOOL
494  uint32_t quantum);
495 
501 void
502 OSSetThreadSpecific(uint32_t id,
503  void *value);
504 
505 
509 BOOL
511 
512 
518 void
520 
521 
525 void
526 OSSleepTicks(OSTime ticks);
527 
528 
537 uint32_t
538 OSSuspendThread(OSThread *thread);
539 
540 
554 void
556 
557 
563 void
565 
566 
573 void
574 OSYieldThread();
575 
576 
577 #ifdef __cplusplus
578 }
579 #endif
580 
OSThreadCleanupCallbackFn
void(* OSThreadCleanupCallbackFn)(OSThread *thread, void *stack)
Definition: thread.h:49
OSTime
int64_t OSTime
Definition: time.h:18
OSSetThreadCancelState
BOOL OSSetThreadCancelState(BOOL state)
Set a thread's cancellation state.
OS_THREAD_ATTRIB_AFFINITY_CPU1
@ OS_THREAD_ATTRIB_AFFINITY_CPU1
Allow the thread to run on CPU1.
Definition: thread.h:82
OSThreadEntryPointFn
int(* OSThreadEntryPointFn)(int argc, const char **argv)
Definition: thread.h:48
OS_THREAD_REQUEST_CANCEL
@ OS_THREAD_REQUEST_CANCEL
Definition: thread.h:73
OSMutex
Definition: mutex.h:35
OSWakeupThread
void OSWakeupThread(OSThreadQueue *queue)
Wake up all threads in queue.
OS_THREAD_REQUEST
OS_THREAD_REQUEST
Definition: thread.h:69
OSThread
struct OSThread OSThread
Definition: mutex.h:19
OSMutexQueue
Definition: thread.h:97
OSSetThreadRunQuantum
BOOL OSSetThreadRunQuantum(OSThread *thread, uint32_t quantum)
Set a thread's run quantum.
OSSleepThread
void OSSleepThread(OSThreadQueue *queue)
Sleep the current thread and add it to a thread queue.
OSMutexQueue::parent
void * parent
Definition: thread.h:101
OSSetThreadDeallocator
OSThreadDeallocatorFn OSSetThreadDeallocator(OSThread *thread, OSThreadDeallocatorFn deallocator)
Set the callback to be called just after a thread is terminated.
OSExitThread
void OSExitThread(int32_t result)
Exit the current thread with a exit code.
WUT_ALIGNAS
struct WUT_ALIGNAS(8) OSThread
Definition: thread.h:120
OS_THREAD_STATE_RUNNING
@ OS_THREAD_STATE_RUNNING
Thread is running.
Definition: thread.h:60
OSSetThreadAffinity
BOOL OSSetThreadAffinity(OSThread *thread, uint32_t affinity)
Set a thread's affinity.
OSJoinThread
BOOL OSJoinThread(OSThread *thread, int *threadResult)
Wait until thread is terminated.
OS_THREAD_REQUEST_SUSPEND
@ OS_THREAD_REQUEST_SUSPEND
Definition: thread.h:72
OSThreadRequest
uint32_t OSThreadRequest
A value from enum OS_THREAD_REQUEST.
Definition: thread.h:43
OS_THREAD_ATTRIB_DETACHED
@ OS_THREAD_ATTRIB_DETACHED
Start the thread detached.
Definition: thread.h:91
OS_THREAD_ATTRIB
OS_THREAD_ATTRIB
Definition: thread.h:76
context.h
threadqueue.h
OSSetThreadName
void OSSetThreadName(OSThread *thread, const char *name)
Set a thread's name.
OSSleepTicks
void OSSleepTicks(OSTime ticks)
Sleep the current thread for a period of time.
OSContext
struct OSContext OSContext
Definition: context.h:15
OSSetThreadSpecific
void OSSetThreadSpecific(uint32_t id, void *value)
Set a thread specific value.
OSTestThreadCancel
void OSTestThreadCancel()
Check to see if the current thread should be cancelled or suspended.
OS_THREAD_STATE_NONE
@ OS_THREAD_STATE_NONE
Definition: thread.h:54
OSSetThreadCleanupCallback
OSThreadCleanupCallbackFn OSSetThreadCleanupCallback(OSThread *thread, OSThreadCleanupCallbackFn callback)
Set the callback to be called just before a thread is terminated.
OS_THREAD_STATE
OS_THREAD_STATE
Definition: thread.h:52
wut.h
OS_THREAD_ATTRIB_AFFINITY_ANY
@ OS_THREAD_ATTRIB_AFFINITY_ANY
Allow the thread to run any CPU.
Definition: thread.h:88
OSIsThreadTerminated
BOOL OSIsThreadTerminated(OSThread *thread)
Returns TRUE if a thread is terminated.
OSCreateThread
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.
OSCheckThreadStackUsage
int32_t OSCheckThreadStackUsage(OSThread *thread)
Get the maximum amount of stack the thread has used.
OSGetDefaultThread
OSThread * OSGetDefaultThread(uint32_t coreID)
Returns the default thread for a specific core.
OSSuspendThread
uint32_t OSSuspendThread(OSThread *thread)
Suspend a thread.
OSGetThreadPriority
int32_t OSGetThreadPriority(OSThread *thread)
Get a thread's base priority.
OSFastMutexQueue::tail
OSFastMutex * tail
Definition: thread.h:112
OSThreadAttributes
uint8_t OSThreadAttributes
A bitfield of enum OS_THREAD_ATTRIB.
Definition: thread.h:46
OS_THREAD_ATTRIB_AFFINITY_CPU0
@ OS_THREAD_ATTRIB_AFFINITY_CPU0
Allow the thread to run on CPU0.
Definition: thread.h:79
OSFastMutex
Definition: fastmutex.h:33
time.h
OSMutexQueue::tail
OSMutex * tail
Definition: thread.h:100
OSRunThread
BOOL OSRunThread(OSThread *thread, OSThreadEntryPointFn entry, int argc, const char **argv)
Run a function on an already created thread.
OS_THREAD_ATTRIB_AFFINITY_CPU2
@ OS_THREAD_ATTRIB_AFFINITY_CPU2
Allow the thread to run on CPU2.
Definition: thread.h:85
OSGetActiveThreadLink
void OSGetActiveThreadLink(OSThread *thread, OSThreadLink *link)
Get the next and previous thread in the thread's active queue.
OSCancelThread
void OSCancelThread(OSThread *thread)
Cancels a thread.
OSDetachThread
void OSDetachThread(OSThread *thread)
Detach thread.
OSSetThreadPriority
BOOL OSSetThreadPriority(OSThread *thread, int32_t priority)
Set a thread's priority.
OSClearThreadStackUsage
void OSClearThreadStackUsage(OSThread *thread)
Disable tracking of thread stack usage.
OSGetThreadAffinity
uint32_t OSGetThreadAffinity(OSThread *thread)
Get a thread's affinity.
OSThreadDeallocatorFn
void(* OSThreadDeallocatorFn)(OSThread *thread, void *stack)
Definition: thread.h:50
OS_THREAD_STATE_WAITING
@ OS_THREAD_STATE_WAITING
Thread is waiting, i.e. on a mutex.
Definition: thread.h:63
OS_THREAD_STATE_MORIBUND
@ OS_THREAD_STATE_MORIBUND
Thread is about to terminate.
Definition: thread.h:66
OS_THREAD_REQUEST_NONE
@ OS_THREAD_REQUEST_NONE
Definition: thread.h:71
OSGetStackPointer
uint32_t OSGetStackPointer()
Return current stack pointer, value of r1 register.
OSFastMutexQueue
Definition: thread.h:109
OSGetCurrentThread
OSThread * OSGetCurrentThread()
Return pointer to OSThread object for the current thread.
OSThreadState
uint8_t OSThreadState
A value from enum OS_THREAD_STATE.
Definition: thread.h:40
BOOL
int32_t BOOL
Definition: wut_types.h:4
OSResumeThread
int32_t OSResumeThread(OSThread *thread)
Resumes a thread.
OSThreadQueue
Definition: threadqueue.h:29
OSMutexQueue::head
OSMutex * head
Definition: thread.h:99
OSIsThreadSuspended
BOOL OSIsThreadSuspended(OSThread *thread)
Returns TRUE if a thread is suspended.
OSGetThreadName
const char * OSGetThreadName(OSThread *thread)
Get a thread's name.
OSYieldThread
void OSYieldThread()
Yield execution to waiting threads with same priority.
OS_THREAD_ATTRIB_STACK_USAGE
@ OS_THREAD_ATTRIB_STACK_USAGE
Enables tracking of stack usage.
Definition: thread.h:94
OSContinueThread
void OSContinueThread(OSThread *thread)
Clears a thread's suspend counter and resumes it.
OSGetThreadSpecific
void * OSGetThreadSpecific(uint32_t id)
Get a thread's specific value set by OSSetThreadSpecific.
OSCheckActiveThreads
int32_t OSCheckActiveThreads()
Returns the count of active threads.
OS_THREAD_STATE_READY
@ OS_THREAD_STATE_READY
Thread is ready to run.
Definition: thread.h:57
OSFastMutexQueue::head
OSFastMutex * head
Definition: thread.h:111
OSSetThreadStackUsage
BOOL OSSetThreadStackUsage(OSThread *thread)
Set thread stack usage tracking.