Main Program Context

A singleton called g_maincontext defines the global program context.

Summary
Main Program ContextA singleton called g_maincontext defines the global program context.
Context Types
Accessing ContextThe process context (processcontext_t) can be accessed with maincontext_t.pcontext_maincontext.
Main ThreadThe main thread which executes the function main() and which is created implicitly by the operating system must call the function maincontext_t.init_maincontext to initialize its processcontext_t and associated threadcontext_t.
Other ThreadsAny additional created thread must call <thread_t.init_threadcontext> at its beginning and <thread_t.free_threadcontext> at its end.
Internal WorkingsThe function maincontext_t.init_maincontext calls processcontext_t.init_processcontext and threadcontext_t.init_threadcontext.

Context Types

  • The process of a running program and its associated threads all have a corresponding context object.
  • Every thread has its own object of type threadcontext_t and the process its corresponding processcontext_t.
  • Every global service, object or subsystem must be accessed through a pointer in either the process or thread context.

Every service which can be accessed from the processcontext_t shall be programmed in thread safe way.  Every service which is referenced only from threadcontext_t is already thread safe C-kern/api/maincontext.hcause only one thread calls this service.  This means that every service in threadcontext_t must be duplicated for every running thread.

Accessing Context

The process context (processcontext_t) can be accessed with maincontext_t.pcontext_maincontext.  The returned process context is the same for every thread.  The thread context (threadcontext_t) can be accessed with maincontext_t.tcontext_maincontext.  Every thread has its own thread context.

Main Thread

The main thread which executes the function main() and which is created implicitly by the operating system must call the function maincontext_t.init_maincontext to initialize its processcontext_t and associated threadcontext_t.

The difference between the main and any other threads is that only the main thread calls maincontext_t.init_maincontext which in turn initializates all subsystems of the C-kernel.  This initialization has to be done only once.

Before the main thread exits (after all other threads have exited) it should call maincontext_t.free_maincontext to make sure all resources are freed.

Other Threads

Any additional created thread must call <thread_t.init_threadcontext> at its beginning and <thread_t.free_threadcontext> at its end.  The thread module implementation does this automatically.  Use thread_t.new_thread and thread_t.delete_thread in this case.

Internal Workings

The function maincontext_t.init_maincontext calls processcontext_t.init_processcontext and threadcontext_t.init_threadcontext.

The function processcontext_t.init_processcontext calls all functions with patterns

int initonce_NAME(void)
int initonce_NAME(service_t**)

defined in any submodule in the same order as defined in “C-kern/resource/config/initprocess”.

The function threadcontext_t.init_threadcontext calls all functions with pattern

int initthread_NAME(service_t**)

defined in any submodule in the same order as defined in “C-kern/resource/config/initthread”.

This init database can be checked against the whole source tree with “C-kern/test/static/check_textdb.sh” - so that no entry is forgotten.

struct processcontext_t
Defines computing environment / context which is shared between all threads of computation.
processcontext_t * pcontext_maincontext(void)
Returns processcontext_t of the current process.
int init_maincontext(const maincontext_e context_type,
int argc,
const char **argv)
Initializes global program context.
int init_processcontext(/*out*/processcontext_t *pcontext)
Initializes the current process context.
int init_threadcontext(/*out*/threadcontext_t *tcontext,
struct processcontext_t *pcontext,
uint8_t context_type)
Creates all top level services which are bound to a single thread.
threadcontext_t * tcontext_maincontext(void)
Returns threadcontext_t of the current thread.
int free_maincontext(void)
Frees global context.
int new_thread(/*out*/thread_t **threadobj,
thread_f thread_main,
void *main_arg)
Creates and starts a new system thread.
int delete_thread(thread_t **threadobj)
Calls join_thread (if not already called) and deletes resources.
Close