muteki
Loading...
Searching...
No Matches
threading.h
Go to the documentation of this file.
1/*
2 * Copyright 2021-present dogtopus
3 * SPDX-License-Identifier: MIT
4 */
5
11#ifndef __MUTEKI_THREADING_H__
12#define __MUTEKI_THREADING_H__
13
14#include <muteki/common.h>
15#include <muteki/errno.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
40
52
56typedef int (*thread_func_t)(void *user_data);
57
63typedef struct {
65 unsigned char active_bytes;
67 unsigned char waiting_by[8];
69
76
81typedef struct {
85 unsigned short size;
87 short pop_idx;
89 short push_idx;
90 short _padding_0xa;
92
96typedef struct thread_s thread_t;
104typedef struct event_s event_t;
113
117struct thread_s {
119 int magic; // always 0x100
121 uintptr_t *sp;
123 void *stack;
125 int exit_code; // init to 0
127 kerrno_t kerrno; // init to 0
129 uintptr_t unk_0x14; // init to 0x80000000
133 short unk_0x1c;
140 short wait_reason; // 0x20
142 short slot; // 0x22
148 unsigned char slot_low3b_bit;
150 unsigned char slot_high3b_bit;
157 union {
159 char unk_0x34[0x20];
161 uintptr_t ktls[8];
162 };
163};
164
170 int magic;
171 int _padding_0x4;
173 short ctr;
179 char _padding_0x13;
180};
181
185struct event_s {
187 int magic;
189 int flag;
191 short latch_on;
197 char _padding_0x13;
198};
199
205 int magic; // 0x00000202
209 unsigned short refcount;
215 char _padding_0x13;
216}; // 0x14
217
223 int magic;
226 short _padding_0x8;
232 char _padding_0x13;
233};
234
244extern thread_t *OSCreateThread(thread_func_t func, void *user_data, size_t stack_size, bool defer_start);
245
253extern int OSTerminateThread(thread_t *thr, int exit_code);
254
270 extern bool OSSetThreadPriority(thread_t *thr, short new_slot);
271
278extern short OSGetThreadPriority(thread_t *thr);
279
287extern bool OSSuspendThread(thread_t *thr);
288
296extern bool OSResumeThread(thread_t *thr);
297
306extern bool OSWakeUpThread(thread_t *thr);
307
315 extern int OSExitThread(int exit_code);
316
323extern void OSSleep(short millis);
324
331extern semaphore_t *OSCreateSemaphore(short init_ctr);
332
341extern wait_result_t OSWaitForSemaphore(semaphore_t *semaphore, short timeout);
342
350extern bool OSReleaseSemaphore(semaphore_t *semaphore);
351
359extern bool OSCloseSemaphore(semaphore_t *semaphore);
360
368extern event_t *OSCreateEvent(short latch_on, int flag);
369
378extern wait_result_t OSWaitForEvent(event_t *event, short timeout);
379
388extern bool OSSetEvent(event_t *event);
389
398extern bool OSResetEvent(event_t *event);
399
407extern bool OSCloseEvent(event_t *event);
408
416
429
437
445
453extern message_queue_t *OSCreateMsgQue(unsigned short size);
454
463extern bool OSPostMsgQue(message_queue_t *queue, const message_queue_message_t *message);
464
473extern bool OSSendMsgQue(message_queue_t *queue, const message_queue_message_t *message);
474
484
494
502extern bool OSCloseMsgQue(message_queue_t *queue);
503
528
529#ifdef __cplusplus
530} // extern "C"
531#endif
532
533#endif // __MUTEKI_THREADING_H__
Common include file for all muteki-shims headers.
#define SYS_ALIGN(n)
Attribute for forcing alignment.
Definition common.h:86
Besta error code constants and functions.
unsigned int kerrno_t
Definition errno.h:22
bool OSPostMsgQue(message_queue_t *queue, const message_queue_message_t *message)
Push a message into the queue.
thread_wait_reason_e
Thread wait reason enum.
Definition threading.h:24
@ WAIT_ON_QUEUE
Definition threading.h:32
@ WAIT_ON_SUSPEND
Definition threading.h:34
@ WAIT_ON_SEMAPHORE
Definition threading.h:28
@ WAIT_ON_NONE
Definition threading.h:26
@ WAIT_ON_CRITICAL_SECTION
Definition threading.h:36
@ WAIT_ON_EVENT
Definition threading.h:30
@ WAIT_ON_SLEEP
Definition threading.h:38
int(* thread_func_t)(void *user_data)
Thread function type.
Definition threading.h:56
wait_result_t OSWaitForEvent(event_t *event, short timeout)
Wait for an event.
enum wait_result_e wait_result_t
Result of waitables.
int OSTerminateThread(thread_t *thr, int exit_code)
Terminate a thread.
wait_result_t OSWaitForSemaphore(semaphore_t *semaphore, short timeout)
Wait and acquire a semaphore.
bool OSSuspendThread(thread_t *thr)
Suspend a thread from outside of that thread.
bool OSPeekMsgQue(message_queue_t *queue, message_queue_message_t *message)
Peek the bottom of the queue without popping the message.
void OSLeaveCriticalSection(critical_section_t *cs)
Leave/release a critical section.
bool OSReleaseSemaphore(semaphore_t *semaphore)
Release a semaphore.
semaphore_t * OSCreateSemaphore(short init_ctr)
Create an semaphore descriptor.
thread_t * OSCreateThread(thread_func_t func, void *user_data, size_t stack_size, bool defer_start)
Create a new thread.
bool OSCloseSemaphore(semaphore_t *semaphore)
Destroy a semaphore.
bool OSResetEvent(event_t *event)
Reset the event flag.
char message_queue_message_t[16]
Message type for message queues.
Definition threading.h:75
bool OSCloseMsgQue(message_queue_t *queue)
Destroy a message queue descriptor.
wait_result_e
Result of waitables.
Definition threading.h:44
@ WAIT_RESULT_TIMEOUT
Definition threading.h:46
@ WAIT_RESULT_ERROR
Definition threading.h:50
@ WAIT_RESULT_RESOLVED
Definition threading.h:48
event_t * OSCreateEvent(short latch_on, int flag)
Create an event descriptor.
bool OSSendMsgQue(message_queue_t *queue, const message_queue_message_t *message)
Push a message into the queue and reschedule immediately.
void OSSleep(short millis)
Sleep for millis milliseconds.
short OSGetCurrentlyRunningTCBPrio(void)
Get the current running thread's priority (slot number).
void OSEnterCriticalSection(critical_section_t *cs)
Enter/aquire a critical section.
void OSInitCriticalSection(critical_section_t *cs)
Initialize a critical section descriptor.
short OSGetThreadPriority(thread_t *thr)
Get the thread priority (slot number).
void OSDeleteCriticalSection(critical_section_t *cs)
Destroy a critical section descriptor.
message_queue_t * OSCreateMsgQue(unsigned short size)
Create a message queue descriptor.
bool OSWakeUpThread(thread_t *thr)
Force wake up a sleeping thread.
bool OSCloseEvent(event_t *event)
Destroy the event descriptor.
bool OSSetThreadPriority(thread_t *thr, short new_slot)
Set the thread priority (slot number).
bool OSGetMsgQue(message_queue_t *queue, message_queue_message_t *message)
Pop a message from the queue.
bool OSResumeThread(thread_t *thr)
Start/restart a previously suspended thread.
int OSExitThread(int exit_code)
Terminate current thread.
bool OSSetEvent(event_t *event)
Set the event flag.
Critical section descriptor structure.
Definition threading.h:203
threading_waitable_t wait_state
Wait state of the current critical section.
Definition threading.h:214
int magic
Definition threading.h:205
thread_t * thr
Definition threading.h:207
unsigned short refcount
Definition threading.h:209
Event descriptor structure.
Definition threading.h:185
int flag
Definition threading.h:189
threading_waitable_t wait_state
Wait state of the current event.
Definition threading.h:196
int magic
Definition threading.h:187
short latch_on
Definition threading.h:191
Nonatomic backend storage for message queues.
Definition threading.h:81
short push_idx
Definition threading.h:89
message_queue_message_t * messages
Definition threading.h:83
short pop_idx
Definition threading.h:87
unsigned short size
Definition threading.h:85
Message queue descriptor structure.
Definition threading.h:221
threading_waitable_t wait_state
Wait state of the current event.
Definition threading.h:231
message_queue_nonatomic_t * storage
Definition threading.h:225
int magic
Definition threading.h:223
Semaphore descriptor structure.
Definition threading.h:168
short ctr
Definition threading.h:173
threading_waitable_t wait_state
Wait state of the current semaphore.
Definition threading.h:178
int magic
Definition threading.h:170
Thread descriptor structure.
Definition threading.h:117
kerrno_t kerrno
Definition threading.h:127
void * stack
Definition threading.h:123
short wait_reason
Definition threading.h:140
unsigned char slot_high3b_bit
Definition threading.h:150
short slot
Definition threading.h:142
uintptr_t ktls[8]
Definition threading.h:161
int magic
Definition threading.h:119
short unk_0x1c
Definition threading.h:133
thread_func_t thread_func
Definition threading.h:131
thread_t * next
Definition threading.h:156
unsigned char slot_low3b_bit
Definition threading.h:148
short sleep_counter
Definition threading.h:135
char slot_low3b
Definition threading.h:144
event_t * event
Definition threading.h:152
thread_t * prev
Definition threading.h:154
char slot_high3b
Definition threading.h:146
int exit_code
Definition threading.h:125
uintptr_t * sp
Definition threading.h:121
char unk_0x34[0x20]
Definition threading.h:159
uintptr_t unk_0x14
Definition threading.h:129
Common data structure for waitables.
Definition threading.h:63
unsigned char active_bytes
Definition threading.h:65