MainContext

Defines service context used by every software component in C-kern(el).

Summary
MainContextDefines service context used by every software component in C-kern(el).
CopyrightThis program is free software.
Files
C-kern/api/maincontext.hHeader file of MainContext.
C-kern/context/maincontext.cImplementation file MainContext impl.
C-kern/api/platform/Linux/syscontext.hLinux specific configuration file LinuxSystemContext.
Types
struct maincontext_tExport maincontext_t.
struct maincontext_startparam_tExport maincontext_startparam_t.
maincontext_thread_fDefine maincontext_thread_f as signature of start function for main thread.
Enumerations
maincontext_eUsed to switch between different implementations.
Variables
g_maincontextGlobal variable which describes the main context for the current process.
Functions
test
unittest_context_maincontextTest initialization process succeeds and global variables are set correctly.
maincontext_startparam_tStart parameters used in <initstart_maincontext>.
struct fields
context_typeSet to a value of maincontext_e.
argcThe number of process arguments.
argvAn array of program arguments.
main_threadThe main threads main function.
maincontext_startparam_INITStatic initializer.
maincontext_tDefines the main top level context of the whole process.
struct fields
size_staticmemSize in bytes of how much of the static memory is allocated.
lifetime
initstart_maincontextCalls platform_t.init_platform, maincontext_t.init_maincontext and runs main_thread.
init_maincontextInitializes global program context.
free_maincontextFrees global context.
abort_maincontextExits the whole process in a controlled manner.
assertfail_maincontextExits the whole process in a controlled manner.
query
self_maincontextReturns maincontext_t of the current process.
pcontext_maincontextReturns processcontext_t of the current process.
tcontext_maincontextReturns threadcontext_t of the current thread.
type_maincontextReturns type <context_e> of current maincontext_t.
progname_maincontextReturns the program name of the running process.
threadid_maincontextReturns the thread id of the calling thread.
query-service
blockmap_maincontextReturns shared blockmap used by pagecache_impl_t (see <pagecache_blockmap_t >).
error_maincontextReturns error string table (see errorcontext_t).
log_maincontextReturns log service log_t (see logwriter_t).
mm_maincontextReturns interfaceable object mm_t for access of memory manager.
objectcache_maincontextReturns interfaceable object objectcache_t for access of cached singleton objects.
pagecache_maincontextReturns object interface pagecache_t to access functionality of pagecache_impl_t.
syncrun_maincontextReturns syncrun_t of current maincontext_t.
sysuser_maincontextReturns <sysusercontext_t> of current maincontext_t.
valuecache_maincontextReturns valuecache_t holding precomputed values.
static-memory
allocstatic_maincontextAllocates size bytes of memory and returns the start address.
freestatic_maincontextFrees size bytes of last allocated memory.
sizestatic_maincontextReturns size in bytes of allocated static memory.
inline implementation
Macros
blockmap_maincontextImplementation of maincontext_t.blockmap_maincontext.
error_maincontextImplementation of maincontext_t.error_maincontext.
log_maincontextInline implementation of maincontext_t.log_maincontext.
mm_maincontextInline implementation of maincontext_t.mm_maincontext.
objectcache_maincontextInline implementation of maincontext_t.objectcache_maincontext.
pagecache_maincontextImplements maincontext_t.pagecache_maincontext.
pcontext_maincontextInline implementation of maincontext_t.pcontext_maincontext.
progname_maincontextInline implementation of maincontext_t.progname_maincontext.
self_maincontextInline implementation of maincontext_t.self_maincontext.
sizestatic_maincontextInline implementation of maincontext_t.sizestatic_maincontext.
syncrun_maincontextInline implementation of maincontext_t.syncrun_maincontext.
sysuser_maincontextInline implementation of maincontext_t.sysuser_maincontext.
tcontext_maincontextInline implementation of maincontext_t.tcontext_maincontext.
threadid_maincontextInline implementation of maincontext_t.threadid_maincontext.
type_maincontextInline implementation of maincontext_t.type_maincontext.
valuecache_maincontextInline implementation of maincontext_t.valuecache_maincontext.

Copyright

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.

Author

© 2012 Jörg Seebohn

Files

C-kern/api/maincontext.h

Header file of MainContext.

