SyncRun impl

Implements SyncRun.

Summary
SyncRun implImplements SyncRun.
CopyrightThis program is free software.
Files
C-kern/api/task/syncrun.hHeader file SyncRun.
C-kern/task/syncrun.cImplementation file SyncRun impl.
Types
struct initqueue_entry_tExports initqueue_entry_t into global namespace.
runqueue_entry_tMake runqueue_entry_t an alias for syncthread_t.
struct run2queue_entry_tExports run2queue_entry_t into global namespace.
struct waitqueue_entry_tExports waitqueue_entry_t into global namespace.
struct wait2queue_entry_tExports wait2queue_entry_t into global namespace.
Enumerations
syncrun_qid_eIndex into syncrun_t.queues.
initqueue_entry_tHelds all data needed to construct a new syncthread_t.
lifetime
init_initqueueentryInitialize initentry with mainfct and initarg.
init2_initqueueentryInitialize initentry with mainfct and initarg and initargsize.
query
sizeentry_initqueueentryThe size in bytes of initqueue_entry_t depending on initargsize.
runqueue_entry_tContains only a single syncthread_t.
lifetime
init_runqueueentryInitializes runentry with thread.
initmove_runqueueentryInitializes dest with src.
run2queue_entry_tContains a single syncthread_t and its corresponding exitevent.
lifetime
init_run2queueentryInitializes runentry with thread and exitevent.
initmove_run2queueentryInitializes dest with src.
waitqueue_entry_tContains waiting thread (syncwait_t).
lifetime
init_waitqueueentryInitializes waitentry with thread and uses srun->waitinfo to read the event and the continuelabel.
initmove_waitqueueentryMoves content of initialized src to dest.
query
cast_waitqueueentryCasts pointer to syncwait_t into pointer to waitqueue_entry_t.
wait2queue_entry_tContains waiting thread (syncwait_t) and its exitevent.
lifetime
init_wait2queueentryInitializes waitentry with thread and exitevent and uses srun->waitinfo to read the event and the continuelabel.
initmove_wait2queueentryMoves content of initialized src to dest.
query
cast_wait2queueentryCasts pointer to syncwait_t into pointer to wait2queue_entry_t.
syncrun_t
static variables
s_syncrun_errtimerAllows to introduce artificial errors during test.
lifetime
query
internal
thread-lifetime
synchronization
run
preparewakeup_syncrunSets all values in srun->wakeup (syncrun_t.wakeup).
preparerun_syncrunSets all values in srun->running (syncrun_t.running).
execwaiting_syncrunCalls callwakeup_syncthread for a waiting thread given in waiting.
execabort_syncrunCalls callabort_syncthread for every thread stored in queue qith id qid.
clearevents_syncrunSet all <syncevent_t.waiting> pointer to 0 which reference threads in the wait queues.
test
test_runTest runall_syncrun with all possible state transitions.
test_run2Test correct variable values of a single thread in every posible state.

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

© 2013 Jörg Seebohn

Files

C-kern/api/task/syncrun.h

Header file SyncRun.

C-kern/task/syncrun.c

Implementation file SyncRun impl.

Types

struct initqueue_entry_t

typedef struct initqueue_entry_t initqueue_entry_t

Exports initqueue_entry_t into global namespace.

runqueue_entry_t

typedef struct runqueue_entry_t runqueue_entry_t

Make runqueue_entry_t an alias for syncthread_t.

struct run2queue_entry_t

typedef struct run2queue_entry_t run2queue_entry_t

Exports run2queue_entry_t into global namespace.

struct waitqueue_entry_t

typedef struct waitqueue_entry_t waitqueue_entry_t

Exports waitqueue_entry_t into global namespace.

struct wait2queue_entry_t

typedef struct wait2queue_entry_t wait2queue_entry_t

Exports wait2queue_entry_t into global namespace.

Enumerations

syncrun_qid_e

