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