10

Task scheduler

  1. typedef list scheduler;
  1. extern scheduler scheduler_new( void );
  2. extern void scheduler_free( scheduler this );
  3. extern task scheduler_addtask( scheduler this, task atask );
  4. extern task scheduler_rmtask( scheduler this, task atask );
  5. extern task scheduler_findtask( scheduler this, char *name, void (*job)( void * ), void *arg );
  6. extern time_t scheduler_run( scheduler this );
  7. extern time_t scheduler_nexttime( scheduler this );
  1. scheduler scheduler_alloc( ) {
  2.     return list_alloc( );
  3. }
  4.  
  5. scheduler scheduler_init( scheduler this ) {
  6.     return this;
  7. }
  8.  
  9. scheduler scheduler_new( void ) {
  10.     return scheduler_init( scheduler_alloc() );
  11. }
  12.  
  13. void scheduler_free( scheduler this ) {
  14.     list_free( this );
  15. }
  1. task scheduler_addtask( scheduler this, task atask ) {
  2.     int i, len = list_length( this );
  3.     task tk;
  4.  
  5.     ONDEBUG2( printf( "[SchedulerAddTask]\n" ));
  6.     for ( i = 0; i < len; i++ ) {
  7.         tk = (task)list_get( this, i );
  8.         if ( tk->attime > atask->attime )
  9.             break;
  10.     }
  11.     ONDEBUG2( printf( "[%s<%i>]\n", atask->name, i ));
  12.  
  13.     task_setstatus( atask, TASKQUEUED );
  14.  
  15.     return (task)list_insert( this, i, atask );
  16. }
  17.  
  18. task scheduler_rmtask( scheduler this, task atask ) {
  19.     int i, len = list_length( this );
  20.     task tk;
  21.  
  22.     ONDEBUG2( printf( "[SchedulerRemoveTask]\n" ));
  23.     for ( i = 0; i < len; i++ ) {
  24.         tk = (task)list_get( this, i );
  25.         if ( tk == atask )
  26.             break;
  27.     }
  28.     ONDEBUG2( printf( "[%s<%i>]\n", atask->name, i ));
  29.  
  30.     task_setstatus( atask, TASKIDLE );
  31.  
  32.     return (task)list_delete( this, i );
  33. }
  34.  
  35. task scheduler_findtask( scheduler this, char *name, void (*job)( void * ), void *arg ) {
  36.     int i, len = list_length( this );
  37.     task tk;
  38.  
  39.     for ( i = 0; i < len; i++ ) {
  40.         tk = (task)list_get( this, i );
  41.         if ( name && strcmp( tk->name, name ) != 0 )
  42.             continue;
  43.         if ( job && tk->job != job )
  44.             continue;
  45.         if ( arg && tk->arg != arg )
  46.             continue;
  47.         return tk;
  48.     }
  49.     return 0;
  50. }
  51.    
  52. time_t scheduler_run( scheduler this ) {
  53.     time_t now = time( 0 );
  54.     task tk;
  55.  
  56.     ONDEBUG2( printf( "[SchedulerRun]\n" ));
  57.     while ( (tk = (task)list_get( this, 0 )) ) {
  58.         if ( tk->attime > now )
  59.             break;
  60.         ONDEBUG2( printf( "[%s]\n", tk->name ));
  61.  
  62.         /* remove task from queue before it is run */
  63.         list_delete( this, 0 );
  64.  
  65.         task_setstatus( tk, TASKRUNNING );
  66.         task_exec( tk );
  67.         task_setstatus( tk, TASKIDLE );
  68.  
  69.         /* again? */
  70.         if ( tk->period && (!tk->count || --tk->count > 0) ) {
  71.             tk->attime += tk->period;   /* next time */
  72.             scheduler_addtask( this, tk );
  73.         }
  74.     }
  75.     return scheduler_nexttime( this );
  76. }
  77.  
  78. time_t scheduler_nexttime( scheduler this ) {
  79.     task tk = list_get( this, 0 );
  80.  
  81.     return tk ? tk->attime : 0;
  82. }

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