Zth (libzth)
Loading...
Searching...
No Matches
init.h File Reference
#include <libzth/macros.h>
#include <stdlib.h>

Go to the source code of this file.

Classes

struct  zth_init_entry
 

Macros

#define ZTH_INIT_CALL_(f, ...)
 Mark the given function f to be invoked during zth_init() .
 
#define ZTH_INIT_CALL(f)
 
#define ZTH_DEINIT_CALL(f)
 Mark the given function f to be invoked at exit.
 
#define ZTH_APP_INIT_CALL_(f, ...)
 
#define ZTH_APP_INIT_CALL(f)   ZTH_APP_INIT_CALL_(f, f();)
 

Functions

void zth_init ()
 Perform one-time global initialization of the Zth library.
 
void zth_preinit ()
 Initialization function to be called by the default-supplied main(), before doing anything else.
 
int zth_postdeinit ()
 Initialization function to be called by the default-supplied main(), just before shutting down.
 
int zth_run (void(fiber)(void *), void *arg)
 Start Zth given the given fiber function.
 
int zth_main (int argc, char **argv)
 Default main function that runs main_fiber.
 

Variables

struct zth_init_entry const * zth_init_head
 
struct zth_init_entryzth_init_tail
 

Macro Definition Documentation

◆ ZTH_APP_INIT_CALL

#define ZTH_APP_INIT_CALL (   f)    ZTH_APP_INIT_CALL_(f, f();)

Definition at line 91 of file init.h.

◆ ZTH_APP_INIT_CALL_

#define ZTH_APP_INIT_CALL_ (   f,
  ... 
)
Value:
struct f##__app_init { \
f##__app_init() noexcept \
{ \
__VA_ARGS__ \
} \
}; \
inline f##__app_init const f##__app_init_;

Definition at line 64 of file init.h.

◆ ZTH_DEINIT_CALL

#define ZTH_DEINIT_CALL (   f)
Value:
ZTH_INIT_CALL_(f, atexit(f);) \
static f##__init f##__deinit_;
#define ZTH_INIT_CALL_(f,...)
Mark the given function f to be invoked during zth_init() .
Definition init.h:34

Mark the given function f to be invoked at exit.

Definition at line 53 of file init.h.

◆ ZTH_INIT_CALL

#define ZTH_INIT_CALL (   f)
Value:
ZTH_INIT_CALL_(f, f();) \
static f##__init const f##__init_;

Definition at line 41 of file init.h.

◆ ZTH_INIT_CALL_

#define ZTH_INIT_CALL_ (   f,
  ... 
)
Value:
struct f##__init : public zth_init_entry { \
f##__init() noexcept \
: zth_init_entry(&exec, nullptr) \
{} \
static void exec() { __VA_ARGS__ }; \
};
Definition init.h:13

Mark the given function f to be invoked during zth_init() .

Definition at line 34 of file init.h.

Function Documentation

◆ zth_init()

void zth_init ( )

Perform one-time global initialization of the Zth library.

Initialization is only done once. It is safe to call it multiple times.

The initialization sequence is initialized by ZTH_INIT_CALL() and processed in the same order as normal static initializers are executed.

Definition at line 38 of file init.cpp.

◆ zth_main()

int zth_main ( int  argc,
char **  argv 
)

Default main function that runs main_fiber.

Unless main() is defined in the application, this function is called by the default-provided weak main().

Definition at line 104 of file init.cpp.

◆ zth_postdeinit()

int zth_postdeinit ( )

Initialization function to be called by the default-supplied main(), just before shutting down.

This function can be used to run machine/board-specific cleanup in main() before returning. The default (weak) implementation does nothing.

Returns
the exit code of the application, which overrides the returned value from main_fiber() when non-zero

Definition at line 24 of file zth_postdeinit.cpp.

◆ zth_preinit()

void zth_preinit ( )

Initialization function to be called by the default-supplied main(), before doing anything else.

This function can be used to run machine/board-specific initialization in main() even before zth_init() is invoked. The default (weak) implementation does nothing.

Definition at line 22 of file zth_preinit.cpp.

◆ zth_run()

int zth_run ( void(fiber)(void *)  ,
void *  arg 
)

Start Zth given the given fiber function.

It can be used instead of zth_main() or main(). In contrast, this function does not call zth_preinit() and zth_postdeinit().

Returns
0 when finished the fiber successfully, otherwise an errno

Definition at line 64 of file init.cpp.

Variable Documentation

◆ zth_init_head

struct zth_init_entry const* zth_init_head
extern

Definition at line 15 of file init.cpp.

◆ zth_init_tail

struct zth_init_entry* zth_init_tail
extern

Definition at line 17 of file init.cpp.