16
Tâche programmable
- 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
Une tâche peut être dans trois états différents. Quand une tâche est ajoutée par un séquenceur à sa liste interne, elle est dans l'état TASKQUEUED. Elle n'est pas en cours d'exécution ou inactive. Quand une tâche est lancée par un séquenceur, son état passe à TASKRUNNING durant l'exécution de la fonction correspondante puis à TASKIDLE dès que la fonction en a terminé. Quand une tâche est explicitement retirée par un séquenceur, son état est changé à 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;
- }
Commentaires