Defines service context used by every software component in C-kern(el).
This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
© 2012 Jörg Seebohn
Header file of MainContext.
Implementation file MainContext impl.
Linux specific configuration file LinuxSystemContext.
typedef struct maincontext_t maincontext_t
Export maincontext_t.
typedef struct maincontext_startparam_t maincontext_startparam_t
Export maincontext_startparam_t.
typedef int ( * maincontext_thread_f ) (maincontext_t * maincontext)
Define maincontext_thread_f as signature of start function for main thread.
Used to switch between different implementations. Services in threadcontext_t can not be shared between threads. Services in processcontext_t are shared between threads.
maincontext_STATIC | An implementation which is configured by a static initializer. Only the log service is supported. This configuration is default after platform_t.init_platform has been called and can not be set with a call to maincontext_t.init_maincontext. |
maincontext_DEFAULT | Default single or multi threading implementation. All content logged to channel log_channel_USERERR is ignored. |
maincontext_CONSOLE | Default single or multi threading implementation for commandline tools. All content logged to channel log_channel_USERERR is immediately written (log_state_UNBUFFERED). All content logged to channel log_channel_ERR is ignored. |
extern struct maincontext_t g_maincontext
Global variable which describes the main context for the current process. The variable is located in process global storage. So every thread references the same maincontext_t.
test | |
unittest_context_maincontext | Test initialization process succeeds and global variables are set correctly. |
struct maincontext_startparam_t
Start parameters used in <initstart_maincontext>.
struct fields | |
context_type | Set to a value of maincontext_e. |
argc | The number of process arguments. |
argv | An array of program arguments. |
main_thread | The main threads main function. |
maincontext_startparam_INIT | Static initializer. |
maincontext_e context_type
Set to a value of maincontext_e. It determines the type of maincontext_t the process wants ro initialize.
struct maincontext_t
Defines the main top level context of the whole process.
It extends the functionality of processcontext_t and combines it with threadcontext_t.
It contains a single copy of processcontext_t and initializes threadcontext_t of the caller (main thread of the process).
Allows access to process & thread specific top level context.
TODO: implement SIGTERM support for shutting down the system in an ordered way (=> abort handler ? => abort handler calls free_maincontext ?)
struct fields | |
size_staticmem | Size in bytes of how much of the static memory is allocated. |
lifetime | |
initstart_maincontext | Calls platform_t.init_platform, maincontext_t.init_maincontext and runs main_thread. |
init_maincontext | Initializes global program context. |
free_maincontext | Frees global context. |
abort_maincontext | Exits the whole process in a controlled manner. |
assertfail_maincontext | Exits the whole process in a controlled manner. |
query | |
self_maincontext | Returns maincontext_t of the current process. |
pcontext_maincontext | Returns processcontext_t of the current process. |
tcontext_maincontext | Returns threadcontext_t of the current thread. |
type_maincontext | Returns type <context_e> of current maincontext_t. |
progname_maincontext | Returns the program name of the running process. |
threadid_maincontext | Returns the thread id of the calling thread. |
query-service | |
blockmap_maincontext | Returns shared blockmap used by pagecache_impl_t (see <pagecache_blockmap_t >). |
error_maincontext | Returns error string table (see errorcontext_t). |
log_maincontext | Returns log service log_t (see logwriter_t). |
mm_maincontext | Returns interfaceable object mm_t for access of memory manager. |
objectcache_maincontext | Returns interfaceable object objectcache_t for access of cached singleton objects. |
pagecache_maincontext | Returns object interface pagecache_t to access functionality of pagecache_impl_t. |
syncrun_maincontext | Returns syncrun_t of current maincontext_t. |
sysuser_maincontext | Returns <sysusercontext_t> of current maincontext_t. |
valuecache_maincontext | Returns valuecache_t holding precomputed values. |
static-memory | |
allocstatic_maincontext | Allocates size bytes of memory and returns the start address. |
freestatic_maincontext | Frees size bytes of last allocated memory. |
sizestatic_maincontext | Returns size in bytes of allocated static memory. |
int initstart_maincontext( const maincontext_startparam_t * startparam )
Calls platform_t.init_platform, maincontext_t.init_maincontext and runs main_thread. This is a convenience function so you do not have to remember the process initialization sequence pattern.
int init_maincontext( const maincontext_e context_type, int argc, const char ** argv )
Initializes global program context. Must be called as first function from the main thread. The main thread must be initialized with platform_t.init_platform before calling this function. EALREADY is returned if it is called more than once. The only service which works without calling this function is logging.
This function calls init_processcontext and init_threadcontext which call every initonce_NAME and initthread_NAME functions in the same order as defined in “C-kern/resource/config/initprocess” and “C-kern/resource/config/initthread”. This init database files are checked against the whole project with “C-kern/test/static/check_textdb.sh”. So that no entry is forgotten.
int free_maincontext( void )
Frees global context. Must be called as last function from the main thread of the whole system.
Ensures that after return the basic logging functionality is working.
This function calls free_threadcontext and then free_processcontext which call every freethread_NAME and freeonce_NAME functions in the reverse order as defined in “C-kern/resource/config/initthread” and “C-kern/resource/config/initprocess”. This init database files are checked against the whole project with “C-kern/test/static/check_textdb.sh”. So that no entry is forgotten.
void assertfail_maincontext( const char * condition, const char * file, int line, const char * funcname )
Exits the whole process in a controlled manner. writes »Assertion failed« to log and calls abort_maincontext.
Do not call assertfail_maincontext directly use the assert macro instead.
maincontext_t * self_maincontext( void )
Returns maincontext_t of the current process.
processcontext_t * pcontext_maincontext( void )
Returns processcontext_t of the current process.
threadcontext_t * tcontext_maincontext( void )
Returns threadcontext_t of the current thread.
maincontext_e type_maincontext( void )
Returns type <context_e> of current maincontext_t.
struct pagecache_blockmap_t * blockmap_maincontext( void )
Returns shared blockmap used by pagecache_impl_t (see <pagecache_blockmap_t >).
Returns error string table (see errorcontext_t).
Returns log service log_t (see logwriter_t). This function can only be implemented as a macro. C99 does not support references.
Returns interfaceable object mm_t for access of memory manager.
Returns interfaceable object objectcache_t for access of cached singleton objects.
Returns object interface pagecache_t to access functionality of pagecache_impl_t.
struct syncrun_t * syncrun_maincontext( void )
Returns syncrun_t of current maincontext_t. It is used to store and run all syncthread_t of the current thread.
Returns <sysusercontext_t> of current maincontext_t. It is used in implementation of module SystemUser.
struct valuecache_t * valuecache_maincontext( void )
Returns valuecache_t holding precomputed values. Every value is cached as a single copy for the whole process.
void * allocstatic_maincontext( uint8_t size )
Allocates size bytes of memory and returns the start address. Used by modules during execution of their initonce_ functions. This memory lives as long maincontext_t lives. Must be called in reverse order of calls to allocstatic_maincontext.
int freestatic_maincontext( uint8_t size )
Frees size bytes of last allocated memory. Must be called in reverse order of calls to allocstatic_maincontext. It is possible to free x calls to allocstatic_maincontext with one call to freestatic_maincontext where all sizes are summed up.
#define blockmap_maincontext( ) (pcontext_maincontext()->blockmap)
Implementation of maincontext_t.blockmap_maincontext.
#define error_maincontext( ) (pcontext_maincontext()->error)
Implementation of maincontext_t.error_maincontext.
#define log_maincontext( ) (tcontext_maincontext()->log)
Inline implementation of maincontext_t.log_maincontext. Uses a global thread-local storage variable to implement the functionality.
#define mm_maincontext( ) (tcontext_maincontext()->mm)
Inline implementation of maincontext_t.mm_maincontext. Uses a global thread-local storage variable to implement the functionality.
#define objectcache_maincontext( ) (tcontext_maincontext()->objectcache)
Inline implementation of maincontext_t.objectcache_maincontext. Uses a global thread-local storage variable to implement the functionality.
#define pagecache_maincontext( ) (tcontext_maincontext()->pagecache)
Implements maincontext_t.pagecache_maincontext.
#define pcontext_maincontext( ) (tcontext_maincontext()->pcontext)
Inline implementation of maincontext_t.pcontext_maincontext.
#define progname_maincontext( ) (self_maincontext()->progname)
Inline implementation of maincontext_t.progname_maincontext.
#define self_maincontext( ) ((maincontext_t*)pcontext_maincontext())
Inline implementation of maincontext_t.self_maincontext.
#define sizestatic_maincontext( ) (self_maincontext()->size_staticmem)
Inline implementation of maincontext_t.sizestatic_maincontext.
#define syncrun_maincontext( ) (tcontext_maincontext()->syncrun)
Inline implementation of maincontext_t.syncrun_maincontext.
#define sysuser_maincontext( ) (pcontext_maincontext()->sysuser)
Inline implementation of maincontext_t.sysuser_maincontext.
#define tcontext_maincontext( ) (sys_context_threadtls())
Inline implementation of maincontext_t.tcontext_maincontext.
#define threadid_maincontext( ) (tcontext_maincontext()->thread_id)
Inline implementation of maincontext_t.threadid_maincontext.
#define type_maincontext( ) (self_maincontext()->type)
Inline implementation of maincontext_t.type_maincontext.
#define valuecache_maincontext( ) (pcontext_maincontext()->valuecache)
Inline implementation of maincontext_t.valuecache_maincontext. Uses a global thread-local storage variable to implement the functionality.
Export maincontext_t.
typedef struct maincontext_t maincontext_t
Defines the main top level context of the whole process.
struct maincontext_t
Export maincontext_startparam_t.
typedef struct maincontext_startparam_t maincontext_startparam_t
Start parameters used in initstart_maincontext.
struct maincontext_startparam_t
Define maincontext_thread_f as signature of start function for main thread.
typedef int ( * maincontext_thread_f ) (maincontext_t * maincontext)
Global variable which describes the main context for the current process.
extern struct maincontext_t g_maincontext
Test initialization process succeeds and global variables are set correctly.
int unittest_context_maincontext( void )
Set to a value of maincontext_e.
maincontext_e context_type
The number of process arguments.
int argc
An array of program arguments.
const char ** argv
The main threads main function.
maincontext_thread_f main_thread
Static initializer.
#define maincontext_startparam_INIT( context_type, argc, argv, main_thread ) { context_type, argc, argv, main_thread }
Size in bytes of how much of the static memory is allocated.
uint16_t size_staticmem
Calls platform_t.init_platform, maincontext_t.init_maincontext and runs main_thread.
int initstart_maincontext( const maincontext_startparam_t * startparam )
Initialize system context and calls main_thread.
int init_platform( mainthread_f main_thread, void * user )
Initializes global program context.
int init_maincontext( const maincontext_e context_type, int argc, const char ** argv )
Frees global context.
int free_maincontext( void )
Exits the whole process in a controlled manner.
void abort_maincontext( int err )
Exits the whole process in a controlled manner.
void assertfail_maincontext( const char * condition, const char * file, int line, const char * funcname )
Returns maincontext_t of the current process.
maincontext_t * self_maincontext( void )
Returns processcontext_t of the current process.
processcontext_t * pcontext_maincontext( void )
Defines computing environment / context which is shared between all threads of computation.
struct processcontext_t
Returns threadcontext_t of the current thread.
threadcontext_t * tcontext_maincontext( void )
Returns type context_e of current maincontext_t.
maincontext_e type_maincontext( void )
Returns the program name of the running process.
const char * progname_maincontext( void )
Returns the thread id of the calling thread.
size_t threadid_maincontext( void )
Returns shared blockmap used by pagecache_impl_t (see <pagecache_blockmap_t >).
struct pagecache_blockmap_t * blockmap_maincontext( void )
Allocates and frees virtual memory pages and caches them.
struct pagecache_impl_t
Stores description of system errors in a string table.
struct errorcontext_t
Uses iobj_DECLARE to declare object supporting interface log_it.
iobj_DECLARE( log_t, log )
A logwriter writes the console channel messages to STDOUT any other channels to STDERR.
struct logwriter_t
Uses iobj_DECLARE to declare interfaceable object.
iobj_DECLARE( mm_t, mm )
Uses iobj_DECLARE to declare interfaceable object.
iobj_DECLARE( objectcache_t, objectcache )
Uses iobj_DECLARE to declare interfaceable object.
iobj_DECLARE( pagecache_t, pagecache )
Returns syncrun_t of current maincontext_t.
struct syncrun_t * syncrun_maincontext( void )
Manages a set of syncthread_t.
struct syncrun_t
Returns valuecache_t holding precomputed values.
struct valuecache_t * valuecache_maincontext( void )
Allocates size bytes of memory and returns the start address.
void * allocstatic_maincontext( uint8_t size )
Frees size bytes of last allocated memory.
int freestatic_maincontext( uint8_t size )
Returns size in bytes of allocated static memory.
uint16_t sizestatic_maincontext( void )
Implementation of maincontext_t.blockmap_maincontext.
#define blockmap_maincontext( ) (pcontext_maincontext()->blockmap)
Implementation of maincontext_t.error_maincontext.
#define error_maincontext( ) (pcontext_maincontext()->error)
Inline implementation of maincontext_t.log_maincontext.
#define log_maincontext( ) (tcontext_maincontext()->log)
Inline implementation of maincontext_t.mm_maincontext.
#define mm_maincontext( ) (tcontext_maincontext()->mm)
Inline implementation of maincontext_t.objectcache_maincontext.
#define objectcache_maincontext( ) (tcontext_maincontext()->objectcache)
Implements maincontext_t.pagecache_maincontext.
#define pagecache_maincontext( ) (tcontext_maincontext()->pagecache)
Inline implementation of maincontext_t.pcontext_maincontext.
#define pcontext_maincontext( ) (tcontext_maincontext()->pcontext)
Inline implementation of maincontext_t.progname_maincontext.
#define progname_maincontext( ) (self_maincontext()->progname)
Inline implementation of maincontext_t.self_maincontext.
#define self_maincontext( ) ((maincontext_t*)pcontext_maincontext())
Inline implementation of maincontext_t.sizestatic_maincontext.
#define sizestatic_maincontext( ) (self_maincontext()->size_staticmem)
Inline implementation of maincontext_t.syncrun_maincontext.
#define syncrun_maincontext( ) (tcontext_maincontext()->syncrun)
Inline implementation of maincontext_t.sysuser_maincontext.
#define sysuser_maincontext( ) (pcontext_maincontext()->sysuser)
Inline implementation of maincontext_t.tcontext_maincontext.
#define tcontext_maincontext( ) (sys_context_threadtls())
Inline implementation of maincontext_t.threadid_maincontext.
#define threadid_maincontext( ) (tcontext_maincontext()->thread_id)
Inline implementation of maincontext_t.type_maincontext.
#define type_maincontext( ) (self_maincontext()->type)
Inline implementation of maincontext_t.valuecache_maincontext.
#define valuecache_maincontext( ) (pcontext_maincontext()->valuecache)
Prints »Assertion failed« and aborts process.
#define assert( expr ) ((expr) ? (void) 0 : assertfail_maincontext(STR(expr), __FILE__, __LINE__, __FUNCTION__))
A simple function context which is executed in a cooperative way.
struct syncthread_t