udphole

Basic UDP wormhole proxy
git clone git://git.finwo.net/app/udphole
Log | Files | Refs | README | LICENSE

test_scheduler.c (943B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <sys/time.h>
      4 
      5 #include "common/scheduler.h"
      6 
      7 typedef struct {
      8   int id;
      9   int count;
     10   int64_t last_run;
     11 } task_data_t;
     12 
     13 int countdown_pt(int64_t timestamp, struct pt_task *task) {
     14   task_data_t *data = task->udata;
     15 
     16   if (timestamp - data->last_run < 100) {
     17     return SCHED_RUNNING;
     18   }
     19   data->last_run = timestamp;
     20 
     21   printf("Task %d: %d\n", data->id, data->count);
     22   data->count--;
     23 
     24   if (data->count < 0) {
     25     printf("Task %d: DONE\n", data->id);
     26     return SCHED_DONE;
     27   }
     28 
     29   return SCHED_RUNNING;
     30 }
     31 
     32 int main(void) {
     33   task_data_t tasks[3] = {
     34     { .id = 1, .count = 3, .last_run = 0 },
     35     { .id = 2, .count = 3, .last_run = 0 },
     36     { .id = 3, .count = 3, .last_run = 0 },
     37   };
     38 
     39   for (int i = 0; i < 3; i++) {
     40     sched_create(countdown_pt, &tasks[i]);
     41   }
     42 
     43   printf("Starting countdown test with 3 parallel tasks...\n");
     44   sched_main();
     45   printf("Test complete!\n");
     46   return 0;
     47 }