11
Programmable task
- typedef struct _task {
- time_t attime;
- long period;
- int count;
- char *name;
- int status;
- void (*job)( void * );
- void *arg;
- } *task;
- #define TASKIDLE 0
- #define TASKQUEUED 1
- #define TASKRUNNING 2
A task can be in three different states. When a task is added by a scheduler to its internal queue, it's in the TASKQUEUED state. It's neither running nor idle. When a task is run by a scheduler, its state changes to TASKRUNNING while the corresponding function is being executed and to TASKIDLE when the function is done. When a task is explicitly removed by a scheduler, its state is changed to TASKIDLE.
- extern task task_new( char *name, time_t attime, int period, int count, void (*job)( void * ), void *arg );
- extern void task_free( task this );
- extern task task_exec( task this );
- extern char * task_getname( task this );
- extern task task_setname( task this, char *name );
- extern time_t task_getattime( task this );
- extern task task_setattime( task this, time_t attime );
- extern int task_getperiod( task this );
- extern task task_setperiod( task this, int period );
- extern int task_getcount( task this );
- extern task task_setcount( task this, int count );
- extern int task_getstatus( task this );
- extern task task_setstatus( task this, int status );
- extern int task_isidle ( task this );
- extern int task_isqueued ( task this );
- extern int task_isrunning ( task this );
- #include "task.h"
- #include <stdlib.h>
- task task_alloc( void ) {
- return (task)calloc( 1, sizeof ( struct _task ));
- }
- task task_init( task this, char *name, time_t attime, int period, int count, void (*job)( void * ), void *arg ) {
- this->attime = attime;
- this->period = period;
- this->count = count;
- this->name = name;
- this->job = job;
- this->arg = arg;
- return this;
- }
- task task_new( char *name, time_t attime, int period, int count, void (*job)( void * ), void *arg ) {
- return task_init( task_alloc(), name, attime, period, count, job, arg );
- }
- void task_free( task this ) {
- free( this );
- }
- task task_exec( task this ) {
- this->job( this->arg );
- return this;
- }
- char * task_getname( task this ) {
- return this->name;
- }
- task task_setname( task this, char *name ) {
- this->name = name;
- return this;
- }
- time_t task_getattime( task this ) {
- return this->attime;
- }
- task task_setattime( task this, time_t attime ) {
- this->attime = attime;
- return this;
- }
- int task_getperiod( task this ) {
- return this->period;
- }
- task task_setperiod( task this, int period ) {
- this->period = period;
- return this;
- }
- int task_getcount( task this ) {
- return this->count;
- }
- task task_setcount( task this, int count ) {
- this->count = count;
- return this;
- }
- int task_getstatus( task this ) {
- return this->status;
- }
- task task_setstatus( task this, int status ) {
- this->status = status;
- return this;
- }
- int task_isidle ( task this ) {
- return this->status == TASKIDLE;
- }
- int task_isqueued ( task this ) {
- return this->status == TASKQUEUED;
- }
- int task_isrunning ( task this ) {
- return this->status == TASKRUNNING;
- }
Comments