C-kern/context/maincontext.c

Implementation file MainContext impl.

C-kern/api/platform/Linux/syscontext.h

Linux specific configuration file LinuxSystemContext.

Types

struct maincontext_t

typedef struct maincontext_t maincontext_t

Export maincontext_t.

struct maincontext_startparam_t

typedef struct maincontext_startparam_t maincontext_startparam_t

Export maincontext_startparam_t.

maincontext_thread_f

typedef int (*maincontext_thread_f) (maincontext_t * maincontext)

Define maincontext_thread_f as signature of start function for main thread.

Enumerations

maincontext_e

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_STATICAn 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_DEFAULTDefault single or multi threading implementation.  All content logged to channel log_channel_USERERR is ignored.
maincontext_CONSOLEDefault 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.

Variables

g_maincontext

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.

Functions

Summary
test
unittest_context_maincontextTest initialization process succeeds and global variables are set correctly.

test

unittest_context_maincontext

int unittest_context_maincontext(void)

Test initialization process succeeds and global variables are set correctly.

maincontext_startparam_t

struct maincontext_startparam_t

Start parameters used in <initstart_maincontext>.

Summary
struct fields
context_typeSet to a value of maincontext_e.
argcThe number of process arguments.
argvAn array of program arguments.
main_threadThe main threads main function.
maincontext_startparam_INITStatic initializer.

struct fields

context_type

maincontext_e context_type

Set to a value of maincontext_e.  It determines the type of maincontext_t the process wants ro initialize.

argc

int argc

The number of process arguments.  Same value as received by

int main(int argc, const char * argv[]).

argv

const char ** argv

An array of program arguments.  The last argument argv[ærgc] should be set to 0.  Same value as received by

int main(int argc, const char * argv[]).

main_thread

maincontext_thread_f main_thread

The main threads main function.  It is started if the environment could be initialized successfully.

maincontext_startparam_INIT

#define maincontext_startparam_INIT(
   context_type,
   argc,
   argv,
   main_thread
) { context_type, argc, argv, main_thread }

Static initializer.

maincontext_t

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

Summary
struct fields
size_staticmemSize in bytes of how much of the static memory is allocated.
lifetime
initstart_maincontextCalls platform_t.init_platform, maincontext_t.init_maincontext and runs main_thread.
init_maincontextInitializes global program context.
free_maincontextFrees global context.
abort_maincontextExits the whole process in a controlled manner.
assertfail_maincontextExits the whole process in a controlled manner.
query
self_maincontextReturns maincontext_t of the current process.
pcontext_maincontextReturns processcontext_t of the current process.
tcontext_maincontextReturns threadcontext_t of the current thread.
type_maincontextReturns type <context_e> of current maincontext_t.
progname_maincontextReturns the program name of the running process.
threadid_maincontextReturns the thread id of the calling thread.
query-service
blockmap_maincontextReturns shared blockmap used by pagecache_impl_t (see <pagecache_blockmap_t >).
error_maincontextReturns error string table (see errorcontext_t).
log_maincontextReturns log service log_t (see logwriter_t).
mm_maincontextReturns interfaceable object mm_t for access of memory manager.
objectcache_maincontextReturns interfaceable object objectcache_t for access of cached singleton objects.
pagecache_maincontextReturns object interface pagecache_t to access functionality of pagecache_impl_t.
syncrun_maincontextReturns syncrun_t of current maincontext_t.
sysuser_maincontextReturns <sysusercontext_t> of current maincontext_t.
valuecache_maincontextReturns valuecache_t holding precomputed values.
static-memory
allocstatic_maincontextAllocates size bytes of memory and returns the start address.
freestatic_maincontextFrees size bytes of last allocated memory.
sizestatic_maincontextReturns size in bytes of allocated static memory.

struct fields

size_staticmem

uint16_t size_staticmem

Size in bytes of how much of the static memory is allocated.

lifetime

initstart_maincontext

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.

init_maincontext

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.

Background

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.

free_maincontext

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.

Background

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.

abort_maincontext

void abort_maincontext(int err)

Exits the whole process in a controlled manner.  Tries to free as many external resources as possible and aborts all transactions. i Before exit TRACE_NOARG_ERRLOG(PROGRAM_ABORT, err) is called.

