aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/scheduler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/scheduler.c')
-rw-r--r--src/lib/util/scheduler.c2564
1 files changed, 2564 insertions, 0 deletions
diff --git a/src/lib/util/scheduler.c b/src/lib/util/scheduler.c
new file mode 100644
index 000000000..70193a0d2
--- /dev/null
+++ b/src/lib/util/scheduler.c
@@ -0,0 +1,2564 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2009-2017, 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file util/scheduler.c
22 * @brief schedule computations using continuation passing style
23 * @author Christian Grothoff
24 */
25
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "disk.h"
29// DEBUG
30#include <inttypes.h>
31
32#define LOG(kind, ...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
33
34#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
35 "util-scheduler", \
36 syscall)
37
38
39#if HAVE_EXECINFO_H
40#include "execinfo.h"
41
42/**
43 * Use lsof to generate file descriptor reports on select error?
44 * (turn off for stable releases).
45 */
46#define USE_LSOF GNUNET_NO
47
48/**
49 * Obtain trace information for all scheduler calls that schedule tasks.
50 */
51#define EXECINFO GNUNET_NO
52
53/**
54 * Check each file descriptor before adding
55 */
56#define DEBUG_FDS GNUNET_NO
57
58/**
59 * Depth of the traces collected via EXECINFO.
60 */
61#define MAX_TRACE_DEPTH 50
62#endif
63
64/**
65 * Should we figure out which tasks are delayed for a while
66 * before they are run? (Consider using in combination with EXECINFO).
67 */
68#define PROFILE_DELAYS GNUNET_NO
69
70/**
71 * Task that were in the queue for longer than this are reported if
72 * PROFILE_DELAYS is active.
73 */
74#define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
75
76
77/**
78 * Argument to be passed from the driver to
79 * #GNUNET_SCHEDULER_do_work(). Contains the
80 * scheduler's internal state.
81 */
82struct GNUNET_SCHEDULER_Handle
83{
84 /**
85 * Passed here to avoid constantly allocating/deallocating
86 * this element, but generally we want to get rid of this.
87 * @deprecated
88 */
89 struct GNUNET_NETWORK_FDSet *rs;
90
91 /**
92 * Passed here to avoid constantly allocating/deallocating
93 * this element, but generally we want to get rid of this.
94 * @deprecated
95 */
96 struct GNUNET_NETWORK_FDSet *ws;
97
98 /**
99 * context of the SIGINT handler
100 */
101 struct GNUNET_SIGNAL_Context *shc_int;
102
103 /**
104 * context of the SIGTERM handler
105 */
106 struct GNUNET_SIGNAL_Context *shc_term;
107
108#if (SIGTERM != GNUNET_TERM_SIG)
109 /**
110 * context of the TERM_SIG handler
111 */
112 struct GNUNET_SIGNAL_Context *shc_gterm;
113#endif
114
115 /**
116 * context of the SIGQUIT handler
117 */
118 struct GNUNET_SIGNAL_Context *shc_quit;
119
120 /**
121 * context of the SIGHUP handler
122 */
123 struct GNUNET_SIGNAL_Context *shc_hup;
124
125 /**
126 * context of the SIGPIPE handler
127 */
128 struct GNUNET_SIGNAL_Context *shc_pipe;
129};
130
131
132/**
133 * Entry in list of pending tasks.
134 */
135struct GNUNET_SCHEDULER_Task
136{
137 /**
138 * This is a linked list.
139 */
140 struct GNUNET_SCHEDULER_Task *next;
141
142 /**
143 * This is a linked list.
144 */
145 struct GNUNET_SCHEDULER_Task *prev;
146
147 /**
148 * Function to run when ready.
149 */
150 GNUNET_SCHEDULER_TaskCallback callback;
151
152 /**
153 * Closure for the @e callback.
154 */
155 void *callback_cls;
156
157 /**
158 * Information about which FDs are ready for this task (and why).
159 */
160 struct GNUNET_SCHEDULER_FdInfo *fds;
161
162 /**
163 * Storage location used for @e fds if we want to avoid
164 * a separate malloc() call in the common case that this
165 * task is only about a single FD.
166 */
167 struct GNUNET_SCHEDULER_FdInfo fdx;
168
169 /**
170 * Size of the @e fds array.
171 */
172 unsigned int fds_len;
173
174 /**
175 * Do we own the network and file handles referenced by the FdInfo
176 * structs in the fds array. This will only be GNUNET_YES if the
177 * task was created by the #GNUNET_SCHEDULER_add_select function.
178 */
179 int own_handles;
180
181 /**
182 * Absolute timeout value for the task, or
183 * #GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
184 */
185 struct GNUNET_TIME_Absolute timeout;
186
187#if PROFILE_DELAYS
188 /**
189 * When was the task scheduled?
190 */
191 struct GNUNET_TIME_Absolute start_time;
192#endif
193
194 /**
195 * Why is the task ready? Set after task is added to ready queue.
196 * Initially set to zero. All reasons that have already been
197 * satisfied (e.g. read or write ready) will be set over time.
198 */
199 enum GNUNET_SCHEDULER_Reason reason;
200
201 /**
202 * Task priority.
203 */
204 enum GNUNET_SCHEDULER_Priority priority;
205
206 /**
207 * Set if we only wait for reading from a single FD, otherwise -1.
208 */
209 int read_fd;
210
211 /**
212 * Set if we only wait for writing to a single FD, otherwise -1.
213 */
214 int write_fd;
215
216 /**
217 * Should the existence of this task in the queue be counted as
218 * reason to not shutdown the scheduler?
219 */
220 int lifeness;
221
222 /**
223 * Is this task run on shutdown?
224 */
225 int on_shutdown;
226
227 /**
228 * Is this task in the ready list?
229 */
230 int in_ready_list;
231
232#if EXECINFO
233 /**
234 * Array of strings which make up a backtrace from the point when this
235 * task was scheduled (essentially, who scheduled the task?)
236 */
237 char **backtrace_strings;
238
239 /**
240 * Size of the backtrace_strings array
241 */
242 int num_backtrace_strings;
243#endif
244
245 /**
246 * Asynchronous scope of the task that scheduled this scope,
247 */
248 struct GNUNET_AsyncScopeSave scope;
249};
250
251/**
252 * Placed at the end of a ready queue to indicate where a scheduler run pass
253 * ends. The next, prev, in_ready_list and priority fields are the only ones
254 * that should be used.
255 */
256static struct GNUNET_SCHEDULER_Task pass_end_marker;
257
258
259/**
260 * A struct representing an event the select driver is waiting for
261 */
262struct Scheduled
263{
264 struct Scheduled *prev;
265
266 struct Scheduled *next;
267
268 /**
269 * the task, the event is related to
270 */
271 struct GNUNET_SCHEDULER_Task *task;
272
273 /**
274 * information about the network socket / file descriptor where
275 * the event is expected to occur
276 */
277 struct GNUNET_SCHEDULER_FdInfo *fdi;
278
279 /**
280 * the event types (multiple event types can be ORed) the select
281 * driver is expected to wait for
282 */
283 enum GNUNET_SCHEDULER_EventType et;
284};
285
286
287/**
288 * Driver context used by GNUNET_SCHEDULER_run
289 */
290struct DriverContext
291{
292 /**
293 * the head of a DLL containing information about the events the
294 * select driver is waiting for
295 */
296 struct Scheduled *scheduled_head;
297
298 /**
299 * the tail of a DLL containing information about the events the
300 * select driver is waiting for
301 */
302 struct Scheduled *scheduled_tail;
303
304 /**
305 * the time when the select driver will wake up again (after
306 * calling select)
307 */
308 struct GNUNET_TIME_Absolute timeout;
309};
310
311
312/**
313 * The driver used for the event loop. Will be handed over to
314 * the scheduler in #GNUNET_SCHEDULER_do_work(), persisted
315 * there in this variable for later use in functions like
316 * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
317 * #GNUNET_SCHEDULER_cancel().
318 */
319static const struct GNUNET_SCHEDULER_Driver *scheduler_driver;
320
321/**
322 * Head of list of tasks waiting for an event.
323 */
324static struct GNUNET_SCHEDULER_Task *pending_head;
325
326/**
327 * Tail of list of tasks waiting for an event.
328 */
329static struct GNUNET_SCHEDULER_Task *pending_tail;
330
331/**
332 * Head of list of tasks waiting for shutdown.
333 */
334static struct GNUNET_SCHEDULER_Task *shutdown_head;
335
336/**
337 * Tail of list of tasks waiting for shutdown.
338 */
339static struct GNUNET_SCHEDULER_Task *shutdown_tail;
340
341/**
342 * List of tasks waiting ONLY for a timeout event.
343 * Sorted by timeout (earliest first). Used so that
344 * we do not traverse the list of these tasks when
345 * building select sets (we just look at the head
346 * to determine the respective timeout ONCE).
347 */
348static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
349
350/**
351 * List of tasks waiting ONLY for a timeout event.
352 * Sorted by timeout (earliest first). Used so that
353 * we do not traverse the list of these tasks when
354 * building select sets (we just look at the head
355 * to determine the respective timeout ONCE).
356 */
357static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
358
359/**
360 * Last inserted task waiting ONLY for a timeout event.
361 * Used to (heuristically) speed up insertion.
362 */
363static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
364
365/**
366 * ID of the task that is running right now.
367 */
368static struct GNUNET_SCHEDULER_Task *active_task;
369
370/**
371 * Head of list of tasks ready to run right now, grouped by importance.
372 */
373static struct
374GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
375
376/**
377 * Tail of list of tasks ready to run right now, grouped by importance.
378 */
379static struct
380GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
381
382/**
383 * Task for installing parent control handlers (it might happen that the
384 * scheduler is shutdown before this task is executed, so
385 * GNUNET_SCHEDULER_shutdown must cancel it in that case)
386 */
387static struct GNUNET_SCHEDULER_Task *install_parent_control_task;
388
389/**
390 * Task for reading from a pipe that signal handlers will use to initiate
391 * shutdown
392 */
393static struct GNUNET_SCHEDULER_Task *shutdown_pipe_task;
394
395/**
396 * Number of tasks on the ready list.
397 */
398static unsigned int ready_count;
399
400/**
401 * Priority of the task running right now. Only
402 * valid while a task is running.
403 */
404static enum GNUNET_SCHEDULER_Priority current_priority;
405
406/**
407 * Priority of the highest task added in the current select
408 * iteration.
409 */
410static enum GNUNET_SCHEDULER_Priority max_priority_added;
411
412/**
413 * Value of the 'lifeness' flag for the current task.
414 */
415static int current_lifeness;
416
417/**
418 * Priority used currently in #GNUNET_SCHEDULER_do_work().
419 */
420static enum GNUNET_SCHEDULER_Priority work_priority;
421
422/**
423 * Function to use as a select() in the scheduler.
424 * If NULL, we use GNUNET_NETWORK_socket_select().
425 */
426static GNUNET_SCHEDULER_select scheduler_select;
427
428/**
429 * Task context of the current task.
430 */
431static struct GNUNET_SCHEDULER_TaskContext tc;
432
433/**
434 * Closure for #scheduler_select.
435 */
436static void *scheduler_select_cls;
437
438
439/**
440 * Sets the select function to use in the scheduler (scheduler_select).
441 *
442 * @param new_select new select function to use
443 * @param new_select_cls closure for @a new_select
444 * @return previously used select function, NULL for default
445 */
446void
447GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
448 void *new_select_cls)
449{
450 scheduler_select = new_select;
451 scheduler_select_cls = new_select_cls;
452}
453
454
455/**
456 * Check that the given priority is legal (and return it).
457 *
458 * @param p priority value to check
459 * @return p on success, 0 on error
460 */
461static enum GNUNET_SCHEDULER_Priority
462check_priority (enum GNUNET_SCHEDULER_Priority p)
463{
464 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
465 return p;
466 GNUNET_assert (0);
467 return 0; /* make compiler happy */
468}
469
470
471/**
472 * chooses the nearest timeout from all pending tasks, to be used
473 * to tell the driver the next wakeup time (using its set_wakeup
474 * callback)
475 */
476struct GNUNET_TIME_Absolute
477get_timeout ()
478{
479 struct GNUNET_SCHEDULER_Task *pos;
480 struct GNUNET_TIME_Absolute now;
481 struct GNUNET_TIME_Absolute timeout;
482
483 pos = pending_timeout_head;
484 now = GNUNET_TIME_absolute_get ();
485 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
486 if (NULL != pos)
487 {
488 if (0 != pos->reason)
489 {
490 return now;
491 }
492 else
493 {
494 timeout = pos->timeout;
495 }
496 }
497 for (pos = pending_head; NULL != pos; pos = pos->next)
498 {
499 if (0 != pos->reason)
500 {
501 return now;
502 }
503 else if ((pos->timeout.abs_value_us !=
504 GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
505 (timeout.abs_value_us > pos->timeout.abs_value_us))
506 {
507 timeout = pos->timeout;
508 }
509 }
510 return timeout;
511}
512
513static void remove_pass_end_marker ()
514{
515 if (pass_end_marker.in_ready_list)
516 {
517 GNUNET_CONTAINER_DLL_remove (ready_head[pass_end_marker.priority],
518 ready_tail[pass_end_marker.priority],
519 &pass_end_marker);
520 pass_end_marker.in_ready_list = GNUNET_NO;
521 }
522}
523
524static void set_work_priority (enum GNUNET_SCHEDULER_Priority p)
525{
526 remove_pass_end_marker ();
527 GNUNET_CONTAINER_DLL_insert_tail (ready_head[p],
528 ready_tail[p],
529 &pass_end_marker);
530 pass_end_marker.priority = p;
531 pass_end_marker.in_ready_list = GNUNET_YES;
532 work_priority = p;
533}
534
535/**
536 * Put a task that is ready for execution into the ready queue.
537 *
538 * @param task task ready for execution
539 */
540static void
541queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
542{
543 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
544
545 GNUNET_CONTAINER_DLL_insert_tail (ready_head[p],
546 ready_tail[p],
547 task);
548 if (p > work_priority)
549 set_work_priority (p);
550 task->in_ready_list = GNUNET_YES;
551 ready_count++;
552}
553
554
555/**
556 * Request the shutdown of a scheduler. Marks all tasks
557 * awaiting shutdown as ready. Note that tasks
558 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
559 * will be delayed until the next shutdown signal.
560 */
561void
562GNUNET_SCHEDULER_shutdown ()
563{
564 struct GNUNET_SCHEDULER_Task *pos;
565
566 LOG (GNUNET_ERROR_TYPE_DEBUG,
567 "GNUNET_SCHEDULER_shutdown\n");
568 if (NULL != install_parent_control_task)
569 {
570 GNUNET_SCHEDULER_cancel (install_parent_control_task);
571 install_parent_control_task = NULL;
572 }
573 if (NULL != shutdown_pipe_task)
574 {
575 GNUNET_SCHEDULER_cancel (shutdown_pipe_task);
576 shutdown_pipe_task = NULL;
577 }
578 while (NULL != (pos = shutdown_head))
579 {
580 GNUNET_CONTAINER_DLL_remove (shutdown_head,
581 shutdown_tail,
582 pos);
583 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
584 queue_ready_task (pos);
585 }
586}
587
588
589/**
590 * Output stack trace of task @a t.
591 *
592 * @param t task to dump stack trace of
593 */
594static void
595dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
596{
597#if EXECINFO
598 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
599 LOG (GNUNET_ERROR_TYPE_WARNING,
600 "Task %p trace %u: %s\n",
601 t,
602 i,
603 t->backtrace_strings[i]);
604#else
605 (void) t;
606#endif
607}
608
609
610/**
611 * Destroy a task (release associated resources)
612 *
613 * @param t task to destroy
614 */
615static void
616destroy_task (struct GNUNET_SCHEDULER_Task *t)
617{
618 LOG (GNUNET_ERROR_TYPE_DEBUG,
619 "destroying task %p\n",
620 t);
621
622 if (GNUNET_YES == t->own_handles)
623 {
624 for (unsigned int i = 0; i != t->fds_len; ++i)
625 {
626 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
627 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
628 if (fd)
629 {
630 GNUNET_NETWORK_socket_free_memory_only_ (
631 (struct GNUNET_NETWORK_Handle *) fd);
632 }
633 if (fh)
634 {
635 // FIXME: on WIN32 this is not enough! A function
636 // GNUNET_DISK_file_free_memory_only would be nice
637 GNUNET_free_nz ((void *) fh);
638 }
639 }
640 }
641 if (t->fds_len > 1)
642 {
643 GNUNET_array_grow (t->fds, t->fds_len, 0);
644 }
645#if EXECINFO
646 GNUNET_free (t->backtrace_strings);
647#endif
648 GNUNET_free (t);
649}
650
651
652/**
653 * Pipe used to communicate shutdown via signal.
654 */
655static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
656
657/**
658 * Process ID of this process at the time we installed the various
659 * signal handlers.
660 */
661static pid_t my_pid;
662
663/**
664 * Signal handler called for SIGPIPE.
665 */
666static void
667sighandler_pipe ()
668{
669 return;
670}
671
672
673/**
674 * Signal handler called for signals that should cause us to shutdown.
675 */
676static void
677sighandler_shutdown (void)
678{
679 static char c;
680 int old_errno = errno; /* backup errno */
681
682 if (getpid () != my_pid)
683 _exit (1); /* we have fork'ed since the signal handler was created,
684 * ignore the signal, see https://gnunet.org/vfork discussion */
685 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (
686 shutdown_pipe_handle,
687 GNUNET_DISK_PIPE_END_WRITE),
688 &c, sizeof(c));
689 errno = old_errno;
690}
691
692
693static void
694shutdown_if_no_lifeness (void)
695{
696 struct GNUNET_SCHEDULER_Task *t;
697
698 if (ready_count > 0)
699 return;
700 for (t = pending_head; NULL != t; t = t->next)
701 if (GNUNET_YES == t->lifeness)
702 return;
703 for (t = shutdown_head; NULL != t; t = t->next)
704 if (GNUNET_YES == t->lifeness)
705 return;
706 for (t = pending_timeout_head; NULL != t; t = t->next)
707 if (GNUNET_YES == t->lifeness)
708 return;
709 /* No lifeness! */
710 GNUNET_SCHEDULER_shutdown ();
711}
712
713
714static int
715select_loop (struct GNUNET_SCHEDULER_Handle *sh,
716 struct DriverContext *context);
717
718
719void
720GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
721 void *task_cls)
722{
723 struct GNUNET_SCHEDULER_Handle *sh;
724 struct GNUNET_SCHEDULER_Driver *driver;
725 struct DriverContext context = {
726 .scheduled_head = NULL,
727 .scheduled_tail = NULL,
728 .timeout = GNUNET_TIME_absolute_get ()
729 };
730
731 driver = GNUNET_SCHEDULER_driver_select ();
732 driver->cls = &context;
733 sh = GNUNET_SCHEDULER_driver_init (driver);
734 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
735 task_cls,
736 GNUNET_SCHEDULER_REASON_STARTUP,
737 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
738 select_loop (sh,
739 &context);
740 GNUNET_SCHEDULER_driver_done (sh);
741 GNUNET_free (driver);
742}
743
744
745/**
746 * Obtain the task context, giving the reason why the current task was
747 * started.
748 *
749 * @return current tasks' scheduler context
750 */
751const struct GNUNET_SCHEDULER_TaskContext *
752GNUNET_SCHEDULER_get_task_context ()
753{
754 GNUNET_assert (NULL != active_task);
755 return &tc;
756}
757
758
759/**
760 * Get information about the current load of this scheduler. Use this
761 * function to determine if an elective task should be added or simply
762 * dropped (if the decision should be made based on the number of
763 * tasks ready to run).
764 *
765 * @param p priority level to look at
766 * @return number of tasks pending right now
767 */
768unsigned int
769GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
770{
771 unsigned int ret;
772
773 GNUNET_assert (NULL != active_task);
774 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
775 return ready_count;
776 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
777 p = current_priority;
778 ret = 0;
779 for (struct GNUNET_SCHEDULER_Task *pos = ready_head[check_priority (p)];
780 NULL != pos;
781 pos = pos->next)
782 ret++;
783 if (pass_end_marker.in_ready_list && pass_end_marker.priority == p)
784 // Don't count the dummy marker
785 ret--;
786 return ret;
787}
788
789
790void
791init_fd_info (struct GNUNET_SCHEDULER_Task *t,
792 const struct GNUNET_NETWORK_Handle *const *read_nh,
793 unsigned int read_nh_len,
794 const struct GNUNET_NETWORK_Handle *const *write_nh,
795 unsigned int write_nh_len,
796 const struct GNUNET_DISK_FileHandle *const *read_fh,
797 unsigned int read_fh_len,
798 const struct GNUNET_DISK_FileHandle *const *write_fh,
799 unsigned int write_fh_len)
800{
801 // FIXME: if we have exactly two network handles / exactly two file handles
802 // and they are equal, we can make one FdInfo with both
803 // GNUNET_SCHEDULER_ET_IN and GNUNET_SCHEDULER_ET_OUT set.
804 struct GNUNET_SCHEDULER_FdInfo *fdi;
805
806 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
807 if (1 == t->fds_len)
808 {
809 fdi = &t->fdx;
810 t->fds = fdi;
811 if (1 == read_nh_len)
812 {
813 GNUNET_assert (NULL != read_nh);
814 GNUNET_assert (NULL != *read_nh);
815 fdi->fd = *read_nh;
816 fdi->et = GNUNET_SCHEDULER_ET_IN;
817 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
818 t->read_fd = fdi->sock;
819 t->write_fd = -1;
820 }
821 else if (1 == write_nh_len)
822 {
823 GNUNET_assert (NULL != write_nh);
824 GNUNET_assert (NULL != *write_nh);
825 fdi->fd = *write_nh;
826 fdi->et = GNUNET_SCHEDULER_ET_OUT;
827 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
828 t->read_fd = -1;
829 t->write_fd = fdi->sock;
830 }
831 else if (1 == read_fh_len)
832 {
833 GNUNET_assert (NULL != read_fh);
834 GNUNET_assert (NULL != *read_fh);
835 fdi->fh = *read_fh;
836 fdi->et = GNUNET_SCHEDULER_ET_IN;
837 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
838 t->read_fd = fdi->sock;
839 t->write_fd = -1;
840 }
841 else
842 {
843 GNUNET_assert (NULL != write_fh);
844 GNUNET_assert (NULL != *write_fh);
845 fdi->fh = *write_fh;
846 fdi->et = GNUNET_SCHEDULER_ET_OUT;
847 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
848 t->read_fd = -1;
849 t->write_fd = fdi->sock;
850 }
851 }
852 else
853 {
854 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
855 t->fds = fdi;
856 t->read_fd = -1;
857 t->write_fd = -1;
858 unsigned int i;
859 for (i = 0; i != read_nh_len; ++i)
860 {
861 fdi->fd = read_nh[i];
862 GNUNET_assert (NULL != fdi->fd);
863 fdi->et = GNUNET_SCHEDULER_ET_IN;
864 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
865 ++fdi;
866 }
867 for (i = 0; i != write_nh_len; ++i)
868 {
869 fdi->fd = write_nh[i];
870 GNUNET_assert (NULL != fdi->fd);
871 fdi->et = GNUNET_SCHEDULER_ET_OUT;
872 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
873 ++fdi;
874 }
875 for (i = 0; i != read_fh_len; ++i)
876 {
877 fdi->fh = read_fh[i];
878 GNUNET_assert (NULL != fdi->fh);
879 fdi->et = GNUNET_SCHEDULER_ET_IN;
880 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
881 ++fdi;
882 }
883 for (i = 0; i != write_fh_len; ++i)
884 {
885 fdi->fh = write_fh[i];
886 GNUNET_assert (NULL != fdi->fh);
887 fdi->et = GNUNET_SCHEDULER_ET_OUT;
888 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
889 ++fdi;
890 }
891 }
892}
893
894
895/**
896 * calls the given function @a func on each FdInfo related to @a t.
897 * Optionally updates the event type field in each FdInfo after calling
898 * @a func.
899 *
900 * @param t the task
901 * @param driver_func the function to call with each FdInfo contained in
902 * in @a t
903 * @param if_not_ready only call @a driver_func on FdInfos that are not
904 * ready
905 * @param et the event type to be set in each FdInfo after calling
906 * @a driver_func on it, or -1 if no updating not desired.
907 */
908static void
909driver_add_multiple (struct GNUNET_SCHEDULER_Task *t)
910{
911 struct GNUNET_SCHEDULER_FdInfo *fdi;
912 int success = GNUNET_YES;
913
914 for (unsigned int i = 0; i != t->fds_len; ++i)
915 {
916 fdi = &t->fds[i];
917 success = scheduler_driver->add (scheduler_driver->cls,
918 t,
919 fdi) && success;
920 fdi->et = GNUNET_SCHEDULER_ET_NONE;
921 }
922 if (GNUNET_YES != success)
923 {
924 LOG (GNUNET_ERROR_TYPE_ERROR,
925 "driver could not add task\n");
926 }
927}
928
929
930static void
931install_parent_control_handler (void *cls)
932{
933 (void) cls;
934 install_parent_control_task = NULL;
935 GNUNET_OS_install_parent_control_handler (NULL);
936}
937
938
939static void
940shutdown_pipe_cb (void *cls)
941{
942 char c;
943 const struct GNUNET_DISK_FileHandle *pr;
944
945 (void) cls;
946 shutdown_pipe_task = NULL;
947 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
948 GNUNET_DISK_PIPE_END_READ);
949 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
950 /* consume the signal */
951 GNUNET_DISK_file_read (pr, &c, sizeof(c));
952 /* mark all active tasks as ready due to shutdown */
953 GNUNET_SCHEDULER_shutdown ();
954 shutdown_pipe_task =
955 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
956 pr,
957 &shutdown_pipe_cb,
958 NULL);
959}
960
961
962/**
963 * Cancel the task with the specified identifier.
964 * The task must not yet have run. Only allowed to be called as long as the
965 * scheduler is running, that is one of the following conditions is met:
966 *
967 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
968 * - #GNUNET_SCHEDULER_driver_init has been run and
969 * #GNUNET_SCHEDULER_driver_done has not been called yet
970 *
971 * @param task id of the task to cancel
972 * @return original closure of the task
973 */
974void *
975GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
976{
977 enum GNUNET_SCHEDULER_Priority p;
978 int is_fd_task;
979 void *ret;
980
981 LOG (GNUNET_ERROR_TYPE_DEBUG,
982 "canceling task %p\n",
983 task);
984
985 /* scheduler must be running */
986 GNUNET_assert (NULL != scheduler_driver);
987 is_fd_task = (NULL != task->fds);
988 if (is_fd_task)
989 {
990 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
991 if (GNUNET_OK != del_result)
992 {
993 LOG (GNUNET_ERROR_TYPE_ERROR,
994 "driver could not delete task\n");
995 GNUNET_assert (0);
996 }
997 }
998 if (! task->in_ready_list)
999 {
1000 if (is_fd_task)
1001 {
1002 GNUNET_CONTAINER_DLL_remove (pending_head,
1003 pending_tail,
1004 task);
1005 }
1006 else if (GNUNET_YES == task->on_shutdown)
1007 {
1008 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1009 shutdown_tail,
1010 task);
1011 }
1012 else
1013 {
1014 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1015 pending_timeout_tail,
1016 task);
1017 if (pending_timeout_last == task)
1018 pending_timeout_last = NULL;
1019 }
1020 }
1021 else
1022 {
1023 p = check_priority (task->priority);
1024 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1025 ready_tail[p],
1026 task);
1027 ready_count--;
1028 }
1029 ret = task->callback_cls;
1030 destroy_task (task);
1031 return ret;
1032}
1033
1034
1035/**
1036 * Initialize backtrace data for task @a t
1037 *
1038 * @param t task to initialize
1039 */
1040static void
1041init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1042{
1043#if EXECINFO
1044 void *backtrace_array[MAX_TRACE_DEPTH];
1045
1046 t->num_backtrace_strings
1047 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1048 t->backtrace_strings =
1049 backtrace_symbols (backtrace_array,
1050 t->num_backtrace_strings);
1051 dump_backtrace (t);
1052#else
1053 (void) t;
1054#endif
1055}
1056
1057
1058/**
1059 * Continue the current execution with the given function. This is
1060 * similar to the other "add" functions except that there is no delay
1061 * and the reason code can be specified.
1062 *
1063 * @param task main function of the task
1064 * @param task_cls closure for @a task
1065 * @param reason reason for task invocation
1066 * @param priority priority to use for the task
1067 */
1068void
1069GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback
1070 task,
1071 void *task_cls,
1072 enum GNUNET_SCHEDULER_Reason
1073 reason,
1074 enum GNUNET_SCHEDULER_Priority
1075 priority)
1076{
1077 struct GNUNET_SCHEDULER_Task *t;
1078
1079 /* scheduler must be running */
1080 GNUNET_assert (NULL != scheduler_driver);
1081 GNUNET_assert (NULL != task);
1082 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1083 t->read_fd = -1;
1084 t->write_fd = -1;
1085 t->callback = task;
1086 t->callback_cls = task_cls;
1087#if PROFILE_DELAYS
1088 t->start_time = GNUNET_TIME_absolute_get ();
1089#endif
1090 t->reason = reason;
1091 t->priority = check_priority (priority);
1092 t->lifeness = current_lifeness;
1093 LOG (GNUNET_ERROR_TYPE_DEBUG,
1094 "Adding continuation task %p\n",
1095 t);
1096 init_backtrace (t);
1097 queue_ready_task (t);
1098}
1099
1100
1101/**
1102 * Schedule a new task to be run at the specified time. The task
1103 * will be scheduled for execution at time @a at.
1104 *
1105 * @param at time when the operation should run
1106 * @param priority priority to use for the task
1107 * @param task main function of the task
1108 * @param task_cls closure of @a task
1109 * @return unique task identifier for the job
1110 * only valid until @a task is started!
1111 */
1112struct GNUNET_SCHEDULER_Task *
1113GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1114 enum GNUNET_SCHEDULER_Priority priority,
1115 GNUNET_SCHEDULER_TaskCallback task,
1116 void *task_cls)
1117{
1118 struct GNUNET_SCHEDULER_Task *t;
1119 struct GNUNET_SCHEDULER_Task *pos;
1120 struct GNUNET_SCHEDULER_Task *prev;
1121 struct GNUNET_TIME_Relative left;
1122
1123 /* scheduler must be running */
1124 GNUNET_assert (NULL != scheduler_driver);
1125 GNUNET_assert (NULL != task);
1126 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1127 GNUNET_async_scope_get (&t->scope);
1128 t->callback = task;
1129 t->callback_cls = task_cls;
1130 t->read_fd = -1;
1131 t->write_fd = -1;
1132#if PROFILE_DELAYS
1133 t->start_time = GNUNET_TIME_absolute_get ();
1134#endif
1135 t->timeout = at;
1136 t->priority = check_priority (priority);
1137 t->lifeness = current_lifeness;
1138 init_backtrace (t);
1139
1140 left = GNUNET_TIME_absolute_get_remaining (at);
1141 if (0 == left.rel_value_us)
1142 {
1143 queue_ready_task (t);
1144 if (priority > work_priority)
1145 work_priority = priority;
1146 return t;
1147 }
1148
1149 /* try tail first (optimization in case we are
1150 * appending to a long list of tasks with timeouts) */
1151 if ((NULL == pending_timeout_head) ||
1152 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us))
1153 {
1154 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1155 pending_timeout_tail,
1156 t);
1157 }
1158 else
1159 {
1160 /* first move from heuristic start backwards to before start time */
1161 prev = pending_timeout_last;
1162 while ((NULL != prev) &&
1163 (prev->timeout.abs_value_us > t->timeout.abs_value_us))
1164 prev = prev->prev;
1165 /* now, move from heuristic start (or head of list) forward to insertion point */
1166 if (NULL == prev)
1167 pos = pending_timeout_head;
1168 else
1169 pos = prev->next;
1170 while ((NULL != pos) && (pos->timeout.abs_value_us <=
1171 t->timeout.abs_value_us))
1172 {
1173 prev = pos;
1174 pos = pos->next;
1175 }
1176 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1177 pending_timeout_tail,
1178 prev,
1179 t);
1180 }
1181 /* finally, update heuristic insertion point to last insertion... */
1182 pending_timeout_last = t;
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "Adding task %p\n",
1185 t);
1186 return t;
1187}
1188
1189
1190/**
1191 * Schedule a new task to be run with a specified delay. The task
1192 * will be scheduled for execution once the delay has expired.
1193 *
1194 * @param delay when should this operation time out?
1195 * @param priority priority to use for the task
1196 * @param task main function of the task
1197 * @param task_cls closure of @a task
1198 * @return unique task identifier for the job
1199 * only valid until @a task is started!
1200 */
1201struct GNUNET_SCHEDULER_Task *
1202GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1203 enum GNUNET_SCHEDULER_Priority
1204 priority,
1205 GNUNET_SCHEDULER_TaskCallback task,
1206 void *task_cls)
1207{
1208 return GNUNET_SCHEDULER_add_at_with_priority (
1209 GNUNET_TIME_relative_to_absolute (delay),
1210 priority,
1211 task,
1212 task_cls);
1213}
1214
1215
1216/**
1217 * Schedule a new task to be run with a specified priority.
1218 *
1219 * @param prio how important is the new task?
1220 * @param task main function of the task
1221 * @param task_cls closure of @a task
1222 * @return unique task identifier for the job
1223 * only valid until @a task is started!
1224 */
1225struct GNUNET_SCHEDULER_Task *
1226GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1227 GNUNET_SCHEDULER_TaskCallback task,
1228 void *task_cls)
1229{
1230 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1231 prio,
1232 task,
1233 task_cls);
1234}
1235
1236
1237/**
1238 * Schedule a new task to be run at the specified time. The task
1239 * will be scheduled for execution once specified time has been
1240 * reached. It will be run with the DEFAULT priority.
1241 *
1242 * @param at time at which this operation should run
1243 * @param task main function of the task
1244 * @param task_cls closure of @a task
1245 * @return unique task identifier for the job
1246 * only valid until @a task is started!
1247 */
1248struct GNUNET_SCHEDULER_Task *
1249GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1250 GNUNET_SCHEDULER_TaskCallback task,
1251 void *task_cls)
1252{
1253 return GNUNET_SCHEDULER_add_at_with_priority (at,
1254 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1255 task,
1256 task_cls);
1257}
1258
1259
1260/**
1261 * Schedule a new task to be run with a specified delay. The task
1262 * will be scheduled for execution once the delay has expired. It
1263 * will be run with the DEFAULT priority.
1264 *
1265 * @param delay when should this operation time out?
1266 * @param task main function of the task
1267 * @param task_cls closure of @a task
1268 * @return unique task identifier for the job
1269 * only valid until @a task is started!
1270 */
1271struct GNUNET_SCHEDULER_Task *
1272GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1273 GNUNET_SCHEDULER_TaskCallback task,
1274 void *task_cls)
1275{
1276 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1277 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1278 task,
1279 task_cls);
1280}
1281
1282
1283/**
1284 * Schedule a new task to be run as soon as possible. Note that this
1285 * does not guarantee that this will be the next task that is being
1286 * run, as other tasks with higher priority (or that are already ready
1287 * to run) might get to run first. Just as with delays, clients must
1288 * not rely on any particular order of execution between tasks
1289 * scheduled concurrently.
1290 *
1291 * The task will be run with the DEFAULT priority.
1292 *
1293 * @param task main function of the task
1294 * @param task_cls closure of @a task
1295 * @return unique task identifier for the job
1296 * only valid until @a task is started!
1297 */
1298struct GNUNET_SCHEDULER_Task *
1299GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1300 void *task_cls)
1301{
1302 struct GNUNET_SCHEDULER_Task *t;
1303
1304 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1305 GNUNET_async_scope_get (&t->scope);
1306 t->callback = task;
1307 t->callback_cls = task_cls;
1308 t->read_fd = -1;
1309 t->write_fd = -1;
1310#if PROFILE_DELAYS
1311 t->start_time = GNUNET_TIME_absolute_get ();
1312#endif
1313 t->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
1314 t->priority = current_priority;
1315 t->on_shutdown = GNUNET_YES;
1316 t->lifeness = current_lifeness;
1317 queue_ready_task (t);
1318 init_backtrace (t);
1319 return t;
1320}
1321
1322
1323/**
1324 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1325 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1326 * invoked.
1327 *
1328 * @param task main function of the task
1329 * @param task_cls closure of @a task
1330 * @return unique task identifier for the job
1331 * only valid until @a task is started!
1332 */
1333struct GNUNET_SCHEDULER_Task *
1334GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1335 void *task_cls)
1336{
1337 struct GNUNET_SCHEDULER_Task *t;
1338
1339 /* scheduler must be running */
1340 GNUNET_assert (NULL != scheduler_driver);
1341 GNUNET_assert (NULL != task);
1342 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1343 GNUNET_async_scope_get (&t->scope);
1344 t->callback = task;
1345 t->callback_cls = task_cls;
1346 t->read_fd = -1;
1347 t->write_fd = -1;
1348#if PROFILE_DELAYS
1349 t->start_time = GNUNET_TIME_absolute_get ();
1350#endif
1351 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1352 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1353 t->on_shutdown = GNUNET_YES;
1354 t->lifeness = GNUNET_NO;
1355 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1356 shutdown_tail,
1357 t);
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 "Adding shutdown task %p\n",
1360 t);
1361 init_backtrace (t);
1362 return t;
1363}
1364
1365
1366struct GNUNET_SCHEDULER_Task *
1367GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1368 GNUNET_SCHEDULER_TaskCallback task,
1369 void *task_cls)
1370{
1371 struct GNUNET_SCHEDULER_Task *ret;
1372
1373 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1374 ret->lifeness = lifeness;
1375 return ret;
1376}
1377
1378
1379#if DEBUG_FDS
1380/**
1381 * check a raw file descriptor and abort if it is bad (for debugging purposes)
1382 *
1383 * @param t the task related to the file descriptor
1384 * @param raw_fd the raw file descriptor to check
1385 */
1386void
1387check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1388{
1389 if (-1 != raw_fd)
1390 {
1391 int flags = fcntl (raw_fd, F_GETFD);
1392
1393 if ((flags == -1) && (errno == EBADF))
1394 {
1395 LOG (GNUNET_ERROR_TYPE_ERROR,
1396 "Got invalid file descriptor %d!\n",
1397 raw_fd);
1398 init_backtrace (t);
1399 GNUNET_assert (0);
1400 }
1401 }
1402}
1403
1404
1405#endif
1406
1407
1408/**
1409 * Schedule a new task to be run with a specified delay or when any of
1410 * the specified file descriptor sets is ready. The delay can be used
1411 * as a timeout on the socket(s) being ready. The task will be
1412 * scheduled for execution once either the delay has expired or any of
1413 * the socket operations is ready. This is the most general
1414 * function of the "add" family. Note that the "prerequisite_task"
1415 * must be satisfied in addition to any of the other conditions. In
1416 * other words, the task will be started when
1417 * <code>
1418 * (prerequisite-run)
1419 * && (delay-ready
1420 * || any-rs-ready
1421 * || any-ws-ready)
1422 * </code>
1423 *
1424 * @param delay how long should we wait?
1425 * @param priority priority to use
1426 * @param rfd file descriptor we want to read (can be -1)
1427 * @param wfd file descriptors we want to write (can be -1)
1428 * @param task main function of the task
1429 * @param task_cls closure of @a task
1430 * @return unique task identifier for the job
1431 * only valid until @a task is started!
1432 */
1433static struct GNUNET_SCHEDULER_Task *
1434add_without_sets (struct GNUNET_TIME_Relative delay,
1435 enum GNUNET_SCHEDULER_Priority priority,
1436 const struct GNUNET_NETWORK_Handle *read_nh,
1437 const struct GNUNET_NETWORK_Handle *write_nh,
1438 const struct GNUNET_DISK_FileHandle *read_fh,
1439 const struct GNUNET_DISK_FileHandle *write_fh,
1440 GNUNET_SCHEDULER_TaskCallback task,
1441 void *task_cls)
1442{
1443 struct GNUNET_SCHEDULER_Task *t;
1444
1445 /* scheduler must be running */
1446 GNUNET_assert (NULL != scheduler_driver);
1447 GNUNET_assert (NULL != task);
1448 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1449 GNUNET_async_scope_get (&t->scope);
1450 init_fd_info (t,
1451 &read_nh,
1452 read_nh ? 1 : 0,
1453 &write_nh,
1454 write_nh ? 1 : 0,
1455 &read_fh,
1456 read_fh ? 1 : 0,
1457 &write_fh,
1458 write_fh ? 1 : 0);
1459 t->callback = task;
1460 t->callback_cls = task_cls;
1461#if DEBUG_FDS
1462 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1463 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1464 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1465 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1466#endif
1467#if PROFILE_DELAYS
1468 t->start_time = GNUNET_TIME_absolute_get ();
1469#endif
1470 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1471 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ?
1472 current_priority : priority);
1473 t->lifeness = current_lifeness;
1474 GNUNET_CONTAINER_DLL_insert (pending_head,
1475 pending_tail,
1476 t);
1477 driver_add_multiple (t);
1478 max_priority_added = GNUNET_MAX (max_priority_added,
1479 t->priority);
1480 init_backtrace (t);
1481 return t;
1482}
1483
1484
1485/**
1486 * Schedule a new task to be run with a specified delay or when the
1487 * specified file descriptor is ready for reading. The delay can be
1488 * used as a timeout on the socket being ready. The task will be
1489 * scheduled for execution once either the delay has expired or the
1490 * socket operation is ready. It will be run with the DEFAULT priority.
1491 * Only allowed to be called as long as the scheduler is running, that
1492 * is one of the following conditions is met:
1493 *
1494 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1495 * - #GNUNET_SCHEDULER_driver_init has been run and
1496 * #GNUNET_SCHEDULER_driver_done has not been called yet
1497 *
1498 * @param delay when should this operation time out?
1499 * @param rfd read file-descriptor
1500 * @param task main function of the task
1501 * @param task_cls closure of @a task
1502 * @return unique task identifier for the job
1503 * only valid until @a task is started!
1504 */
1505struct GNUNET_SCHEDULER_Task *
1506GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1507 struct GNUNET_NETWORK_Handle *rfd,
1508 GNUNET_SCHEDULER_TaskCallback task,
1509 void *task_cls)
1510{
1511 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1512 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1513 rfd, task, task_cls);
1514}
1515
1516
1517/**
1518 * Schedule a new task to be run with a specified priority and to be
1519 * run after the specified delay or when the specified file descriptor
1520 * is ready for reading. The delay can be used as a timeout on the
1521 * socket being ready. The task will be scheduled for execution once
1522 * either the delay has expired or the socket operation is ready. It
1523 * will be run with the DEFAULT priority.
1524 * Only allowed to be called as long as the scheduler is running, that
1525 * is one of the following conditions is met:
1526 *
1527 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1528 * - #GNUNET_SCHEDULER_driver_init has been run and
1529 * #GNUNET_SCHEDULER_driver_done has not been called yet
1530 *
1531 * @param delay when should this operation time out?
1532 * @param priority priority to use for the task
1533 * @param rfd read file-descriptor
1534 * @param task main function of the task
1535 * @param task_cls closure of @a task
1536 * @return unique task identifier for the job
1537 * only valid until @a task is started!
1538 */
1539struct GNUNET_SCHEDULER_Task *
1540GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1541 enum GNUNET_SCHEDULER_Priority
1542 priority,
1543 struct GNUNET_NETWORK_Handle *rfd,
1544 GNUNET_SCHEDULER_TaskCallback task,
1545 void *task_cls)
1546{
1547 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1548 rfd,
1549 GNUNET_YES,
1550 GNUNET_NO,
1551 task, task_cls);
1552}
1553
1554
1555/**
1556 * Schedule a new task to be run with a specified delay or when the
1557 * specified file descriptor is ready for writing. The delay can be
1558 * used as a timeout on the socket being ready. The task will be
1559 * scheduled for execution once either the delay has expired or the
1560 * socket operation is ready. It will be run with the priority of
1561 * the calling task.
1562 * Only allowed to be called as long as the scheduler is running, that
1563 * is one of the following conditions is met:
1564 *
1565 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1566 * - #GNUNET_SCHEDULER_driver_init has been run and
1567 * #GNUNET_SCHEDULER_driver_done has not been called yet
1568 *
1569 * @param delay when should this operation time out?
1570 * @param wfd write file-descriptor
1571 * @param task main function of the task
1572 * @param task_cls closure of @a task
1573 * @return unique task identifier for the job
1574 * only valid until @a task is started!
1575 */
1576struct GNUNET_SCHEDULER_Task *
1577GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1578 struct GNUNET_NETWORK_Handle *wfd,
1579 GNUNET_SCHEDULER_TaskCallback task,
1580 void *task_cls)
1581{
1582 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1583 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1584 wfd,
1585 GNUNET_NO, GNUNET_YES,
1586 task, task_cls);
1587}
1588
1589
1590/**
1591 * Schedule a new task to be run with a specified delay or when the
1592 * specified file descriptor is ready. The delay can be
1593 * used as a timeout on the socket being ready. The task will be
1594 * scheduled for execution once either the delay has expired or the
1595 * socket operation is ready.
1596 * Only allowed to be called as long as the scheduler is running, that
1597 * is one of the following conditions is met:
1598 *
1599 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1600 * - #GNUNET_SCHEDULER_driver_init has been run and
1601 * #GNUNET_SCHEDULER_driver_done has not been called yet
1602 *
1603 * @param delay when should this operation time out?
1604 * @param priority priority of the task
1605 * @param fd file-descriptor
1606 * @param on_read whether to poll the file-descriptor for readability
1607 * @param on_write whether to poll the file-descriptor for writability
1608 * @param task main function of the task
1609 * @param task_cls closure of task
1610 * @return unique task identifier for the job
1611 * only valid until "task" is started!
1612 */
1613struct GNUNET_SCHEDULER_Task *
1614GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1615 enum GNUNET_SCHEDULER_Priority priority,
1616 struct GNUNET_NETWORK_Handle *fd,
1617 int on_read,
1618 int on_write,
1619 GNUNET_SCHEDULER_TaskCallback task,
1620 void *task_cls)
1621{
1622 /* scheduler must be running */
1623 GNUNET_assert (NULL != scheduler_driver);
1624 GNUNET_assert (on_read || on_write);
1625 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1626 return add_without_sets (delay, priority,
1627 on_read ? fd : NULL,
1628 on_write ? fd : NULL,
1629 NULL,
1630 NULL,
1631 task, task_cls);
1632}
1633
1634
1635/**
1636 * Schedule a new task to be run with a specified delay or when the
1637 * specified file descriptor is ready for reading. The delay can be
1638 * used as a timeout on the socket being ready. The task will be
1639 * scheduled for execution once either the delay has expired or the
1640 * socket operation is ready. It will be run with the DEFAULT priority.
1641 * Only allowed to be called as long as the scheduler is running, that
1642 * is one of the following conditions is met:
1643 *
1644 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1645 * - #GNUNET_SCHEDULER_driver_init has been run and
1646 * #GNUNET_SCHEDULER_driver_done has not been called yet
1647 *
1648 * @param delay when should this operation time out?
1649 * @param rfd read file-descriptor
1650 * @param task main function of the task
1651 * @param task_cls closure of @a task
1652 * @return unique task identifier for the job
1653 * only valid until @a task is started!
1654 */
1655struct GNUNET_SCHEDULER_Task *
1656GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1657 const struct GNUNET_DISK_FileHandle *rfd,
1658 GNUNET_SCHEDULER_TaskCallback task,
1659 void *task_cls)
1660{
1661 return GNUNET_SCHEDULER_add_file_with_priority (
1662 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1663 rfd, GNUNET_YES, GNUNET_NO,
1664 task, task_cls);
1665}
1666
1667
1668/**
1669 * Schedule a new task to be run with a specified delay or when the
1670 * specified file descriptor is ready for writing. The delay can be
1671 * used as a timeout on the socket being ready. The task will be
1672 * scheduled for execution once either the delay has expired or the
1673 * socket operation is ready. It will be run with the DEFAULT priority.
1674 * Only allowed to be called as long as the scheduler is running, that
1675 * is one of the following conditions is met:
1676 *
1677 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1678 * - #GNUNET_SCHEDULER_driver_init has been run and
1679 * #GNUNET_SCHEDULER_driver_done has not been called yet
1680 *
1681 * @param delay when should this operation time out?
1682 * @param wfd write file-descriptor
1683 * @param task main function of the task
1684 * @param task_cls closure of @a task
1685 * @return unique task identifier for the job
1686 * only valid until @a task is started!
1687 */
1688struct GNUNET_SCHEDULER_Task *
1689GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1690 const struct GNUNET_DISK_FileHandle *wfd,
1691 GNUNET_SCHEDULER_TaskCallback task,
1692 void *task_cls)
1693{
1694 return GNUNET_SCHEDULER_add_file_with_priority (
1695 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1696 wfd, GNUNET_NO, GNUNET_YES,
1697 task, task_cls);
1698}
1699
1700
1701/**
1702 * Schedule a new task to be run with a specified delay or when the
1703 * specified file descriptor is ready. The delay can be
1704 * used as a timeout on the socket being ready. The task will be
1705 * scheduled for execution once either the delay has expired or the
1706 * socket operation is ready.
1707 * Only allowed to be called as long as the scheduler is running, that
1708 * is one of the following conditions is met:
1709 *
1710 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1711 * - #GNUNET_SCHEDULER_driver_init has been run and
1712 * #GNUNET_SCHEDULER_driver_done has not been called yet
1713 *
1714 * @param delay when should this operation time out?
1715 * @param priority priority of the task
1716 * @param fd file-descriptor
1717 * @param on_read whether to poll the file-descriptor for readability
1718 * @param on_write whether to poll the file-descriptor for writability
1719 * @param task main function of the task
1720 * @param task_cls closure of @a task
1721 * @return unique task identifier for the job
1722 * only valid until @a task is started!
1723 */
1724struct GNUNET_SCHEDULER_Task *
1725GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1726 enum GNUNET_SCHEDULER_Priority
1727 priority,
1728 const struct
1729 GNUNET_DISK_FileHandle *fd,
1730 int on_read, int on_write,
1731 GNUNET_SCHEDULER_TaskCallback task,
1732 void *task_cls)
1733{
1734 /* scheduler must be running */
1735 GNUNET_assert (NULL != scheduler_driver);
1736 GNUNET_assert (on_read || on_write);
1737 GNUNET_assert (fd->fd >= 0);
1738 return add_without_sets (delay, priority,
1739 NULL,
1740 NULL,
1741 on_read ? fd : NULL,
1742 on_write ? fd : NULL,
1743 task, task_cls);
1744}
1745
1746
1747void
1748extract_handles (const struct GNUNET_NETWORK_FDSet *fdset,
1749 const struct GNUNET_NETWORK_Handle ***ntarget,
1750 unsigned int *extracted_nhandles,
1751 const struct GNUNET_DISK_FileHandle ***ftarget,
1752 unsigned int *extracted_fhandles)
1753{
1754 // FIXME: this implementation only works for unix, for WIN32 the file handles
1755 // in fdset must be handled separately
1756 const struct GNUNET_NETWORK_Handle **nhandles;
1757 const struct GNUNET_DISK_FileHandle **fhandles;
1758 unsigned int nhandles_len;
1759 unsigned int fhandles_len;
1760
1761 nhandles = NULL;
1762 fhandles = NULL;
1763 nhandles_len = 0;
1764 fhandles_len = 0;
1765 for (int sock = 0; sock != fdset->nsds; ++sock)
1766 {
1767 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1768 {
1769 struct GNUNET_NETWORK_Handle *nhandle;
1770 struct GNUNET_DISK_FileHandle *fhandle;
1771
1772 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1773 if (NULL != nhandle)
1774 {
1775 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1776 }
1777 else
1778 {
1779 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1780 if (NULL != fhandle)
1781 {
1782 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1783 }
1784 else
1785 {
1786 GNUNET_assert (0);
1787 }
1788 }
1789 }
1790 }
1791 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1792 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1793 *extracted_nhandles = nhandles_len;
1794 *extracted_fhandles = fhandles_len;
1795}
1796
1797
1798/**
1799 * Schedule a new task to be run with a specified delay or when any of
1800 * the specified file descriptor sets is ready. The delay can be used
1801 * as a timeout on the socket(s) being ready. The task will be
1802 * scheduled for execution once either the delay has expired or any of
1803 * the socket operations is ready. This is the most general
1804 * function of the "add" family. Note that the "prerequisite_task"
1805 * must be satisfied in addition to any of the other conditions. In
1806 * other words, the task will be started when
1807 * <code>
1808 * (prerequisite-run)
1809 * && (delay-ready
1810 * || any-rs-ready
1811 * || any-ws-ready) )
1812 * </code>
1813 * Only allowed to be called as long as the scheduler is running, that
1814 * is one of the following conditions is met:
1815 *
1816 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1817 * - #GNUNET_SCHEDULER_driver_init has been run and
1818 * #GNUNET_SCHEDULER_driver_done has not been called yet
1819 *
1820 * @param prio how important is this task?
1821 * @param delay how long should we wait?
1822 * @param rs set of file descriptors we want to read (can be NULL)
1823 * @param ws set of file descriptors we want to write (can be NULL)
1824 * @param task main function of the task
1825 * @param task_cls closure of @a task
1826 * @return unique task identifier for the job
1827 * only valid until @a task is started!
1828 */
1829struct GNUNET_SCHEDULER_Task *
1830GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1831 struct GNUNET_TIME_Relative delay,
1832 const struct GNUNET_NETWORK_FDSet *rs,
1833 const struct GNUNET_NETWORK_FDSet *ws,
1834 GNUNET_SCHEDULER_TaskCallback task,
1835 void *task_cls)
1836{
1837 struct GNUNET_SCHEDULER_Task *t;
1838 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL;
1839 const struct GNUNET_NETWORK_Handle **write_nhandles = NULL;
1840 const struct GNUNET_DISK_FileHandle **read_fhandles = NULL;
1841 const struct GNUNET_DISK_FileHandle **write_fhandles = NULL;
1842 unsigned int read_nhandles_len = 0;
1843 unsigned int write_nhandles_len = 0;
1844 unsigned int read_fhandles_len = 0;
1845 unsigned int write_fhandles_len = 0;
1846
1847 /* scheduler must be running */
1848 GNUNET_assert (NULL != scheduler_driver);
1849 GNUNET_assert (NULL != task);
1850 int no_rs = (NULL == rs);
1851 int no_ws = (NULL == ws);
1852 int empty_rs = (NULL != rs) && (0 == rs->nsds);
1853 int empty_ws = (NULL != ws) && (0 == ws->nsds);
1854 int no_fds = (no_rs && no_ws) ||
1855 (empty_rs && empty_ws) ||
1856 (no_rs && empty_ws) ||
1857 (no_ws && empty_rs);
1858 if (! no_fds)
1859 {
1860 if (NULL != rs)
1861 {
1862 extract_handles (rs,
1863 &read_nhandles,
1864 &read_nhandles_len,
1865 &read_fhandles,
1866 &read_fhandles_len);
1867 }
1868 if (NULL != ws)
1869 {
1870 extract_handles (ws,
1871 &write_nhandles,
1872 &write_nhandles_len,
1873 &write_fhandles,
1874 &write_fhandles_len);
1875 }
1876 }
1877 /**
1878 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty
1879 * although its maximum FD number (nsds) is greater than 0. We handle
1880 * this case gracefully because some libraries such as libmicrohttpd
1881 * only provide a hint what the maximum FD number in an FD set might be
1882 * and not the exact FD number (see e.g. gnunet-rest-service.c)
1883 */int no_fds_extracted = (0 == read_nhandles_len) &&
1884 (0 == read_fhandles_len) &&
1885 (0 == write_nhandles_len) &&
1886 (0 == write_fhandles_len);
1887 if (no_fds || no_fds_extracted)
1888 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1889 prio,
1890 task,
1891 task_cls);
1892 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1893 GNUNET_async_scope_get (&t->scope);
1894 init_fd_info (t,
1895 read_nhandles,
1896 read_nhandles_len,
1897 write_nhandles,
1898 write_nhandles_len,
1899 read_fhandles,
1900 read_fhandles_len,
1901 write_fhandles,
1902 write_fhandles_len);
1903 t->callback = task;
1904 t->callback_cls = task_cls;
1905 t->own_handles = GNUNET_YES;
1906 /* free the arrays of pointers to network / file handles, the actual
1907 * handles will be freed in destroy_task */
1908 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1909 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1910 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1911 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1912#if PROFILE_DELAYS
1913 t->start_time = GNUNET_TIME_absolute_get ();
1914#endif
1915 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1916 t->priority =
1917 check_priority ((prio ==
1918 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1919 prio);
1920 t->lifeness = current_lifeness;
1921 GNUNET_CONTAINER_DLL_insert (pending_head,
1922 pending_tail,
1923 t);
1924 driver_add_multiple (t);
1925 max_priority_added = GNUNET_MAX (max_priority_added,
1926 t->priority);
1927 LOG (GNUNET_ERROR_TYPE_DEBUG,
1928 "Adding task %p\n",
1929 t);
1930 init_backtrace (t);
1931 return t;
1932}
1933
1934
1935/**
1936 * Function used by event-loop implementations to signal the scheduler
1937 * that a particular @a task is ready due to an event specified in the
1938 * et field of @a fdi.
1939 *
1940 * This function will then queue the task to notify the application
1941 * that the task is ready (with the respective priority).
1942 *
1943 * @param task the task that is ready
1944 * @param fdi information about the related FD
1945 */
1946void
1947GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1948 struct GNUNET_SCHEDULER_FdInfo *fdi)
1949{
1950 enum GNUNET_SCHEDULER_Reason reason;
1951
1952 reason = task->reason;
1953 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1954 (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)))
1955 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1956 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1957 (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)))
1958 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1959 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1960 task->reason = reason;
1961 if (GNUNET_NO == task->in_ready_list)
1962 {
1963 GNUNET_CONTAINER_DLL_remove (pending_head,
1964 pending_tail,
1965 task);
1966 queue_ready_task (task);
1967 }
1968}
1969
1970
1971/**
1972 * Function called by external event loop implementations to tell the
1973 * scheduler to run some of the tasks that are ready. Must be called
1974 * only after #GNUNET_SCHEDULER_driver_init has been called and before
1975 * #GNUNET_SCHEDULER_driver_done is called.
1976 * This function may return even though there are tasks left to run
1977 * just to give other tasks a chance as well. If we return #GNUNET_YES,
1978 * the event loop implementation should call this function again as
1979 * soon as possible, while if we return #GNUNET_NO it must block until
1980 * either the operating system has more work (the scheduler has no more
1981 * work to do right now) or the timeout set by the scheduler (using the
1982 * set_wakeup callback) is reached.
1983 *
1984 * @param sh scheduler handle that was returned by
1985 * #GNUNET_SCHEDULER_driver_init
1986 * @return #GNUNET_YES if there are more tasks that are ready,
1987 * and thus we would like to run more (yield to avoid
1988 * blocking other activities for too long) #GNUNET_NO
1989 * if we are done running tasks (yield to block)
1990 */
1991int
1992GNUNET_SCHEDULER_do_work (struct GNUNET_SCHEDULER_Handle *sh)
1993{
1994 struct GNUNET_SCHEDULER_Task *pos;
1995 struct GNUNET_TIME_Absolute now;
1996
1997 /* check for tasks that reached the timeout! */
1998 now = GNUNET_TIME_absolute_get ();
1999 pos = pending_timeout_head;
2000 while (NULL != pos)
2001 {
2002 struct GNUNET_SCHEDULER_Task *next = pos->next;
2003 if (now.abs_value_us >= pos->timeout.abs_value_us)
2004 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
2005 if (0 == pos->reason)
2006 break;
2007 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
2008 pending_timeout_tail,
2009 pos);
2010 if (pending_timeout_last == pos)
2011 pending_timeout_last = NULL;
2012 queue_ready_task (pos);
2013 pos = next;
2014 }
2015 pos = pending_head;
2016 while (NULL != pos)
2017 {
2018 struct GNUNET_SCHEDULER_Task *next = pos->next;
2019 if (now.abs_value_us >= pos->timeout.abs_value_us)
2020 {
2021 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
2022 GNUNET_CONTAINER_DLL_remove (pending_head,
2023 pending_tail,
2024 pos);
2025 queue_ready_task (pos);
2026 }
2027 pos = next;
2028 }
2029
2030 if (0 == ready_count)
2031 {
2032 struct GNUNET_TIME_Absolute timeout = get_timeout ();
2033
2034 if (timeout.abs_value_us > now.abs_value_us)
2035 {
2036 /**
2037 * The event loop called this function before the current timeout was
2038 * reached (and no FD tasks are ready). This is acceptable if
2039 *
2040 * - the system time was changed while the driver was waiting for
2041 * the timeout
2042 * - an external event loop called GNUnet API functions outside of
2043 * the callbacks called in GNUNET_SCHEDULER_do_work and thus
2044 * wasn't notified about the new timeout
2045 *
2046 * It might also mean we are busy-waiting because of a programming
2047 * error in the external event loop.
2048 */
2049 LOG (GNUNET_ERROR_TYPE_DEBUG,
2050 "GNUNET_SCHEDULER_do_work did not find any ready "
2051 "tasks and timeout has not been reached yet.\n");
2052 }
2053 else
2054 {
2055 /**
2056 * the current timeout was reached but no ready tasks were found,
2057 * internal scheduler error!
2058 */
2059 GNUNET_assert (0);
2060 }
2061 }
2062 else
2063 {
2064 /* find out which task priority level we are going to
2065 process this time */
2066 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
2067 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
2068 /* yes, p>0 is correct, 0 is "KEEP" which should
2069 * always be an empty queue (see assertion)! */
2070 for (work_priority = GNUNET_SCHEDULER_PRIORITY_COUNT - 1;
2071 work_priority > 0;
2072 work_priority--)
2073 {
2074 pos = ready_head[work_priority];
2075 if (NULL != pos)
2076 break;
2077 }
2078 GNUNET_assert (NULL != pos); /* ready_count wrong? */
2079
2080 /* process all *existing* tasks at this priority
2081 level, then yield */
2082 set_work_priority (work_priority);
2083 while (NULL != (pos = ready_head[work_priority])
2084 && pos != &pass_end_marker)
2085 {
2086 GNUNET_CONTAINER_DLL_remove (ready_head[work_priority],
2087 ready_tail[work_priority],
2088 pos);
2089 ready_count--;
2090 current_priority = pos->priority;
2091 current_lifeness = pos->lifeness;
2092 active_task = pos;
2093#if PROFILE_DELAYS
2094 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
2095 DELAY_THRESHOLD.rel_value_us)
2096 {
2097 LOG (GNUNET_ERROR_TYPE_DEBUG,
2098 "Task %p took %s to be scheduled\n",
2099 pos,
2100 GNUNET_STRINGS_relative_time_to_string (
2101 GNUNET_TIME_absolute_get_duration (pos->start_time),
2102 GNUNET_YES));
2103 }
2104#endif
2105 tc.reason = pos->reason;
2106 GNUNET_NETWORK_fdset_zero (sh->rs);
2107 GNUNET_NETWORK_fdset_zero (sh->ws);
2108 // FIXME: do we have to remove FdInfos from fds if they are not ready?
2109 tc.fds_len = pos->fds_len;
2110 tc.fds = pos->fds;
2111 for (unsigned int i = 0; i != pos->fds_len; ++i)
2112 {
2113 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
2114 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
2115 {
2116 GNUNET_NETWORK_fdset_set_native (sh->rs,
2117 fdi->sock);
2118 }
2119 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2120 {
2121 GNUNET_NETWORK_fdset_set_native (sh->ws,
2122 fdi->sock);
2123 }
2124 }
2125 tc.read_ready = sh->rs;
2126 tc.write_ready = sh->ws;
2127 LOG (GNUNET_ERROR_TYPE_DEBUG,
2128 "Running task %p\n",
2129 pos);
2130 GNUNET_assert (NULL != pos->callback);
2131 {
2132 struct GNUNET_AsyncScopeSave old_scope;
2133 if (pos->scope.have_scope)
2134 GNUNET_async_scope_enter (&pos->scope.scope_id, &old_scope);
2135 else
2136 GNUNET_async_scope_get (&old_scope);
2137 pos->callback (pos->callback_cls);
2138 GNUNET_async_scope_restore (&old_scope);
2139 }
2140 if (NULL != pos->fds)
2141 {
2142 int del_result = scheduler_driver->del (scheduler_driver->cls,
2143 pos);
2144 if (GNUNET_OK != del_result)
2145 {
2146 LOG (GNUNET_ERROR_TYPE_ERROR,
2147 "driver could not delete task %p\n", pos);
2148 GNUNET_assert (0);
2149 }
2150 }
2151 active_task = NULL;
2152 dump_backtrace (pos);
2153 destroy_task (pos);
2154 }
2155 remove_pass_end_marker ();
2156 }
2157 shutdown_if_no_lifeness ();
2158 if (0 == ready_count)
2159 {
2160 scheduler_driver->set_wakeup (scheduler_driver->cls,
2161 get_timeout ());
2162 return GNUNET_NO;
2163 }
2164 scheduler_driver->set_wakeup (scheduler_driver->cls,
2165 GNUNET_TIME_absolute_get ());
2166 return GNUNET_YES;
2167}
2168
2169
2170/**
2171 * Function called by external event loop implementations to initialize
2172 * the scheduler. An external implementation has to provide @a driver
2173 * which contains callbacks for the scheduler (see definition of struct
2174 * #GNUNET_SCHEDULER_Driver). The callbacks are used to instruct the
2175 * external implementation to watch for events. If it detects any of
2176 * those events it is expected to call #GNUNET_SCHEDULER_do_work to let
2177 * the scheduler handle it. If an event is related to a specific task
2178 * (e.g. the scheduler gave instructions to watch a file descriptor),
2179 * the external implementation is expected to mark that task ready
2180 * before by calling #GNUNET_SCHEDULER_task_ready.
2181
2182 * This function has to be called before any tasks are scheduled and
2183 * before GNUNET_SCHEDULER_do_work is called for the first time. It
2184 * allocates resources that have to be freed again by calling
2185 * #GNUNET_SCHEDULER_driver_done.
2186 *
2187 * This function installs the same signal handlers as
2188 * #GNUNET_SCHEDULER_run. This means SIGTERM (and other similar signals)
2189 * will induce a call to #GNUNET_SCHEDULER_shutdown during the next
2190 * call to #GNUNET_SCHEDULER_do_work. As a result, SIGTERM causes all
2191 * active tasks to be scheduled with reason
2192 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added afterwards
2193 * will execute normally!). Note that any particular signal will only
2194 * shut down one scheduler; applications should always only create a
2195 * single scheduler.
2196 *
2197 * @param driver to use for the event loop
2198 * @return handle to be passed to #GNUNET_SCHEDULER_do_work and
2199 * #GNUNET_SCHEDULER_driver_done
2200 */
2201struct GNUNET_SCHEDULER_Handle *
2202GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver)
2203{
2204 struct GNUNET_SCHEDULER_Handle *sh;
2205 const struct GNUNET_DISK_FileHandle *pr;
2206
2207 /* scheduler must not be running */
2208 GNUNET_assert (NULL == scheduler_driver);
2209 GNUNET_assert (NULL == shutdown_pipe_handle);
2210 /* general set-up */
2211 sh = GNUNET_new (struct GNUNET_SCHEDULER_Handle);
2212 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE);
2213 GNUNET_assert (NULL != shutdown_pipe_handle);
2214 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2215 GNUNET_DISK_PIPE_END_READ);
2216 my_pid = getpid ();
2217 scheduler_driver = driver;
2218
2219 /* install signal handlers */
2220 LOG (GNUNET_ERROR_TYPE_DEBUG,
2221 "Registering signal handlers\n");
2222 sh->shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2223 &sighandler_shutdown);
2224 sh->shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2225 &sighandler_shutdown);
2226#if (SIGTERM != GNUNET_TERM_SIG)
2227 sh->shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2228 &sighandler_shutdown);
2229#endif
2230 sh->shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2231 &sighandler_pipe);
2232 sh->shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2233 &sighandler_shutdown);
2234 sh->shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2235 &sighandler_shutdown);
2236
2237 /* Setup initial tasks */
2238 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2239 current_lifeness = GNUNET_NO;
2240 /* ensure this task runs first, by using a priority level reserved for
2241 the scheduler (not really shutdown, but start-up ;-) */
2242 install_parent_control_task =
2243 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_SHUTDOWN,
2244 &install_parent_control_handler,
2245 NULL);
2246 shutdown_pipe_task =
2247 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2248 pr,
2249 &shutdown_pipe_cb,
2250 NULL);
2251 current_lifeness = GNUNET_YES;
2252 scheduler_driver->set_wakeup (scheduler_driver->cls,
2253 get_timeout ());
2254 /* begin main event loop */
2255 sh->rs = GNUNET_NETWORK_fdset_create ();
2256 sh->ws = GNUNET_NETWORK_fdset_create ();
2257 GNUNET_NETWORK_fdset_handle_set (sh->rs, pr);
2258 return sh;
2259}
2260
2261
2262/**
2263 * Counter-part of #GNUNET_SCHEDULER_driver_init. Has to be called
2264 * by external event loop implementations after the scheduler has
2265 * shut down. This is the case if both of the following conditions
2266 * are met:
2267 *
2268 * - all tasks the scheduler has added through the driver's add
2269 * callback have been removed again through the driver's del
2270 * callback
2271 * - the timeout the scheduler has set through the driver's
2272 * add_wakeup callback is FOREVER
2273 *
2274 * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
2275 */
2276void
2277GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
2278{
2279 GNUNET_assert (NULL == pending_head);
2280 GNUNET_assert (NULL == pending_timeout_head);
2281 GNUNET_assert (NULL == shutdown_head);
2282 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i)
2283 {
2284 GNUNET_assert (NULL == ready_head[i]);
2285 }
2286 GNUNET_NETWORK_fdset_destroy (sh->rs);
2287 GNUNET_NETWORK_fdset_destroy (sh->ws);
2288
2289 /* uninstall signal handlers */
2290 GNUNET_SIGNAL_handler_uninstall (sh->shc_int);
2291 GNUNET_SIGNAL_handler_uninstall (sh->shc_term);
2292#if (SIGTERM != GNUNET_TERM_SIG)
2293 GNUNET_SIGNAL_handler_uninstall (sh->shc_gterm);
2294#endif
2295 GNUNET_SIGNAL_handler_uninstall (sh->shc_pipe);
2296 GNUNET_SIGNAL_handler_uninstall (sh->shc_quit);
2297 GNUNET_SIGNAL_handler_uninstall (sh->shc_hup);
2298 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2299 shutdown_pipe_handle = NULL;
2300 scheduler_driver = NULL;
2301 GNUNET_free (sh);
2302}
2303
2304
2305static int
2306select_loop (struct GNUNET_SCHEDULER_Handle *sh,
2307 struct DriverContext *context)
2308{
2309 struct GNUNET_NETWORK_FDSet *rs;
2310 struct GNUNET_NETWORK_FDSet *ws;
2311 int select_result;
2312
2313 GNUNET_assert (NULL != context);
2314 rs = GNUNET_NETWORK_fdset_create ();
2315 ws = GNUNET_NETWORK_fdset_create ();
2316 while ((NULL != context->scheduled_head) ||
2317 (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
2318 context->timeout.abs_value_us))
2319 {
2320 struct GNUNET_TIME_Relative time_remaining;
2321
2322 LOG (GNUNET_ERROR_TYPE_DEBUG,
2323 "select timeout = %s\n",
2324 GNUNET_STRINGS_absolute_time_to_string (context->timeout));
2325
2326 GNUNET_NETWORK_fdset_zero (rs);
2327 GNUNET_NETWORK_fdset_zero (ws);
2328
2329 for (struct Scheduled *pos = context->scheduled_head;
2330 NULL != pos;
2331 pos = pos->next)
2332 {
2333 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2334 {
2335 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2336 }
2337 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2338 {
2339 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2340 }
2341 }
2342 time_remaining = GNUNET_TIME_absolute_get_remaining (context->timeout);
2343 if (0 < ready_count)
2344 time_remaining = GNUNET_TIME_UNIT_ZERO;
2345 if (NULL == scheduler_select)
2346 {
2347 select_result = GNUNET_NETWORK_socket_select (rs,
2348 ws,
2349 NULL,
2350 time_remaining);
2351 }
2352 else
2353 {
2354 select_result = scheduler_select (scheduler_select_cls,
2355 rs,
2356 ws,
2357 NULL,
2358 time_remaining);
2359 }
2360 if (select_result == GNUNET_SYSERR)
2361 {
2362 if (errno == EINTR)
2363 continue;
2364
2365 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
2366 "select");
2367#if USE_LSOF
2368 char lsof[512];
2369
2370 snprintf (lsof,
2371 sizeof(lsof),
2372 "lsof -p %d",
2373 getpid ());
2374 (void) close (1);
2375 (void) dup2 (2, 1);
2376 if (0 != system (lsof))
2377 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2378 "system");
2379#endif
2380#if DEBUG_FDS
2381 for (struct Scheduled *s = context->scheduled_head;
2382 NULL != s;
2383 s = s->next)
2384 {
2385 int flags = fcntl (s->fdi->sock,
2386 F_GETFD);
2387
2388 if ((flags == -1) &&
2389 (EBADF == errno))
2390 {
2391 LOG (GNUNET_ERROR_TYPE_ERROR,
2392 "Got invalid file descriptor %d!\n",
2393 s->fdi->sock);
2394#if EXECINFO
2395 dump_backtrace (s->task);
2396#endif
2397 }
2398 }
2399#endif
2400 GNUNET_assert (0);
2401 GNUNET_NETWORK_fdset_destroy (rs);
2402 GNUNET_NETWORK_fdset_destroy (ws);
2403 return GNUNET_SYSERR;
2404 }
2405 if (select_result > 0)
2406 {
2407 for (struct Scheduled *pos = context->scheduled_head;
2408 NULL != pos;
2409 pos = pos->next)
2410 {
2411 int is_ready = GNUNET_NO;
2412
2413 if ((0 != (GNUNET_SCHEDULER_ET_IN & pos->et)) &&
2414 (GNUNET_YES ==
2415 GNUNET_NETWORK_fdset_test_native (rs,
2416 pos->fdi->sock)) )
2417 {
2418 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2419 is_ready = GNUNET_YES;
2420 }
2421 if ((0 != (GNUNET_SCHEDULER_ET_OUT & pos->et)) &&
2422 (GNUNET_YES ==
2423 GNUNET_NETWORK_fdset_test_native (ws,
2424 pos->fdi->sock)) )
2425 {
2426 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2427 is_ready = GNUNET_YES;
2428 }
2429 if (GNUNET_YES == is_ready)
2430 {
2431 GNUNET_SCHEDULER_task_ready (pos->task,
2432 pos->fdi);
2433 }
2434 }
2435 }
2436 if (GNUNET_YES == GNUNET_SCHEDULER_do_work (sh))
2437 {
2438 LOG (GNUNET_ERROR_TYPE_DEBUG,
2439 "scheduler has more tasks ready!\n");
2440 }
2441 }
2442 GNUNET_NETWORK_fdset_destroy (rs);
2443 GNUNET_NETWORK_fdset_destroy (ws);
2444 return GNUNET_OK;
2445}
2446
2447
2448static int
2449select_add (void *cls,
2450 struct GNUNET_SCHEDULER_Task *task,
2451 struct GNUNET_SCHEDULER_FdInfo *fdi)
2452{
2453 struct DriverContext *context = cls;
2454
2455 GNUNET_assert (NULL != context);
2456 GNUNET_assert (NULL != task);
2457 GNUNET_assert (NULL != fdi);
2458 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2459 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2460
2461 if (! ((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2462 {
2463 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2464 return GNUNET_SYSERR;
2465 }
2466
2467 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2468 scheduled->task = task;
2469 scheduled->fdi = fdi;
2470 scheduled->et = fdi->et;
2471
2472 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2473 context->scheduled_tail,
2474 scheduled);
2475 return GNUNET_OK;
2476}
2477
2478
2479static int
2480select_del (void *cls,
2481 struct GNUNET_SCHEDULER_Task *task)
2482{
2483 struct DriverContext *context;
2484 struct Scheduled *pos;
2485 int ret;
2486
2487 GNUNET_assert (NULL != cls);
2488
2489 context = cls;
2490 ret = GNUNET_SYSERR;
2491 pos = context->scheduled_head;
2492 while (NULL != pos)
2493 {
2494 struct Scheduled *next = pos->next;
2495 if (pos->task == task)
2496 {
2497 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2498 context->scheduled_tail,
2499 pos);
2500 GNUNET_free (pos);
2501 ret = GNUNET_OK;
2502 }
2503 pos = next;
2504 }
2505 return ret;
2506}
2507
2508
2509static void
2510select_set_wakeup (void *cls,
2511 struct GNUNET_TIME_Absolute dt)
2512{
2513 struct DriverContext *context = cls;
2514
2515 GNUNET_assert (NULL != context);
2516 context->timeout = dt;
2517}
2518
2519
2520/**
2521 * Obtain the driver for using select() as the event loop.
2522 *
2523 * @return NULL on error
2524 */
2525struct GNUNET_SCHEDULER_Driver *
2526GNUNET_SCHEDULER_driver_select ()
2527{
2528 struct GNUNET_SCHEDULER_Driver *select_driver;
2529
2530 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2531
2532 select_driver->add = &select_add;
2533 select_driver->del = &select_del;
2534 select_driver->set_wakeup = &select_set_wakeup;
2535
2536 return select_driver;
2537}
2538
2539
2540/**
2541 * Change the async scope for the currently executing task and (transitively)
2542 * for all tasks scheduled by the current task after calling this function.
2543 * Nested tasks can begin their own nested async scope.
2544 *
2545 * Once the current task is finished, the async scope ID is reset to
2546 * its previous value.
2547 *
2548 * Must only be called from a running task.
2549 *
2550 * @param aid the asynchronous scope id to enter
2551 */
2552void
2553GNUNET_SCHEDULER_begin_async_scope (struct GNUNET_AsyncScopeId *aid)
2554{
2555 struct GNUNET_AsyncScopeSave dummy_old_scope;
2556
2557 GNUNET_assert (NULL != active_task);
2558 /* Since we're in a task, the context will be automatically
2559 restored by the scheduler. */
2560 GNUNET_async_scope_enter (aid, &dummy_old_scope);
2561}
2562
2563
2564/* end of scheduler.c */