Muteki
EABI code execution on Besta® RTOS devices.
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
10
11#ifndef __MUTEKI_THREADING_H__
12#define __MUTEKI_THREADING_H__
13
14#include "common.h"
15#include "errno.h"
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
61
83
101
105typedef int (*bxc_thread_func_t)(void *user_data);
106
112typedef struct {
116 unsigned char active_bytes;
121 unsigned char waiting_by[8];
123
129typedef unsigned char bxc_queue_message_t[16] SYS_ALIGN(4);
130
135typedef struct {
143 unsigned short size;
147 short pop_idx;
151 short push_idx;
157
158
161typedef struct bxc_thread_s bxc_thread_t;
165typedef struct bxc_semaphore_s bxc_semaphore_t;
169typedef struct bxc_event_s bxc_event_t;
173typedef struct bxc_cs_s bxc_cs_t;
177typedef struct bxc_queue_s bxc_queue_t;
186struct bxc_thread_s {
187 /**
188 * @brief Magic. Always `0x100`.
189 * @see bxc_threading_kind_e
190 */
191 int magic; // always 0x100
196 uintptr_t *sp;
200 void *stack;
204 int exit_code; // init to 0
208 bxc_errno_t kerrno; // init to 0
212 uintptr_t unk_0x14; // init to 0x80000000
226 short timeout;
231 short sleep_counter;
236 short wait_reason; // 0x20
240 short slot; // 0x22
244 char slot_low3b;
248 char slot_high3b;
252 unsigned char slot_low3b_bit;
256 unsigned char slot_high3b_bit;
269 union {
270 /**
271 * @brief Old placeholder name.
272 * @deprecated Confirmed to be a name field and might contain data on certain builds.
273 */
274 char unk_0x34[0x20];
279 uintptr_t ktls[8];
283 char name[0x20];
284 };
285};
286
287
290struct bxc_semaphore_s {
291 /**
292 * @brief Magic. Always `0x200`.
293 * @see bxc_threading_kind_e
294 */
295 int magic;
303 short ctr;
312};
313
316
317struct bxc_event_s {
320 * @see bxc_threading_kind_e
321 */
322 int magic;
326 int flag;
330 short latch_on;
340};
341
345struct bxc_cs_s {
349 * @see bxc_threading_kind_e
350 */
351 int magic; // 0x00000202
359 unsigned short refcount;
369}; // 0x14
370
374struct bxc_queue_s {
375
407
410 int magic;
411 /**
412 * @brief Defined by synchronization primitive.
413 */
414 intptr_t user_data0;
418 short user_data1;
427 char user_data2;
428 };
429
436
449 bxc_cs_t cs;
460};
461
468
471extern bxc_thread_t *OSCreateThread(bxc_thread_func_t func, void *user_data, size_t stack_size, bool defer_start);
472
480extern int OSTerminateThread(bxc_thread_t *thr, int exit_code);
481
497 extern bool OSSetThreadPriority(bxc_thread_t *thr, short new_slot);
498
506
515
523extern bool OSResumeThread(bxc_thread_t *thr);
524
533extern bool OSWakeUpThread(bxc_thread_t *thr);
534
542 extern int OSExitThread(int exit_code);
543
555extern void OSSleep(short time_units);
556
563extern bxc_semaphore_t *OSCreateSemaphore(short init_ctr);
564
572extern bxc_wait_result_t OSWaitForSemaphore(bxc_semaphore_t *semaphore, short timeout);
573
581extern bool OSReleaseSemaphore(bxc_semaphore_t *semaphore);
582
590extern bool OSCloseSemaphore(bxc_semaphore_t *semaphore);
591
599extern bxc_event_t *OSCreateEvent(short latch_on, int flag);
600
608extern bxc_wait_result_t OSWaitForEvent(bxc_event_t *event, short timeout);
609
618extern bool OSSetEvent(bxc_event_t *event);
619
628extern bool OSResetEvent(bxc_event_t *event);
629
637extern bool OSCloseEvent(bxc_event_t *event);
638
646
659
667
677
686extern bxc_queue_t *OSCreateMsgQue(unsigned short size);
687
696extern bool OSPostMsgQue(bxc_queue_t *queue, const bxc_queue_message_t *message);
697
707extern bool OSSendMsgQue(bxc_queue_t *queue, const bxc_queue_message_t *message);
708
718extern bool OSPeekMsgQue(bxc_queue_t *queue, bxc_queue_message_t *message);
719
728extern bool OSGetMsgQue(bxc_queue_t *queue, bxc_queue_message_t *message);
729
737extern bool OSCloseMsgQue(bxc_queue_t *queue);
738
763
764#ifdef __cplusplus
765} // extern "C"
766#endif
767
768#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 bxc_errno_t
Definition errno.h:22
void OSLeaveCriticalSection(bxc_cs_t *cs)
Leave/release a critical section.
struct bxc_event_s bxc_event_t
Event descriptor type.
Definition threading.h:170
bool OSResumeThread(bxc_thread_t *thr)
Start/restart a previously suspended thread.
bool OSSuspendThread(bxc_thread_t *thr)
Suspend a thread from outside of that thread.
bxc_wait_reason_e
Thread wait reason enum.
Definition threading.h:24
@ BXC_WAIT_ON_NONE
Nothing.
Definition threading.h:28
@ BXC_WAIT_ON_SUSPEND
Waiting to be unsuspended by OSResumeThread().
Definition threading.h:44
@ BXC_WAIT_ON_SLEEP
Deprecated name of BXC_WAIT_ON_TIMEOUT.
Definition threading.h:53
@ BXC_WAIT_ON_YIELD
Waiting to take back control after a yield/timeout.
Definition threading.h:59
@ BXC_WAIT_ON_QUEUE
Waiting for a message queue push.
Definition threading.h:40
@ BXC_WAIT_ON_CRITICAL_SECTION
Waiting for a critical section to be released.
Definition threading.h:48
@ BXC_WAIT_ON_SEMAPHORE
Waiting on a semaphore.
Definition threading.h:32
@ BXC_WAIT_ON_EVENT
Waiting on an event.
Definition threading.h:36
bxc_wait_result_e
Result of waitables.
Definition threading.h:87
@ BXC_WAIT_RESULT_ERROR
An error occurred.
Definition threading.h:99
@ BXC_WAIT_RESULT_TIMEOUT
Timeout before the event is set.
Definition threading.h:91
@ BXC_WAIT_RESULT_RESOLVED
The event is set.
Definition threading.h:95
bxc_event_t * OSCreateEvent(short latch_on, int flag)
Create an event descriptor.
int OSTerminateThread(bxc_thread_t *thr, int exit_code)
Terminate a thread.
bool OSGetMsgQue(bxc_queue_t *queue, bxc_queue_message_t *message)
Pop a message from the queue.
struct bxc_thread_s bxc_thread_t
Thread descriptor type.
Definition threading.h:162
bool OSPeekMsgQue(bxc_queue_t *queue, bxc_queue_message_t *message)
Pop a message from the queue asynchronously.
bool OSResetEvent(bxc_event_t *event)
Reset the event flag.
struct bxc_queue_s bxc_queue_t
Message queue descriptor type.
Definition threading.h:178
void OSSleep(short time_units)
Sleep for number of scheduler time_units .
enum bxc_wait_result_e bxc_wait_result_t
Result of waitables.
bxc_threading_kind_e
Runtime threading descriptor kind code.
Definition threading.h:65
@ BXC_THREADING_KIND_THREAD
Thread.
Definition threading.h:69
@ BXC_THREADING_KIND_SEMAPHORE
Semaphore.
Definition threading.h:73
@ BXC_THREADING_KIND_EVENT
Event.
Definition threading.h:77
@ BXC_THREADING_KIND_CS_QUEUE
Critical section or queue.
Definition threading.h:81
bool OSSetEvent(bxc_event_t *event)
Set the event flag.
bxc_thread_t * OSCreateThread(bxc_thread_func_t func, void *user_data, size_t stack_size, bool defer_start)
Create a new thread.
bool OSReleaseSemaphore(bxc_semaphore_t *semaphore)
Release a semaphore.
bxc_semaphore_t * OSCreateSemaphore(short init_ctr)
Create an semaphore descriptor.
bool OSCloseSemaphore(bxc_semaphore_t *semaphore)
Destroy a semaphore.
void OSDeleteCriticalSection(bxc_cs_t *cs)
Destroy a critical section descriptor.
int(*) bxc_thread_func_t(void *user_data)
Thread function type.
Definition threading.h:105
bxc_wait_result_t OSWaitForEvent(bxc_event_t *event, short timeout)
Wait for an event.
bool OSPostMsgQue(bxc_queue_t *queue, const bxc_queue_message_t *message)
Push a message into the queue.
struct bxc_semaphore_s bxc_semaphore_t
Semaphore descriptor type.
Definition threading.h:166
bxc_wait_result_t OSWaitForSemaphore(bxc_semaphore_t *semaphore, short timeout)
Wait and acquire a semaphore.
short OSGetCurrentlyRunningTCBPrio(void)
Get the current running thread's priority (slot number).
bool OSCloseEvent(bxc_event_t *event)
Destroy the event descriptor.
bool OSCloseMsgQue(bxc_queue_t *queue)
Destroy a message queue descriptor.
bool OSSetThreadPriority(bxc_thread_t *thr, short new_slot)
Set the thread priority (slot number).
void OSEnterCriticalSection(bxc_cs_t *cs)
Enter/aquire a critical section.
bool OSSendMsgQue(bxc_queue_t *queue, const bxc_queue_message_t *message)
Push a message into the queue and reschedule immediately.
short OSGetThreadPriority(bxc_thread_t *thr)
Get the thread priority (slot number).
union bxc_waitable_desc_u bxc_waitable_desc_t
Waitable object union used by bxc_thread_t.
Definition threading.h:182
bool OSWakeUpThread(bxc_thread_t *thr)
Force wake up a sleeping thread.
bxc_queue_t * OSCreateMsgQue(unsigned short size)
Create a message queue descriptor.
void OSInitCriticalSection(bxc_cs_t *cs)
Initialize a critical section descriptor.
struct bxc_cs_s bxc_cs_t
Critical section descriptor type.
Definition threading.h:174
int OSExitThread(int exit_code)
Terminate current thread.
unsigned char bxc_queue_message_t[16]
Message type for message queues.
Definition threading.h:129
Critical section descriptor structure.
Definition threading.h:349
unsigned short refcount
Reference counter.
Definition threading.h:363
int magic
Magic. Always 0x202.
Definition threading.h:355
bxc_waitable_t wait_state
Wait state of the current critical section.
Definition threading.h:368
bxc_thread_t * thr
Thread descriptor that currently holds the CS.
Definition threading.h:359
char _padding_0x13
Unknown. Probably padding bytes.
Definition threading.h:368
Event descriptor structure.
Definition threading.h:320
char _padding_0x13
Unknown. Probably padding bytes.
Definition threading.h:339
int flag
Flag value. 1 is set and 0 is clear.
Definition threading.h:329
int magic
Magic. Always 0x201.
Definition threading.h:325
short latch_on
Latch on after event wait.
Definition threading.h:333
bxc_waitable_t wait_state
Wait state of the current event.
Definition threading.h:338
Nonatomic backend storage for message queues.
Definition threading.h:135
bxc_queue_message_t * messages
Message body.
Definition threading.h:139
short pop_idx
Pop index.
Definition threading.h:147
short _padding_0xa
Unknown. Probably padding bytes.
Definition threading.h:155
short push_idx
Push index.
Definition threading.h:151
unsigned short size
Number of chunks.
Definition threading.h:143
Message queue descriptor structure.
Definition threading.h:379
int magic
Magic. Always 0x202.
Definition threading.h:385
char _padding_0x13
Unknown. Probably padding bytes.
Definition threading.h:398
bxc_queue_nonatomic_t * storage
Storage structure.
Definition threading.h:390
short _padding_0x8
Unknown. Probably padding bytes.
Definition threading.h:389
bxc_waitable_t wait_state
Wait state of the current event.
Definition threading.h:394
Semaphore descriptor structure.
Definition threading.h:291
int magic
Magic. Always 0x200.
Definition threading.h:296
char _padding_0x13
Unknown. Probably padding bytes.
Definition threading.h:311
short ctr
Counter.
Definition threading.h:303
int _padding_0x4
Unknown. Probably padding bytes.
Definition threading.h:299
bxc_waitable_t wait_state
Wait state of the current semaphore.
Definition threading.h:307
Thread descriptor structure.
Definition threading.h:187
short slot
Slot number. For scheduler.
Definition threading.h:241
unsigned char slot_low3b_bit
Lower 3 bit bitmask of the slot number. For scheduler.
Definition threading.h:253
bxc_thread_t * prev
Previous thread descriptor.
Definition threading.h:265
int magic
Magic. Always 0x100.
Definition threading.h:192
int exit_code
Exit code of the thread. Initializes to 0.
Definition threading.h:205
bxc_thread_t * next
Next thread descriptor.
Definition threading.h:269
bxc_waitable_desc_t * event
Waitable descriptor (excluding critical sections) that the thread is waiting for.
Definition threading.h:261
bxc_thread_func_t thread_func
Thread function entrypoint.
Definition threading.h:217
uintptr_t unk_0x14
Unknown. Initializes to 0x80000000.
Definition threading.h:213
char slot_high3b
Upper 3 bit of the slot number. For scheduler.
Definition threading.h:249
short wait_reason
Current wait reason of the thread.
Definition threading.h:237
void * stack
Allocated stack memory.
Definition threading.h:201
bxc_errno_t kerrno
Error code.
Definition threading.h:209
unsigned char slot_high3b_bit
Upper 3 bit bitmask of the slot number. For scheduler.
Definition threading.h:257
uintptr_t * sp
Stack pointer.
Definition threading.h:197
short timeout
Thread execution timeout in number of scheduler ticks.
Definition threading.h:227
short sleep_counter
Number of scheduler time units left to sleep.
Definition threading.h:232
char slot_low3b
Lower 3 bit of the slot number. For scheduler.
Definition threading.h:245
bxc_waitable_t wait_state
Wait state of the current descriptor.
short user_data1
Defined by synchronization primitive.
char user_data2
Defined by synchronization primitive.
intptr_t user_data0
Defined by synchronization primitive.
Common data structure for waitables.
Definition threading.h:112
unsigned char active_bytes
Bitfield that indicates which waiting_by bytes are currently active.
Definition threading.h:116
unsigned char waiting_by[8]
Bitfield that tracks threads that are waiting for this waitable.
Definition threading.h:121
char name[0x20]
Name of the thread. For debugging purposes.
uintptr_t ktls[8]
Kernel TLS (Muteki-specific).
char unk_0x34[0x20]
Old placeholder name.
Generic waitable descriptor.
Definition threading.h:411
bxc_semaphore_t semaphore
Semaphore-specific reader.
Definition threading.h:441
bxc_event_t event
Event-specific reader.
Definition threading.h:446
bxc_cs_t cs
Critical section-specific reader.
Definition threading.h:456
bxc_queue_t queue
Queue-specific reader.
Definition threading.h:466