Index into syncrun_t.queues. syncrun_qid_INIT - Index of queue holding initqueue_entry_t which describe an unitialized thread with its init argument. syncrun_qid_RUN - Index of queue holding runqueue_entry_t which describe already initialized threads. syncrun_qid_RUN2 - Index of queue holding run2queue_entry_t which describe already initialized threads.  These threads generate an exit event. syncrun_qid_WAIT - Index of queue holding waitqueue_entry_t which describes a thread waiting for an event/condition. syncrun_qid_WAIT2 - Index of queue holding wait2queue_entry_t which describes a thread waiting for an event/condition.  These threads generate an exit event. syncrun_qid_WLIST - Index of queue holding entries of syncwlist_t (see wlistentry_t).  Every entry references a waiting thread in <syncrun_qid_WAIT> or <syncrun_qid_WAIT2>. syncrun_qid_WAKEUP - Index of queue holding entries of syncevent_t which references a waiting thread in <syncrun_qid_WAIT> or <syncrun_qid_WAIT2>.

initqueue_entry_t

struct initqueue_entry_t

Helds all data needed to construct a new syncthread_t.  The structure contains a syncthread_t its possible unused exitevent (syncevent_t) and the initargsize and initarg[] data of an possible allocated init argument.

Summary
lifetime
init_initqueueentryInitialize initentry with mainfct and initarg.
init2_initqueueentryInitialize initentry with mainfct and initarg and initargsize.
query
sizeentry_initqueueentryThe size in bytes of initqueue_entry_t depending on initargsize.

lifetime

init_initqueueentry

static inline void init_initqueueentry(initqueue_entry_t *initentry,
syncrun_f mainfct,
void *initarg)

Initialize initentry with mainfct and initarg.  The size initargsize of the allocated init argument is set to 0.

init2_initqueueentry

static inline void init2_initqueueentry(initqueue_entry_t *initentry,
syncrun_f mainfct,
uint8_t initargsize)

Initialize initentry with mainfct and initarg and initargsize.  The caller must have reserved sizeentry_initqueueentry bytes for initentry.

query

sizeentry_initqueueentry

static inline uint16_t sizeentry_initqueueentry(uint8_t initargsize)

The size in bytes of initqueue_entry_t depending on initargsize.  If initargsize is set to 0 the returned value equals sizeof(initqueue_entry_t).

runqueue_entry_t

struct runqueue_entry_t

Contains only a single syncthread_t.  Entry will be stored in queue with id <syncrun_qid_RUN>.

Summary
lifetime
init_runqueueentryInitializes runentry with thread.
initmove_runqueueentryInitializes dest with src.

lifetime

init_runqueueentry

static inline void init_runqueueentry(/*out*/runqueue_entry_t *runentry,
const syncthread_t *thread)

Initializes runentry with thread.  Content of thread is copied as plain bytes.

initmove_runqueueentry

static inline void initmove_runqueueentry(/*out*/runqueue_entry_t *dest,
runqueue_entry_t *src)

Initializes dest with src. src is not changed but considered uninitialized after the operation.

run2queue_entry_t

struct run2queue_entry_t

Contains a single syncthread_t and its corresponding exitevent.  Entry will be stored in queue with id <syncrun_qid_RUN2>.

Summary
lifetime
init_run2queueentryInitializes runentry with thread and exitevent.
initmove_run2queueentryInitializes dest with src.

lifetime

init_run2queueentry

static inline void init_run2queueentry(/*out*/run2queue_entry_t *runentry,
syncthread_t *thread,
syncevent_t *exitevent)

Initializes runentry with thread and exitevent.  After the operation exitevent has been moved into runentry and is considered uninitialized.

initmove_run2queueentry

static inline void initmove_run2queueentry(/*out*/run2queue_entry_t *dest,
run2queue_entry_t *src)

Initializes dest with src.  After the operation src is considered uninitialized.

waitqueue_entry_t

struct waitqueue_entry_t

Contains waiting thread (syncwait_t).  Entry will be stored in queue with id <syncrun_qid_WAIT>.

Summary
lifetime
init_waitqueueentryInitializes waitentry with thread and uses srun->waitinfo to read the event and the continuelabel.
initmove_waitqueueentryMoves content of initialized src to dest.
query
cast_waitqueueentryCasts pointer to syncwait_t into pointer to waitqueue_entry_t.

lifetime

init_waitqueueentry

static inline void init_waitqueueentry(/*out*/waitqueue_entry_t *waitentry,
syncrun_t *srun,
syncthread_t *thread)

