11

Programmable task

  1. typedef struct _task {
  2.     time_t attime;
  3.     long period;
  4.     int count;
  5.     char *name;
  6.     int status;
  7.     void (*job)( void * );
  8.     void *arg;
  9. } *task;
  1. #define TASKIDLE        0
  2. #define TASKQUEUED      1
  3. #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.

  1. extern task task_new( char *name, time_t attime, int period, int count, void (*job)( void * ), void *arg );
  2. extern void task_free( task this );
  3. extern task task_exec( task this );
  4. extern char * task_getname( task this );
  5. extern task task_setname( task this, char *name );
  6. extern time_t task_getattime( task this );
  7. extern task task_setattime( task this, time_t attime );
  8. extern int task_getperiod( task this );
  9. extern task task_setperiod( task this, int period );
  10. extern int task_getcount( task this );
  11. extern task task_setcount( task this, int count );
  12. extern int task_getstatus( task this );
  13. extern task task_setstatus( task this, int status );
  14. extern int task_isidle ( task this );
  15. extern int task_isqueued ( task this );
  16. extern int task_isrunning ( task this );
  1. #include "task.h"
  2.  
  3. #include <stdlib.h>
  4.  
  5. task task_alloc( void ) {
  6.     return (task)calloc( 1, sizeof ( struct _task ));
  7. }
  8.  
  9. task task_init( task this, char *name, time_t attime, int period, int count, void (*job)( void * ), void *arg ) {
  10.     this->attime = attime;
  11.     this->period = period;
  12.     this->count = count;
  13.     this->name = name;
  14.     this->job = job;
  15.     this->arg = arg;
  16.  
  17.     return this;
  18. }
  19.  
  20. task task_new( char *name, time_t attime, int period, int count, void (*job)( void * ), void *arg ) {
  21.     return task_init( task_alloc(), name, attime, period, count, job, arg );
  22. }
  23.  
  24. void task_free( task this ) {
  25.     free( this );
  26. }
  27.  
  28. task task_exec( task this ) {
  29.     this->job( this->arg );
  30.     return this;
  31. }
  32.  
  33. char * task_getname( task this ) {
  34.     return this->name;
  35. }
  36.  
  37. task task_setname( task this, char *name ) {
  38.     this->name = name;
  39.     return this;
  40. }
  41.  
  42. time_t task_getattime( task this ) {
  43.     return this->attime;
  44. }
  45.  
  46. task task_setattime( task this, time_t attime ) {
  47.     this->attime = attime;
  48.     return this;
  49. }
  50.  
  51. int task_getperiod( task this ) {
  52.     return this->period;
  53. }
  54.  
  55. task task_setperiod( task this, int period ) {
  56.     this->period = period;
  57.     return this;
  58. }
  59.  
  60. int task_getcount( task this ) {
  61.     return this->count;
  62. }
  63.  
  64. task task_setcount( task this, int count ) {
  65.     this->count = count;
  66.     return this;
  67. }
  68.  
  69. int task_getstatus( task this ) {
  70.     return this->status;
  71. }
  72.  
  73. task task_setstatus( task this, int status ) {
  74.     this->status = status;
  75.     return this;
  76. }
  77.  
  78. int task_isidle ( task this ) {
  79.     return this->status == TASKIDLE;
  80. }
  81.  
  82. int task_isqueued ( task this ) {
  83.     return this->status == TASKQUEUED;
  84. }
  85.  
  86. int task_isrunning ( task this ) {
  87.     return this->status == TASKRUNNING;
  88. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].