bio
 
Loading...
Searching...
No Matches
Miscellaneous functions

Data Structures

struct  bio_tag_t
 A unique tag. More...
 

Macros

#define BIO_TAG_INIT(NAME)
 Utility macro for initializing a tag.
 

Typedefs

typedef int64_t bio_time_t
 A timestamp.
 

Enumerations

enum  bio_exit_reason_t { BIO_EXIT_OS_REQUEST , BIO_EXIT_TERMINATE }
 Result for bio_wait_for_exit. More...
 

Functions

void bio_run_async (bio_entrypoint_t task, void *userdata, bio_signal_t signal)
 Run a function in the async thread pool.
 
static void bio_run_async_and_wait (bio_entrypoint_t task, void *userdata)
 Convenient function to start an async task and wait for it to complete.
 
bio_time_t bio_current_time_ms (void)
 Return the current time in milliseconds.
 
bio_exit_reason_t bio_wait_for_exit (void)
 Wait for an exit signal to be delivered by the OS.
 

Detailed Description

Macro Definition Documentation

◆ BIO_TAG_INIT

#define BIO_TAG_INIT (   NAME)
Value:
{ \
.name = NAME, \
.file = __FILE__, \
.line = __LINE__, \
}

Utility macro for initializing a tag.

See also
bio_tag_t

Typedef Documentation

◆ bio_time_t

typedef int64_t bio_time_t

Enumeration Type Documentation

◆ bio_exit_reason_t

Result for bio_wait_for_exit.

Enumerator
BIO_EXIT_OS_REQUEST 

The OS has requested that the program exits.

BIO_EXIT_TERMINATE 

bio_terminate has been called

Function Documentation

◆ bio_current_time_ms()

bio_time_t bio_current_time_ms ( void  )

Return the current time in milliseconds.

Take note that this is taken from an undetermined platform-dependent point. That is to say, it is only suitable for measuring intervals and has no relation to UNIX time or boot time.

◆ bio_run_async()

void bio_run_async ( bio_entrypoint_t  task,
void *  userdata,
bio_signal_t  signal 
)

Run a function in the async thread pool.

When lengthy or potentially blocking work needs to be performed, this should be used to avoid blocking the main thread.

The number of async thread is configured through bio_options_t::num_threads. More threads will allow more tasks to be executed in parallel.

Each thread has a queue whose size is configured through bio_options_t::queue_size. A larger queue size will allow more tasks to be pending.

bio will always try to load balance the thread pool and send a new task to the least loaded thread. However, when all queues are full, the calling coroutine will enter a busy wait loop. This is done using bio_yield so other coroutines still get to run.

Internally, bio also uses the async thread pool to convert a potentially blocking syscall to an asynchronous one when there is no async equivalence for the runnning platform.

Parameters
taskEntrypoint of the function
userdataArbitrary userdata passed to the function. This data must remain valid until the function finishes. Since this function does not return a value, any result should be written to this data instead.
signalThe signal that will be raised when task finishes execution
See also
bio_run_async_and_wait

◆ bio_run_async_and_wait()

static void bio_run_async_and_wait ( bio_entrypoint_t  task,
void *  userdata 
)
inlinestatic

Convenient function to start an async task and wait for it to complete.

◆ bio_wait_for_exit()

bio_exit_reason_t bio_wait_for_exit ( void  )

Wait for an exit signal to be delivered by the OS.

This typically happens when the user presses Ctrl+C or shuts down the machine. The calling coroutine will be suspended until that happens. This function will return BIO_EXIT_OS_REQUEST.

The calling coroutine will also be turned into a daemon upon calling this function. If no exit signal was sent by the OS and the program terminates on its own, this function will return BIO_EXIT_TERMINATE.

Remarks
If no coroutine has called this function and the OS sends an exit signal, the default platform-specific action will be taken. Usually, it just means exiting without cleanup.
Returns
The exit reason