d40853963a9b5b3e39e7445caac79039a49fb590
[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 #ifdef LINUX
34 #include "execinfo.h"
35
36
37 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
38
39 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
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,
531            "Checking readiness of task: %llu / %p\n", pos->id,
532            pos->callback_cls);
533 #endif
534       next = pos->next;
535       if (GNUNET_YES == is_ready (pos, now, rs, ws))
536         {
537           if (prev == NULL)
538             pending = next;
539           else
540             prev->next = next;
541           queue_ready_task (pos);
542           pos = next;
543           continue;
544         }
545       prev = pos;
546       pos = next;
547     }
548 }
549
550
551 /**
552  * Request the shutdown of a scheduler.  Marks all currently
553  * pending tasks as ready because of shutdown.  This will
554  * cause all tasks to run (as soon as possible, respecting
555  * priorities and prerequisite tasks).  Note that tasks
556  * scheduled AFTER this call may still be delayed arbitrarily.
557  */
558 void
559 GNUNET_SCHEDULER_shutdown ()
560 {
561   struct Task *pos;
562   int i;
563
564   pos = pending_timeout;
565   while (pos != NULL)
566     {
567       pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
568       /* we don't move the task into the ready queue yet; check_ready
569        * will do that later, possibly adding additional
570        * readiness-factors */
571       pos = pos->next;
572     }
573   pos = pending;
574   while (pos != NULL)
575     {
576       pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
577       /* we don't move the task into the ready queue yet; check_ready
578        * will do that later, possibly adding additional
579        * readiness-factors */
580       pos = pos->next;
581     }
582   for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++)
583     {
584       pos = ready[i];
585       while (pos != NULL)
586         {
587           pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
588           /* we don't move the task into the ready queue yet; check_ready
589            * will do that later, possibly adding additional
590            * readiness-factors */
591           pos = pos->next;
592         }
593     }
594 }
595
596
597 /**
598  * Destroy a task (release associated resources)
599  *
600  * @param t task to destroy
601  */
602 static void
603 destroy_task (struct Task *t)
604 {
605   if (NULL != t->read_set)
606     GNUNET_NETWORK_fdset_destroy (t->read_set);
607   if (NULL != t->write_set)
608     GNUNET_NETWORK_fdset_destroy (t->write_set);
609 #if EXECINFO
610   GNUNET_free (t->backtrace_strings);
611 #endif
612   GNUNET_free (t);
613 }
614
615
616 /**
617  * Run at least one task in the highest-priority queue that is not
618  * empty.  Keep running tasks until we are either no longer running
619  * "URGENT" tasks or until we have at least one "pending" task (which
620  * may become ready, hence we should select on it).  Naturally, if
621  * there are no more ready tasks, we also return.
622  *
623  * @param rs FDs ready for reading
624  * @param ws FDs ready for writing
625  */
626 static void
627 run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
628 {
629   enum GNUNET_SCHEDULER_Priority p;
630   struct Task *pos;
631   struct GNUNET_SCHEDULER_TaskContext tc;
632
633   max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
634   do
635     {
636       if (ready_count == 0)
637         return;
638       GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
639       /* yes, p>0 is correct, 0 is "KEEP" which should
640        * always be an empty queue (see assertion)! */
641       for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
642         {
643           pos = ready[p];
644           if (pos != NULL)
645             break;
646         }
647       GNUNET_assert (pos != NULL);      /* ready_count wrong? */
648       ready[p] = pos->next;
649       ready_count--;
650       if (current_priority != pos->priority)
651         {
652           current_priority = pos->priority;
653           (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
654                                                  pos->priority);
655         }
656       current_lifeness = pos->lifeness;
657       active_task = pos;
658 #if PROFILE_DELAYS
659       if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
660           DELAY_THRESHOLD.rel_value)
661         {
662           LOG (GNUNET_ERROR_TYPE_ERROR,
663                "Task %llu took %llums to be scheduled\n", pos->id,
664                (unsigned long long)
665                GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
666         }
667 #endif
668       tc.reason = pos->reason;
669       tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
670       if ((pos->read_fd != -1) &&
671           (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
672         GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
673       tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
674       if ((pos->write_fd != -1) &&
675           (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
676         GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
677       if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
678           (pos->write_fd != -1) &&
679           (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
680         abort ();               // added to ready in previous select loop!
681 #if DEBUG_TASKS
682       LOG (GNUNET_ERROR_TYPE_DEBUG,
683            "Running task: %llu / %p\n", pos->id, pos->callback_cls);
684 #endif
685       pos->callback (pos->callback_cls, &tc);
686 #if EXECINFO
687       int i;
688
689       for (i = 0; i < pos->num_backtrace_strings; i++)
690         LOG (GNUNET_ERROR_TYPE_ERROR,
691              "Task %llu trace %d: %s\n", pos->id,
692              i, pos->backtrace_strings[i]);
693 #endif
694       active_task = NULL;
695       destroy_task (pos);
696       tasks_run++;
697     }
698   while ((pending == NULL) || (p >= max_priority_added));
699 }
700
701 /**
702  * Pipe used to communicate shutdown via signal.
703  */
704 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
705
706 /**
707  * Signal handler called for SIGPIPE.
708  */
709 #ifndef MINGW
710 static void
711 sighandler_pipe ()
712 {
713   return;
714 }
715 #endif
716 /**
717  * Signal handler called for signals that should cause us to shutdown.
718  */
719 static void
720 sighandler_shutdown ()
721 {
722   static char c;
723   int old_errno = errno;        /* backup errno */
724
725   GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
726                           (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
727                           &c, sizeof (c));
728   errno = old_errno;
729 }
730
731
732 /**
733  * Check if the system is still life. Trigger shutdown if we
734  * have tasks, but none of them give us lifeness.
735  *
736  * @return GNUNET_OK to continue the main loop,
737  *         GNUNET_NO to exit
738  */
739 static int
740 check_lifeness ()
741 {
742   struct Task *t;
743
744   if (ready_count > 0)
745     return GNUNET_OK;
746   for (t = pending; NULL != t; t = t->next)
747     if (t->lifeness == GNUNET_YES)
748       return GNUNET_OK;
749   for (t = pending_timeout; NULL != t; t = t->next)
750     if (t->lifeness == GNUNET_YES)
751       return GNUNET_OK;
752   if ((NULL != pending) || (NULL != pending_timeout))
753     {
754       GNUNET_SCHEDULER_shutdown ();
755       return GNUNET_OK;
756     }
757   return GNUNET_NO;
758 }
759
760
761 /**
762  * Initialize and run scheduler.  This function will return when all
763  * tasks have completed.  On systems with signals, receiving a SIGTERM
764  * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
765  * to be run after the active task is complete.  As a result, SIGTERM
766  * causes all active tasks to be scheduled with reason
767  * "GNUNET_SCHEDULER_REASON_SHUTDOWN".  (However, tasks added
768  * afterwards will execute normally!). Note that any particular signal
769  * will only shut down one scheduler; applications should always only
770  * create a single scheduler.
771  *
772  * @param task task to run immediately
773  * @param task_cls closure of task
774  */
775 void
776 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
777 {
778   struct GNUNET_NETWORK_FDSet *rs;
779   struct GNUNET_NETWORK_FDSet *ws;
780   struct GNUNET_TIME_Relative timeout;
781   int ret;
782   struct GNUNET_SIGNAL_Context *shc_int;
783   struct GNUNET_SIGNAL_Context *shc_term;
784
785 #ifndef MINGW
786   struct GNUNET_SIGNAL_Context *shc_quit;
787   struct GNUNET_SIGNAL_Context *shc_hup;
788   struct GNUNET_SIGNAL_Context *shc_pipe;
789 #endif
790   unsigned long long last_tr;
791   unsigned int busy_wait_warning;
792   const struct GNUNET_DISK_FileHandle *pr;
793   char c;
794
795   GNUNET_assert (active_task == NULL);
796   rs = GNUNET_NETWORK_fdset_create ();
797   ws = GNUNET_NETWORK_fdset_create ();
798   GNUNET_assert (shutdown_pipe_handle == NULL);
799   shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
800   GNUNET_assert (shutdown_pipe_handle != NULL);
801   pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
802                                 GNUNET_DISK_PIPE_END_READ);
803   GNUNET_assert (pr != NULL);
804   shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
805   shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
806 #ifndef MINGW
807   shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
808   shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
809   shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
810 #endif
811   current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
812   current_lifeness = GNUNET_YES;
813   GNUNET_SCHEDULER_add_continuation (task, task_cls,
814                                      GNUNET_SCHEDULER_REASON_STARTUP);
815 #if ENABLE_WINDOWS_WORKAROUNDS
816   active_task = (void *) (long) -1;     /* force passing of sanity check */
817   GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
818                                           &GNUNET_OS_install_parent_control_handler,
819                                           NULL);
820   active_task = NULL;
821 #endif
822   last_tr = 0;
823   busy_wait_warning = 0;
824   while (GNUNET_OK == check_lifeness ())
825     {
826       GNUNET_NETWORK_fdset_zero (rs);
827       GNUNET_NETWORK_fdset_zero (ws);
828       timeout = GNUNET_TIME_UNIT_FOREVER_REL;
829       update_sets (rs, ws, &timeout);
830       GNUNET_NETWORK_fdset_handle_set (rs, pr);
831       if (ready_count > 0)
832         {
833           /* no blocking, more work already ready! */
834           timeout = GNUNET_TIME_UNIT_ZERO;
835         }
836       if (NULL == scheduler_select)
837         ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
838       else
839         ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
840       if (ret == GNUNET_SYSERR)
841         {
842           if (errno == EINTR)
843             continue;
844
845           LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
846 #ifndef MINGW
847 #if USE_LSOF
848           char lsof[512];
849
850           snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
851           (void) close (1);
852           (void) dup2 (2, 1);
853           if (0 != system (lsof))
854             LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system");
855 #endif
856 #endif
857           abort ();
858           break;
859         }
860       if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
861         {
862           LOG (GNUNET_ERROR_TYPE_WARNING,
863                _("Looks like we're busy waiting...\n"));
864           sleep (1);            /* mitigate */
865         }
866       check_ready (rs, ws);
867       run_ready (rs, ws);
868       if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
869         {
870           /* consume the signal */
871           GNUNET_DISK_file_read (pr, &c, sizeof (c));
872           /* mark all active tasks as ready due to shutdown */
873           GNUNET_SCHEDULER_shutdown ();
874         }
875       if (last_tr == tasks_run)
876         {
877           busy_wait_warning++;
878         }
879       else
880         {
881           last_tr = tasks_run;
882           busy_wait_warning = 0;
883         }
884     }
885   GNUNET_SIGNAL_handler_uninstall (shc_int);
886   GNUNET_SIGNAL_handler_uninstall (shc_term);
887 #ifndef MINGW
888   GNUNET_SIGNAL_handler_uninstall (shc_pipe);
889   GNUNET_SIGNAL_handler_uninstall (shc_quit);
890   GNUNET_SIGNAL_handler_uninstall (shc_hup);
891 #endif
892   GNUNET_DISK_pipe_close (shutdown_pipe_handle);
893   shutdown_pipe_handle = NULL;
894   GNUNET_NETWORK_fdset_destroy (rs);
895   GNUNET_NETWORK_fdset_destroy (ws);
896 }
897
898
899 /**
900  * Obtain the reason code for why the current task was
901  * started.  Will return the same value as
902  * the GNUNET_SCHEDULER_TaskContext's reason field.
903  *
904  * @return reason(s) why the current task is run
905  */
906 enum GNUNET_SCHEDULER_Reason
907 GNUNET_SCHEDULER_get_reason ()
908 {
909   GNUNET_assert (active_task != NULL);
910   return active_task->reason;
911 }
912
913
914 /**
915  * Get information about the current load of this scheduler.  Use this
916  * function to determine if an elective task should be added or simply
917  * dropped (if the decision should be made based on the number of
918  * tasks ready to run).
919  *
920  * @param p priority level to look at
921  * @return number of tasks pending right now
922  */
923 unsigned int
924 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
925 {
926   struct Task *pos;
927   unsigned int ret;
928
929   GNUNET_assert (active_task != NULL);
930   if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
931     return ready_count;
932   if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
933     p = current_priority;
934   ret = 0;
935   pos = ready[check_priority (p)];
936   while (pos != NULL)
937     {
938       pos = pos->next;
939       ret++;
940     }
941   return ret;
942 }
943
944
945 /**
946  * Cancel the task with the specified identifier.
947  * The task must not yet have run.
948  *
949  * @param task id of the task to cancel
950  * @return original closure of the task
951  */
952 void *
953 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
954 {
955   struct Task *t;
956   struct Task *prev;
957   enum GNUNET_SCHEDULER_Priority p;
958   int to;
959   void *ret;
960
961   GNUNET_assert (active_task != NULL);
962   to = 0;
963   prev = NULL;
964   t = pending;
965   while (t != NULL)
966     {
967       if (t->id == task)
968         break;
969       prev = t;
970       t = t->next;
971     }
972   if (t == NULL)
973     {
974       prev = NULL;
975       to = 1;
976       t = pending_timeout;
977       while (t != NULL)
978         {
979           if (t->id == task)
980             break;
981           prev = t;
982           t = t->next;
983         }
984       if (pending_timeout_last == t)
985         pending_timeout_last = NULL;
986     }
987   p = 0;
988   while (t == NULL)
989     {
990       p++;
991       if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
992         {
993           LOG (GNUNET_ERROR_TYPE_ERROR,
994                _("Attempt to cancel dead task %llu!\n"),
995                (unsigned long long) task);
996           GNUNET_assert (0);
997         }
998       prev = NULL;
999       t = ready[p];
1000       while (t != NULL)
1001         {
1002           if (t->id == task)
1003             {
1004               ready_count--;
1005               break;
1006             }
1007           prev = t;
1008           t = t->next;
1009         }
1010     }
1011   if (prev == NULL)
1012     {
1013       if (p == 0)
1014         {
1015           if (to == 0)
1016             {
1017               pending = t->next;
1018             }
1019           else
1020             {
1021               pending_timeout = t->next;
1022             }
1023         }
1024       else
1025         {
1026           ready[p] = t->next;
1027         }
1028     }
1029   else
1030     {
1031       prev->next = t->next;
1032     }
1033   ret = t->callback_cls;
1034 #if DEBUG_TASKS
1035   LOG (GNUNET_ERROR_TYPE_DEBUG,
1036        "Canceling task: %llu / %p\n", task, t->callback_cls);
1037 #endif
1038   destroy_task (t);
1039   return ret;
1040 }
1041
1042
1043 /**
1044  * Continue the current execution with the given function.  This is
1045  * similar to the other "add" functions except that there is no delay
1046  * and the reason code can be specified.
1047  *
1048  * @param task main function of the task
1049  * @param task_cls closure for 'main'
1050  * @param reason reason for task invocation
1051  */
1052 void
1053 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1054                                    enum GNUNET_SCHEDULER_Reason reason)
1055 {
1056   struct Task *t;
1057
1058 #if EXECINFO
1059   void *backtrace_array[50];
1060 #endif
1061
1062   GNUNET_assert (NULL != task);
1063   GNUNET_assert ((active_task != NULL) ||
1064                  (reason == GNUNET_SCHEDULER_REASON_STARTUP));
1065   t = GNUNET_malloc (sizeof (struct Task));
1066 #if EXECINFO
1067   t->num_backtrace_strings = backtrace (backtrace_array, 50);
1068   t->backtrace_strings =
1069     backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1070 #endif
1071   t->read_fd = -1;
1072   t->write_fd = -1;
1073   t->callback = task;
1074   t->callback_cls = task_cls;
1075   t->id = ++last_id;
1076 #if PROFILE_DELAYS
1077   t->start_time = GNUNET_TIME_absolute_get ();
1078 #endif
1079   t->reason = reason;
1080   t->priority = current_priority;
1081   t->lifeness = current_lifeness;
1082 #if DEBUG_TASKS
1083   LOG (GNUNET_ERROR_TYPE_DEBUG,
1084        "Adding continuation task: %llu / %p\n", t->id, t->callback_cls);
1085 #endif
1086   queue_ready_task (t);
1087 }
1088
1089
1090
1091 /**
1092  * Schedule a new task to be run after the specified prerequisite task
1093  * has completed. It will be run with the priority of the calling
1094  * task.
1095  *
1096  * @param prerequisite_task run this task after the task with the given
1097  *        task identifier completes (and any of our other
1098  *        conditions, such as delay, read or write-readiness
1099  *        are satisfied).  Use  GNUNET_SCHEDULER_NO_TASK to not have any dependency
1100  *        on completion of other tasks (this will cause the task to run as
1101  *        soon as possible).
1102  * @param task main function of the task
1103  * @param task_cls closure of task
1104  * @return unique task identifier for the job
1105  *         only valid until "task" is started!
1106  */
1107 GNUNET_SCHEDULER_TaskIdentifier
1108 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1109                             GNUNET_SCHEDULER_Task task, void *task_cls)
1110 {
1111   return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1112                                       prerequisite_task,
1113                                       GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1114                                       task_cls);
1115 }
1116
1117
1118 /**
1119  * Schedule a new task to be run with a specified priority.
1120  *
1121  * @param prio how important is the new task?
1122  * @param task main function of the task
1123  * @param task_cls closure of task
1124  * @return unique task identifier for the job
1125  *         only valid until "task" is started!
1126  */
1127 GNUNET_SCHEDULER_TaskIdentifier
1128 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1129                                     GNUNET_SCHEDULER_Task task,
1130                                     void *task_cls)
1131 {
1132   return GNUNET_SCHEDULER_add_select (prio, GNUNET_SCHEDULER_NO_TASK,
1133                                       GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1134                                       task_cls);
1135 }
1136
1137
1138
1139 /**
1140  * Schedule a new task to be run with a specified delay.  The task
1141  * will be scheduled for execution once the delay has expired. It
1142  * will be run with the priority of the calling task.
1143  *
1144  * @param delay when should this operation time out? Use
1145  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
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_delayed (struct GNUNET_TIME_Relative delay,
1153                               GNUNET_SCHEDULER_Task task, void *task_cls)
1154 {
1155 #if 1
1156   /* new, optimized version */
1157   struct Task *t;
1158   struct Task *pos;
1159   struct Task *prev;
1160
1161 #if EXECINFO
1162   void *backtrace_array[MAX_TRACE_DEPTH];
1163 #endif
1164
1165   GNUNET_assert (active_task != NULL);
1166   GNUNET_assert (NULL != task);
1167   t = GNUNET_malloc (sizeof (struct Task));
1168   t->callback = task;
1169   t->callback_cls = task_cls;
1170 #if EXECINFO
1171   t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1172   t->backtrace_strings =
1173     backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1174 #endif
1175   t->read_fd = -1;
1176   t->write_fd = -1;
1177   t->id = ++last_id;
1178 #if PROFILE_DELAYS
1179   t->start_time = GNUNET_TIME_absolute_get ();
1180 #endif
1181   t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1182   t->priority = current_priority;
1183   t->lifeness = current_lifeness;
1184   /* try tail first (optimization in case we are
1185    * appending to a long list of tasks with timeouts) */
1186   prev = pending_timeout_last;
1187   if (prev != NULL)
1188     {
1189       if (prev->timeout.abs_value > t->timeout.abs_value)
1190         prev = NULL;
1191       else
1192         pos = prev->next;       /* heuristic success! */
1193     }
1194   if (prev == NULL)
1195     {
1196       /* heuristic failed, do traversal of timeout list */
1197       pos = pending_timeout;
1198     }
1199   while ((pos != NULL) &&
1200          ((pos->timeout.abs_value <= t->timeout.abs_value) ||
1201           (pos->reason != 0)))
1202     {
1203       prev = pos;
1204       pos = pos->next;
1205     }
1206   if (prev == NULL)
1207     pending_timeout = t;
1208   else
1209     prev->next = t;
1210   t->next = pos;
1211   /* hyper-optimization... */
1212   pending_timeout_last = t;
1213
1214 #if DEBUG_TASKS
1215   LOG (GNUNET_ERROR_TYPE_DEBUG,
1216        "Adding task: %llu / %p\n", t->id, t->callback_cls);
1217 #endif
1218 #if EXECINFO
1219   int i;
1220
1221   for (i = 0; i < t->num_backtrace_strings; i++)
1222     LOG (GNUNET_ERROR_TYPE_DEBUG,
1223          "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]);
1224 #endif
1225   return t->id;
1226
1227 #else
1228   /* unoptimized version */
1229   return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1230                                       GNUNET_SCHEDULER_NO_TASK, delay, NULL,
1231                                       NULL, task, task_cls);
1232 #endif
1233 }
1234
1235
1236
1237 /**
1238  * Schedule a new task to be run as soon as possible. The task
1239  * will be run with the priority of the calling task.
1240  *
1241  * @param task main function of the task
1242  * @param task_cls closure of task
1243  * @return unique task identifier for the job
1244  *         only valid until "task" is started!
1245  */
1246 GNUNET_SCHEDULER_TaskIdentifier
1247 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1248 {
1249   return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1250                                       GNUNET_SCHEDULER_NO_TASK,
1251                                       GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1252                                       task_cls);
1253 }
1254
1255
1256 /**
1257  * Schedule a new task to be run as soon as possible with the
1258  * (transitive) ignore-shutdown flag either explicitly set or
1259  * explicitly enabled.  This task (and all tasks created from it,
1260  * other than by another call to this function) will either count or
1261  * not count for the 'lifeness' of the process.  This API is only
1262  * useful in a few special cases.
1263  *
1264  * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
1265  * @param task main function of the task
1266  * @param task_cls closure of task
1267  * @return unique task identifier for the job
1268  *         only valid until "task" is started!
1269  */
1270 GNUNET_SCHEDULER_TaskIdentifier
1271 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1272                                         GNUNET_SCHEDULER_Task task,
1273                                         void *task_cls)
1274 {
1275   GNUNET_SCHEDULER_TaskIdentifier ret;
1276
1277   ret =
1278     GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1279                                  GNUNET_SCHEDULER_NO_TASK,
1280                                  GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1281                                  task_cls);
1282   GNUNET_assert (pending->id == ret);
1283   pending->lifeness = lifeness;
1284   return ret;
1285 }
1286
1287
1288
1289
1290 /**
1291  * Schedule a new task to be run with a specified delay or when any of
1292  * the specified file descriptor sets is ready.  The delay can be used
1293  * as a timeout on the socket(s) being ready.  The task will be
1294  * scheduled for execution once either the delay has expired or any of
1295  * the socket operations is ready.  This is the most general
1296  * function of the "add" family.  Note that the "prerequisite_task"
1297  * must be satisfied in addition to any of the other conditions.  In
1298  * other words, the task will be started when
1299  * <code>
1300  * (prerequisite-run)
1301  * && (delay-ready
1302  *     || any-rs-ready
1303  *     || any-ws-ready
1304  *     || shutdown-active )
1305  * </code>
1306  *
1307  * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1308  *        which means that the task will only be run after we receive SIGTERM
1309  * @param rfd file descriptor we want to read (can be -1)
1310  * @param wfd file descriptors we want to write (can be -1)
1311  * @param task main function of the task
1312  * @param task_cls closure of task
1313  * @return unique task identifier for the job
1314  *         only valid until "task" is started!
1315  */
1316 #ifndef MINGW
1317 GNUNET_SCHEDULER_TaskIdentifier
1318 add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1319                   GNUNET_SCHEDULER_Task task, void *task_cls)
1320 {
1321   struct Task *t;
1322
1323 #if EXECINFO
1324   void *backtrace_array[MAX_TRACE_DEPTH];
1325 #endif
1326
1327   GNUNET_assert (active_task != NULL);
1328   GNUNET_assert (NULL != task);
1329   t = GNUNET_malloc (sizeof (struct Task));
1330   t->callback = task;
1331   t->callback_cls = task_cls;
1332 #if EXECINFO
1333   t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1334   t->backtrace_strings =
1335     backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1336 #endif
1337 #if DEBUG_FDS
1338   if (-1 != rfd)
1339     {
1340       int flags = fcntl (rfd, F_GETFD);
1341
1342       if ((flags == -1) && (errno == EBADF))
1343         {
1344           LOG (GNUNET_ERROR_TYPE_ERROR,
1345                "Got invalid file descriptor %d!\n", rfd);
1346 #if EXECINFO
1347           int i;
1348
1349           for (i = 0; i < t->num_backtrace_strings; i++)
1350             LOG (GNUNET_ERROR_TYPE_DEBUG,
1351                  "Trace: %s\n", t->backtrace_strings[i]);
1352 #endif
1353           GNUNET_assert (0);
1354         }
1355     }
1356   if (-1 != wfd)
1357     {
1358       int flags = fcntl (wfd, F_GETFD);
1359
1360       if (flags == -1 && errno == EBADF)
1361         {
1362           LOG (GNUNET_ERROR_TYPE_ERROR,
1363                "Got invalid file descriptor %d!\n", wfd);
1364 #if EXECINFO
1365           int i;
1366
1367           for (i = 0; i < t->num_backtrace_strings; i++)
1368             LOG (GNUNET_ERROR_TYPE_DEBUG,
1369                  "Trace: %s\n", t->backtrace_strings[i]);
1370 #endif
1371           GNUNET_assert (0);
1372         }
1373     }
1374 #endif
1375   t->read_fd = rfd;
1376   GNUNET_assert (wfd >= -1);
1377   t->write_fd = wfd;
1378   t->id = ++last_id;
1379 #if PROFILE_DELAYS
1380   t->start_time = GNUNET_TIME_absolute_get ();
1381 #endif
1382   t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1383   t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1384   t->priority = check_priority (current_priority);
1385   t->lifeness = current_lifeness;
1386   t->next = pending;
1387   pending = t;
1388   max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1389 #if DEBUG_TASKS
1390   LOG (GNUNET_ERROR_TYPE_DEBUG,
1391        "Adding task: %llu / %p\n", t->id, t->callback_cls);
1392 #endif
1393 #if EXECINFO
1394   int i;
1395
1396   for (i = 0; i < t->num_backtrace_strings; i++)
1397     LOG (GNUNET_ERROR_TYPE_DEBUG,
1398          "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]);
1399 #endif
1400   return t->id;
1401 }
1402 #endif
1403
1404
1405
1406 /**
1407  * Schedule a new task to be run with a specified delay or when the
1408  * specified file descriptor is ready for reading.  The delay can be
1409  * used as a timeout on the socket being ready.  The task will be
1410  * scheduled for execution once either the delay has expired or the
1411  * socket operation is ready.  It will be run with the priority of
1412  * the calling task.
1413  *
1414  * @param delay when should this operation time out? Use
1415  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1416  * @param rfd read file-descriptor
1417  * @param task main function of the task
1418  * @param task_cls closure of task
1419  * @return unique task identifier for the job
1420  *         only valid until "task" is started!
1421  */
1422 GNUNET_SCHEDULER_TaskIdentifier
1423 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1424                                struct GNUNET_NETWORK_Handle * rfd,
1425                                GNUNET_SCHEDULER_Task task, void *task_cls)
1426 {
1427 #if MINGW
1428   struct GNUNET_NETWORK_FDSet *rs;
1429   GNUNET_SCHEDULER_TaskIdentifier ret;
1430
1431   GNUNET_assert (rfd != NULL);
1432   rs = GNUNET_NETWORK_fdset_create ();
1433   GNUNET_NETWORK_fdset_set (rs, rfd);
1434   ret =
1435     GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1436                                  GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1437                                  task, task_cls);
1438   GNUNET_NETWORK_fdset_destroy (rs);
1439   return ret;
1440 #else
1441   return add_without_sets (delay, GNUNET_NETWORK_get_fd (rfd), -1, task,
1442                            task_cls);
1443 #endif
1444 }
1445
1446
1447 /**
1448  * Schedule a new task to be run with a specified delay or when the
1449  * specified file descriptor is ready for writing.  The delay can be
1450  * used as a timeout on the socket being ready.  The task will be
1451  * scheduled for execution once either the delay has expired or the
1452  * socket operation is ready.  It will be run with the priority of
1453  * the calling task.
1454  *
1455  * @param delay when should this operation time out? Use
1456  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1457  * @param wfd write file-descriptor
1458  * @param task main function of the task
1459  * @param task_cls closure of task
1460  * @return unique task identifier for the job
1461  *         only valid until "task" is started!
1462  */
1463 GNUNET_SCHEDULER_TaskIdentifier
1464 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1465                                 struct GNUNET_NETWORK_Handle * wfd,
1466                                 GNUNET_SCHEDULER_Task task, void *task_cls)
1467 {
1468 #if MINGW
1469   struct GNUNET_NETWORK_FDSet *ws;
1470   GNUNET_SCHEDULER_TaskIdentifier ret;
1471
1472   GNUNET_assert (wfd != NULL);
1473   ws = GNUNET_NETWORK_fdset_create ();
1474   GNUNET_NETWORK_fdset_set (ws, wfd);
1475   ret =
1476     GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1477                                  GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1478                                  task, task_cls);
1479   GNUNET_NETWORK_fdset_destroy (ws);
1480   return ret;
1481 #else
1482   GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0);
1483   return add_without_sets (delay, -1, GNUNET_NETWORK_get_fd (wfd), task,
1484                            task_cls);
1485 #endif
1486 }
1487
1488
1489 /**
1490  * Schedule a new task to be run with a specified delay or when the
1491  * specified file descriptor is ready for reading.  The delay can be
1492  * used as a timeout on the socket being ready.  The task will be
1493  * scheduled for execution once either the delay has expired or the
1494  * socket operation is ready. It will be run with the priority of
1495  * the calling task.
1496  *
1497  * @param delay when should this operation time out? Use
1498  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1499  * @param rfd read file-descriptor
1500  * @param task main function of the task
1501  * @param task_cls closure of task
1502  * @return unique task identifier for the job
1503  *         only valid until "task" is started!
1504  */
1505 GNUNET_SCHEDULER_TaskIdentifier
1506 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1507                                 const struct GNUNET_DISK_FileHandle * rfd,
1508                                 GNUNET_SCHEDULER_Task task, void *task_cls)
1509 {
1510 #if MINGW
1511   struct GNUNET_NETWORK_FDSet *rs;
1512   GNUNET_SCHEDULER_TaskIdentifier ret;
1513
1514   GNUNET_assert (rfd != NULL);
1515   rs = GNUNET_NETWORK_fdset_create ();
1516   GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1517   ret =
1518     GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1519                                  GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1520                                  task, task_cls);
1521   GNUNET_NETWORK_fdset_destroy (rs);
1522   return ret;
1523 #else
1524   int fd;
1525
1526   GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1527   return add_without_sets (delay, fd, -1, task, task_cls);
1528
1529 #endif
1530 }
1531
1532
1533 /**
1534  * Schedule a new task to be run with a specified delay or when the
1535  * specified file descriptor is ready for writing.  The delay can be
1536  * used as a timeout on the socket being ready.  The task will be
1537  * scheduled for execution once either the delay has expired or the
1538  * socket operation is ready. It will be run with the priority of
1539  * the calling task.
1540  *
1541  * @param delay when should this operation time out? Use
1542  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1543  * @param wfd write file-descriptor
1544  * @param task main function of the task
1545  * @param task_cls closure of task
1546  * @return unique task identifier for the job
1547  *         only valid until "task" is started!
1548  */
1549 GNUNET_SCHEDULER_TaskIdentifier
1550 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1551                                  const struct GNUNET_DISK_FileHandle * wfd,
1552                                  GNUNET_SCHEDULER_Task task, void *task_cls)
1553 {
1554 #if MINGW
1555   struct GNUNET_NETWORK_FDSet *ws;
1556   GNUNET_SCHEDULER_TaskIdentifier ret;
1557
1558   GNUNET_assert (wfd != NULL);
1559   ws = GNUNET_NETWORK_fdset_create ();
1560   GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1561   ret =
1562     GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1563                                  GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1564                                  task, task_cls);
1565   GNUNET_NETWORK_fdset_destroy (ws);
1566   return ret;
1567 #else
1568   int fd;
1569
1570   GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1571   GNUNET_assert (fd >= 0);
1572   return add_without_sets (delay, -1, fd, task, task_cls);
1573
1574 #endif
1575 }
1576
1577
1578
1579 /**
1580  * Schedule a new task to be run with a specified delay or when any of
1581  * the specified file descriptor sets is ready.  The delay can be used
1582  * as a timeout on the socket(s) being ready.  The task will be
1583  * scheduled for execution once either the delay has expired or any of
1584  * the socket operations is ready.  This is the most general
1585  * function of the "add" family.  Note that the "prerequisite_task"
1586  * must be satisfied in addition to any of the other conditions.  In
1587  * other words, the task will be started when
1588  * <code>
1589  * (prerequisite-run)
1590  * && (delay-ready
1591  *     || any-rs-ready
1592  *     || any-ws-ready
1593  *     || (shutdown-active && run-on-shutdown) )
1594  * </code>
1595  *
1596  * @param prio how important is this task?
1597  * @param prerequisite_task run this task after the task with the given
1598  *        task identifier completes (and any of our other
1599  *        conditions, such as delay, read or write-readiness
1600  *        are satisfied).  Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1601  *        on completion of other tasks.
1602  * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1603  *        which means that the task will only be run after we receive SIGTERM
1604  * @param rs set of file descriptors we want to read (can be NULL)
1605  * @param ws set of file descriptors we want to write (can be NULL)
1606  * @param task main function of the task
1607  * @param task_cls closure of task
1608  * @return unique task identifier for the job
1609  *         only valid until "task" is started!
1610  */
1611 GNUNET_SCHEDULER_TaskIdentifier
1612 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1613                              GNUNET_SCHEDULER_TaskIdentifier
1614                              prerequisite_task,
1615                              struct GNUNET_TIME_Relative delay,
1616                              const struct GNUNET_NETWORK_FDSet * rs,
1617                              const struct GNUNET_NETWORK_FDSet * ws,
1618                              GNUNET_SCHEDULER_Task task, void *task_cls)
1619 {
1620   struct Task *t;
1621
1622 #if EXECINFO
1623   void *backtrace_array[MAX_TRACE_DEPTH];
1624 #endif
1625
1626   GNUNET_assert (active_task != NULL);
1627   GNUNET_assert (NULL != task);
1628   t = GNUNET_malloc (sizeof (struct Task));
1629   t->callback = task;
1630   t->callback_cls = task_cls;
1631 #if EXECINFO
1632   t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1633   t->backtrace_strings =
1634     backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1635 #endif
1636   t->read_fd = -1;
1637   t->write_fd = -1;
1638   if (rs != NULL)
1639     {
1640       t->read_set = GNUNET_NETWORK_fdset_create ();
1641       GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1642     }
1643   if (ws != NULL)
1644     {
1645       t->write_set = GNUNET_NETWORK_fdset_create ();
1646       GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1647     }
1648   t->id = ++last_id;
1649 #if PROFILE_DELAYS
1650   t->start_time = GNUNET_TIME_absolute_get ();
1651 #endif
1652   t->prereq_id = prerequisite_task;
1653   t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1654   t->priority =
1655     check_priority ((prio ==
1656                      GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1657                     prio);
1658   t->lifeness = current_lifeness;
1659   t->next = pending;
1660   pending = t;
1661   max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1662 #if DEBUG_TASKS
1663   LOG (GNUNET_ERROR_TYPE_DEBUG,
1664        "Adding task: %llu / %p\n", t->id, t->callback_cls);
1665 #endif
1666 #if EXECINFO
1667   int i;
1668
1669   for (i = 0; i < t->num_backtrace_strings; i++)
1670     LOG (GNUNET_ERROR_TYPE_DEBUG,
1671          "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]);
1672 #endif
1673   return t->id;
1674 }
1675
1676 /* end of scheduler.c */