Initializes waitentry with thread and uses srun->waitinfo to read the event and the continuelabel.  The continuelabel stored in syncrun_t.waitinfo is the place, where execution will continue after wakeup.

initmove_waitqueueentry

static inline void initmove_waitqueueentry(/*out*/waitqueue_entry_t *dest,
waitqueue_entry_t *src)

Moves content of initialized src to dest.  After return dest contains previous content of dest (back pointer of referenced objects are adapted).  Src is left untouched but is considered in an invalid, uninitialized state.  You have to initialize it before src can be used again.

query

cast_waitqueueentry

static inline waitqueue_entry_t * cast_waitqueueentry(syncwait_t *waitentry)

Casts pointer to syncwait_t into pointer to waitqueue_entry_t.

wait2queue_entry_t

struct wait2queue_entry_t

Contains waiting thread (syncwait_t) and its exitevent.  Entry will be stored in queue with id <syncrun_qid_WAIT2>.

Summary
lifetime
init_wait2queueentryInitializes waitentry with thread and exitevent and uses srun->waitinfo to read the event and the continuelabel.
initmove_wait2queueentryMoves content of initialized src to dest.
query
cast_wait2queueentryCasts pointer to syncwait_t into pointer to wait2queue_entry_t.

lifetime

init_wait2queueentry

static inline void init_wait2queueentry(/*out*/wait2queue_entry_t *waitentry,
syncrun_t *srun,
syncthread_t *thread,
syncevent_t *exitevent)

Initializes waitentry with thread and exitevent and uses srun->waitinfo to read the event and the continuelabel.  The continuelabel stored in syncrun_t.waitinfo is the place, where execution will continue after wakeup.

initmove_wait2queueentry

static inline void initmove_wait2queueentry(/*out*/wait2queue_entry_t *dest,
wait2queue_entry_t *src)

Moves content of initialized src to dest.  After return dest contains previous content of dest (back pointer of referenced objects are adapted).  Src is left untouched but is considered in an invalid, uninitialized state.  You have to initialize it before src can be used again.

query

cast_wait2queueentry

static inline wait2queue_entry_t * cast_wait2queueentry(syncwait_t *waitentry)

Casts pointer to syncwait_t into pointer to wait2queue_entry_t.

syncrun_t

Summary
static variables
s_syncrun_errtimerAllows to introduce artificial errors during test.
lifetime
query
internal
thread-lifetime
synchronization
run
preparewakeup_syncrunSets all values in srun->wakeup (syncrun_t.wakeup).
preparerun_syncrunSets all values in srun->running (syncrun_t.running).
execwaiting_syncrunCalls callwakeup_syncthread for a waiting thread given in waiting.
execabort_syncrunCalls callabort_syncthread for every thread stored in queue qith id qid.
clearevents_syncrunSet all <syncevent_t.waiting> pointer to 0 which reference threads in the wait queues.
test
test_runTest runall_syncrun with all possible state transitions.
test_run2Test correct variable values of a single thread in every posible state.

static variables

s_syncrun_errtimer

static test_errortimer_t s_syncrun_errtimer

Allows to introduce artificial errors during test.  Only defined if KONFIG_UNITTEST is defined.

lifetime

query

internal

thread-lifetime

synchronization

run

preparewakeup_syncrun

static inline void preparewakeup_syncrun(syncrun_t *srun,
void *continuelabel,
int retcode)

Sets all values in srun->wakeup (syncrun_t.wakeup).  Parameter continuelabel and retcode are copied into the corresponding data members of wakeup.  This function is called before a waiting threads is woken up.

preparerun_syncrun

static inline int preparerun_syncrun(syncrun_t *srun,
syncthread_t *running_thread)

Sets all values in srun->running (syncrun_t.running).  The exitevent of the last started thread is set to 0 meaning that there is no previously started thread.  The running thread thread is set to value of parameter running_thread.  The state of the thread is initialized to syncrun_state_CONTINUE.  Therefore threads run until they exit or abort themselves.

execwaiting_syncrun

static int execwaiting_syncrun(syncrun_t *srun,
syncwait_t *waiting,
int retcode)

