[pisa-src] r1105 - trunk/libpisa

  • From: Thomas Jansen <mithi@xxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Tue, 13 Oct 2009 13:42:39 +0200

Author: tjansen
Date: Tue Oct 13 13:42:39 2009
New Revision: 1105

Log:
Allow tasks to be added from within task handlers.

Modified:
   trunk/libpisa/scheduler.c

Modified: trunk/libpisa/scheduler.c
==============================================================================
--- trunk/libpisa/scheduler.c   Tue Oct 13 13:24:05 2009        (r1104)
+++ trunk/libpisa/scheduler.c   Tue Oct 13 13:42:39 2009        (r1105)
@@ -151,7 +151,7 @@
                cur->next = cur->next->next;
        }
 
-       free(task);
+       task->next = NULL;
 }
 
 /**
@@ -162,19 +162,39 @@
 void pisa_sched_run(void)
 {
        struct timeval now;
+       struct pisa_sched_task *run = NULL, *run_tail = NULL;
 
+       /* Move actions that will be executed from the task list to a run
+        * list. The runlist is executed after the mutex has been released to
+        * allow addition of new tasks in a task handler (useful for
+        * periodically recurring tasks). */
        pthread_mutex_lock(&mutex_list);
 
        gettimeofday(&now, NULL);
        while (head && pisa_time_before(&head->due, &now)) {
                head->func(head->data);
                pisa_sched_remove_internal(head);
+               if (run_tail == NULL)
+                       run = run_tail = head;
+               else {
+                       run_tail->next = head;
+                       run_tail = head;
+               }
+       }
+
+       pthread_mutex_unlock(&mutex_list);
+
+       /* Execute the run list */
+       while (run) {
+               struct pisa_sched_task *tmp;
+               run->func(run->data);
+               tmp = run;
+               run = run->next;
+               free(tmp);
        }
 
        *flag = 0;
        write(pipefd[1], "0", 1);
-
-       pthread_mutex_unlock(&mutex_list);
 }
 
 /**
@@ -224,6 +244,8 @@
        pthread_mutex_unlock(&mutex_list);
 
        pisa_sched_remove_internal(task);
+       free(task);
+
        write(pipefd[1], "0", 1);
 
        pthread_mutex_unlock(&mutex_list);

Other related posts:

  • » [pisa-src] r1105 - trunk/libpisa - Thomas Jansen