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