Calls callwakeup_syncthread for a waiting thread given in waiting.  The argument retcode is set into srun->wakeup and is the return code of the exited syncthread_t.  Function waitforexit_syncrun returns this retcode as its own return value.  For other types of events retcode is ignored (see waitforevent_syncrun and waitforlist_syncrun).  If the called syncthread exits or aborts and another thread waits for its exit the function calls itself.  If the waiting thread waits for another event after return it will be kept in the wait queue.  If its state is syncrun_state_CONTINUE after return it will be moved into queue <syncrun_qid_RUN> or queue <syncrun_qid_RUN2>.

execabort_syncrun

static int execabort_syncrun(syncrun_t *srun,
enum syncrun_qid_e qid,
uint16_t entrysize)

Calls callabort_syncthread for every thread stored in queue qith id qid.  The parameter entrysize gives the size of an entry in the queue.  The initqueue is therefore not supported.  All queue entries are removed.  This function assumes that clearevents_syncrun has been called before.  If you abort threads stored in queue with qid <syncrun_qid_RUN2> or <syncrun_qid_WAIT2> other threads waiting for them to exit are not woken up.  Therefore execabort_syncrun is always called in the context of <abortall_syncrun>.

clearevents_syncrun

static int clearevents_syncrun(syncrun_t *srun)

Set all <syncevent_t.waiting> pointer to 0 which reference threads in the wait queues.  This ensures that events do not referencewaiting threads after the waiting queues are freed.

test

test_run

static int test_run(void)

Test runall_syncrun with all possible state transitions.  All state transitions of a thread in every queue are tested.

test_run2

static int test_run2(void)

Test correct variable values of a single thread in every posible state.