assertfail_maincontext

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.

query

self_maincontext

maincontext_t * self_maincontext(void)

Returns maincontext_t of the current process.

pcontext_maincontext

processcontext_t * pcontext_maincontext(void)

Returns processcontext_t of the current process.

tcontext_maincontext

threadcontext_t * tcontext_maincontext(void)

Returns threadcontext_t of the current thread.

type_maincontext

maincontext_e type_maincontext(void)

Returns type <context_e> of current maincontext_t.

progname_maincontext

const char * progname_maincontext(void)

Returns the program name of the running process.  The returned value is the argv[0] delivered as parameter in <initmain_maincontext> without any leading path.

threadid_maincontext

size_t threadid_maincontext(void)

Returns the thread id of the calling thread.

query-service

blockmap_maincontext

struct pagecache_blockmap_t * blockmap_maincontext(void)

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).  This function can only be implemented as a macro.  C99 does not support references.

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

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.

sysuser_maincontext

Returns <sysusercontext_t> of current maincontext_t.  It is used in implementation of module SystemUser.

valuecache_maincontext

struct valuecache_t * valuecache_maincontext(void)

Returns valuecache_t holding precomputed values.  Every value is cached as a single copy for the whole process.

static-memory

allocstatic_maincontext

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.

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

sizestatic_maincontext

uint16_t sizestatic_maincontext(void)

Returns size in bytes of allocated static memory.

Macros

blockmap_maincontext

#define blockmap_maincontext() (pcontext_maincontext()->blockmap)

Implementation of maincontext_t.blockmap_maincontext.

error_maincontext

#define error_maincontext() (pcontext_maincontext()->error)

Implementation of maincontext_t.error_maincontext.

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

mm_maincontext

#define mm_maincontext() (tcontext_maincontext()->mm)

Inline implementation of maincontext_t.mm_maincontext.  Uses a global thread-local storage variable to implement the functionality.

objectcache_maincontext

#define objectcache_maincontext() (tcontext_maincontext()->objectcache)

Inline implementation of maincontext_t.objectcache_maincontext.  Uses a global thread-local storage variable to implement the functionality.

pagecache_maincontext

#define pagecache_maincontext() (tcontext_maincontext()->pagecache)

Implements maincontext_t.pagecache_maincontext.

pcontext_maincontext

#define pcontext_maincontext() (tcontext_maincontext()->pcontext)

Inline implementation of maincontext_t.pcontext_maincontext.

progname_maincontext

#define progname_maincontext() (self_maincontext()->progname)

Inline implementation of maincontext_t.progname_maincontext.

self_maincontext

#define self_maincontext() ((maincontext_t*)pcontext_maincontext())

Inline implementation of maincontext_t.self_maincontext.

sizestatic_maincontext

#define sizestatic_maincontext() (self_maincontext()->size_staticmem)

Inline implementation of maincontext_t.sizestatic_maincontext.

syncrun_maincontext

#define syncrun_maincontext() (tcontext_maincontext()->syncrun)

Inline implementation of maincontext_t.syncrun_maincontext.

sysuser_maincontext

#define sysuser_maincontext() (pcontext_maincontext()->sysuser)

Inline implementation of maincontext_t.sysuser_maincontext.

tcontext_maincontext

#define tcontext_maincontext() (sys_context_threadtls())

Inline implementation of maincontext_t.tcontext_maincontext.

threadid_maincontext

#define threadid_maincontext() (tcontext_maincontext()->thread_id)

Inline implementation of maincontext_t.threadid_maincontext.

type_maincontext

#define type_maincontext() (self_maincontext()->type)

Inline implementation of maincontext_t.type_maincontext.

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

