wut  1.0.0-alpha
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)
120 struct OSThread
121 {
123 
125  uint32_t tag;
126 
129 
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 
152 
155 
158 
161 
164 
167 
169  void *stackStart;
170 
172  void *stackEnd;
173 
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 
191 
194 
197 
200 
203 
205  int32_t needSuspend;
206 
208  int32_t suspendResult;
209 
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 
int32_t basePriority
Base priority of thread, 0 is highest priority, 31 is lowest priority.
Definition: thread.h:143
OSMutex * head
Definition: thread.h:99
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.
int32_t exitValue
Exit value.
Definition: thread.h:146
int64_t OSTime
Definition: time.h:18
int32_t OSCheckThreadStackUsage(OSThread *thread)
Get the maximum amount of stack the thread has used.
OSThreadLink activeLink
Link for global active thread queue.
Definition: thread.h:166
OSThread * OSGetDefaultThread(uint32_t coreID)
Returns the default thread for a specific core.
uint8_t OSThreadAttributes
A bitfield of enum OS_THREAD_ATTRIB.
Definition: thread.h:46
OSThread * OSGetCurrentThread()
Return pointer to OSThread object for the current thread.
int32_t OSGetThreadPriority(OSThread *thread)
Get a thread&#39;s base priority.
void OSCancelThread(OSThread *thread)
Cancels a thread.
uint32_t OSThreadRequest
A value from enum OS_THREAD_REQUEST.
Definition: thread.h:43
BOOL cancelState
If TRUE then a thread can be cancelled or suspended, set with OSSetThreadCancelState.
Definition: thread.h:199
Thread is running.
Definition: thread.h:60
void OSSleepTicks(OSTime ticks)
Sleep the current thread for a period of time.
void(* OSThreadDeallocatorFn)(OSThread *thread, void *stack)
Definition: thread.h:50
OSFastMutex * head
Definition: thread.h:111
int32_t priority
Actual priority of thread.
Definition: thread.h:140
BOOL OSIsThreadSuspended(OSThread *thread)
Returns TRUE if a thread is suspended.
Start the thread detached.
Definition: thread.h:91
void OSYieldThread()
Yield execution to waiting threads with same priority.
const char * OSGetThreadName(OSThread *thread)
Get a thread&#39;s name.
const char * name
Thread name, accessed with OSSetThreadName and OSGetThreadName.
Definition: thread.h:185
OSThreadQueue suspendQueue
Queue of threads waiting for a thread to be suspended.
Definition: thread.h:211
OS_THREAD_STATE
Definition: thread.h:52
uint32_t tag
Should always be set to the value OS_THREAD_TAG.
Definition: thread.h:125
BOOL OSSetThreadRunQuantum(OSThread *thread, uint32_t quantum)
Set a thread&#39;s run quantum.
uint32_t OSSuspendThread(OSThread *thread)
Suspend a thread.
void OSDetachThread(OSThread *thread)
Detach thread.
Allow the thread to run any CPU.
Definition: thread.h:88
uint8_t OSThreadState
A value from enum OS_THREAD_STATE.
Definition: thread.h:40
OSContext context
Definition: thread.h:122
BOOL OSSetThreadCancelState(BOOL state)
Set a thread&#39;s cancellation state.
OSThreadDeallocatorFn deallocator
Called just after a thread is terminated, set with OSSetThreadDeallocator.
Definition: thread.h:196
OSMutex * tail
Definition: thread.h:100
uint16_t id
Unique thread ID.
Definition: thread.h:134
int(* OSThreadEntryPointFn)(int argc, const char **argv)
Definition: thread.h:48
BOOL OSSetThreadPriority(OSThread *thread, int32_t priority)
Set a thread&#39;s priority.
int32_t OSResumeThread(OSThread *thread)
Resumes a thread.
OSThreadQueue joinQueue
Queue of threads waiting to join this thread.
Definition: thread.h:157
Definition: mutex.h:35
OS_THREAD_ATTRIB
Definition: thread.h:76
OS_THREAD_REQUEST
Definition: thread.h:69
uint32_t OSGetStackPointer()
Return current stack pointer, value of r1 register.
void * OSGetThreadSpecific(uint32_t id)
Get a thread&#39;s specific value set by OSSetThreadSpecific.
OSMutexQueue mutexQueue
Queue of mutexes this thread owns.
Definition: thread.h:163
void OSGetActiveThreadLink(OSThread *thread, OSThreadLink *link)
Get the next and previous thread in the thread&#39;s active queue.
Allow the thread to run on CPU2.
Definition: thread.h:85
int32_t OSCheckActiveThreads()
Returns the count of active threads.
OSThreadState state
Bitfield of OS_THREAD_STATE.
Definition: thread.h:128
Allow the thread to run on CPU0.
Definition: thread.h:79
BOOL OSSetThreadAffinity(OSThread *thread, uint32_t affinity)
Set a thread&#39;s affinity.
BOOL OSJoinThread(OSThread *thread, int *threadResult)
Wait until thread is terminated.
void OSWakeupThread(OSThreadQueue *queue)
Wake up all threads in queue.
OSThreadCleanupCallbackFn cleanupCallback
Called just before thread is terminated, set with OSSetThreadCleanupCallback.
Definition: thread.h:193
void OSContinueThread(OSThread *thread)
Clears a thread&#39;s suspend counter and resumes it.
OSThreadRequest requestFlag
Current thread request, used for cancelleing and suspending the thread.
Definition: thread.h:202
void OSSetThreadName(OSThread *thread, const char *name)
Set a thread&#39;s name.
void * userStackPointer
The stack pointer passed in OSCreateThread.
Definition: thread.h:190
BOOL OSSetThreadStackUsage(OSThread *thread)
Set thread stack usage tracking.
Thread is about to terminate.
Definition: thread.h:66
OSFastMutex * tail
Definition: thread.h:112
OSMutex * mutex
Mutex this thread is waiting to lock.
Definition: thread.h:160
int32_t BOOL
Definition: wut_types.h:4
OSThreadEntryPointFn entryPoint
Thread entry point.
Definition: thread.h:175
Thread is waiting, i.e. on a mutex.
Definition: thread.h:63
uint32_t OSGetThreadAffinity(OSThread *thread)
Get a thread&#39;s affinity.
void * stackStart
Stack start (top, highest address)
Definition: thread.h:169
void * specific[0x10]
Thread specific values, accessed with OSSetThreadSpecific and OSGetThreadSpecific.
Definition: thread.h:180
void OSSleepThread(OSThreadQueue *queue)
Sleep the current thread and add it to a thread queue.
OSThreadLink link
Link used for thread queue.
Definition: thread.h:154
BOOL OSRunThread(OSThread *thread, OSThreadEntryPointFn entry, int argc, const char **argv)
Run a function on an already created thread.
void OSSetThreadSpecific(uint32_t id, void *value)
Set a thread specific value.
OSThreadAttributes attr
Bitfield of OS_THREAD_ATTRIB.
Definition: thread.h:131
void OSTestThreadCancel()
Check to see if the current thread should be cancelled or suspended.
void OSExitThread(int32_t result)
Exit the current thread with a exit code.
OSThreadDeallocatorFn OSSetThreadDeallocator(OSThread *thread, OSThreadDeallocatorFn deallocator)
Set the callback to be called just after a thread is terminated.
OSThreadCleanupCallbackFn OSSetThreadCleanupCallback(OSThread *thread, OSThreadCleanupCallbackFn callback)
Set the callback to be called just before a thread is terminated.
Enables tracking of stack usage.
Definition: thread.h:94
void * stackEnd
Stack end (bottom, lowest address)
Definition: thread.h:172
void(* OSThreadCleanupCallbackFn)(OSThread *thread, void *stack)
Definition: thread.h:49
BOOL OSIsThreadTerminated(OSThread *thread)
Returns TRUE if a thread is terminated.
void * parent
Definition: thread.h:101
Thread is ready to run.
Definition: thread.h:57
int32_t suspendCounter
Suspend count (increased by OSSuspendThread).
Definition: thread.h:137
int32_t suspendResult
Result of thread suspend.
Definition: thread.h:208
void OSClearThreadStackUsage(OSThread *thread)
Disable tracking of thread stack usage.
OSThreadQueue * queue
Queue the thread is currently waiting on.
Definition: thread.h:151
Allow the thread to run on CPU1.
Definition: thread.h:82
int32_t needSuspend
Pending suspend request count.
Definition: thread.h:205