libnds
Macros | Typedefs | Functions
cothread.h File Reference

Cooperative multithreading system. More...

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>

Macros

#define COTHREAD_DETACHED   (1 << 0)
 Flags a thread as detached. More...
 

Typedefs

typedef int comutex_t
 Mutex.
 
typedef int cothread_t
 Thread ID.
 

Functions

static void comutex_acquire (comutex_t *mutex)
 Waits in a loop until the mutex is available. More...
 
static bool comutex_init (comutex_t *mutex)
 Initializes a mutex. More...
 
static void comutex_release (comutex_t *mutex)
 Releases a mutex. More...
 
static bool comutex_try_acquire (comutex_t *mutex)
 Tries to acquire a mutex without blocking execution. More...
 
cothread_t cothread_create (int(*entrypoint)(void *), void *arg, size_t stack_size, unsigned int flags)
 Creates a thread and allocate the stack for it. More...
 
cothread_t cothread_create_manual (int(*entrypoint)(void *), void *arg, void *stack_base, size_t stack_size, unsigned int flags)
 Create a thread. More...
 
int cothread_delete (cothread_t thread)
 Deletes a running thread and frees all memory used by it. More...
 
int cothread_detach (cothread_t thread)
 Detach the specified thread. More...
 
cothread_t cothread_get_current (void)
 Returns ID of the thread that is running currently. More...
 
int cothread_get_exit_code (cothread_t thread)
 If the thread has ended, this function returns the exit code. More...
 
bool cothread_has_joined (cothread_t thread)
 Used to determine if a thread is running or if it has ended (joined). More...
 
void cothread_yield (void)
 Tells the scheduler to switch to a different thread. More...
 
void cothread_yield_irq (uint32_t flags)
 Tells the scheduler to switch to a different thread until the specified IRQ has happened. More...
 
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. More...
 

Detailed Description

Cooperative multithreading system.

Only enabled in the ARM9 at the moment.

Macro Definition Documentation

◆ COTHREAD_DETACHED

#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.

Function Documentation

◆ comutex_acquire()

static void comutex_acquire ( comutex_t mutex)
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.

Parameters
mutexPointer to the mutex.

◆ comutex_init()

static bool comutex_init ( comutex_t mutex)
inlinestatic

Initializes a mutex.

Parameters
mutexPointer to the mutex.
Returns
It returns true if the mutex has been initialized, false if not.

◆ comutex_release()

static void comutex_release ( comutex_t mutex)
inlinestatic

Releases a mutex.

Parameters
mutexPointer to the mutex.

◆ comutex_try_acquire()

static bool comutex_try_acquire ( comutex_t mutex)
inlinestatic

Tries to acquire a mutex without blocking execution.

Parameters
mutexPointer to the mutex.
Returns
It returns true if the mutex has been acquired, false if not.

◆ cothread_create()

cothread_t cothread_create ( int(*)(void *)  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.

Parameters
entrypointFunction to be run. The argument is the value of 'arg' passed to cothread_create().
argArgument to be passed to entrypoint.
stack_sizeSize 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.
flagsSet of ORed flags (like COTHREAD_DETACHED) or 0.
Returns
On success, it returns 0. On failure, it returns -1 and sets errno.

◆ cothread_create_manual()

cothread_t cothread_create_manual ( int(*)(void *)  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.

Parameters
entrypointFunction to be run. The argument is the value of 'arg' passed to cothread_create_manual().
argArgument to be passed to entrypoint.
stack_basePointer to the base of the memory to be used as stack. It must be aligned to 64 bit.
stack_sizeSize of the stack. Must be aligned to 64 bit.
flagsSet of ORed flags (like COTHREAD_DETACHED) or 0.
Returns
On success, it returns 0. On failure, it returns -1 and sets errno.

◆ cothread_delete()

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.

Parameters
threadThread ID.
Returns
On success, it returns 0. On failure, it returns -1 and sets errno.

◆ cothread_detach()

int cothread_detach ( cothread_t  thread)

Detach the specified thread.

Parameters
threadThe thread to detach.
Returns
On success, it returns 0. On failure, it returns -1 and sets errno.

◆ cothread_get_current()

cothread_t cothread_get_current ( void  )

Returns ID of the thread that is running currently.

Returns
Thread ID of the current thread.

◆ cothread_get_exit_code()

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).

Parameters
threadThread ID.
Returns
Returns the exit code if the thread has finished, -1 otherwise. It will set errno as well (for example, if the thread is still running, it will set errno to EBUSY).

◆ cothread_has_joined()

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).

Parameters
threadThread ID.
Returns
Returns true if the thread has ended, false otherwise. It can also set errno.

◆ cothread_yield()

void cothread_yield ( void  )

Tells the scheduler to switch to a different thread.

This can also be called from main().

◆ cothread_yield_irq()

void cothread_yield_irq ( uint32_t  flags)

Tells the scheduler to switch to a different thread until the specified IRQ has happened.

Parameters
flagsIRQ flags to wait for.

◆ cothread_yield_irq_aux()

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.

Parameters
flagsAUX IRQ flags to wait for.
Note
ARM7 only.