This object helps to manage a set of syncthread_t in a set of different memory queues.
Implements SyncRun.
typedef struct initqueue_entry_t initqueue_entry_t
Exports initqueue_entry_t into global namespace.
struct initqueue_entry_t
Helds all data needed to construct a new syncthread_t.
typedef struct runqueue_entry_t runqueue_entry_t
Make runqueue_entry_t an alias for syncthread_t.
struct syncthread_t
A simple function context which is executed in a cooperative way.
typedef struct run2queue_entry_t run2queue_entry_t
Exports run2queue_entry_t into global namespace.
struct run2queue_entry_t
Contains a single syncthread_t and its corresponding exitevent.
typedef struct waitqueue_entry_t waitqueue_entry_t
Exports waitqueue_entry_t into global namespace.
struct waitqueue_entry_t
Contains waiting thread (syncwait_t).
typedef struct wait2queue_entry_t wait2queue_entry_t
Exports wait2queue_entry_t into global namespace.
struct wait2queue_entry_t
Contains waiting thread (syncwait_t) and its exitevent.
syncqueue_t queues[7]
Queues wich are used to store syncthread_t.
static inline void init_initqueueentry(initqueue_entry_t *initentry,
syncrun_f mainfct,
void *initarg)
Initialize initentry with mainfct and initarg.
static inline void init2_initqueueentry(initqueue_entry_t *initentry,
syncrun_f mainfct,
uint8_t initargsize)
Initialize initentry with mainfct and initarg and initargsize.
static inline uint16_t sizeentry_initqueueentry(uint8_t initargsize)
The size in bytes of initqueue_entry_t depending on initargsize.
static inline void init_runqueueentry(/*out*/runqueue_entry_t *runentry,
const syncthread_t *thread)
Initializes runentry with thread.
static inline void initmove_runqueueentry(/*out*/runqueue_entry_t *dest,
runqueue_entry_t *src)
Initializes dest with src.
static inline void init_run2queueentry(/*out*/run2queue_entry_t *runentry,
syncthread_t *thread,
syncevent_t *exitevent)
Initializes runentry with thread and exitevent.
static inline void initmove_run2queueentry(/*out*/run2queue_entry_t *dest,
run2queue_entry_t *src)
Initializes dest with src.
struct syncwait_t
Describes a syncthread_t waiting for a syncevent_t.
static inline void init_waitqueueentry(/*out*/waitqueue_entry_t *waitentry,
syncrun_t *srun,
syncthread_t *thread)
Initializes waitentry with thread and uses srun->waitinfo to read the event and the continuelabel.
static inline void initmove_waitqueueentry(/*out*/waitqueue_entry_t *dest,
waitqueue_entry_t *src)
Moves content of initialized src to dest.
static inline waitqueue_entry_t * cast_waitqueueentry(syncwait_t *waitentry)
Casts pointer to syncwait_t into pointer to waitqueue_entry_t.
static inline void init_wait2queueentry(/*out*/wait2queue_entry_t *waitentry,
syncrun_t *srun,
syncthread_t *thread,
syncevent_t *exitevent)
Initializes waitentry with thread and exitevent and uses srun->waitinfo to read the event and the continuelabel.
static inline void initmove_wait2queueentry(/*out*/wait2queue_entry_t *dest,
wait2queue_entry_t *src)
Moves content of initialized src to dest.
static inline wait2queue_entry_t * cast_wait2queueentry(syncwait_t *waitentry)
Casts pointer to syncwait_t into pointer to wait2queue_entry_t.
static test_errortimer_t s_syncrun_errtimer
Allows to introduce artificial errors during test.
static inline void preparewakeup_syncrun(syncrun_t *srun,
void *continuelabel,
int retcode)
Sets all values in srun->wakeup (syncrun_t.wakeup).
struct { void * continuelabel ; int retcode ; } wakeup
Stores information used during wakeup of a thread which waited for an event.
static inline int preparerun_syncrun(syncrun_t *srun,
syncthread_t *running_thread)
Sets all values in srun->running (syncrun_t.running).
struct { struct syncevent_t * laststarted ; struct syncthread_t * thread ; uint8_t state ; } running
Stores information of the current thread and its running state.
static int execwaiting_syncrun(syncrun_t *srun,
syncwait_t *waiting,
int retcode)
Calls callwakeup_syncthread for a waiting thread given in waiting.
#define callwakeup_syncthread(
   sthread
) ( __extension__ ({ typeof(sthread) _sthread = (sthread) ; _sthread->mainfct(_sthread, syncthread_signal_WAKEUP) ; }))
Implements syncthread_t.callwakeup_syncthread.
static int execabort_syncrun(syncrun_t *srun,
enum syncrun_qid_e qid,
uint16_t entrysize)
Calls callabort_syncthread for every thread stored in queue qith id qid.
#define callabort_syncthread(
   sthread
) ( __extension__ ({ typeof(sthread) _sthread = (sthread) ; _sthread->mainfct(_sthread, syncthread_signal_ABORT) ; }))
Implements syncthread_t.callabort_syncthread.
static int clearevents_syncrun(syncrun_t *srun)
Set all syncevent_t.waiting pointer to 0 which reference threads in the wait queues.
static int test_run(void)
Test runall_syncrun with all possible state transitions.
static int test_run2(void)
Test correct variable values of a single thread in every posible state.
struct syncwlist_t
Implements a double linked list of syncevent_t and stores them in a syncqueue_t.
struct wlistentry_t
Type of entry stored in syncwlist_t.
struct syncevent_t
Event type stores a pointer to a single waiting syncwait_t.
struct { struct syncwlist_t * wlist ; struct syncevent_t * event ; void * continuelabel ; } waitinfo
Stores waiting information of the running thread if its state is changed to syncrun_state_WAIT.
#define KONFIG_UNITTEST
Define this in your Makefile to include additional code for testing single components.
The thread needs to run at least one mroe time.
#define waitforexit_syncrun(
   srun
) ( __extension__ ({ __label__ waitlabel ; setstatewait_syncrun((srun), srun->running.laststarted, __extension__ && waitlabel) ; return 0 ; waitlabel: retcode_syncrun(srun) ; }))
Implements syncrun_t.waitforexit_syncrun.
#define waitforevent_syncrun(
   srun,
   syncevent
) ( __extension__ ({ __label__ waitlabel ; setstatewait_syncrun((srun), (syncevent), __extension__ && waitlabel) ; return 0 ; waitlabel: (void)0 ; }))
Implements syncrun_t.waitforevent_syncrun.
#define waitforlist_syncrun(
   srun,
   syncwlist
) ( __extension__ ({ __label__ waitlabel ; setstatewaitlist_syncrun((srun), (syncwlist), __extension__ && waitlabel) ; return 0 ; waitlabel: (void)0 ; }))
Implements syncrun_t.waitforlist_syncrun.
Close