libnds
|
Cooperative multithreading system. More...
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
Macros | |
#define | COTHREAD_DETACHED (1 << 0) |
Flags a thread as detached. | |
Typedefs | |
typedef int | comutex_t |
Mutex. | |
typedef int(* | cothread_entrypoint_t) (void *) |
Thread entrypoint. | |
typedef int | cothread_t |
Thread ID. | |
Functions | |
static void | comutex_acquire (comutex_t *mutex) |
Waits in a loop until the mutex is available. | |
static bool | comutex_init (comutex_t *mutex) |
Initializes a mutex. | |
static void | comutex_release (comutex_t *mutex) |
Releases a mutex. | |
static bool | comutex_try_acquire (comutex_t *mutex) |
Tries to acquire a mutex without blocking execution. | |
cothread_t | cothread_create (cothread_entrypoint_t entrypoint, void *arg, size_t stack_size, unsigned int flags) |
Creates a thread and allocate the stack for it. | |
cothread_t | cothread_create_manual (cothread_entrypoint_t entrypoint, void *arg, void *stack_base, size_t stack_size, unsigned int flags) |
Create a thread. | |
int | cothread_delete (cothread_t thread) |
Deletes a running thread and frees all memory used by it. | |
int | cothread_detach (cothread_t thread) |
Detach the specified thread. | |
cothread_t | cothread_get_current (void) |
Returns ID of the thread that is running currently. | |
int | cothread_get_exit_code (cothread_t thread) |
If the thread has ended, this function returns the exit code. | |
bool | cothread_has_joined (cothread_t thread) |
Used to determine if a thread is running or if it has ended (joined). | |
void | cothread_yield (void) |
Tells the scheduler to switch to a different thread. | |
void | cothread_yield_irq (uint32_t flags) |
Tells the scheduler to switch to a different thread until the specified IRQ has happened. | |
void | cothread_yield_irq_aux (uint32_t flags) |
Tells the scheduler to switch to a different thread until the specified ARM7 AUX IRQ has happened. | |
Cooperative multithreading system.
Only enabled in the ARM9 at the moment.
#define COTHREAD_DETACHED (1 << 0) |
Flags a thread as detached.
A detached thread deallocates all memory used by it when it ends. Calling cothread_has_joined() or cothread_get_exit_code() isn't allowed.
|
inlinestatic |
Waits in a loop until the mutex is available.
The main body of the loop calls cothread_yield() after each try, so that other threads can take control of the CPU and eventually release the mutex.
mutex | Pointer to the mutex. |
|
inlinestatic |
Initializes a mutex.
mutex | Pointer to the mutex. |
|
inlinestatic |
Releases a mutex.
mutex | Pointer to the mutex. |
|
inlinestatic |
Tries to acquire a mutex without blocking execution.
mutex | Pointer to the mutex. |
cothread_t cothread_create | ( | cothread_entrypoint_t | entrypoint, |
void * | arg, | ||
size_t | stack_size, | ||
unsigned int | flags | ||
) |
Creates a thread and allocate the stack for it.
This stack will be freed when the thread is deleted.
Important: If this thread is going to do filesystem accesses, you need to assign it a reasonably big stack size.
entrypoint | Function to be run. The argument is the value of 'arg' passed to cothread_create(). |
arg | Argument to be passed to entrypoint. |
stack_size | Size of the stack. If it is set to zero it will use a default value. If non-zero, it must be aligned to 64 bit. |
flags | Set of ORed flags (like COTHREAD_DETACHED) or 0. |
cothread_t cothread_create_manual | ( | cothread_entrypoint_t | entrypoint, |
void * | arg, | ||
void * | stack_base, | ||
size_t | stack_size, | ||
unsigned int | flags | ||
) |
Create a thread.
The stack is owned by the caller of this function, and it has to be freed manually after the thread ends.
entrypoint | Function to be run. The argument is the value of 'arg' passed to cothread_create_manual(). |
arg | Argument to be passed to entrypoint. |
stack_base | Pointer to the base of the memory to be used as stack. It must be aligned to 64 bit. |
stack_size | Size of the stack. Must be aligned to 64 bit. |
flags | Set of ORed flags (like COTHREAD_DETACHED) or 0. |
int cothread_delete | ( | cothread_t | thread | ) |
Deletes a running thread and frees all memory used by it.
It isn't possible to delete the currently running thread.
thread | Thread ID. |
int cothread_detach | ( | cothread_t | thread | ) |
Detach the specified thread.
thread | The thread to detach. |
cothread_t cothread_get_current | ( | void | ) |
Returns ID of the thread that is running currently.
int cothread_get_exit_code | ( | cothread_t | thread | ) |
If the thread has ended, this function returns the exit code.
Don't call this if the thread is detached, it will never return an exit code because the thread information will be deleted as soon as the thread ends (and, at that point, it won't exist, so the function will return an error code).
thread | Thread ID. |
bool cothread_has_joined | ( | cothread_t | thread | ) |
Used to determine if a thread is running or if it has ended (joined).
Don't call this if the thread is detached. It will always return false because as soon as the thread ends all information associated to it will be deleted (and, at that point, it won't exist, so the function will return false along an error code).
thread | Thread ID. |
void cothread_yield | ( | void | ) |
Tells the scheduler to switch to a different thread.
This can also be called from main().
void cothread_yield_irq | ( | uint32_t | flags | ) |
Tells the scheduler to switch to a different thread until the specified IRQ has happened.
flags | IRQ flags to wait for. |
void cothread_yield_irq_aux | ( | uint32_t | flags | ) |
Tells the scheduler to switch to a different thread until the specified ARM7 AUX IRQ has happened.
flags | AUX IRQ flags to wait for. |