Defines service context used by every software component in C-kern(el).
Implementation file of global init anf free functions.
Defines sys_context_threadtls and other thread context specific functions.
typedef struct maincontext_t maincontext_t
Export maincontext_t.
struct maincontext_t
Defines the main top level context of the whole process.
typedef struct maincontext_startparam_t maincontext_startparam_t
Export maincontext_startparam_t.
struct maincontext_startparam_t
Start parameters used in initstart_maincontext.
typedef int (*maincontext_thread_f) (maincontext_t * maincontext)
Define maincontext_thread_f as signature of start function for main thread.
extern struct maincontext_t g_maincontext
Global variable which describes the main context for the current process.
int unittest_context_maincontext(void)
Test initialization process succeeds and global variables are set correctly.
maincontext_e context_type
Set to a value of maincontext_e.
Used to switch between different implementations.
int argc
The number of process arguments.
const char ** argv
An array of program arguments.
maincontext_thread_f main_thread
The main threads main function.
#define maincontext_startparam_INIT(
   context_type,
   argc,
   argv,
   main_thread
) { context_type, argc, argv, main_thread }
Static initializer.
uint16_t size_staticmem
Size in bytes of how much of the static memory is allocated.
int initstart_maincontext(const maincontext_startparam_t *startparam)
Calls platform_t.init_platform, maincontext_t.init_maincontext and runs main_thread.
int init_platform(mainthread_f main_thread,
void *user)
Initialize system context and calls main_thread.
int init_maincontext(const maincontext_e context_type,
int argc,
const char **argv)
Initializes global program context.
int free_maincontext(void)
Frees global context.
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)
Exits the whole process in a controlled manner.
maincontext_t * self_maincontext(void)
Returns maincontext_t of the current process.
processcontext_t * pcontext_maincontext(void)
Returns processcontext_t of the current process.
struct processcontext_t
Defines computing environment / context which is shared between all threads of computation.
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.
const char * progname_maincontext(void)
Returns the program name of the running process.
size_t threadid_maincontext(void)
Returns the thread id of the calling thread.
struct pagecache_blockmap_t * blockmap_maincontext(void)
Returns shared blockmap used by pagecache_impl_t (see <pagecache_blockmap_t >).
struct pagecache_impl_t
Allocates and frees virtual memory pages and caches them.
struct errorcontext_t
Stores description of system errors in a string table.
iobj_DECLARE(log_t,
log)
Uses iobj_DECLARE to declare object supporting interface log_it.
struct logwriter_t
A logwriter writes the console channel messages to STDOUT any other channels to STDERR.
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)
Uses iobj_DECLARE to declare interfaceable object.
struct syncrun_t * syncrun_maincontext(void)
Returns syncrun_t of current maincontext_t.
struct syncrun_t
Manages a set of syncthread_t.
struct valuecache_t * valuecache_maincontext(void)
Returns valuecache_t holding precomputed values.
void * allocstatic_maincontext(uint8_t size)
Allocates size bytes of memory and returns the start address.
int freestatic_maincontext(uint8_t size)
Frees size bytes of last allocated memory.
uint16_t sizestatic_maincontext(void)
Returns size in bytes of allocated static memory.
#define blockmap_maincontext() (pcontext_maincontext()->blockmap)
Implementation of maincontext_t.blockmap_maincontext.
#define error_maincontext() (pcontext_maincontext()->error)
Implementation of maincontext_t.error_maincontext.
Returns error string table (see errorcontext_t).
#define log_maincontext() (tcontext_maincontext()->log)
Inline implementation of maincontext_t.log_maincontext.
Returns log service log_t (see logwriter_t).
#define mm_maincontext() (tcontext_maincontext()->mm)
Inline implementation of maincontext_t.mm_maincontext.
Returns interfaceable object mm_t for access of memory manager.
#define objectcache_maincontext() (tcontext_maincontext()->objectcache)
Inline implementation of maincontext_t.objectcache_maincontext.
Returns interfaceable object objectcache_t for access of cached singleton objects.
#define pagecache_maincontext() (tcontext_maincontext()->pagecache)
Implements maincontext_t.pagecache_maincontext.
Returns object interface pagecache_t to access functionality of pagecache_impl_t.
#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.
Returns sysusercontext_t of current maincontext_t.
#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 STDERR channel for log output.
Every log entry is constructed in a buffer and if the last part of the entry is added to it the log entry is written out as a whole.
Uses error channel for log output.
#define assert(
   expr
) ((expr) ? (void) 0 : assertfail_maincontext(STR(expr), __FILE__, __LINE__, __FUNCTION__))
Prints »Assertion failed« and aborts process.
struct syncthread_t
A simple function context which is executed in a cooperative way.
Offers interface for accessing current system user and authentication of users.
Close