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 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 typedef struct OSContext OSContext;
17 typedef struct OSFastMutex OSFastMutex;
19 typedef struct OSMutex OSMutex;
20 typedef struct OSMutexQueue OSMutexQueue;
21 typedef struct OSThread OSThread;
22 
23 typedef uint8_t OSThreadState;
24 typedef uint32_t OSThreadRequest;
25 typedef uint8_t OSThreadAttributes;
26 
27 typedef int (*OSThreadEntryPointFn)(int argc, const char **argv);
28 typedef void (*OSThreadCleanupCallbackFn)(OSThread *thread, void *stack);
29 typedef void (*OSThreadDeallocatorFn)(OSThread *thread, void *stack);
30 
31 enum OSThreadState
32 {
38 };
39 
40 enum OSThreadRequest
41 {
45 };
46 
47 enum OSThreadAttributes
48 {
54 };
55 
56 #define OS_CONTEXT_TAG 0x4F53436F6E747874ull
57 
58 struct OSContext
59 {
60  uint64_t tag;
61  uint32_t gpr[32];
62  uint32_t cr;
63  uint32_t lr;
64  uint32_t ctr;
65  uint32_t xer;
66  uint32_t srr0;
67  uint32_t srr1;
68  UNKNOWN(0x14);
69  uint32_t fpscr;
70  double fpr[32];
71  uint16_t spinLockCount;
72  uint16_t state;
73  uint32_t gqr[8];
74  UNKNOWN(4);
75  double psf[32];
76  uint64_t coretime[3];
77  uint64_t starttime;
78  uint32_t error;
79  UNKNOWN(4);
80  uint32_t pmc1;
81  uint32_t pmc2;
82  uint32_t pmc3;
83  uint32_t pmc4;
84  uint32_t mmcr0;
85  uint32_t mmcr1;
86 };
87 CHECK_OFFSET(OSContext, 0x00, tag);
88 CHECK_OFFSET(OSContext, 0x08, gpr);
89 CHECK_OFFSET(OSContext, 0x88, cr);
90 CHECK_OFFSET(OSContext, 0x8c, lr);
91 CHECK_OFFSET(OSContext, 0x90, ctr);
92 CHECK_OFFSET(OSContext, 0x94, xer);
93 CHECK_OFFSET(OSContext, 0x98, srr0);
94 CHECK_OFFSET(OSContext, 0x9c, srr1);
95 CHECK_OFFSET(OSContext, 0xb4, fpscr);
96 CHECK_OFFSET(OSContext, 0xb8, fpr);
97 CHECK_OFFSET(OSContext, 0x1b8, spinLockCount);
98 CHECK_OFFSET(OSContext, 0x1ba, state);
99 CHECK_OFFSET(OSContext, 0x1bc, gqr);
100 CHECK_OFFSET(OSContext, 0x1e0, psf);
101 CHECK_OFFSET(OSContext, 0x2e0, coretime);
102 CHECK_OFFSET(OSContext, 0x2f8, starttime);
103 CHECK_OFFSET(OSContext, 0x300, error);
104 CHECK_OFFSET(OSContext, 0x308, pmc1);
105 CHECK_OFFSET(OSContext, 0x30c, pmc2);
106 CHECK_OFFSET(OSContext, 0x310, pmc3);
107 CHECK_OFFSET(OSContext, 0x314, pmc4);
108 CHECK_OFFSET(OSContext, 0x318, mmcr0);
109 CHECK_OFFSET(OSContext, 0x31c, mmcr1);
110 CHECK_SIZE(OSContext, 0x320);
111 
113 {
116  void *parent;
117  UNKNOWN(4);
118 };
119 CHECK_OFFSET(OSMutexQueue, 0x0, head);
120 CHECK_OFFSET(OSMutexQueue, 0x4, tail);
121 CHECK_OFFSET(OSMutexQueue, 0x8, parent);
122 CHECK_SIZE(OSMutexQueue, 0x10);
123 
125 {
128 };
129 CHECK_OFFSET(OSFastMutexQueue, 0x00, head);
130 CHECK_OFFSET(OSFastMutexQueue, 0x04, tail);
131 CHECK_SIZE(OSFastMutexQueue, 0x08);
132 
133 #define OS_THREAD_TAG 0x74487244u
134 
135 struct OSThread
136 {
138  uint32_t tag;
139  OSThreadState state;
140  OSThreadAttributes attr;
141  uint16_t id;
142  int32_t suspendCounter;
143  int32_t priority; // Actual priority of thread
144  int32_t basePriority; // Base priority of thread
145  int32_t exitValue; // Value from OSExitThread
146  UNKNOWN(0x35C - 0x338);
147  OSThreadQueue *queue; // Queue the thread is on
148  OSThreadLink link; // Thread queue link
149  OSThreadQueue joinQueue; // Queue of threads waiting to join this
150  OSMutex *mutex; // Mutex we are waiting to lock
151  OSMutexQueue mutexQueue; // Mutexes owned by this thread
152  OSThreadLink activeLink; // Link on active thread queue
153  void *stackStart; // Stack starting value (top, highest address)
154  void *stackEnd; // Stack end value (bottom, lowest address)
155  OSThreadEntryPointFn entryPoint; // Entry point from OSCreateThread
156  UNKNOWN(0x57c - 0x3a0);
157  uint32_t specific[0x10]; // OSSetThreadSpecific / OSGetThreadSpecific
158  UNKNOWN(0x5c0 - 0x5bc);
159  const char *name; // Thread name
160  UNKNOWN(0x4);
161  void *userStackPointer; // The stack specified in OSCreateThread
162  OSThreadCleanupCallbackFn cleanupCallback; // Set with OSSetThreadCleanupCallback
163  OSThreadDeallocatorFn deallocator; // Set with OSSetThreadDeallocator
164  uint32_t cancelState; // Is listening to requestFlag enabled
165  OSThreadRequest requestFlag; // Request flag for cancel or suspend
166  int32_t needSuspend; // How many pending suspends we have
167  int32_t suspendResult; // Result of suspend
168  OSThreadQueue suspendQueue; // Queue of threads waiting for suspend to finish
169  UNKNOWN(0x69c - 0x5f4);
170 };
171 CHECK_OFFSET(OSThread, 0x320, tag);
172 CHECK_OFFSET(OSThread, 0x324, state);
173 CHECK_OFFSET(OSThread, 0x325, attr);
174 CHECK_OFFSET(OSThread, 0x326, id);
175 CHECK_OFFSET(OSThread, 0x328, suspendCounter);
176 CHECK_OFFSET(OSThread, 0x32c, priority);
177 CHECK_OFFSET(OSThread, 0x330, basePriority);
178 CHECK_OFFSET(OSThread, 0x334, exitValue);
179 CHECK_OFFSET(OSThread, 0x35c, queue);
180 CHECK_OFFSET(OSThread, 0x360, link);
181 CHECK_OFFSET(OSThread, 0x368, joinQueue);
182 CHECK_OFFSET(OSThread, 0x378, mutex);
183 CHECK_OFFSET(OSThread, 0x37c, mutexQueue);
184 CHECK_OFFSET(OSThread, 0x38c, activeLink);
185 CHECK_OFFSET(OSThread, 0x394, stackStart);
186 CHECK_OFFSET(OSThread, 0x398, stackEnd);
187 CHECK_OFFSET(OSThread, 0x39c, entryPoint);
188 CHECK_OFFSET(OSThread, 0x57c, specific);
189 CHECK_OFFSET(OSThread, 0x5c0, name);
190 CHECK_OFFSET(OSThread, 0x5c8, userStackPointer);
191 CHECK_OFFSET(OSThread, 0x5cc, cleanupCallback);
192 CHECK_OFFSET(OSThread, 0x5d0, deallocator);
193 CHECK_OFFSET(OSThread, 0x5d4, cancelState);
194 CHECK_OFFSET(OSThread, 0x5d8, requestFlag);
195 CHECK_OFFSET(OSThread, 0x5dc, needSuspend);
196 CHECK_OFFSET(OSThread, 0x5e0, suspendResult);
197 CHECK_OFFSET(OSThread, 0x5e4, suspendQueue);
198 CHECK_SIZE(OSThread, 0x69c);
199 
200 void
201 OSCancelThread(OSThread *thread);
202 
203 int32_t
205 
206 int32_t
208 
209 void
211 
212 void
213 OSContinueThread(OSThread *thread);
214 
215 BOOL
216 OSCreateThread(OSThread *thread,
217  OSThreadEntryPointFn entry,
218  int32_t argc,
219  char *argv,
220  void *stack,
221  uint32_t stackSize,
222  int32_t priority,
223  OSThreadAttributes attributes);
224 
225 void
226 OSDetachThread(OSThread *thread);
227 
228 void
229 OSExitThread(int32_t result);
230 
231 void
233  OSThreadLink *link);
234 
235 OSThread *
237 
238 OSThread *
239 OSGetDefaultThread(uint32_t coreID);
240 
241 uint32_t
243 
244 uint32_t
246 
247 const char *
248 OSGetThreadName(OSThread *thread);
249 
250 int32_t
252 
253 uint32_t
254 OSGetThreadSpecific(uint32_t id);
255 
256 BOOL
258 
259 BOOL
261 
262 BOOL
263 OSJoinThread(OSThread *thread,
264  int *threadResult);
265 
266 void
268 
269 int32_t
270 OSResumeThread(OSThread *thread);
271 
272 BOOL
273 OSRunThread(OSThread *thread,
274  OSThreadEntryPointFn entry,
275  int argc,
276  const char **argv);
277 
278 BOOL
280  uint32_t affinity);
281 
282 BOOL
284 
287  OSThreadCleanupCallbackFn callback);
288 
291  OSThreadDeallocatorFn deallocator);
292 
293 void
294 OSSetThreadName(OSThread *thread,
295  const char *name);
296 
297 BOOL
299  int32_t priority);
300 
301 BOOL
303  uint32_t quantum);
304 
305 void
306 OSSetThreadSpecific(uint32_t id,
307  uint32_t value);
308 
309 BOOL
311 
312 void
314 
315 void
316 OSSleepTicks(OSTime ticks);
317 
318 uint32_t
319 OSSuspendThread(OSThread *thread);
320 
321 void
323 
324 void
326 
327 void
328 OSYieldThread();
329 
330 #ifdef __cplusplus
331 }
332 #endif
333 
OSThreadCleanupCallbackFn cleanupCallback
Definition: thread.h:162
OSThreadRequest
Definition: thread.h:40
OSThreadDeallocatorFn deallocator
Definition: thread.h:163
uint32_t mmcr0
Definition: thread.h:84
uint32_t gpr[32]
Definition: thread.h:61
void OSSetThreadSpecific(uint32_t id, uint32_t value)
uint32_t srr1
Definition: thread.h:67
int32_t priority
Definition: thread.h:143
OSThreadLink link
Definition: thread.h:148
uint32_t gqr[8]
Definition: thread.h:73
OSThreadQueue * queue
Definition: thread.h:147
OSContext context
Definition: thread.h:137
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:70
int32_t suspendCounter
Definition: thread.h:142
uint64_t tag
Definition: thread.h:60
OSThread * OSGetDefaultThread(uint32_t coreID)
int32_t basePriority
Definition: thread.h:144
OSThread * OSGetCurrentThread()
int32_t OSGetThreadPriority(OSThread *thread)
void OSCancelThread(OSThread *thread)
uint32_t pmc1
Definition: thread.h:80
const char * name
Definition: thread.h:159
uint16_t id
Definition: thread.h:141
void OSSleepTicks(OSTime ticks)
void * stackStart
Definition: thread.h:153
void(* OSThreadDeallocatorFn)(OSThread *thread, void *stack)
Definition: thread.h:29
BOOL OSIsThreadSuspended(OSThread *thread)
void OSYieldThread()
const char * OSGetThreadName(OSThread *thread)
uint32_t pmc3
Definition: thread.h:82
int32_t exitValue
Definition: thread.h:145
OSThreadLink activeLink
Definition: thread.h:152
OSMutex * tail
Definition: thread.h:115
BOOL OSSetThreadRunQuantum(OSThread *thread, uint32_t quantum)
OSThreadAttributes attr
Definition: thread.h:140
uint32_t ctr
Definition: thread.h:64
uint32_t OSSuspendThread(OSThread *thread)
OSThreadState state
Definition: thread.h:139
void OSDetachThread(OSThread *thread)
BOOL OSSetThreadCancelState(BOOL state)
int(* OSThreadEntryPointFn)(int argc, const char **argv)
Definition: thread.h:27
BOOL OSSetThreadPriority(OSThread *thread, int32_t priority)
int32_t OSResumeThread(OSThread *thread)
int32_t needSuspend
Definition: thread.h:166
OSMutex * mutex
Definition: thread.h:150
uint32_t srr0
Definition: thread.h:66
uint32_t OSGetStackPointer()
void OSGetActiveThreadLink(OSThread *thread, OSThreadLink *link)
int32_t OSCheckActiveThreads()
void OSPrintCurrentThreadState()
void * stackEnd
Definition: thread.h:154
void * parent
Definition: thread.h:116
uint32_t cr
Definition: thread.h:62
OSThreadState
Definition: thread.h:31
BOOL OSSetThreadAffinity(OSThread *thread, uint32_t affinity)
uint16_t spinLockCount
Definition: thread.h:71
BOOL OSJoinThread(OSThread *thread, int *threadResult)
uint32_t tag
Definition: thread.h:138
OSThreadAttributes
Definition: thread.h:47
void OSWakeupThread(OSThreadQueue *queue)
uint32_t pmc2
Definition: thread.h:81
void OSContinueThread(OSThread *thread)
OSThreadRequest requestFlag
Definition: thread.h:165
OSFastMutex * head
Definition: thread.h:126
void OSSetThreadName(OSThread *thread, const char *name)
uint32_t specific[0x10]
Definition: thread.h:157
OSThreadEntryPointFn entryPoint
Definition: thread.h:155
uint32_t fpscr
Definition: thread.h:69
OSThreadQueue suspendQueue
Definition: thread.h:168
BOOL OSSetThreadStackUsage(OSThread *thread)
int BOOL
Definition: wut_types.h:4
uint32_t lr
Definition: thread.h:63
Definition: mutex.h:32
uint32_t cancelState
Definition: thread.h:164
void * userStackPointer
Definition: thread.h:161
uint64_t starttime
Definition: thread.h:77
OSMutex * head
Definition: thread.h:114
uint64_t coretime[3]
Definition: thread.h:76
int32_t suspendResult
Definition: thread.h:167
uint32_t xer
Definition: thread.h:65
uint32_t error
Definition: thread.h:78
uint32_t OSGetThreadAffinity(OSThread *thread)
OSThreadQueue joinQueue
Definition: thread.h:149
void OSSleepThread(OSThreadQueue *queue)
BOOL OSRunThread(OSThread *thread, OSThreadEntryPointFn entry, int argc, const char **argv)
double psf[32]
Definition: thread.h:75
void OSTestThreadCancel()
void OSExitThread(int32_t result)
uint32_t mmcr1
Definition: thread.h:85
OSThreadDeallocatorFn OSSetThreadDeallocator(OSThread *thread, OSThreadDeallocatorFn deallocator)
OSThreadCleanupCallbackFn OSSetThreadCleanupCallback(OSThread *thread, OSThreadCleanupCallbackFn callback)
OSMutexQueue mutexQueue
Definition: thread.h:151
uint16_t state
Definition: thread.h:72
void(* OSThreadCleanupCallbackFn)(OSThread *thread, void *stack)
Definition: thread.h:28
OSFastMutex * tail
Definition: thread.h:127
BOOL OSIsThreadTerminated(OSThread *thread)
uint32_t pmc4
Definition: thread.h:83
uint32_t OSGetThreadSpecific(uint32_t id)
void OSClearThreadStackUsage(OSThread *thread)