warn
[oweals/gnunet.git] / src / util / scheduler.c
1 /*
2       This file is part of GNUnet
3       (C) 2009 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 2, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file util/scheduler.c
23  * @brief schedule computations using continuation passing style
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_signal_lib.h"
31 #include "gnunet_time_lib.h"
32 #include "disk.h"
33 #ifdef LINUX
34 #include "execinfo.h"
35
36
37 /**
38  * Use lsof to generate file descriptor reports on select error?
39  * (turn off for stable releases).
40  */
41 #define USE_LSOF GNUNET_YES
42
43 /**
44  * Obtain trace information for all scheduler calls that schedule tasks.
45  */
46 #define EXECINFO GNUNET_NO
47
48 /**
49  * Depth of the traces collected via EXECINFO.
50  */
51 #define MAX_TRACE_DEPTH 50
52 #endif
53
54 #define DEBUG_TASKS GNUNET_NO
55
56 /**
57  * Should we figure out which tasks are delayed for a while
58  * before they are run? (Consider using in combination with EXECINFO).
59  */
60 #define PROFILE_DELAYS GNUNET_NO
61
62 /**
63  * Task that were in the queue for longer than this are reported if
64  * PROFILE_DELAYS is active.
65  */
66 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
67
68 /**
69  * Linked list of pending tasks.
70  */
71 struct Task
72 {
73   /**
74    * This is a linked list.
75    */
76   struct Task *next;
77
78   /**
79    * Function to run when ready.
80    */
81   GNUNET_SCHEDULER_Task callback;
82
83   /**
84    * Closure for the callback.
85    */
86   void *callback_cls;
87
88   /**
89    * Set of file descriptors this task is waiting
90    * for for reading.  Once ready, this is updated
91    * to reflect the set of file descriptors ready
92    * for operation.
93    */
94   struct GNUNET_NETWORK_FDSet *read_set;
95
96   /**
97    * Set of file descriptors this task is waiting for for writing.
98    * Once ready, this is updated to reflect the set of file
99    * descriptors ready for operation.
100    */
101   struct GNUNET_NETWORK_FDSet *write_set;
102
103   /**
104    * Unique task identifier.
105    */
106   GNUNET_SCHEDULER_TaskIdentifier id;
107
108   /**
109    * Identifier of a prerequisite task.
110    */
111   GNUNET_SCHEDULER_TaskIdentifier prereq_id;
112
113   /**
114    * Absolute timeout value for the task, or
115    * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
116    */
117   struct GNUNET_TIME_Absolute timeout;
118
119 #if PROFILE_DELAYS
120   /**
121    * When was the task scheduled?
122    */
123   struct GNUNET_TIME_Absolute start_time;
124 #endif
125
126   /**
127    * Why is the task ready?  Set after task is added to ready queue.
128    * Initially set to zero.  All reasons that have already been
129    * satisfied (i.e.  read or write ready) will be set over time.
130    */
131   enum GNUNET_SCHEDULER_Reason reason;
132
133   /**
134    * Task priority.
135    */
136   enum GNUNET_SCHEDULER_Priority priority;
137
138   /**
139    * Set if we only wait for reading from a single FD, otherwise -1.
140    */
141   int read_fd;
142
143   /**
144    * Set if we only wait for writing to a single FD, otherwise -1.
145    */
146   int write_fd;
147
148 #if EXECINFO
149   /**
150    * Array of strings which make up a backtrace from the point when this
151    * task was scheduled (essentially, who scheduled the task?)
152    */
153   char **backtrace_strings;
154
155   /**
156    * Size of the backtrace_strings array
157    */
158   int num_backtrace_strings;
159 #endif
160
161
162 };
163
164
165 /**
166  * List of tasks waiting for an event.
167  */
168 static struct Task *pending;
169
170 /**
171  * List of tasks waiting ONLY for a timeout event.
172  * Sorted by timeout (earliest first).  Used so that
173  * we do not traverse the list of these tasks when
174  * building select sets (we just look at the head
175  * to determine the respective timeout ONCE).
176  */
177 static struct Task *pending_timeout;
178
179 /**
180  * Last inserted task waiting ONLY for a timeout event.
181  * Used to (heuristically) speed up insertion.
182  */
183 static struct Task *pending_timeout_last;
184
185 /**
186  * ID of the task that is running right now.
187  */
188 static struct Task *active_task;
189
190 /**
191  * List of tasks ready to run right now,
192  * grouped by importance.
193  */
194 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
195
196 /**
197  * Identity of the last task queued.  Incremented for each task to
198  * generate a unique task ID (it is virtually impossible to start
199  * more than 2^64 tasks during the lifetime of a process).
200  */
201 static GNUNET_SCHEDULER_TaskIdentifier last_id;
202
203 /**
204  * Highest number so that all tasks with smaller identifiers
205  * have already completed.  Also the lowest number of a task
206  * still waiting to be executed.
207  */
208 static GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
209
210 /**
211  * Number of tasks on the ready list.
212  */
213 static unsigned int ready_count;
214
215 /**
216  * How many tasks have we run so far?
217  */
218 static unsigned long long tasks_run;
219
220 /**
221  * Priority of the task running right now.  Only
222  * valid while a task is running.
223  */
224 static enum GNUNET_SCHEDULER_Priority current_priority;
225
226 /**
227  * Priority of the highest task added in the current select
228  * iteration.
229  */
230 static enum GNUNET_SCHEDULER_Priority max_priority_added;
231
232
233 /**
234  * Check that the given priority is legal (and return it).
235  *
236  * @param p priority value to check
237  * @return p on success, 0 on error
238  */
239 static enum GNUNET_SCHEDULER_Priority
240 check_priority (enum GNUNET_SCHEDULER_Priority p)
241 {
242   if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
243     return p;
244   GNUNET_assert (0);
245   return 0;                     /* make compiler happy */
246 }
247
248
249 /**
250  * Is a task with this identifier still pending?  Also updates
251  * "lowest_pending_id" as a side-effect (for faster checks in the
252  * future), but only if the return value is "GNUNET_NO" (and
253  * the "lowest_pending_id" check failed).
254  *
255  * @param id which task are we checking for
256  * @return GNUNET_YES if so, GNUNET_NO if not
257  */
258 static int
259 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
260 {
261   struct Task *pos;
262   enum GNUNET_SCHEDULER_Priority p;
263   GNUNET_SCHEDULER_TaskIdentifier min;
264
265   if (id < lowest_pending_id)
266     return GNUNET_NO;
267   min = -1;                     /* maximum value */
268   pos = pending;
269   while (pos != NULL)
270     {
271       if (pos->id == id)
272         return GNUNET_YES;
273       if (pos->id < min)
274         min = pos->id;
275       pos = pos->next;
276     }
277   pos = pending_timeout;
278   while (pos != NULL)
279     {
280       if (pos->id == id)
281         return GNUNET_YES;
282       if (pos->id < min)
283         min = pos->id;
284       pos = pos->next;
285     }
286   for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
287     {
288       pos = ready[p];
289       while (pos != NULL)
290         {
291           if (pos->id == id)
292             return GNUNET_YES;
293           if (pos->id < min)
294             min = pos->id;
295           pos = pos->next;
296         }
297     }
298   lowest_pending_id = min;
299   return GNUNET_NO;
300 }
301
302
303 /**
304  * Update all sets and timeout for select.
305  *
306  * @param rs read-set, set to all FDs we would like to read (updated)
307  * @param ws write-set, set to all FDs we would like to write (updated)
308  * @param timeout next timeout (updated)
309  */
310 static void
311 update_sets (struct GNUNET_NETWORK_FDSet *rs,
312              struct GNUNET_NETWORK_FDSet *ws,
313              struct GNUNET_TIME_Relative *timeout)
314 {
315   struct Task *pos;
316   struct GNUNET_TIME_Absolute now;
317   struct GNUNET_TIME_Relative to;
318
319   now = GNUNET_TIME_absolute_get ();
320   pos = pending_timeout;
321   if (pos != NULL) 
322     {
323       to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
324       if (timeout->rel_value > to.rel_value)
325         *timeout = to;
326       if (pos->reason != 0)
327         *timeout = GNUNET_TIME_UNIT_ZERO;
328     }
329   pos = pending;
330   while (pos != NULL)
331     {
332       if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
333           (GNUNET_YES == is_pending (pos->prereq_id)))
334         {
335           pos = pos->next;
336           continue;
337         }
338       if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
339         {
340           to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
341           if (timeout->rel_value > to.rel_value)
342             *timeout = to;
343         }
344       if (pos->read_fd != -1)
345         GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
346       if (pos->write_fd != -1)
347         GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
348       if (pos->read_set != NULL)
349         GNUNET_NETWORK_fdset_add (rs, pos->read_set);
350       if (pos->write_set != NULL)
351         GNUNET_NETWORK_fdset_add (ws, pos->write_set);
352       if (pos->reason != 0)
353         *timeout = GNUNET_TIME_UNIT_ZERO;
354       pos = pos->next;
355     }
356 }
357
358
359 /**
360  * Check if the ready set overlaps with the set we want to have ready.
361  * If so, update the want set (set all FDs that are ready).  If not,
362  * return GNUNET_NO.
363  *
364  * @param ready set that is ready
365  * @param want set that we want to be ready
366  * @return GNUNET_YES if there was some overlap
367  */
368 static int
369 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
370               struct GNUNET_NETWORK_FDSet *want)
371 {
372   if ( (NULL == want) || (NULL == ready) )
373     return GNUNET_NO;
374   if (GNUNET_NETWORK_fdset_overlap (ready, want))
375     {
376       /* copy all over (yes, there maybe unrelated bits,
377          but this should not hurt well-written clients) */
378       GNUNET_NETWORK_fdset_copy (want, ready);
379       return GNUNET_YES;
380     }
381   return GNUNET_NO;
382 }
383
384
385 /**
386  * Check if the given task is eligible to run now.
387  * Also set the reason why it is eligible.
388  *
389  * @param task task to check if it is ready
390  * @param now the current time
391  * @param rs set of FDs ready for reading
392  * @param ws set of FDs ready for writing
393  * @return GNUNET_YES if we can run it, GNUNET_NO if not.
394  */
395 static int
396 is_ready (struct Task *task,
397           struct GNUNET_TIME_Absolute now,
398           const struct GNUNET_NETWORK_FDSet *rs,
399           const struct GNUNET_NETWORK_FDSet *ws)
400 {
401   enum GNUNET_SCHEDULER_Reason reason;
402
403   reason = task->reason;
404   if (now.abs_value >= task->timeout.abs_value)
405     reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
406   if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
407        ( ( (task->read_fd != -1) &&
408            (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd)) ) ||
409          (set_overlaps (rs, task->read_set) ) ) )
410     reason |= GNUNET_SCHEDULER_REASON_READ_READY;
411   if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
412       ( ( (task->write_fd != -1) &&
413           (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)) ) ||
414         (set_overlaps (ws, task->write_set) ) ) )
415     reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
416   if (reason == 0)
417     return GNUNET_NO;           /* not ready */    
418   if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
419     {
420       if (GNUNET_YES == is_pending (task->prereq_id))
421         {
422           task->reason = reason;
423           return GNUNET_NO;       /* prereq waiting */
424         }
425       reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
426     }
427   task->reason = reason;
428   return GNUNET_YES;
429 }
430
431
432 /**
433  * Put a task that is ready for execution into the ready queue.
434  *
435  * @param task task ready for execution
436  */
437 static void
438 queue_ready_task (struct Task *task)
439 {
440   enum GNUNET_SCHEDULER_Priority p = task->priority;
441   if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
442     p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
443   task->next = ready[check_priority (p)];
444   ready[check_priority (p)] = task;
445   ready_count++;
446 }
447
448
449 /**
450  * Check which tasks are ready and move them
451  * to the respective ready queue.
452  *
453  * @param rs FDs ready for reading
454  * @param ws FDs ready for writing
455  */
456 static void
457 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
458              const struct GNUNET_NETWORK_FDSet *ws)
459 {
460   struct Task *pos;
461   struct Task *prev;
462   struct Task *next;
463   struct GNUNET_TIME_Absolute now;
464
465   now = GNUNET_TIME_absolute_get ();
466   prev = NULL;
467   pos = pending_timeout;
468   while (pos != NULL)
469     {
470       next = pos->next;
471       if (now.abs_value >= pos->timeout.abs_value)
472         pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
473       if (0 == pos->reason)
474         break;
475       pending_timeout = next;
476       if (pending_timeout_last == pos)
477         pending_timeout_last = NULL;
478       queue_ready_task (pos);
479       pos = next;
480     }
481   pos = pending;
482   while (pos != NULL)
483     {
484 #if DEBUG_TASKS
485       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486                   "Checking readiness of task: %llu / %p\n",
487                   pos->id, pos->callback_cls);
488 #endif
489       next = pos->next;
490       if (GNUNET_YES == is_ready (pos, now, rs, ws))
491         {
492           if (prev == NULL)
493             pending = next;
494           else
495             prev->next = next;
496           queue_ready_task (pos);
497           pos = next;
498           continue;
499         }
500       prev = pos;
501       pos = next;
502     }
503 }
504
505
506 /**
507  * Request the shutdown of a scheduler.  Marks all currently
508  * pending tasks as ready because of shutdown.  This will
509  * cause all tasks to run (as soon as possible, respecting
510  * priorities and prerequisite tasks).  Note that tasks
511  * scheduled AFTER this call may still be delayed arbitrarily.
512  */
513 void
514 GNUNET_SCHEDULER_shutdown ()
515 {
516   struct Task *pos;
517   int i;
518
519   pos = pending_timeout;
520   while (pos != NULL)
521     {
522       pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
523       /* we don't move the task into the ready queue yet; check_ready
524          will do that later, possibly adding additional
525          readiness-factors */
526       pos = pos->next;
527     }
528   pos = pending;
529   while (pos != NULL)
530     {
531       pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
532       /* we don't move the task into the ready queue yet; check_ready
533          will do that later, possibly adding additional
534          readiness-factors */
535       pos = pos->next;
536     }
537   for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
538     {
539       pos = ready[i];
540       while (pos != NULL)
541         {
542           pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
543           /* we don't move the task into the ready queue yet; check_ready
544              will do that later, possibly adding additional
545              readiness-factors */
546           pos = pos->next;
547         }
548     }  
549 }
550
551
552 /**
553  * Destroy a task (release associated resources)
554  *
555  * @param t task to destroy
556  */
557 static void
558 destroy_task (struct Task *t)
559 {
560   if (NULL != t->read_set)
561     GNUNET_NETWORK_fdset_destroy (t->read_set);
562   if (NULL != t->write_set)
563     GNUNET_NETWORK_fdset_destroy (t->write_set);
564 #if EXECINFO
565   GNUNET_free (t->backtrace_strings);
566 #endif
567   GNUNET_free (t);
568 }
569
570
571 /**
572  * Run at least one task in the highest-priority queue that is not
573  * empty.  Keep running tasks until we are either no longer running
574  * "URGENT" tasks or until we have at least one "pending" task (which
575  * may become ready, hence we should select on it).  Naturally, if
576  * there are no more ready tasks, we also return.  
577  *
578  * @param rs FDs ready for reading
579  * @param ws FDs ready for writing
580  */
581 static void
582 run_ready (struct GNUNET_NETWORK_FDSet *rs,
583            struct GNUNET_NETWORK_FDSet *ws)
584 {
585   enum GNUNET_SCHEDULER_Priority p;
586   struct Task *pos;
587   struct GNUNET_SCHEDULER_TaskContext tc;
588
589   max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
590   do
591     {
592       if (ready_count == 0)
593         return;
594       GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
595       /* yes, p>0 is correct, 0 is "KEEP" which should
596          always be an empty queue (see assertion)! */
597       for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
598         {
599           pos = ready[p];
600           if (pos != NULL)
601             break;
602         }
603       GNUNET_assert (pos != NULL);      /* ready_count wrong? */
604       ready[p] = pos->next;
605       ready_count--;
606       if (current_priority != pos->priority)
607         {
608           current_priority = pos->priority;
609           (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), 
610                                                  pos->priority);
611         }
612       active_task = pos;
613 #if PROFILE_DELAYS
614       if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
615           DELAY_THRESHOLD.rel_value)
616         {
617           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
618                       "Task %llu took %llums to be scheduled\n",
619                       pos->id,
620                       (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
621         }
622 #endif
623       tc.reason = pos->reason;
624       tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set; 
625       if ( (pos->read_fd != -1) &&
626            (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
627         GNUNET_NETWORK_fdset_set_native (rs,
628                                          pos->read_fd);
629       tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
630       if ( (pos->write_fd != -1) &&
631            (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
632         GNUNET_NETWORK_fdset_set_native (ws,
633                                          pos->write_fd);
634       if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
635            (pos->write_fd != -1) &&
636            (! GNUNET_NETWORK_fdset_test_native (ws,
637                                                 pos->write_fd))) 
638         abort (); // added to ready in previous select loop!
639 #if DEBUG_TASKS
640       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641                   "Running task: %llu / %p\n", pos->id, pos->callback_cls);
642 #endif
643       pos->callback (pos->callback_cls, &tc);
644 #if EXECINFO
645       int i;
646       for (i=0;i<pos->num_backtrace_strings;i++)
647         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
648                     "Task %llu trace %d: %s\n",
649                     pos->id,
650                     i,
651                     pos->backtrace_strings[i]);
652 #endif
653       active_task = NULL;
654       destroy_task (pos);
655       tasks_run++;
656     }
657   while ( (pending == NULL) || (p >= max_priority_added) );
658 }
659
660 /**
661  * Pipe used to communicate shutdown via signal.
662  */
663 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
664
665 /**
666  * Signal handler called for SIGPIPE.
667  */
668 #ifndef MINGW
669 static void
670 sighandler_pipe ()
671 {
672   return;
673 }
674 #endif
675 /**
676  * Signal handler called for signals that should cause us to shutdown.
677  */
678 static void
679 sighandler_shutdown ()
680 {
681   static char c;
682   int old_errno = errno; /* backup errno */
683
684   GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
685                           (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
686                           sizeof (c));
687   errno = old_errno;
688 }
689
690
691 /**
692  * Initialize and run scheduler.  This function will return when all
693  * tasks have completed.  On systems with signals, receiving a SIGTERM
694  * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
695  * to be run after the active task is complete.  As a result, SIGTERM
696  * causes all active tasks to be scheduled with reason
697  * "GNUNET_SCHEDULER_REASON_SHUTDOWN".  (However, tasks added
698  * afterwards will execute normally!). Note that any particular signal
699  * will only shut down one scheduler; applications should always only
700  * create a single scheduler.
701  *
702  * @param task task to run immediately
703  * @param task_cls closure of task
704  */
705 void
706 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
707 {
708   struct GNUNET_NETWORK_FDSet *rs;
709   struct GNUNET_NETWORK_FDSet *ws;
710   struct GNUNET_TIME_Relative timeout;
711   int ret;
712   struct GNUNET_SIGNAL_Context *shc_int;
713   struct GNUNET_SIGNAL_Context *shc_term;
714 #ifndef MINGW
715   struct GNUNET_SIGNAL_Context *shc_quit;
716   struct GNUNET_SIGNAL_Context *shc_hup;
717   struct GNUNET_SIGNAL_Context *shc_pipe;
718 #endif
719   unsigned long long last_tr;
720   unsigned int busy_wait_warning;
721   const struct GNUNET_DISK_FileHandle *pr;
722   char c;
723
724   GNUNET_assert (active_task == NULL);
725   rs = GNUNET_NETWORK_fdset_create ();
726   ws = GNUNET_NETWORK_fdset_create ();
727   GNUNET_assert (shutdown_pipe_handle == NULL);
728   shutdown_pipe_handle =  GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
729   GNUNET_assert (shutdown_pipe_handle != NULL);
730   pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
731   GNUNET_assert (pr != NULL);
732   shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
733   shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
734 #ifndef MINGW
735   shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
736   shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
737   shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
738 #endif
739   current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
740   GNUNET_SCHEDULER_add_continuation (task,
741                                      task_cls,
742                                      GNUNET_SCHEDULER_REASON_STARTUP);
743 #if ENABLE_WINDOWS_WORKAROUNDS
744   GNUNET_SCHEDULER_add_continuation (&GNUNET_OS_install_parent_control_handler,
745                                      NULL, GNUNET_SCHEDULER_REASON_STARTUP);
746 #endif
747   last_tr = 0;
748   busy_wait_warning = 0;
749   while ((pending != NULL) ||
750          (pending_timeout != NULL) ||
751          (ready_count > 0))
752     {
753       GNUNET_NETWORK_fdset_zero (rs);
754       GNUNET_NETWORK_fdset_zero (ws);
755       timeout = GNUNET_TIME_UNIT_FOREVER_REL;
756       update_sets (rs, ws, &timeout);
757       GNUNET_NETWORK_fdset_handle_set (rs, pr);
758       if (ready_count > 0)
759         {
760           /* no blocking, more work already ready! */
761           timeout = GNUNET_TIME_UNIT_ZERO;
762         }
763       ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
764       if (ret == GNUNET_SYSERR)
765         {
766           if (errno == EINTR)
767             continue;
768
769           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
770 #ifndef MINGW
771 #if USE_LSOF
772           char lsof[512];
773           snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
774           (void) close (1);
775           (void) dup2 (2, 1);
776           if (0 != system (lsof))
777             GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "system");    
778 #endif
779 #endif
780           abort ();
781           break;
782         }
783       if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
784         {
785           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
786                       _("Looks like we're busy waiting...\n"));
787           sleep (1);            /* mitigate */
788         }
789       check_ready (rs, ws);
790       run_ready (rs, ws);
791       if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
792         {
793           /* consume the signal */
794           GNUNET_DISK_file_read (pr, &c, sizeof (c));
795           /* mark all active tasks as ready due to shutdown */
796           GNUNET_SCHEDULER_shutdown ();
797         }
798       if (last_tr == tasks_run)
799         {
800           busy_wait_warning++;
801         }
802       else
803         {
804           last_tr = tasks_run;
805           busy_wait_warning = 0;
806         }
807     }
808   GNUNET_SIGNAL_handler_uninstall (shc_int);
809   GNUNET_SIGNAL_handler_uninstall (shc_term);
810 #ifndef MINGW
811   GNUNET_SIGNAL_handler_uninstall (shc_pipe);
812   GNUNET_SIGNAL_handler_uninstall (shc_quit);
813   GNUNET_SIGNAL_handler_uninstall (shc_hup);
814 #endif
815   GNUNET_DISK_pipe_close (shutdown_pipe_handle);
816   shutdown_pipe_handle = NULL;
817   GNUNET_NETWORK_fdset_destroy (rs);
818   GNUNET_NETWORK_fdset_destroy (ws);
819 }
820
821
822 /**
823  * Obtain the reason code for why the current task was
824  * started.  Will return the same value as 
825  * the GNUNET_SCHEDULER_TaskContext's reason field.
826  *
827  * @return reason(s) why the current task is run
828  */
829 enum GNUNET_SCHEDULER_Reason
830 GNUNET_SCHEDULER_get_reason ()
831 {
832   GNUNET_assert (active_task != NULL);
833   return active_task->reason;
834 }
835
836
837 /**
838  * Get information about the current load of this scheduler.  Use this
839  * function to determine if an elective task should be added or simply
840  * dropped (if the decision should be made based on the number of
841  * tasks ready to run).
842  *
843  * @param p priority level to look at
844  * @return number of tasks pending right now
845  */
846 unsigned int
847 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
848 {
849   struct Task *pos;
850   unsigned int ret;
851
852   GNUNET_assert (active_task != NULL);
853   if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
854     return ready_count;
855   if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
856     p = current_priority;
857   ret = 0;
858   pos = ready[check_priority (p)];
859   while (pos != NULL)
860     {
861       pos = pos->next;
862       ret++;
863     }
864   return ret;
865 }
866
867
868 /**
869  * Cancel the task with the specified identifier.
870  * The task must not yet have run.
871  *
872  * @param task id of the task to cancel
873  * @return original closure of the task
874  */
875 void *
876 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
877 {
878   struct Task *t;
879   struct Task *prev;
880   enum GNUNET_SCHEDULER_Priority p;
881   int to;
882   void *ret;
883
884   GNUNET_assert (active_task != NULL);
885   to = 0;
886   prev = NULL;
887   t = pending;
888   while (t != NULL)
889     {
890       if (t->id == task)
891         break;
892       prev = t;
893       t = t->next;
894     }
895   if (t == NULL)
896     {
897       prev = NULL;
898       to = 1;
899       t = pending_timeout;
900       while (t != NULL)
901         {
902           if (t->id == task)
903             break;
904           prev = t;
905           t = t->next;
906         }
907       if (pending_timeout_last == t)
908         pending_timeout_last = NULL;
909     }
910   p = 0;
911   while (t == NULL)
912     {
913       p++;
914       GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
915       prev = NULL;
916       t = ready[p];
917       while (t != NULL)
918         {
919           if (t->id == task)
920             {
921               ready_count--;
922               break;
923             }
924           prev = t;
925           t = t->next;
926         }
927     }
928   if (prev == NULL)
929     {
930       if (p == 0)
931         {
932           if (to == 0)
933             {
934               pending = t->next;
935             }
936           else
937             {
938               pending_timeout = t->next;
939             }
940         }
941       else
942         {
943           ready[p] = t->next;
944         }
945     }
946   else
947     {
948       prev->next = t->next;
949     }
950   ret = t->callback_cls;
951 #if DEBUG_TASKS
952   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953               "Canceling task: %llu / %p\n", task, t->callback_cls);
954 #endif
955   destroy_task (t);
956   return ret;
957 }
958
959
960 /**
961  * Continue the current execution with the given function.  This is
962  * similar to the other "add" functions except that there is no delay
963  * and the reason code can be specified.
964  *
965  * @param task main function of the task
966  * @param task_cls closure for 'main'
967  * @param reason reason for task invocation
968  */
969 void
970 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
971                                    void *task_cls,
972                                    enum GNUNET_SCHEDULER_Reason reason)
973 {
974   struct Task *t;
975 #if EXECINFO
976   void *backtrace_array[50];
977 #endif
978
979   GNUNET_assert ( (active_task != NULL) ||
980                   (reason == GNUNET_SCHEDULER_REASON_STARTUP) );
981   t = GNUNET_malloc (sizeof (struct Task));
982 #if EXECINFO
983   t->num_backtrace_strings = backtrace(backtrace_array, 50);
984   t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
985 #endif
986   t->read_fd = -1;
987   t->write_fd = -1;
988   t->callback = task;
989   t->callback_cls = task_cls;
990   t->id = ++last_id;
991 #if PROFILE_DELAYS
992   t->start_time = GNUNET_TIME_absolute_get ();
993 #endif
994   t->reason = reason;
995   t->priority = current_priority;
996 #if DEBUG_TASKS
997   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998               "Adding continuation task: %llu / %p\n",
999               t->id, t->callback_cls);
1000 #endif
1001   queue_ready_task (t);
1002 }
1003
1004
1005
1006 /**
1007  * Schedule a new task to be run after the specified prerequisite task
1008  * has completed. It will be run with the priority of the calling
1009  * task.
1010  *
1011  * @param prerequisite_task run this task after the task with the given
1012  *        task identifier completes (and any of our other
1013  *        conditions, such as delay, read or write-readiness
1014  *        are satisfied).  Use  GNUNET_SCHEDULER_NO_TASK to not have any dependency
1015  *        on completion of other tasks (this will cause the task to run as
1016  *        soon as possible).
1017  * @param task main function of the task
1018  * @param task_cls closure of task
1019  * @return unique task identifier for the job
1020  *         only valid until "task" is started!
1021  */
1022 GNUNET_SCHEDULER_TaskIdentifier
1023 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1024                             GNUNET_SCHEDULER_Task task, void *task_cls)
1025 {
1026   return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1027                                       prerequisite_task,
1028                                       GNUNET_TIME_UNIT_ZERO,
1029                                       NULL, NULL, task, task_cls);
1030 }
1031
1032
1033 /**
1034  * Schedule a new task to be run with a specified priority.
1035  *
1036  * @param prio how important is the new task?
1037  * @param task main function of the task
1038  * @param task_cls closure of task
1039  * @return unique task identifier for the job
1040  *         only valid until "task" is started!
1041  */
1042 GNUNET_SCHEDULER_TaskIdentifier
1043 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1044                                     GNUNET_SCHEDULER_Task task,
1045                                     void *task_cls)
1046 {
1047   return GNUNET_SCHEDULER_add_select (prio,
1048                                       GNUNET_SCHEDULER_NO_TASK,
1049                                       GNUNET_TIME_UNIT_ZERO,
1050                                       NULL, NULL, task, task_cls);
1051 }
1052
1053
1054
1055 /**
1056  * Schedule a new task to be run with a specified delay.  The task
1057  * will be scheduled for execution once the delay has expired. It
1058  * will be run with the priority of the calling task.
1059  *
1060  * @param delay when should this operation time out? Use 
1061  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1062  * @param task main function of the task
1063  * @param task_cls closure of task
1064  * @return unique task identifier for the job
1065  *         only valid until "task" is started!
1066  */
1067 GNUNET_SCHEDULER_TaskIdentifier
1068 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1069                               GNUNET_SCHEDULER_Task task, void *task_cls)
1070 {
1071 #if 1
1072   /* new, optimized version */
1073   struct Task *t;
1074   struct Task *pos;
1075   struct Task *prev;
1076 #if EXECINFO
1077   void *backtrace_array[MAX_TRACE_DEPTH];
1078 #endif
1079
1080   GNUNET_assert (active_task != NULL);
1081   GNUNET_assert (NULL != task);
1082   t = GNUNET_malloc (sizeof (struct Task));
1083   t->callback = task;
1084   t->callback_cls = task_cls;
1085 #if EXECINFO
1086   t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1087   t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1088 #endif
1089   t->read_fd = -1;
1090   t->write_fd = -1;
1091   t->id = ++last_id;
1092 #if PROFILE_DELAYS
1093   t->start_time = GNUNET_TIME_absolute_get ();
1094 #endif
1095   t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1096   t->priority = current_priority;
1097   /* try tail first (optimization in case we are
1098      appending to a long list of tasks with timeouts) */
1099   prev = pending_timeout_last;
1100   if (prev != NULL) 
1101     {
1102       if (prev->timeout.abs_value > t->timeout.abs_value)
1103         prev = NULL;
1104       else
1105         pos = prev->next; /* heuristic success! */
1106     }
1107   if (prev == NULL)
1108     {
1109       /* heuristic failed, do traversal of timeout list */
1110       pos = pending_timeout;
1111     }
1112   while ( (pos != NULL) &&
1113           ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
1114             (pos->reason != 0) ) )
1115     {
1116       prev = pos;
1117       pos = pos->next;
1118     }
1119   if (prev == NULL)
1120     pending_timeout = t;
1121   else
1122     prev->next = t;
1123   t->next = pos;
1124   /* hyper-optimization... */
1125   pending_timeout_last = t;
1126
1127 #if DEBUG_TASKS
1128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129               "Adding task: %llu / %p\n", t->id, t->callback_cls);
1130 #endif
1131 #if EXECINFO
1132   int i;
1133
1134   for (i=0;i<t->num_backtrace_strings;i++)
1135       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136                   "Task %llu trace %d: %s\n",
1137                   t->id,
1138                   i,
1139                   t->backtrace_strings[i]);
1140 #endif
1141   return t->id;
1142
1143 #else
1144   /* unoptimized version */
1145   return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1146                                       GNUNET_SCHEDULER_NO_TASK, delay,
1147                                       NULL, NULL, task, task_cls);
1148 #endif
1149 }
1150
1151
1152
1153 /**
1154  * Schedule a new task to be run as soon as possible. The task
1155  * will be run with the priority of the calling task.
1156  *
1157  * @param task main function of the task
1158  * @param task_cls closure of task
1159  * @return unique task identifier for the job
1160  *         only valid until "task" is started!
1161  */
1162 GNUNET_SCHEDULER_TaskIdentifier
1163 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
1164                                                   void *task_cls)
1165 {
1166   return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1167                                       GNUNET_SCHEDULER_NO_TASK,
1168                                       GNUNET_TIME_UNIT_ZERO,
1169                                       NULL, NULL, task, task_cls);
1170 }
1171
1172
1173
1174
1175 /**
1176  * Schedule a new task to be run with a specified delay or when any of
1177  * the specified file descriptor sets is ready.  The delay can be used
1178  * as a timeout on the socket(s) being ready.  The task will be
1179  * scheduled for execution once either the delay has expired or any of
1180  * the socket operations is ready.  This is the most general
1181  * function of the "add" family.  Note that the "prerequisite_task"
1182  * must be satisfied in addition to any of the other conditions.  In
1183  * other words, the task will be started when
1184  * <code>
1185  * (prerequisite-run)
1186  * && (delay-ready
1187  *     || any-rs-ready
1188  *     || any-ws-ready
1189  *     || shutdown-active )
1190  * </code>
1191  *
1192  * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1193  *        which means that the task will only be run after we receive SIGTERM
1194  * @param rfd file descriptor we want to read (can be -1)
1195  * @param wfd file descriptors we want to write (can be -1)
1196  * @param task main function of the task
1197  * @param task_cls closure of task
1198  * @return unique task identifier for the job
1199  *         only valid until "task" is started!
1200  */
1201 #ifndef MINGW
1202 GNUNET_SCHEDULER_TaskIdentifier
1203 add_without_sets (struct GNUNET_TIME_Relative delay,
1204                   int rfd,
1205                   int wfd,
1206                   GNUNET_SCHEDULER_Task task, void *task_cls)
1207 {
1208   struct Task *t;
1209 #if EXECINFO
1210   void *backtrace_array[MAX_TRACE_DEPTH];
1211 #endif
1212
1213   GNUNET_assert (active_task != NULL);
1214   GNUNET_assert (NULL != task);
1215   t = GNUNET_malloc (sizeof (struct Task));
1216   t->callback = task;
1217   t->callback_cls = task_cls;
1218 #if EXECINFO
1219   t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1220   t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1221 #endif
1222   t->read_fd = rfd;
1223   GNUNET_assert(wfd >= -1);
1224   t->write_fd = wfd;
1225   t->id = ++last_id;
1226 #if PROFILE_DELAYS
1227   t->start_time = GNUNET_TIME_absolute_get ();
1228 #endif
1229   t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1230   t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1231   t->priority = check_priority (current_priority);
1232   t->next = pending;
1233   pending = t;
1234   max_priority_added = GNUNET_MAX (max_priority_added,
1235                                           t->priority);
1236 #if DEBUG_TASKS
1237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238               "Adding task: %llu / %p\n", t->id, t->callback_cls);
1239 #endif
1240 #if EXECINFO
1241   int i;
1242
1243   for (i=0;i<t->num_backtrace_strings;i++)
1244       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245                   "Task %llu trace %d: %s\n",
1246                   t->id,
1247                   i,
1248                   t->backtrace_strings[i]);
1249 #endif
1250   return t->id;
1251 }
1252 #endif
1253
1254
1255
1256 /**
1257  * Schedule a new task to be run with a specified delay or when the
1258  * specified file descriptor is ready for reading.  The delay can be
1259  * used as a timeout on the socket being ready.  The task will be
1260  * scheduled for execution once either the delay has expired or the
1261  * socket operation is ready.  It will be run with the priority of
1262  * the calling task.
1263  *
1264  * @param delay when should this operation time out? Use 
1265  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1266  * @param rfd read file-descriptor
1267  * @param task main function of the task
1268  * @param task_cls closure of task
1269  * @return unique task identifier for the job
1270  *         only valid until "task" is started!
1271  */
1272 GNUNET_SCHEDULER_TaskIdentifier
1273 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1274                                struct GNUNET_NETWORK_Handle * rfd,
1275                                GNUNET_SCHEDULER_Task task, void *task_cls)
1276 {
1277 #if MINGW
1278   struct GNUNET_NETWORK_FDSet *rs;
1279   GNUNET_SCHEDULER_TaskIdentifier ret;
1280
1281   GNUNET_assert (rfd != NULL);
1282   rs = GNUNET_NETWORK_fdset_create ();
1283   GNUNET_NETWORK_fdset_set (rs, rfd);
1284   ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1285                                      GNUNET_SCHEDULER_NO_TASK, delay,
1286                                      rs, NULL, task, task_cls);
1287   GNUNET_NETWORK_fdset_destroy (rs);
1288   return ret;
1289 #else
1290   return add_without_sets (delay,
1291                            GNUNET_NETWORK_get_fd (rfd),
1292                            -1,
1293                            task,
1294                            task_cls);
1295 #endif
1296 }
1297
1298
1299 /**
1300  * Schedule a new task to be run with a specified delay or when the
1301  * specified file descriptor is ready for writing.  The delay can be
1302  * used as a timeout on the socket being ready.  The task will be
1303  * scheduled for execution once either the delay has expired or the
1304  * socket operation is ready.  It will be run with the priority of
1305  * the calling task.
1306  *
1307  * @param delay when should this operation time out? Use 
1308  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1309  * @param wfd write file-descriptor
1310  * @param task main function of the task
1311  * @param task_cls closure of task
1312  * @return unique task identifier for the job
1313  *         only valid until "task" is started!
1314  */
1315 GNUNET_SCHEDULER_TaskIdentifier
1316 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1317                                 struct GNUNET_NETWORK_Handle * wfd,
1318                                 GNUNET_SCHEDULER_Task task, void *task_cls)
1319 {
1320 #if MINGW
1321   struct GNUNET_NETWORK_FDSet *ws;
1322   GNUNET_SCHEDULER_TaskIdentifier ret;
1323
1324   GNUNET_assert (wfd != NULL);
1325   ws = GNUNET_NETWORK_fdset_create ();
1326   GNUNET_NETWORK_fdset_set (ws, wfd);
1327   ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1328                                      GNUNET_SCHEDULER_NO_TASK, delay,
1329                                      NULL, ws, task, task_cls);
1330   GNUNET_NETWORK_fdset_destroy (ws);
1331   return ret;
1332 #else
1333   GNUNET_assert(GNUNET_NETWORK_get_fd(wfd) >= 0);
1334   return add_without_sets (delay,
1335                            -1,
1336                            GNUNET_NETWORK_get_fd (wfd),
1337                            task,
1338                            task_cls);
1339 #endif
1340 }
1341
1342
1343 /**
1344  * Schedule a new task to be run with a specified delay or when the
1345  * specified file descriptor is ready for reading.  The delay can be
1346  * used as a timeout on the socket being ready.  The task will be
1347  * scheduled for execution once either the delay has expired or the
1348  * socket operation is ready. It will be run with the priority of
1349  * the calling task.
1350  *
1351  * @param delay when should this operation time out? Use 
1352  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1353  * @param rfd read file-descriptor
1354  * @param task main function of the task
1355  * @param task_cls closure of task
1356  * @return unique task identifier for the job
1357  *         only valid until "task" is started!
1358  */
1359 GNUNET_SCHEDULER_TaskIdentifier
1360 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1361                                 const struct GNUNET_DISK_FileHandle * rfd,
1362                                 GNUNET_SCHEDULER_Task task, void *task_cls)
1363 {
1364 #if MINGW
1365   struct GNUNET_NETWORK_FDSet *rs;
1366   GNUNET_SCHEDULER_TaskIdentifier ret;
1367
1368   GNUNET_assert (rfd != NULL);
1369   rs = GNUNET_NETWORK_fdset_create ();
1370   GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1371   ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1372                                      GNUNET_SCHEDULER_NO_TASK, delay,
1373                                      rs, NULL, task, task_cls);
1374   GNUNET_NETWORK_fdset_destroy (rs);
1375   return ret;
1376 #else
1377   int fd;
1378
1379   GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1380   return add_without_sets (delay,
1381                            fd,
1382                            -1,
1383                            task,
1384                            task_cls);
1385
1386 #endif
1387 }
1388
1389
1390 /**
1391  * Schedule a new task to be run with a specified delay or when the
1392  * specified file descriptor is ready for writing.  The delay can be
1393  * used as a timeout on the socket being ready.  The task will be
1394  * scheduled for execution once either the delay has expired or the
1395  * socket operation is ready. It will be run with the priority of
1396  * the calling task.
1397  *
1398  * @param delay when should this operation time out? Use 
1399  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1400  * @param wfd write file-descriptor
1401  * @param task main function of the task
1402  * @param task_cls closure of task
1403  * @return unique task identifier for the job
1404  *         only valid until "task" is started!
1405  */
1406 GNUNET_SCHEDULER_TaskIdentifier
1407 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1408                                  const struct GNUNET_DISK_FileHandle * wfd,
1409                                  GNUNET_SCHEDULER_Task task, void *task_cls)
1410 {
1411 #if MINGW
1412   struct GNUNET_NETWORK_FDSet *ws;
1413   GNUNET_SCHEDULER_TaskIdentifier ret;
1414
1415   GNUNET_assert (wfd != NULL);
1416   ws = GNUNET_NETWORK_fdset_create ();
1417   GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1418   ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1419                                      GNUNET_SCHEDULER_NO_TASK,
1420                                      delay, NULL, ws, task, task_cls);
1421   GNUNET_NETWORK_fdset_destroy (ws);
1422   return ret;
1423 #else
1424   int fd;
1425
1426   GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1427   GNUNET_assert(fd >= 0);
1428   return add_without_sets (delay,
1429                            -1,
1430                            fd,
1431                            task,
1432                            task_cls);
1433
1434 #endif
1435 }
1436
1437
1438
1439 /**
1440  * Schedule a new task to be run with a specified delay or when any of
1441  * the specified file descriptor sets is ready.  The delay can be used
1442  * as a timeout on the socket(s) being ready.  The task will be
1443  * scheduled for execution once either the delay has expired or any of
1444  * the socket operations is ready.  This is the most general
1445  * function of the "add" family.  Note that the "prerequisite_task"
1446  * must be satisfied in addition to any of the other conditions.  In
1447  * other words, the task will be started when
1448  * <code>
1449  * (prerequisite-run)
1450  * && (delay-ready
1451  *     || any-rs-ready
1452  *     || any-ws-ready
1453  *     || (shutdown-active && run-on-shutdown) )
1454  * </code>
1455  *
1456  * @param prio how important is this task?
1457  * @param prerequisite_task run this task after the task with the given
1458  *        task identifier completes (and any of our other
1459  *        conditions, such as delay, read or write-readiness
1460  *        are satisfied).  Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1461  *        on completion of other tasks.
1462  * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1463  *        which means that the task will only be run after we receive SIGTERM
1464  * @param rs set of file descriptors we want to read (can be NULL)
1465  * @param ws set of file descriptors we want to write (can be NULL)
1466  * @param task main function of the task
1467  * @param task_cls closure of task
1468  * @return unique task identifier for the job
1469  *         only valid until "task" is started!
1470  */
1471 GNUNET_SCHEDULER_TaskIdentifier
1472 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1473                              GNUNET_SCHEDULER_TaskIdentifier
1474                              prerequisite_task,
1475                              struct GNUNET_TIME_Relative delay,
1476                              const struct GNUNET_NETWORK_FDSet * rs,
1477                              const struct GNUNET_NETWORK_FDSet * ws,
1478                              GNUNET_SCHEDULER_Task task, void *task_cls)
1479 {
1480   struct Task *t;
1481 #if EXECINFO
1482   void *backtrace_array[MAX_TRACE_DEPTH];
1483 #endif
1484
1485   GNUNET_assert (active_task != NULL);
1486   GNUNET_assert (NULL != task);
1487   t = GNUNET_malloc (sizeof (struct Task));
1488   t->callback = task;
1489   t->callback_cls = task_cls;
1490 #if EXECINFO
1491   t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1492   t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1493 #endif
1494   t->read_fd = -1;
1495   t->write_fd = -1;
1496   if (rs != NULL)
1497     {
1498       t->read_set = GNUNET_NETWORK_fdset_create ();
1499       GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1500     }
1501   if (ws != NULL)
1502     {
1503       t->write_set = GNUNET_NETWORK_fdset_create ();
1504       GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1505     }
1506   t->id = ++last_id;
1507 #if PROFILE_DELAYS
1508   t->start_time = GNUNET_TIME_absolute_get ();
1509 #endif
1510   t->prereq_id = prerequisite_task;
1511   t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1512   t->priority =
1513     check_priority ((prio ==
1514                      GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1515                     : prio);
1516   t->next = pending;
1517   pending = t;
1518   max_priority_added = GNUNET_MAX (max_priority_added,
1519                                           t->priority);
1520 #if DEBUG_TASKS
1521   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522               "Adding task: %llu / %p\n", t->id, t->callback_cls);
1523 #endif
1524 #if EXECINFO
1525   int i;
1526
1527   for (i=0;i<t->num_backtrace_strings;i++)
1528       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1529                   "Task %llu trace %d: %s\n",
1530                   t->id,
1531                   i,
1532                   t->backtrace_strings[i]);
1533 #endif
1534   return t->id;
1535 }
1536
1537 /* end of scheduler.c */