aboutsummaryrefslogtreecommitdiff
path: root/src/util/scheduler.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/util/scheduler.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/util/scheduler.c')
-rw-r--r--src/util/scheduler.c1887
1 files changed, 956 insertions, 931 deletions
diff --git a/src/util/scheduler.c b/src/util/scheduler.c
index f379bce42..89dba3799 100644
--- a/src/util/scheduler.c
+++ b/src/util/scheduler.c
@@ -28,9 +28,11 @@
28// DEBUG 28// DEBUG
29#include <inttypes.h> 29#include <inttypes.h>
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-scheduler", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
32 32
33#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror(kind, "util-scheduler", syscall) 33#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
34 "util-scheduler", \
35 syscall)
34 36
35 37
36#if HAVE_EXECINFO_H 38#if HAVE_EXECINFO_H
@@ -76,7 +78,8 @@
76 * #GNUNET_SCHEDULER_do_work(). Contains the 78 * #GNUNET_SCHEDULER_do_work(). Contains the
77 * scheduler's internal state. 79 * scheduler's internal state.
78 */ 80 */
79struct GNUNET_SCHEDULER_Handle { 81struct GNUNET_SCHEDULER_Handle
82{
80 /** 83 /**
81 * Passed here to avoid constantly allocating/deallocating 84 * Passed here to avoid constantly allocating/deallocating
82 * this element, but generally we want to get rid of this. 85 * this element, but generally we want to get rid of this.
@@ -128,7 +131,8 @@ struct GNUNET_SCHEDULER_Handle {
128/** 131/**
129 * Entry in list of pending tasks. 132 * Entry in list of pending tasks.
130 */ 133 */
131struct GNUNET_SCHEDULER_Task { 134struct GNUNET_SCHEDULER_Task
135{
132 /** 136 /**
133 * This is a linked list. 137 * This is a linked list.
134 */ 138 */
@@ -247,7 +251,8 @@ struct GNUNET_SCHEDULER_Task {
247/** 251/**
248 * A struct representing an event the select driver is waiting for 252 * A struct representing an event the select driver is waiting for
249 */ 253 */
250struct Scheduled { 254struct Scheduled
255{
251 struct Scheduled *prev; 256 struct Scheduled *prev;
252 257
253 struct Scheduled *next; 258 struct Scheduled *next;
@@ -274,7 +279,8 @@ struct Scheduled {
274/** 279/**
275 * Driver context used by GNUNET_SCHEDULER_run 280 * Driver context used by GNUNET_SCHEDULER_run
276 */ 281 */
277struct DriverContext { 282struct DriverContext
283{
278 /** 284 /**
279 * the head of a DLL containing information about the events the 285 * the head of a DLL containing information about the events the
280 * select driver is waiting for 286 * select driver is waiting for
@@ -356,12 +362,14 @@ static struct GNUNET_SCHEDULER_Task *active_task;
356/** 362/**
357 * Head of list of tasks ready to run right now, grouped by importance. 363 * Head of list of tasks ready to run right now, grouped by importance.
358 */ 364 */
359static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT]; 365static struct
366GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
360 367
361/** 368/**
362 * Tail of list of tasks ready to run right now, grouped by importance. 369 * Tail of list of tasks ready to run right now, grouped by importance.
363 */ 370 */
364static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT]; 371static struct
372GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
365 373
366/** 374/**
367 * Task for installing parent control handlers (it might happen that the 375 * Task for installing parent control handlers (it might happen that the
@@ -423,8 +431,8 @@ static void *scheduler_select_cls;
423 * @return previously used select function, NULL for default 431 * @return previously used select function, NULL for default
424 */ 432 */
425void 433void
426GNUNET_SCHEDULER_set_select(GNUNET_SCHEDULER_select new_select, 434GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
427 void *new_select_cls) 435 void *new_select_cls)
428{ 436{
429 scheduler_select = new_select; 437 scheduler_select = new_select;
430 scheduler_select_cls = new_select_cls; 438 scheduler_select_cls = new_select_cls;
@@ -438,11 +446,11 @@ GNUNET_SCHEDULER_set_select(GNUNET_SCHEDULER_select new_select,
438 * @return p on success, 0 on error 446 * @return p on success, 0 on error
439 */ 447 */
440static enum GNUNET_SCHEDULER_Priority 448static enum GNUNET_SCHEDULER_Priority
441check_priority(enum GNUNET_SCHEDULER_Priority p) 449check_priority (enum GNUNET_SCHEDULER_Priority p)
442{ 450{
443 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT)) 451 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
444 return p; 452 return p;
445 GNUNET_assert(0); 453 GNUNET_assert (0);
446 return 0; /* make compiler happy */ 454 return 0; /* make compiler happy */
447} 455}
448 456
@@ -453,38 +461,39 @@ check_priority(enum GNUNET_SCHEDULER_Priority p)
453 * callback) 461 * callback)
454 */ 462 */
455struct GNUNET_TIME_Absolute 463struct GNUNET_TIME_Absolute
456get_timeout() 464get_timeout ()
457{ 465{
458 struct GNUNET_SCHEDULER_Task *pos; 466 struct GNUNET_SCHEDULER_Task *pos;
459 struct GNUNET_TIME_Absolute now; 467 struct GNUNET_TIME_Absolute now;
460 struct GNUNET_TIME_Absolute timeout; 468 struct GNUNET_TIME_Absolute timeout;
461 469
462 pos = pending_timeout_head; 470 pos = pending_timeout_head;
463 now = GNUNET_TIME_absolute_get(); 471 now = GNUNET_TIME_absolute_get ();
464 timeout = GNUNET_TIME_UNIT_FOREVER_ABS; 472 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
465 if (NULL != pos) 473 if (NULL != pos)
474 {
475 if (0 != pos->reason)
466 { 476 {
467 if (0 != pos->reason) 477 return now;
468 { 478 }
469 return now; 479 else
470 } 480 {
471 else 481 timeout = pos->timeout;
472 {
473 timeout = pos->timeout;
474 }
475 } 482 }
483 }
476 for (pos = pending_head; NULL != pos; pos = pos->next) 484 for (pos = pending_head; NULL != pos; pos = pos->next)
485 {
486 if (0 != pos->reason)
477 { 487 {
478 if (0 != pos->reason) 488 return now;
479 { 489 }
480 return now; 490 else if ((pos->timeout.abs_value_us !=
481 } 491 GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
482 else if ((pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) && 492 (timeout.abs_value_us > pos->timeout.abs_value_us))
483 (timeout.abs_value_us > pos->timeout.abs_value_us)) 493 {
484 { 494 timeout = pos->timeout;
485 timeout = pos->timeout;
486 }
487 } 495 }
496 }
488 return timeout; 497 return timeout;
489} 498}
490 499
@@ -495,13 +504,13 @@ get_timeout()
495 * @param task task ready for execution 504 * @param task task ready for execution
496 */ 505 */
497static void 506static void
498queue_ready_task(struct GNUNET_SCHEDULER_Task *task) 507queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
499{ 508{
500 enum GNUNET_SCHEDULER_Priority p = check_priority(task->priority); 509 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
501 510
502 GNUNET_CONTAINER_DLL_insert(ready_head[p], 511 GNUNET_CONTAINER_DLL_insert (ready_head[p],
503 ready_tail[p], 512 ready_tail[p],
504 task); 513 task);
505 task->in_ready_list = GNUNET_YES; 514 task->in_ready_list = GNUNET_YES;
506 ready_count++; 515 ready_count++;
507} 516}
@@ -514,30 +523,30 @@ queue_ready_task(struct GNUNET_SCHEDULER_Task *task)
514 * will be delayed until the next shutdown signal. 523 * will be delayed until the next shutdown signal.
515 */ 524 */
516void 525void
517GNUNET_SCHEDULER_shutdown() 526GNUNET_SCHEDULER_shutdown ()
518{ 527{
519 struct GNUNET_SCHEDULER_Task *pos; 528 struct GNUNET_SCHEDULER_Task *pos;
520 529
521 LOG(GNUNET_ERROR_TYPE_DEBUG, 530 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "GNUNET_SCHEDULER_shutdown\n"); 531 "GNUNET_SCHEDULER_shutdown\n");
523 if (NULL != install_parent_control_task) 532 if (NULL != install_parent_control_task)
524 { 533 {
525 GNUNET_SCHEDULER_cancel(install_parent_control_task); 534 GNUNET_SCHEDULER_cancel (install_parent_control_task);
526 install_parent_control_task = NULL; 535 install_parent_control_task = NULL;
527 } 536 }
528 if (NULL != shutdown_pipe_task) 537 if (NULL != shutdown_pipe_task)
529 { 538 {
530 GNUNET_SCHEDULER_cancel(shutdown_pipe_task); 539 GNUNET_SCHEDULER_cancel (shutdown_pipe_task);
531 shutdown_pipe_task = NULL; 540 shutdown_pipe_task = NULL;
532 } 541 }
533 while (NULL != (pos = shutdown_head)) 542 while (NULL != (pos = shutdown_head))
534 { 543 {
535 GNUNET_CONTAINER_DLL_remove(shutdown_head, 544 GNUNET_CONTAINER_DLL_remove (shutdown_head,
536 shutdown_tail, 545 shutdown_tail,
537 pos); 546 pos);
538 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN; 547 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
539 queue_ready_task(pos); 548 queue_ready_task (pos);
540 } 549 }
541} 550}
542 551
543 552
@@ -547,17 +556,17 @@ GNUNET_SCHEDULER_shutdown()
547 * @param t task to dump stack trace of 556 * @param t task to dump stack trace of
548 */ 557 */
549static void 558static void
550dump_backtrace(struct GNUNET_SCHEDULER_Task *t) 559dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
551{ 560{
552#if EXECINFO 561#if EXECINFO
553 for (unsigned int i = 0; i < t->num_backtrace_strings; i++) 562 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
554 LOG(GNUNET_ERROR_TYPE_WARNING, 563 LOG (GNUNET_ERROR_TYPE_WARNING,
555 "Task %p trace %u: %s\n", 564 "Task %p trace %u: %s\n",
556 t, 565 t,
557 i, 566 i,
558 t->backtrace_strings[i]); 567 t->backtrace_strings[i]);
559#else 568#else
560 (void)t; 569 (void) t;
561#endif 570#endif
562} 571}
563 572
@@ -568,40 +577,41 @@ dump_backtrace(struct GNUNET_SCHEDULER_Task *t)
568 * @param t task to destroy 577 * @param t task to destroy
569 */ 578 */
570static void 579static void
571destroy_task(struct GNUNET_SCHEDULER_Task *t) 580destroy_task (struct GNUNET_SCHEDULER_Task *t)
572{ 581{
573 unsigned int i; 582 unsigned int i;
574 583
575 LOG(GNUNET_ERROR_TYPE_DEBUG, 584 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "destroying task %p\n", 585 "destroying task %p\n",
577 t); 586 t);
578 587
579 if (GNUNET_YES == t->own_handles) 588 if (GNUNET_YES == t->own_handles)
589 {
590 for (i = 0; i != t->fds_len; ++i)
580 { 591 {
581 for (i = 0; i != t->fds_len; ++i) 592 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
582 { 593 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
583 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd; 594 if (fd)
584 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh; 595 {
585 if (fd) 596 GNUNET_NETWORK_socket_free_memory_only_ ((struct
586 { 597 GNUNET_NETWORK_Handle *) fd);
587 GNUNET_NETWORK_socket_free_memory_only_((struct GNUNET_NETWORK_Handle *)fd); 598 }
588 } 599 if (fh)
589 if (fh) 600 {
590 { 601 // FIXME: on WIN32 this is not enough! A function
591 // FIXME: on WIN32 this is not enough! A function 602 // GNUNET_DISK_file_free_memory_only would be nice
592 // GNUNET_DISK_file_free_memory_only would be nice 603 GNUNET_free ((void *) fh);
593 GNUNET_free((void *)fh); 604 }
594 }
595 }
596 } 605 }
606 }
597 if (t->fds_len > 1) 607 if (t->fds_len > 1)
598 { 608 {
599 GNUNET_array_grow(t->fds, t->fds_len, 0); 609 GNUNET_array_grow (t->fds, t->fds_len, 0);
600 } 610 }
601#if EXECINFO 611#if EXECINFO
602 GNUNET_free(t->backtrace_strings); 612 GNUNET_free (t->backtrace_strings);
603#endif 613#endif
604 GNUNET_free(t); 614 GNUNET_free (t);
605} 615}
606 616
607 617
@@ -620,7 +630,7 @@ static pid_t my_pid;
620 * Signal handler called for SIGPIPE. 630 * Signal handler called for SIGPIPE.
621 */ 631 */
622static void 632static void
623sighandler_pipe() 633sighandler_pipe ()
624{ 634{
625 return; 635 return;
626} 636}
@@ -634,37 +644,37 @@ sighandler_pipe()
634// * 644// *
635// * @param ms how many ms to wait 645// * @param ms how many ms to wait
636// */ 646// */
637//static void 647// static void
638//short_wait (unsigned int ms) 648// short_wait (unsigned int ms)
639//{ 649// {
640// struct GNUNET_TIME_Relative timeout; 650// struct GNUNET_TIME_Relative timeout;
641// 651//
642// timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms); 652// timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
643// (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout); 653// (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
644//} 654// }
645 655
646 656
647/** 657/**
648 * Signal handler called for signals that should cause us to shutdown. 658 * Signal handler called for signals that should cause us to shutdown.
649 */ 659 */
650static void 660static void
651sighandler_shutdown() 661sighandler_shutdown ()
652{ 662{
653 static char c; 663 static char c;
654 int old_errno = errno; /* backup errno */ 664 int old_errno = errno; /* backup errno */
655 665
656 if (getpid() != my_pid) 666 if (getpid () != my_pid)
657 _exit(1); /* we have fork'ed since the signal handler was created, 667 _exit (1); /* we have fork'ed since the signal handler was created,
658 * ignore the signal, see https://gnunet.org/vfork discussion */ 668 * ignore the signal, see https://gnunet.org/vfork discussion */
659 GNUNET_DISK_file_write(GNUNET_DISK_pipe_handle 669 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
660 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), 670 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
661 &c, sizeof(c)); 671 &c, sizeof(c));
662 errno = old_errno; 672 errno = old_errno;
663} 673}
664 674
665 675
666static void 676static void
667shutdown_if_no_lifeness() 677shutdown_if_no_lifeness ()
668{ 678{
669 struct GNUNET_SCHEDULER_Task *t; 679 struct GNUNET_SCHEDULER_Task *t;
670 680
@@ -680,13 +690,13 @@ shutdown_if_no_lifeness()
680 if (GNUNET_YES == t->lifeness) 690 if (GNUNET_YES == t->lifeness)
681 return; 691 return;
682 /* No lifeness! */ 692 /* No lifeness! */
683 GNUNET_SCHEDULER_shutdown(); 693 GNUNET_SCHEDULER_shutdown ();
684} 694}
685 695
686 696
687static int 697static int
688select_loop(struct GNUNET_SCHEDULER_Handle *sh, 698select_loop (struct GNUNET_SCHEDULER_Handle *sh,
689 struct DriverContext *context); 699 struct DriverContext *context);
690 700
691 701
692/** 702/**
@@ -704,26 +714,26 @@ select_loop(struct GNUNET_SCHEDULER_Handle *sh,
704 * @param task_cls closure of @a task 714 * @param task_cls closure of @a task
705 */ 715 */
706void 716void
707GNUNET_SCHEDULER_run(GNUNET_SCHEDULER_TaskCallback task, 717GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
708 void *task_cls) 718 void *task_cls)
709{ 719{
710 struct GNUNET_SCHEDULER_Handle *sh; 720 struct GNUNET_SCHEDULER_Handle *sh;
711 struct GNUNET_SCHEDULER_Driver *driver; 721 struct GNUNET_SCHEDULER_Driver *driver;
712 struct DriverContext context = { .scheduled_head = NULL, 722 struct DriverContext context = { .scheduled_head = NULL,
713 .scheduled_tail = NULL, 723 .scheduled_tail = NULL,
714 .timeout = GNUNET_TIME_absolute_get() }; 724 .timeout = GNUNET_TIME_absolute_get () };
715 725
716 driver = GNUNET_SCHEDULER_driver_select(); 726 driver = GNUNET_SCHEDULER_driver_select ();
717 driver->cls = &context; 727 driver->cls = &context;
718 sh = GNUNET_SCHEDULER_driver_init(driver); 728 sh = GNUNET_SCHEDULER_driver_init (driver);
719 GNUNET_SCHEDULER_add_with_reason_and_priority(task, 729 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
720 task_cls, 730 task_cls,
721 GNUNET_SCHEDULER_REASON_STARTUP, 731 GNUNET_SCHEDULER_REASON_STARTUP,
722 GNUNET_SCHEDULER_PRIORITY_DEFAULT); 732 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
723 select_loop(sh, 733 select_loop (sh,
724 &context); 734 &context);
725 GNUNET_SCHEDULER_driver_done(sh); 735 GNUNET_SCHEDULER_driver_done (sh);
726 GNUNET_free(driver); 736 GNUNET_free (driver);
727} 737}
728 738
729 739
@@ -734,9 +744,9 @@ GNUNET_SCHEDULER_run(GNUNET_SCHEDULER_TaskCallback task,
734 * @return current tasks' scheduler context 744 * @return current tasks' scheduler context
735 */ 745 */
736const struct GNUNET_SCHEDULER_TaskContext * 746const struct GNUNET_SCHEDULER_TaskContext *
737GNUNET_SCHEDULER_get_task_context() 747GNUNET_SCHEDULER_get_task_context ()
738{ 748{
739 GNUNET_assert(NULL != active_task); 749 GNUNET_assert (NULL != active_task);
740 return &tc; 750 return &tc;
741} 751}
742 752
@@ -751,33 +761,33 @@ GNUNET_SCHEDULER_get_task_context()
751 * @return number of tasks pending right now 761 * @return number of tasks pending right now
752 */ 762 */
753unsigned int 763unsigned int
754GNUNET_SCHEDULER_get_load(enum GNUNET_SCHEDULER_Priority p) 764GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
755{ 765{
756 struct GNUNET_SCHEDULER_Task *pos; 766 struct GNUNET_SCHEDULER_Task *pos;
757 unsigned int ret; 767 unsigned int ret;
758 768
759 GNUNET_assert(NULL != active_task); 769 GNUNET_assert (NULL != active_task);
760 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT) 770 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
761 return ready_count; 771 return ready_count;
762 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP) 772 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
763 p = current_priority; 773 p = current_priority;
764 ret = 0; 774 ret = 0;
765 for (pos = ready_head[check_priority(p)]; NULL != pos; pos = pos->next) 775 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
766 ret++; 776 ret++;
767 return ret; 777 return ret;
768} 778}
769 779
770 780
771void 781void
772init_fd_info(struct GNUNET_SCHEDULER_Task *t, 782init_fd_info (struct GNUNET_SCHEDULER_Task *t,
773 const struct GNUNET_NETWORK_Handle *const *read_nh, 783 const struct GNUNET_NETWORK_Handle *const *read_nh,
774 unsigned int read_nh_len, 784 unsigned int read_nh_len,
775 const struct GNUNET_NETWORK_Handle *const *write_nh, 785 const struct GNUNET_NETWORK_Handle *const *write_nh,
776 unsigned int write_nh_len, 786 unsigned int write_nh_len,
777 const struct GNUNET_DISK_FileHandle *const *read_fh, 787 const struct GNUNET_DISK_FileHandle *const *read_fh,
778 unsigned int read_fh_len, 788 unsigned int read_fh_len,
779 const struct GNUNET_DISK_FileHandle *const *write_fh, 789 const struct GNUNET_DISK_FileHandle *const *write_fh,
780 unsigned int write_fh_len) 790 unsigned int write_fh_len)
781{ 791{
782 // FIXME: if we have exactly two network handles / exactly two file handles 792 // FIXME: if we have exactly two network handles / exactly two file handles
783 // and they are equal, we can make one FdInfo with both 793 // and they are equal, we can make one FdInfo with both
@@ -786,90 +796,90 @@ init_fd_info(struct GNUNET_SCHEDULER_Task *t,
786 796
787 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len; 797 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
788 if (1 == t->fds_len) 798 if (1 == t->fds_len)
799 {
800 fdi = &t->fdx;
801 t->fds = fdi;
802 if (1 == read_nh_len)
789 { 803 {
790 fdi = &t->fdx; 804 GNUNET_assert (NULL != read_nh);
791 t->fds = fdi; 805 GNUNET_assert (NULL != *read_nh);
792 if (1 == read_nh_len) 806 fdi->fd = *read_nh;
793 { 807 fdi->et = GNUNET_SCHEDULER_ET_IN;
794 GNUNET_assert(NULL != read_nh); 808 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
795 GNUNET_assert(NULL != *read_nh); 809 t->read_fd = fdi->sock;
796 fdi->fd = *read_nh; 810 t->write_fd = -1;
797 fdi->et = GNUNET_SCHEDULER_ET_IN;
798 fdi->sock = GNUNET_NETWORK_get_fd(*read_nh);
799 t->read_fd = fdi->sock;
800 t->write_fd = -1;
801 }
802 else if (1 == write_nh_len)
803 {
804 GNUNET_assert(NULL != write_nh);
805 GNUNET_assert(NULL != *write_nh);
806 fdi->fd = *write_nh;
807 fdi->et = GNUNET_SCHEDULER_ET_OUT;
808 fdi->sock = GNUNET_NETWORK_get_fd(*write_nh);
809 t->read_fd = -1;
810 t->write_fd = fdi->sock;
811 }
812 else if (1 == read_fh_len)
813 {
814 GNUNET_assert(NULL != read_fh);
815 GNUNET_assert(NULL != *read_fh);
816 fdi->fh = *read_fh;
817 fdi->et = GNUNET_SCHEDULER_ET_IN;
818 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
819 t->read_fd = fdi->sock;
820 t->write_fd = -1;
821 }
822 else
823 {
824 GNUNET_assert(NULL != write_fh);
825 GNUNET_assert(NULL != *write_fh);
826 fdi->fh = *write_fh;
827 fdi->et = GNUNET_SCHEDULER_ET_OUT;
828 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
829 t->read_fd = -1;
830 t->write_fd = fdi->sock;
831 }
832 } 811 }
833 else 812 else if (1 == write_nh_len)
834 { 813 {
835 fdi = GNUNET_new_array(t->fds_len, struct GNUNET_SCHEDULER_FdInfo); 814 GNUNET_assert (NULL != write_nh);
836 t->fds = fdi; 815 GNUNET_assert (NULL != *write_nh);
816 fdi->fd = *write_nh;
817 fdi->et = GNUNET_SCHEDULER_ET_OUT;
818 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
837 t->read_fd = -1; 819 t->read_fd = -1;
820 t->write_fd = fdi->sock;
821 }
822 else if (1 == read_fh_len)
823 {
824 GNUNET_assert (NULL != read_fh);
825 GNUNET_assert (NULL != *read_fh);
826 fdi->fh = *read_fh;
827 fdi->et = GNUNET_SCHEDULER_ET_IN;
828 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
829 t->read_fd = fdi->sock;
838 t->write_fd = -1; 830 t->write_fd = -1;
839 unsigned int i;
840 for (i = 0; i != read_nh_len; ++i)
841 {
842 fdi->fd = read_nh[i];
843 GNUNET_assert(NULL != fdi->fd);
844 fdi->et = GNUNET_SCHEDULER_ET_IN;
845 fdi->sock = GNUNET_NETWORK_get_fd(read_nh[i]);
846 ++fdi;
847 }
848 for (i = 0; i != write_nh_len; ++i)
849 {
850 fdi->fd = write_nh[i];
851 GNUNET_assert(NULL != fdi->fd);
852 fdi->et = GNUNET_SCHEDULER_ET_OUT;
853 fdi->sock = GNUNET_NETWORK_get_fd(write_nh[i]);
854 ++fdi;
855 }
856 for (i = 0; i != read_fh_len; ++i)
857 {
858 fdi->fh = read_fh[i];
859 GNUNET_assert(NULL != fdi->fh);
860 fdi->et = GNUNET_SCHEDULER_ET_IN;
861 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
862 ++fdi;
863 }
864 for (i = 0; i != write_fh_len; ++i)
865 {
866 fdi->fh = write_fh[i];
867 GNUNET_assert(NULL != fdi->fh);
868 fdi->et = GNUNET_SCHEDULER_ET_OUT;
869 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
870 ++fdi;
871 }
872 } 831 }
832 else
833 {
834 GNUNET_assert (NULL != write_fh);
835 GNUNET_assert (NULL != *write_fh);
836 fdi->fh = *write_fh;
837 fdi->et = GNUNET_SCHEDULER_ET_OUT;
838 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
839 t->read_fd = -1;
840 t->write_fd = fdi->sock;
841 }
842 }
843 else
844 {
845 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
846 t->fds = fdi;
847 t->read_fd = -1;
848 t->write_fd = -1;
849 unsigned int i;
850 for (i = 0; i != read_nh_len; ++i)
851 {
852 fdi->fd = read_nh[i];
853 GNUNET_assert (NULL != fdi->fd);
854 fdi->et = GNUNET_SCHEDULER_ET_IN;
855 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
856 ++fdi;
857 }
858 for (i = 0; i != write_nh_len; ++i)
859 {
860 fdi->fd = write_nh[i];
861 GNUNET_assert (NULL != fdi->fd);
862 fdi->et = GNUNET_SCHEDULER_ET_OUT;
863 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
864 ++fdi;
865 }
866 for (i = 0; i != read_fh_len; ++i)
867 {
868 fdi->fh = read_fh[i];
869 GNUNET_assert (NULL != fdi->fh);
870 fdi->et = GNUNET_SCHEDULER_ET_IN;
871 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
872 ++fdi;
873 }
874 for (i = 0; i != write_fh_len; ++i)
875 {
876 fdi->fh = write_fh[i];
877 GNUNET_assert (NULL != fdi->fh);
878 fdi->et = GNUNET_SCHEDULER_ET_OUT;
879 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
880 ++fdi;
881 }
882 }
873} 883}
874 884
875 885
@@ -887,56 +897,56 @@ init_fd_info(struct GNUNET_SCHEDULER_Task *t,
887 * @a driver_func on it, or -1 if no updating not desired. 897 * @a driver_func on it, or -1 if no updating not desired.
888 */ 898 */
889static void 899static void
890driver_add_multiple(struct GNUNET_SCHEDULER_Task *t) 900driver_add_multiple (struct GNUNET_SCHEDULER_Task *t)
891{ 901{
892 struct GNUNET_SCHEDULER_FdInfo *fdi; 902 struct GNUNET_SCHEDULER_FdInfo *fdi;
893 int success = GNUNET_YES; 903 int success = GNUNET_YES;
894 904
895 for (unsigned int i = 0; i != t->fds_len; ++i) 905 for (unsigned int i = 0; i != t->fds_len; ++i)
896 { 906 {
897 fdi = &t->fds[i]; 907 fdi = &t->fds[i];
898 success = scheduler_driver->add(scheduler_driver->cls, 908 success = scheduler_driver->add (scheduler_driver->cls,
899 t, 909 t,
900 fdi) && success; 910 fdi) && success;
901 fdi->et = GNUNET_SCHEDULER_ET_NONE; 911 fdi->et = GNUNET_SCHEDULER_ET_NONE;
902 } 912 }
903 if (GNUNET_YES != success) 913 if (GNUNET_YES != success)
904 { 914 {
905 LOG(GNUNET_ERROR_TYPE_ERROR, 915 LOG (GNUNET_ERROR_TYPE_ERROR,
906 "driver could not add task\n"); 916 "driver could not add task\n");
907 } 917 }
908} 918}
909 919
910 920
911static void 921static void
912install_parent_control_handler(void *cls) 922install_parent_control_handler (void *cls)
913{ 923{
914 (void)cls; 924 (void) cls;
915 install_parent_control_task = NULL; 925 install_parent_control_task = NULL;
916 GNUNET_OS_install_parent_control_handler(NULL); 926 GNUNET_OS_install_parent_control_handler (NULL);
917} 927}
918 928
919 929
920static void 930static void
921shutdown_pipe_cb(void *cls) 931shutdown_pipe_cb (void *cls)
922{ 932{
923 char c; 933 char c;
924 const struct GNUNET_DISK_FileHandle *pr; 934 const struct GNUNET_DISK_FileHandle *pr;
925 935
926 (void)cls; 936 (void) cls;
927 shutdown_pipe_task = NULL; 937 shutdown_pipe_task = NULL;
928 pr = GNUNET_DISK_pipe_handle(shutdown_pipe_handle, 938 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
929 GNUNET_DISK_PIPE_END_READ); 939 GNUNET_DISK_PIPE_END_READ);
930 GNUNET_assert(!GNUNET_DISK_handle_invalid(pr)); 940 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
931 /* consume the signal */ 941 /* consume the signal */
932 GNUNET_DISK_file_read(pr, &c, sizeof(c)); 942 GNUNET_DISK_file_read (pr, &c, sizeof(c));
933 /* mark all active tasks as ready due to shutdown */ 943 /* mark all active tasks as ready due to shutdown */
934 GNUNET_SCHEDULER_shutdown(); 944 GNUNET_SCHEDULER_shutdown ();
935 shutdown_pipe_task = 945 shutdown_pipe_task =
936 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 946 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
937 pr, 947 pr,
938 &shutdown_pipe_cb, 948 &shutdown_pipe_cb,
939 NULL); 949 NULL);
940} 950}
941 951
942 952
@@ -953,62 +963,62 @@ shutdown_pipe_cb(void *cls)
953 * @return original closure of the task 963 * @return original closure of the task
954 */ 964 */
955void * 965void *
956GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task) 966GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
957{ 967{
958 enum GNUNET_SCHEDULER_Priority p; 968 enum GNUNET_SCHEDULER_Priority p;
959 int is_fd_task; 969 int is_fd_task;
960 void *ret; 970 void *ret;
961 971
962 LOG(GNUNET_ERROR_TYPE_DEBUG, 972 LOG (GNUNET_ERROR_TYPE_DEBUG,
963 "canceling task %p\n", 973 "canceling task %p\n",
964 task); 974 task);
965 975
966 /* scheduler must be running */ 976 /* scheduler must be running */
967 GNUNET_assert(NULL != scheduler_driver); 977 GNUNET_assert (NULL != scheduler_driver);
968 is_fd_task = (NULL != task->fds); 978 is_fd_task = (NULL != task->fds);
969 if (is_fd_task) 979 if (is_fd_task)
980 {
981 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
982 if (GNUNET_OK != del_result)
970 { 983 {
971 int del_result = scheduler_driver->del(scheduler_driver->cls, task); 984 LOG (GNUNET_ERROR_TYPE_ERROR,
972 if (GNUNET_OK != del_result) 985 "driver could not delete task\n");
973 { 986 GNUNET_assert (0);
974 LOG(GNUNET_ERROR_TYPE_ERROR,
975 "driver could not delete task\n");
976 GNUNET_assert(0);
977 }
978 } 987 }
979 if (!task->in_ready_list) 988 }
989 if (! task->in_ready_list)
990 {
991 if (is_fd_task)
980 { 992 {
981 if (is_fd_task) 993 GNUNET_CONTAINER_DLL_remove (pending_head,
982 { 994 pending_tail,
983 GNUNET_CONTAINER_DLL_remove(pending_head, 995 task);
984 pending_tail,
985 task);
986 }
987 else if (GNUNET_YES == task->on_shutdown)
988 {
989 GNUNET_CONTAINER_DLL_remove(shutdown_head,
990 shutdown_tail,
991 task);
992 }
993 else
994 {
995 GNUNET_CONTAINER_DLL_remove(pending_timeout_head,
996 pending_timeout_tail,
997 task);
998 if (pending_timeout_last == task)
999 pending_timeout_last = NULL;
1000 }
1001 } 996 }
1002 else 997 else if (GNUNET_YES == task->on_shutdown)
1003 { 998 {
1004 p = check_priority(task->priority); 999 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1005 GNUNET_CONTAINER_DLL_remove(ready_head[p], 1000 shutdown_tail,
1006 ready_tail[p], 1001 task);
1007 task); 1002 }
1008 ready_count--; 1003 else
1004 {
1005 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1006 pending_timeout_tail,
1007 task);
1008 if (pending_timeout_last == task)
1009 pending_timeout_last = NULL;
1009 } 1010 }
1011 }
1012 else
1013 {
1014 p = check_priority (task->priority);
1015 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1016 ready_tail[p],
1017 task);
1018 ready_count--;
1019 }
1010 ret = task->callback_cls; 1020 ret = task->callback_cls;
1011 destroy_task(task); 1021 destroy_task (task);
1012 return ret; 1022 return ret;
1013} 1023}
1014 1024
@@ -1019,19 +1029,19 @@ GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
1019 * @param t task to initialize 1029 * @param t task to initialize
1020 */ 1030 */
1021static void 1031static void
1022init_backtrace(struct GNUNET_SCHEDULER_Task *t) 1032init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1023{ 1033{
1024#if EXECINFO 1034#if EXECINFO
1025 void *backtrace_array[MAX_TRACE_DEPTH]; 1035 void *backtrace_array[MAX_TRACE_DEPTH];
1026 1036
1027 t->num_backtrace_strings 1037 t->num_backtrace_strings
1028 = backtrace(backtrace_array, MAX_TRACE_DEPTH); 1038 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1029 t->backtrace_strings = 1039 t->backtrace_strings =
1030 backtrace_symbols(backtrace_array, 1040 backtrace_symbols (backtrace_array,
1031 t->num_backtrace_strings); 1041 t->num_backtrace_strings);
1032 dump_backtrace(t); 1042 dump_backtrace (t);
1033#else 1043#else
1034 (void)t; 1044 (void) t;
1035#endif 1045#endif
1036} 1046}
1037 1047
@@ -1047,32 +1057,35 @@ init_backtrace(struct GNUNET_SCHEDULER_Task *t)
1047 * @param priority priority to use for the task 1057 * @param priority priority to use for the task
1048 */ 1058 */
1049void 1059void
1050GNUNET_SCHEDULER_add_with_reason_and_priority(GNUNET_SCHEDULER_TaskCallback task, 1060GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback
1051 void *task_cls, 1061 task,
1052 enum GNUNET_SCHEDULER_Reason reason, 1062 void *task_cls,
1053 enum GNUNET_SCHEDULER_Priority priority) 1063 enum GNUNET_SCHEDULER_Reason
1064 reason,
1065 enum GNUNET_SCHEDULER_Priority
1066 priority)
1054{ 1067{
1055 struct GNUNET_SCHEDULER_Task *t; 1068 struct GNUNET_SCHEDULER_Task *t;
1056 1069
1057 /* scheduler must be running */ 1070 /* scheduler must be running */
1058 GNUNET_assert(NULL != scheduler_driver); 1071 GNUNET_assert (NULL != scheduler_driver);
1059 GNUNET_assert(NULL != task); 1072 GNUNET_assert (NULL != task);
1060 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1073 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1061 t->read_fd = -1; 1074 t->read_fd = -1;
1062 t->write_fd = -1; 1075 t->write_fd = -1;
1063 t->callback = task; 1076 t->callback = task;
1064 t->callback_cls = task_cls; 1077 t->callback_cls = task_cls;
1065#if PROFILE_DELAYS 1078#if PROFILE_DELAYS
1066 t->start_time = GNUNET_TIME_absolute_get(); 1079 t->start_time = GNUNET_TIME_absolute_get ();
1067#endif 1080#endif
1068 t->reason = reason; 1081 t->reason = reason;
1069 t->priority = check_priority(priority); 1082 t->priority = check_priority (priority);
1070 t->lifeness = current_lifeness; 1083 t->lifeness = current_lifeness;
1071 LOG(GNUNET_ERROR_TYPE_DEBUG, 1084 LOG (GNUNET_ERROR_TYPE_DEBUG,
1072 "Adding continuation task %p\n", 1085 "Adding continuation task %p\n",
1073 t); 1086 t);
1074 init_backtrace(t); 1087 init_backtrace (t);
1075 queue_ready_task(t); 1088 queue_ready_task (t);
1076} 1089}
1077 1090
1078 1091
@@ -1088,68 +1101,69 @@ GNUNET_SCHEDULER_add_with_reason_and_priority(GNUNET_SCHEDULER_TaskCallback task
1088 * only valid until @a task is started! 1101 * only valid until @a task is started!
1089 */ 1102 */
1090struct GNUNET_SCHEDULER_Task * 1103struct GNUNET_SCHEDULER_Task *
1091GNUNET_SCHEDULER_add_at_with_priority(struct GNUNET_TIME_Absolute at, 1104GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1092 enum GNUNET_SCHEDULER_Priority priority, 1105 enum GNUNET_SCHEDULER_Priority priority,
1093 GNUNET_SCHEDULER_TaskCallback task, 1106 GNUNET_SCHEDULER_TaskCallback task,
1094 void *task_cls) 1107 void *task_cls)
1095{ 1108{
1096 struct GNUNET_SCHEDULER_Task *t; 1109 struct GNUNET_SCHEDULER_Task *t;
1097 struct GNUNET_SCHEDULER_Task *pos; 1110 struct GNUNET_SCHEDULER_Task *pos;
1098 struct GNUNET_SCHEDULER_Task *prev; 1111 struct GNUNET_SCHEDULER_Task *prev;
1099 1112
1100 /* scheduler must be running */ 1113 /* scheduler must be running */
1101 GNUNET_assert(NULL != scheduler_driver); 1114 GNUNET_assert (NULL != scheduler_driver);
1102 GNUNET_assert(NULL != task); 1115 GNUNET_assert (NULL != task);
1103 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1116 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1104 GNUNET_async_scope_get(&t->scope); 1117 GNUNET_async_scope_get (&t->scope);
1105 t->callback = task; 1118 t->callback = task;
1106 t->callback_cls = task_cls; 1119 t->callback_cls = task_cls;
1107 t->read_fd = -1; 1120 t->read_fd = -1;
1108 t->write_fd = -1; 1121 t->write_fd = -1;
1109#if PROFILE_DELAYS 1122#if PROFILE_DELAYS
1110 t->start_time = GNUNET_TIME_absolute_get(); 1123 t->start_time = GNUNET_TIME_absolute_get ();
1111#endif 1124#endif
1112 t->timeout = at; 1125 t->timeout = at;
1113 t->priority = check_priority(priority); 1126 t->priority = check_priority (priority);
1114 t->lifeness = current_lifeness; 1127 t->lifeness = current_lifeness;
1115 /* try tail first (optimization in case we are 1128 /* try tail first (optimization in case we are
1116 * appending to a long list of tasks with timeouts) */ 1129 * appending to a long list of tasks with timeouts) */
1117 if ((NULL == pending_timeout_head) || 1130 if ((NULL == pending_timeout_head) ||
1118 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us)) 1131 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us))
1119 { 1132 {
1120 GNUNET_CONTAINER_DLL_insert(pending_timeout_head, 1133 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1121 pending_timeout_tail, 1134 pending_timeout_tail,
1122 t); 1135 t);
1123 } 1136 }
1124 else 1137 else
1138 {
1139 /* first move from heuristic start backwards to before start time */
1140 prev = pending_timeout_last;
1141 while ((NULL != prev) &&
1142 (prev->timeout.abs_value_us > t->timeout.abs_value_us))
1143 prev = prev->prev;
1144 /* now, move from heuristic start (or head of list) forward to insertion point */
1145 if (NULL == prev)
1146 pos = pending_timeout_head;
1147 else
1148 pos = prev->next;
1149 while ((NULL != pos) && (pos->timeout.abs_value_us <=
1150 t->timeout.abs_value_us))
1125 { 1151 {
1126 /* first move from heuristic start backwards to before start time */ 1152 prev = pos;
1127 prev = pending_timeout_last; 1153 pos = pos->next;
1128 while ((NULL != prev) &&
1129 (prev->timeout.abs_value_us > t->timeout.abs_value_us))
1130 prev = prev->prev;
1131 /* now, move from heuristic start (or head of list) forward to insertion point */
1132 if (NULL == prev)
1133 pos = pending_timeout_head;
1134 else
1135 pos = prev->next;
1136 while ((NULL != pos) && (pos->timeout.abs_value_us <= t->timeout.abs_value_us))
1137 {
1138 prev = pos;
1139 pos = pos->next;
1140 }
1141 GNUNET_CONTAINER_DLL_insert_after(pending_timeout_head,
1142 pending_timeout_tail,
1143 prev,
1144 t);
1145 } 1154 }
1155 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1156 pending_timeout_tail,
1157 prev,
1158 t);
1159 }
1146 /* finally, update heuristic insertion point to last insertion... */ 1160 /* finally, update heuristic insertion point to last insertion... */
1147 pending_timeout_last = t; 1161 pending_timeout_last = t;
1148 1162
1149 LOG(GNUNET_ERROR_TYPE_DEBUG, 1163 LOG (GNUNET_ERROR_TYPE_DEBUG,
1150 "Adding task %p\n", 1164 "Adding task %p\n",
1151 t); 1165 t);
1152 init_backtrace(t); 1166 init_backtrace (t);
1153 return t; 1167 return t;
1154} 1168}
1155 1169
@@ -1166,15 +1180,17 @@ GNUNET_SCHEDULER_add_at_with_priority(struct GNUNET_TIME_Absolute at,
1166 * only valid until @a task is started! 1180 * only valid until @a task is started!
1167 */ 1181 */
1168struct GNUNET_SCHEDULER_Task * 1182struct GNUNET_SCHEDULER_Task *
1169GNUNET_SCHEDULER_add_delayed_with_priority(struct GNUNET_TIME_Relative delay, 1183GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1170 enum GNUNET_SCHEDULER_Priority priority, 1184 enum GNUNET_SCHEDULER_Priority
1171 GNUNET_SCHEDULER_TaskCallback task, 1185 priority,
1172 void *task_cls) 1186 GNUNET_SCHEDULER_TaskCallback task,
1187 void *task_cls)
1173{ 1188{
1174 return GNUNET_SCHEDULER_add_at_with_priority(GNUNET_TIME_relative_to_absolute(delay), 1189 return GNUNET_SCHEDULER_add_at_with_priority (
1175 priority, 1190 GNUNET_TIME_relative_to_absolute (delay),
1176 task, 1191 priority,
1177 task_cls); 1192 task,
1193 task_cls);
1178} 1194}
1179 1195
1180 1196
@@ -1188,14 +1204,14 @@ GNUNET_SCHEDULER_add_delayed_with_priority(struct GNUNET_TIME_Relative delay,
1188 * only valid until @a task is started! 1204 * only valid until @a task is started!
1189 */ 1205 */
1190struct GNUNET_SCHEDULER_Task * 1206struct GNUNET_SCHEDULER_Task *
1191GNUNET_SCHEDULER_add_with_priority(enum GNUNET_SCHEDULER_Priority prio, 1207GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1192 GNUNET_SCHEDULER_TaskCallback task, 1208 GNUNET_SCHEDULER_TaskCallback task,
1193 void *task_cls) 1209 void *task_cls)
1194{ 1210{
1195 return GNUNET_SCHEDULER_add_delayed_with_priority(GNUNET_TIME_UNIT_ZERO, 1211 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1196 prio, 1212 prio,
1197 task, 1213 task,
1198 task_cls); 1214 task_cls);
1199} 1215}
1200 1216
1201 1217
@@ -1211,14 +1227,14 @@ GNUNET_SCHEDULER_add_with_priority(enum GNUNET_SCHEDULER_Priority prio,
1211 * only valid until @a task is started! 1227 * only valid until @a task is started!
1212 */ 1228 */
1213struct GNUNET_SCHEDULER_Task * 1229struct GNUNET_SCHEDULER_Task *
1214GNUNET_SCHEDULER_add_at(struct GNUNET_TIME_Absolute at, 1230GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1215 GNUNET_SCHEDULER_TaskCallback task, 1231 GNUNET_SCHEDULER_TaskCallback task,
1216 void *task_cls) 1232 void *task_cls)
1217{ 1233{
1218 return GNUNET_SCHEDULER_add_at_with_priority(at, 1234 return GNUNET_SCHEDULER_add_at_with_priority (at,
1219 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1235 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1220 task, 1236 task,
1221 task_cls); 1237 task_cls);
1222} 1238}
1223 1239
1224 1240
@@ -1234,14 +1250,14 @@ GNUNET_SCHEDULER_add_at(struct GNUNET_TIME_Absolute at,
1234 * only valid until @a task is started! 1250 * only valid until @a task is started!
1235 */ 1251 */
1236struct GNUNET_SCHEDULER_Task * 1252struct GNUNET_SCHEDULER_Task *
1237GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, 1253GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1238 GNUNET_SCHEDULER_TaskCallback task, 1254 GNUNET_SCHEDULER_TaskCallback task,
1239 void *task_cls) 1255 void *task_cls)
1240{ 1256{
1241 return GNUNET_SCHEDULER_add_delayed_with_priority(delay, 1257 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1242 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1258 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1243 task, 1259 task,
1244 task_cls); 1260 task_cls);
1245} 1261}
1246 1262
1247 1263
@@ -1261,12 +1277,12 @@ GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay,
1261 * only valid until @a task is started! 1277 * only valid until @a task is started!
1262 */ 1278 */
1263struct GNUNET_SCHEDULER_Task * 1279struct GNUNET_SCHEDULER_Task *
1264GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, 1280GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1265 void *task_cls) 1281 void *task_cls)
1266{ 1282{
1267 return GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_ZERO, 1283 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1268 task, 1284 task,
1269 task_cls); 1285 task_cls);
1270} 1286}
1271 1287
1272 1288
@@ -1281,34 +1297,34 @@ GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task,
1281 * only valid until @a task is started! 1297 * only valid until @a task is started!
1282 */ 1298 */
1283struct GNUNET_SCHEDULER_Task * 1299struct GNUNET_SCHEDULER_Task *
1284GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, 1300GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1285 void *task_cls) 1301 void *task_cls)
1286{ 1302{
1287 struct GNUNET_SCHEDULER_Task *t; 1303 struct GNUNET_SCHEDULER_Task *t;
1288 1304
1289 /* scheduler must be running */ 1305 /* scheduler must be running */
1290 GNUNET_assert(NULL != scheduler_driver); 1306 GNUNET_assert (NULL != scheduler_driver);
1291 GNUNET_assert(NULL != task); 1307 GNUNET_assert (NULL != task);
1292 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1308 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1293 GNUNET_async_scope_get(&t->scope); 1309 GNUNET_async_scope_get (&t->scope);
1294 t->callback = task; 1310 t->callback = task;
1295 t->callback_cls = task_cls; 1311 t->callback_cls = task_cls;
1296 t->read_fd = -1; 1312 t->read_fd = -1;
1297 t->write_fd = -1; 1313 t->write_fd = -1;
1298#if PROFILE_DELAYS 1314#if PROFILE_DELAYS
1299 t->start_time = GNUNET_TIME_absolute_get(); 1315 t->start_time = GNUNET_TIME_absolute_get ();
1300#endif 1316#endif
1301 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS; 1317 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1302 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN; 1318 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1303 t->on_shutdown = GNUNET_YES; 1319 t->on_shutdown = GNUNET_YES;
1304 t->lifeness = GNUNET_NO; 1320 t->lifeness = GNUNET_NO;
1305 GNUNET_CONTAINER_DLL_insert(shutdown_head, 1321 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1306 shutdown_tail, 1322 shutdown_tail,
1307 t); 1323 t);
1308 LOG(GNUNET_ERROR_TYPE_DEBUG, 1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "Adding shutdown task %p\n", 1325 "Adding shutdown task %p\n",
1310 t); 1326 t);
1311 init_backtrace(t); 1327 init_backtrace (t);
1312 return t; 1328 return t;
1313} 1329}
1314 1330
@@ -1328,13 +1344,13 @@ GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task,
1328 * only valid until @a task is started! 1344 * only valid until @a task is started!
1329 */ 1345 */
1330struct GNUNET_SCHEDULER_Task * 1346struct GNUNET_SCHEDULER_Task *
1331GNUNET_SCHEDULER_add_now_with_lifeness(int lifeness, 1347GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1332 GNUNET_SCHEDULER_TaskCallback task, 1348 GNUNET_SCHEDULER_TaskCallback task,
1333 void *task_cls) 1349 void *task_cls)
1334{ 1350{
1335 struct GNUNET_SCHEDULER_Task *ret; 1351 struct GNUNET_SCHEDULER_Task *ret;
1336 1352
1337 ret = GNUNET_SCHEDULER_add_now(task, task_cls); 1353 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1338 ret->lifeness = lifeness; 1354 ret->lifeness = lifeness;
1339 return ret; 1355 return ret;
1340} 1356}
@@ -1348,21 +1364,21 @@ GNUNET_SCHEDULER_add_now_with_lifeness(int lifeness,
1348 * @param raw_fd the raw file descriptor to check 1364 * @param raw_fd the raw file descriptor to check
1349 */ 1365 */
1350void 1366void
1351check_fd(struct GNUNET_SCHEDULER_Task *t, int raw_fd) 1367check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1352{ 1368{
1353 if (-1 != raw_fd) 1369 if (-1 != raw_fd)
1354 { 1370 {
1355 int flags = fcntl(raw_fd, F_GETFD); 1371 int flags = fcntl (raw_fd, F_GETFD);
1356 1372
1357 if ((flags == -1) && (errno == EBADF)) 1373 if ((flags == -1) && (errno == EBADF))
1358 { 1374 {
1359 LOG(GNUNET_ERROR_TYPE_ERROR, 1375 LOG (GNUNET_ERROR_TYPE_ERROR,
1360 "Got invalid file descriptor %d!\n", 1376 "Got invalid file descriptor %d!\n",
1361 raw_fd); 1377 raw_fd);
1362 init_backtrace(t); 1378 init_backtrace (t);
1363 GNUNET_assert(0); 1379 GNUNET_assert (0);
1364 }
1365 } 1380 }
1381 }
1366} 1382}
1367#endif 1383#endif
1368 1384
@@ -1393,52 +1409,53 @@ check_fd(struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1393 * only valid until @a task is started! 1409 * only valid until @a task is started!
1394 */ 1410 */
1395static struct GNUNET_SCHEDULER_Task * 1411static struct GNUNET_SCHEDULER_Task *
1396add_without_sets(struct GNUNET_TIME_Relative delay, 1412add_without_sets (struct GNUNET_TIME_Relative delay,
1397 enum GNUNET_SCHEDULER_Priority priority, 1413 enum GNUNET_SCHEDULER_Priority priority,
1398 const struct GNUNET_NETWORK_Handle *read_nh, 1414 const struct GNUNET_NETWORK_Handle *read_nh,
1399 const struct GNUNET_NETWORK_Handle *write_nh, 1415 const struct GNUNET_NETWORK_Handle *write_nh,
1400 const struct GNUNET_DISK_FileHandle *read_fh, 1416 const struct GNUNET_DISK_FileHandle *read_fh,
1401 const struct GNUNET_DISK_FileHandle *write_fh, 1417 const struct GNUNET_DISK_FileHandle *write_fh,
1402 GNUNET_SCHEDULER_TaskCallback task, 1418 GNUNET_SCHEDULER_TaskCallback task,
1403 void *task_cls) 1419 void *task_cls)
1404{ 1420{
1405 struct GNUNET_SCHEDULER_Task *t; 1421 struct GNUNET_SCHEDULER_Task *t;
1406 1422
1407 /* scheduler must be running */ 1423 /* scheduler must be running */
1408 GNUNET_assert(NULL != scheduler_driver); 1424 GNUNET_assert (NULL != scheduler_driver);
1409 GNUNET_assert(NULL != task); 1425 GNUNET_assert (NULL != task);
1410 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1426 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1411 GNUNET_async_scope_get(&t->scope); 1427 GNUNET_async_scope_get (&t->scope);
1412 init_fd_info(t, 1428 init_fd_info (t,
1413 &read_nh, 1429 &read_nh,
1414 read_nh ? 1 : 0, 1430 read_nh ? 1 : 0,
1415 &write_nh, 1431 &write_nh,
1416 write_nh ? 1 : 0, 1432 write_nh ? 1 : 0,
1417 &read_fh, 1433 &read_fh,
1418 read_fh ? 1 : 0, 1434 read_fh ? 1 : 0,
1419 &write_fh, 1435 &write_fh,
1420 write_fh ? 1 : 0); 1436 write_fh ? 1 : 0);
1421 t->callback = task; 1437 t->callback = task;
1422 t->callback_cls = task_cls; 1438 t->callback_cls = task_cls;
1423#if DEBUG_FDS 1439#if DEBUG_FDS
1424 check_fd(t, NULL != read_nh ? GNUNET_NETWORK_get_fd(read_nh) : -1); 1440 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1425 check_fd(t, NULL != write_nh ? GNUNET_NETWORK_get_fd(write_nh) : -1); 1441 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1426 check_fd(t, NULL != read_fh ? read_fh->fd : -1); 1442 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1427 check_fd(t, NULL != write_fh ? write_fh->fd : -1); 1443 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1428#endif 1444#endif
1429#if PROFILE_DELAYS 1445#if PROFILE_DELAYS
1430 t->start_time = GNUNET_TIME_absolute_get(); 1446 t->start_time = GNUNET_TIME_absolute_get ();
1431#endif 1447#endif
1432 t->timeout = GNUNET_TIME_relative_to_absolute(delay); 1448 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1433 t->priority = check_priority((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority); 1449 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ?
1450 current_priority : priority);
1434 t->lifeness = current_lifeness; 1451 t->lifeness = current_lifeness;
1435 GNUNET_CONTAINER_DLL_insert(pending_head, 1452 GNUNET_CONTAINER_DLL_insert (pending_head,
1436 pending_tail, 1453 pending_tail,
1437 t); 1454 t);
1438 driver_add_multiple(t); 1455 driver_add_multiple (t);
1439 max_priority_added = GNUNET_MAX(max_priority_added, 1456 max_priority_added = GNUNET_MAX (max_priority_added,
1440 t->priority); 1457 t->priority);
1441 init_backtrace(t); 1458 init_backtrace (t);
1442 return t; 1459 return t;
1443} 1460}
1444 1461
@@ -1464,14 +1481,14 @@ add_without_sets(struct GNUNET_TIME_Relative delay,
1464 * only valid until @a task is started! 1481 * only valid until @a task is started!
1465 */ 1482 */
1466struct GNUNET_SCHEDULER_Task * 1483struct GNUNET_SCHEDULER_Task *
1467GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, 1484GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1468 struct GNUNET_NETWORK_Handle *rfd, 1485 struct GNUNET_NETWORK_Handle *rfd,
1469 GNUNET_SCHEDULER_TaskCallback task, 1486 GNUNET_SCHEDULER_TaskCallback task,
1470 void *task_cls) 1487 void *task_cls)
1471{ 1488{
1472 return GNUNET_SCHEDULER_add_read_net_with_priority(delay, 1489 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1473 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1490 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1474 rfd, task, task_cls); 1491 rfd, task, task_cls);
1475} 1492}
1476 1493
1477 1494
@@ -1498,17 +1515,18 @@ GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay,
1498 * only valid until @a task is started! 1515 * only valid until @a task is started!
1499 */ 1516 */
1500struct GNUNET_SCHEDULER_Task * 1517struct GNUNET_SCHEDULER_Task *
1501GNUNET_SCHEDULER_add_read_net_with_priority(struct GNUNET_TIME_Relative delay, 1518GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1502 enum GNUNET_SCHEDULER_Priority priority, 1519 enum GNUNET_SCHEDULER_Priority
1503 struct GNUNET_NETWORK_Handle *rfd, 1520 priority,
1504 GNUNET_SCHEDULER_TaskCallback task, 1521 struct GNUNET_NETWORK_Handle *rfd,
1505 void *task_cls) 1522 GNUNET_SCHEDULER_TaskCallback task,
1523 void *task_cls)
1506{ 1524{
1507 return GNUNET_SCHEDULER_add_net_with_priority(delay, priority, 1525 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1508 rfd, 1526 rfd,
1509 GNUNET_YES, 1527 GNUNET_YES,
1510 GNUNET_NO, 1528 GNUNET_NO,
1511 task, task_cls); 1529 task, task_cls);
1512} 1530}
1513 1531
1514 1532
@@ -1534,16 +1552,16 @@ GNUNET_SCHEDULER_add_read_net_with_priority(struct GNUNET_TIME_Relative delay,
1534 * only valid until @a task is started! 1552 * only valid until @a task is started!
1535 */ 1553 */
1536struct GNUNET_SCHEDULER_Task * 1554struct GNUNET_SCHEDULER_Task *
1537GNUNET_SCHEDULER_add_write_net(struct GNUNET_TIME_Relative delay, 1555GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1538 struct GNUNET_NETWORK_Handle *wfd, 1556 struct GNUNET_NETWORK_Handle *wfd,
1539 GNUNET_SCHEDULER_TaskCallback task, 1557 GNUNET_SCHEDULER_TaskCallback task,
1540 void *task_cls) 1558 void *task_cls)
1541{ 1559{
1542 return GNUNET_SCHEDULER_add_net_with_priority(delay, 1560 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1543 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1561 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1544 wfd, 1562 wfd,
1545 GNUNET_NO, GNUNET_YES, 1563 GNUNET_NO, GNUNET_YES,
1546 task, task_cls); 1564 task, task_cls);
1547} 1565}
1548 1566
1549/** 1567/**
@@ -1570,24 +1588,24 @@ GNUNET_SCHEDULER_add_write_net(struct GNUNET_TIME_Relative delay,
1570 * only valid until "task" is started! 1588 * only valid until "task" is started!
1571 */ 1589 */
1572struct GNUNET_SCHEDULER_Task * 1590struct GNUNET_SCHEDULER_Task *
1573GNUNET_SCHEDULER_add_net_with_priority(struct GNUNET_TIME_Relative delay, 1591GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1574 enum GNUNET_SCHEDULER_Priority priority, 1592 enum GNUNET_SCHEDULER_Priority priority,
1575 struct GNUNET_NETWORK_Handle *fd, 1593 struct GNUNET_NETWORK_Handle *fd,
1576 int on_read, 1594 int on_read,
1577 int on_write, 1595 int on_write,
1578 GNUNET_SCHEDULER_TaskCallback task, 1596 GNUNET_SCHEDULER_TaskCallback task,
1579 void *task_cls) 1597 void *task_cls)
1580{ 1598{
1581 /* scheduler must be running */ 1599 /* scheduler must be running */
1582 GNUNET_assert(NULL != scheduler_driver); 1600 GNUNET_assert (NULL != scheduler_driver);
1583 GNUNET_assert(on_read || on_write); 1601 GNUNET_assert (on_read || on_write);
1584 GNUNET_assert(GNUNET_NETWORK_get_fd(fd) >= 0); 1602 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1585 return add_without_sets(delay, priority, 1603 return add_without_sets (delay, priority,
1586 on_read ? fd : NULL, 1604 on_read ? fd : NULL,
1587 on_write ? fd : NULL, 1605 on_write ? fd : NULL,
1588 NULL, 1606 NULL,
1589 NULL, 1607 NULL,
1590 task, task_cls); 1608 task, task_cls);
1591} 1609}
1592 1610
1593 1611
@@ -1612,11 +1630,12 @@ GNUNET_SCHEDULER_add_net_with_priority(struct GNUNET_TIME_Relative delay,
1612 * only valid until @a task is started! 1630 * only valid until @a task is started!
1613 */ 1631 */
1614struct GNUNET_SCHEDULER_Task * 1632struct GNUNET_SCHEDULER_Task *
1615GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay, 1633GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1616 const struct GNUNET_DISK_FileHandle *rfd, 1634 const struct GNUNET_DISK_FileHandle *rfd,
1617 GNUNET_SCHEDULER_TaskCallback task, void *task_cls) 1635 GNUNET_SCHEDULER_TaskCallback task,
1636 void *task_cls)
1618{ 1637{
1619 return GNUNET_SCHEDULER_add_file_with_priority( 1638 return GNUNET_SCHEDULER_add_file_with_priority (
1620 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1639 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1621 rfd, GNUNET_YES, GNUNET_NO, 1640 rfd, GNUNET_YES, GNUNET_NO,
1622 task, task_cls); 1641 task, task_cls);
@@ -1644,11 +1663,12 @@ GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay,
1644 * only valid until @a task is started! 1663 * only valid until @a task is started!
1645 */ 1664 */
1646struct GNUNET_SCHEDULER_Task * 1665struct GNUNET_SCHEDULER_Task *
1647GNUNET_SCHEDULER_add_write_file(struct GNUNET_TIME_Relative delay, 1666GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1648 const struct GNUNET_DISK_FileHandle *wfd, 1667 const struct GNUNET_DISK_FileHandle *wfd,
1649 GNUNET_SCHEDULER_TaskCallback task, void *task_cls) 1668 GNUNET_SCHEDULER_TaskCallback task,
1669 void *task_cls)
1650{ 1670{
1651 return GNUNET_SCHEDULER_add_file_with_priority( 1671 return GNUNET_SCHEDULER_add_file_with_priority (
1652 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT, 1672 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1653 wfd, GNUNET_NO, GNUNET_YES, 1673 wfd, GNUNET_NO, GNUNET_YES,
1654 task, task_cls); 1674 task, task_cls);
@@ -1679,31 +1699,34 @@ GNUNET_SCHEDULER_add_write_file(struct GNUNET_TIME_Relative delay,
1679 * only valid until @a task is started! 1699 * only valid until @a task is started!
1680 */ 1700 */
1681struct GNUNET_SCHEDULER_Task * 1701struct GNUNET_SCHEDULER_Task *
1682GNUNET_SCHEDULER_add_file_with_priority(struct GNUNET_TIME_Relative delay, 1702GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1683 enum GNUNET_SCHEDULER_Priority priority, 1703 enum GNUNET_SCHEDULER_Priority
1684 const struct GNUNET_DISK_FileHandle *fd, 1704 priority,
1685 int on_read, int on_write, 1705 const struct
1686 GNUNET_SCHEDULER_TaskCallback task, void *task_cls) 1706 GNUNET_DISK_FileHandle *fd,
1707 int on_read, int on_write,
1708 GNUNET_SCHEDULER_TaskCallback task,
1709 void *task_cls)
1687{ 1710{
1688 /* scheduler must be running */ 1711 /* scheduler must be running */
1689 GNUNET_assert(NULL != scheduler_driver); 1712 GNUNET_assert (NULL != scheduler_driver);
1690 GNUNET_assert(on_read || on_write); 1713 GNUNET_assert (on_read || on_write);
1691 GNUNET_assert(fd->fd >= 0); 1714 GNUNET_assert (fd->fd >= 0);
1692 return add_without_sets(delay, priority, 1715 return add_without_sets (delay, priority,
1693 NULL, 1716 NULL,
1694 NULL, 1717 NULL,
1695 on_read ? fd : NULL, 1718 on_read ? fd : NULL,
1696 on_write ? fd : NULL, 1719 on_write ? fd : NULL,
1697 task, task_cls); 1720 task, task_cls);
1698} 1721}
1699 1722
1700 1723
1701void 1724void
1702extract_handles(const struct GNUNET_NETWORK_FDSet *fdset, 1725extract_handles (const struct GNUNET_NETWORK_FDSet *fdset,
1703 const struct GNUNET_NETWORK_Handle ***ntarget, 1726 const struct GNUNET_NETWORK_Handle ***ntarget,
1704 unsigned int *extracted_nhandles, 1727 unsigned int *extracted_nhandles,
1705 const struct GNUNET_DISK_FileHandle ***ftarget, 1728 const struct GNUNET_DISK_FileHandle ***ftarget,
1706 unsigned int *extracted_fhandles) 1729 unsigned int *extracted_fhandles)
1707{ 1730{
1708 // FIXME: this implementation only works for unix, for WIN32 the file handles 1731 // FIXME: this implementation only works for unix, for WIN32 the file handles
1709 // in fdset must be handled separately 1732 // in fdset must be handled separately
@@ -1717,31 +1740,31 @@ extract_handles(const struct GNUNET_NETWORK_FDSet *fdset,
1717 nhandles_len = 0; 1740 nhandles_len = 0;
1718 fhandles_len = 0; 1741 fhandles_len = 0;
1719 for (int sock = 0; sock != fdset->nsds; ++sock) 1742 for (int sock = 0; sock != fdset->nsds; ++sock)
1743 {
1744 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1720 { 1745 {
1721 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native(fdset, sock)) 1746 struct GNUNET_NETWORK_Handle *nhandle;
1747 struct GNUNET_DISK_FileHandle *fhandle;
1748
1749 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1750 if (NULL != nhandle)
1751 {
1752 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1753 }
1754 else
1755 {
1756 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1757 if (NULL != fhandle)
1758 {
1759 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1760 }
1761 else
1722 { 1762 {
1723 struct GNUNET_NETWORK_Handle *nhandle; 1763 GNUNET_assert (0);
1724 struct GNUNET_DISK_FileHandle *fhandle;
1725
1726 nhandle = GNUNET_NETWORK_socket_box_native(sock);
1727 if (NULL != nhandle)
1728 {
1729 GNUNET_array_append(nhandles, nhandles_len, nhandle);
1730 }
1731 else
1732 {
1733 fhandle = GNUNET_DISK_get_handle_from_int_fd(sock);
1734 if (NULL != fhandle)
1735 {
1736 GNUNET_array_append(fhandles, fhandles_len, fhandle);
1737 }
1738 else
1739 {
1740 GNUNET_assert(0);
1741 }
1742 }
1743 } 1764 }
1765 }
1744 } 1766 }
1767 }
1745 *ntarget = nhandles_len > 0 ? nhandles : NULL; 1768 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1746 *ftarget = fhandles_len > 0 ? fhandles : NULL; 1769 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1747 *extracted_nhandles = nhandles_len; 1770 *extracted_nhandles = nhandles_len;
@@ -1781,12 +1804,12 @@ extract_handles(const struct GNUNET_NETWORK_FDSet *fdset,
1781 * only valid until @a task is started! 1804 * only valid until @a task is started!
1782 */ 1805 */
1783struct GNUNET_SCHEDULER_Task * 1806struct GNUNET_SCHEDULER_Task *
1784GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio, 1807GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1785 struct GNUNET_TIME_Relative delay, 1808 struct GNUNET_TIME_Relative delay,
1786 const struct GNUNET_NETWORK_FDSet *rs, 1809 const struct GNUNET_NETWORK_FDSet *rs,
1787 const struct GNUNET_NETWORK_FDSet *ws, 1810 const struct GNUNET_NETWORK_FDSet *ws,
1788 GNUNET_SCHEDULER_TaskCallback task, 1811 GNUNET_SCHEDULER_TaskCallback task,
1789 void *task_cls) 1812 void *task_cls)
1790{ 1813{
1791 struct GNUNET_SCHEDULER_Task *t; 1814 struct GNUNET_SCHEDULER_Task *t;
1792 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL; 1815 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL;
@@ -1799,8 +1822,8 @@ GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio,
1799 unsigned int write_fhandles_len = 0; 1822 unsigned int write_fhandles_len = 0;
1800 1823
1801 /* scheduler must be running */ 1824 /* scheduler must be running */
1802 GNUNET_assert(NULL != scheduler_driver); 1825 GNUNET_assert (NULL != scheduler_driver);
1803 GNUNET_assert(NULL != task); 1826 GNUNET_assert (NULL != task);
1804 int no_rs = (NULL == rs); 1827 int no_rs = (NULL == rs);
1805 int no_ws = (NULL == ws); 1828 int no_ws = (NULL == ws);
1806 int empty_rs = (NULL != rs) && (0 == rs->nsds); 1829 int empty_rs = (NULL != rs) && (0 == rs->nsds);
@@ -1809,25 +1832,25 @@ GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio,
1809 (empty_rs && empty_ws) || 1832 (empty_rs && empty_ws) ||
1810 (no_rs && empty_ws) || 1833 (no_rs && empty_ws) ||
1811 (no_ws && empty_rs); 1834 (no_ws && empty_rs);
1812 if (!no_fds) 1835 if (! no_fds)
1836 {
1837 if (NULL != rs)
1813 { 1838 {
1814 if (NULL != rs) 1839 extract_handles (rs,
1815 { 1840 &read_nhandles,
1816 extract_handles(rs, 1841 &read_nhandles_len,
1817 &read_nhandles, 1842 &read_fhandles,
1818 &read_nhandles_len, 1843 &read_fhandles_len);
1819 &read_fhandles,
1820 &read_fhandles_len);
1821 }
1822 if (NULL != ws)
1823 {
1824 extract_handles(ws,
1825 &write_nhandles,
1826 &write_nhandles_len,
1827 &write_fhandles,
1828 &write_fhandles_len);
1829 }
1830 } 1844 }
1845 if (NULL != ws)
1846 {
1847 extract_handles (ws,
1848 &write_nhandles,
1849 &write_nhandles_len,
1850 &write_fhandles,
1851 &write_fhandles_len);
1852 }
1853 }
1831 /** 1854 /**
1832 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty 1855 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty
1833 * although its maximum FD number (nsds) is greater than 0. We handle 1856 * although its maximum FD number (nsds) is greater than 0. We handle
@@ -1840,49 +1863,49 @@ GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio,
1840 (0 == write_nhandles_len) && 1863 (0 == write_nhandles_len) &&
1841 (0 == write_fhandles_len); 1864 (0 == write_fhandles_len);
1842 if (no_fds || no_fds_extracted) 1865 if (no_fds || no_fds_extracted)
1843 return GNUNET_SCHEDULER_add_delayed_with_priority(delay, 1866 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1844 prio, 1867 prio,
1845 task, 1868 task,
1846 task_cls); 1869 task_cls);
1847 t = GNUNET_new(struct GNUNET_SCHEDULER_Task); 1870 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1848 GNUNET_async_scope_get(&t->scope); 1871 GNUNET_async_scope_get (&t->scope);
1849 init_fd_info(t, 1872 init_fd_info (t,
1850 read_nhandles, 1873 read_nhandles,
1851 read_nhandles_len, 1874 read_nhandles_len,
1852 write_nhandles, 1875 write_nhandles,
1853 write_nhandles_len, 1876 write_nhandles_len,
1854 read_fhandles, 1877 read_fhandles,
1855 read_fhandles_len, 1878 read_fhandles_len,
1856 write_fhandles, 1879 write_fhandles,
1857 write_fhandles_len); 1880 write_fhandles_len);
1858 t->callback = task; 1881 t->callback = task;
1859 t->callback_cls = task_cls; 1882 t->callback_cls = task_cls;
1860 t->own_handles = GNUNET_YES; 1883 t->own_handles = GNUNET_YES;
1861 /* free the arrays of pointers to network / file handles, the actual 1884 /* free the arrays of pointers to network / file handles, the actual
1862 * handles will be freed in destroy_task */ 1885 * handles will be freed in destroy_task */
1863 GNUNET_array_grow(read_nhandles, read_nhandles_len, 0); 1886 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1864 GNUNET_array_grow(write_nhandles, write_nhandles_len, 0); 1887 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1865 GNUNET_array_grow(read_fhandles, read_fhandles_len, 0); 1888 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1866 GNUNET_array_grow(write_fhandles, write_fhandles_len, 0); 1889 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1867#if PROFILE_DELAYS 1890#if PROFILE_DELAYS
1868 t->start_time = GNUNET_TIME_absolute_get(); 1891 t->start_time = GNUNET_TIME_absolute_get ();
1869#endif 1892#endif
1870 t->timeout = GNUNET_TIME_relative_to_absolute(delay); 1893 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1871 t->priority = 1894 t->priority =
1872 check_priority((prio == 1895 check_priority ((prio ==
1873 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : 1896 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1874 prio); 1897 prio);
1875 t->lifeness = current_lifeness; 1898 t->lifeness = current_lifeness;
1876 GNUNET_CONTAINER_DLL_insert(pending_head, 1899 GNUNET_CONTAINER_DLL_insert (pending_head,
1877 pending_tail, 1900 pending_tail,
1878 t); 1901 t);
1879 driver_add_multiple(t); 1902 driver_add_multiple (t);
1880 max_priority_added = GNUNET_MAX(max_priority_added, 1903 max_priority_added = GNUNET_MAX (max_priority_added,
1881 t->priority); 1904 t->priority);
1882 LOG(GNUNET_ERROR_TYPE_DEBUG, 1905 LOG (GNUNET_ERROR_TYPE_DEBUG,
1883 "Adding task %p\n", 1906 "Adding task %p\n",
1884 t); 1907 t);
1885 init_backtrace(t); 1908 init_backtrace (t);
1886 return t; 1909 return t;
1887} 1910}
1888 1911
@@ -1899,8 +1922,8 @@ GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio,
1899 * @param fdi information about the related FD 1922 * @param fdi information about the related FD
1900 */ 1923 */
1901void 1924void
1902GNUNET_SCHEDULER_task_ready(struct GNUNET_SCHEDULER_Task *task, 1925GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1903 struct GNUNET_SCHEDULER_FdInfo *fdi) 1926 struct GNUNET_SCHEDULER_FdInfo *fdi)
1904{ 1927{
1905 enum GNUNET_SCHEDULER_Reason reason; 1928 enum GNUNET_SCHEDULER_Reason reason;
1906 1929
@@ -1914,12 +1937,12 @@ GNUNET_SCHEDULER_task_ready(struct GNUNET_SCHEDULER_Task *task,
1914 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE; 1937 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1915 task->reason = reason; 1938 task->reason = reason;
1916 if (GNUNET_NO == task->in_ready_list) 1939 if (GNUNET_NO == task->in_ready_list)
1917 { 1940 {
1918 GNUNET_CONTAINER_DLL_remove(pending_head, 1941 GNUNET_CONTAINER_DLL_remove (pending_head,
1919 pending_tail, 1942 pending_tail,
1920 task); 1943 task);
1921 queue_ready_task(task); 1944 queue_ready_task (task);
1922 } 1945 }
1923} 1946}
1924 1947
1925 1948
@@ -1944,173 +1967,174 @@ GNUNET_SCHEDULER_task_ready(struct GNUNET_SCHEDULER_Task *task,
1944 * if we are done running tasks (yield to block) 1967 * if we are done running tasks (yield to block)
1945 */ 1968 */
1946int 1969int
1947GNUNET_SCHEDULER_do_work(struct GNUNET_SCHEDULER_Handle *sh) 1970GNUNET_SCHEDULER_do_work (struct GNUNET_SCHEDULER_Handle *sh)
1948{ 1971{
1949 enum GNUNET_SCHEDULER_Priority p; 1972 enum GNUNET_SCHEDULER_Priority p;
1950 struct GNUNET_SCHEDULER_Task *pos; 1973 struct GNUNET_SCHEDULER_Task *pos;
1951 struct GNUNET_TIME_Absolute now; 1974 struct GNUNET_TIME_Absolute now;
1952 1975
1953 /* check for tasks that reached the timeout! */ 1976 /* check for tasks that reached the timeout! */
1954 now = GNUNET_TIME_absolute_get(); 1977 now = GNUNET_TIME_absolute_get ();
1955 pos = pending_timeout_head; 1978 pos = pending_timeout_head;
1956 while (NULL != pos) 1979 while (NULL != pos)
1957 { 1980 {
1958 struct GNUNET_SCHEDULER_Task *next = pos->next; 1981 struct GNUNET_SCHEDULER_Task *next = pos->next;
1959 if (now.abs_value_us >= pos->timeout.abs_value_us) 1982 if (now.abs_value_us >= pos->timeout.abs_value_us)
1960 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; 1983 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1961 if (0 == pos->reason) 1984 if (0 == pos->reason)
1962 break; 1985 break;
1963 GNUNET_CONTAINER_DLL_remove(pending_timeout_head, 1986 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1964 pending_timeout_tail, 1987 pending_timeout_tail,
1965 pos); 1988 pos);
1966 if (pending_timeout_last == pos) 1989 if (pending_timeout_last == pos)
1967 pending_timeout_last = NULL; 1990 pending_timeout_last = NULL;
1968 queue_ready_task(pos); 1991 queue_ready_task (pos);
1969 pos = next; 1992 pos = next;
1970 } 1993 }
1971 pos = pending_head; 1994 pos = pending_head;
1972 while (NULL != pos) 1995 while (NULL != pos)
1996 {
1997 struct GNUNET_SCHEDULER_Task *next = pos->next;
1998 if (now.abs_value_us >= pos->timeout.abs_value_us)
1973 { 1999 {
1974 struct GNUNET_SCHEDULER_Task *next = pos->next; 2000 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1975 if (now.abs_value_us >= pos->timeout.abs_value_us) 2001 GNUNET_CONTAINER_DLL_remove (pending_head,
1976 { 2002 pending_tail,
1977 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; 2003 pos);
1978 GNUNET_CONTAINER_DLL_remove(pending_head, 2004 queue_ready_task (pos);
1979 pending_tail,
1980 pos);
1981 queue_ready_task(pos);
1982 }
1983 pos = next;
1984 } 2005 }
2006 pos = next;
2007 }
1985 2008
1986 if (0 == ready_count) 2009 if (0 == ready_count)
1987 { 2010 {
1988 struct GNUNET_TIME_Absolute timeout = get_timeout(); 2011 struct GNUNET_TIME_Absolute timeout = get_timeout ();
1989 2012
1990 if (timeout.abs_value_us > now.abs_value_us) 2013 if (timeout.abs_value_us > now.abs_value_us)
1991 { 2014 {
1992 /** 2015 /**
1993 * The event loop called this function before the current timeout was 2016 * The event loop called this function before the current timeout was
1994 * reached (and no FD tasks are ready). This is acceptable if 2017 * reached (and no FD tasks are ready). This is acceptable if
1995 * 2018 *
1996 * - the system time was changed while the driver was waiting for 2019 * - the system time was changed while the driver was waiting for
1997 * the timeout 2020 * the timeout
1998 * - an external event loop called GNUnet API functions outside of 2021 * - an external event loop called GNUnet API functions outside of
1999 * the callbacks called in GNUNET_SCHEDULER_do_work and thus 2022 * the callbacks called in GNUNET_SCHEDULER_do_work and thus
2000 * wasn't notified about the new timeout 2023 * wasn't notified about the new timeout
2001 * 2024 *
2002 * It might also mean we are busy-waiting because of a programming 2025 * It might also mean we are busy-waiting because of a programming
2003 * error in the external event loop. 2026 * error in the external event loop.
2004 */ 2027 */
2005 LOG(GNUNET_ERROR_TYPE_DEBUG, 2028 LOG (GNUNET_ERROR_TYPE_DEBUG,
2006 "GNUNET_SCHEDULER_do_work did not find any ready " 2029 "GNUNET_SCHEDULER_do_work did not find any ready "
2007 "tasks and timeout has not been reached yet.\n"); 2030 "tasks and timeout has not been reached yet.\n");
2008 }
2009 else
2010 {
2011 /**
2012 * the current timeout was reached but no ready tasks were found,
2013 * internal scheduler error!
2014 */
2015 GNUNET_assert(0);
2016 }
2017 } 2031 }
2032 else
2033 {
2034 /**
2035 * the current timeout was reached but no ready tasks were found,
2036 * internal scheduler error!
2037 */
2038 GNUNET_assert (0);
2039 }
2040 }
2018 else 2041 else
2042 {
2043 /* find out which task priority level we are going to
2044 process this time */
2045 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
2046 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
2047 /* yes, p>0 is correct, 0 is "KEEP" which should
2048 * always be an empty queue (see assertion)! */
2049 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
2019 { 2050 {
2020 /* find out which task priority level we are going to 2051 pos = ready_head[p];
2021 process this time */ 2052 if (NULL != pos)
2022 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP; 2053 break;
2023 GNUNET_assert(NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]); 2054 }
2024 /* yes, p>0 is correct, 0 is "KEEP" which should 2055 GNUNET_assert (NULL != pos); /* ready_count wrong? */
2025 * always be an empty queue (see assertion)! */
2026 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
2027 {
2028 pos = ready_head[p];
2029 if (NULL != pos)
2030 break;
2031 }
2032 GNUNET_assert(NULL != pos); /* ready_count wrong? */
2033 2056
2034 /* process all tasks at this priority level, then yield */ 2057 /* process all tasks at this priority level, then yield */
2035 while (NULL != (pos = ready_head[p])) 2058 while (NULL != (pos = ready_head[p]))
2036 { 2059 {
2037 GNUNET_CONTAINER_DLL_remove(ready_head[p], 2060 GNUNET_CONTAINER_DLL_remove (ready_head[p],
2038 ready_tail[p], 2061 ready_tail[p],
2039 pos); 2062 pos);
2040 ready_count--; 2063 ready_count--;
2041 current_priority = pos->priority; 2064 current_priority = pos->priority;
2042 current_lifeness = pos->lifeness; 2065 current_lifeness = pos->lifeness;
2043 active_task = pos; 2066 active_task = pos;
2044#if PROFILE_DELAYS 2067#if PROFILE_DELAYS
2045 if (GNUNET_TIME_absolute_get_duration(pos->start_time).rel_value_us > 2068 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
2046 DELAY_THRESHOLD.rel_value_us) 2069 DELAY_THRESHOLD.rel_value_us)
2047 { 2070 {
2048 LOG(GNUNET_ERROR_TYPE_DEBUG, 2071 LOG (GNUNET_ERROR_TYPE_DEBUG,
2049 "Task %p took %s to be scheduled\n", 2072 "Task %p took %s to be scheduled\n",
2050 pos, 2073 pos,
2051 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(pos->start_time), 2074 GNUNET_STRINGS_relative_time_to_string (
2052 GNUNET_YES)); 2075 GNUNET_TIME_absolute_get_duration (pos->start_time),
2053 } 2076 GNUNET_YES));
2077 }
2054#endif 2078#endif
2055 tc.reason = pos->reason; 2079 tc.reason = pos->reason;
2056 GNUNET_NETWORK_fdset_zero(sh->rs); 2080 GNUNET_NETWORK_fdset_zero (sh->rs);
2057 GNUNET_NETWORK_fdset_zero(sh->ws); 2081 GNUNET_NETWORK_fdset_zero (sh->ws);
2058 // FIXME: do we have to remove FdInfos from fds if they are not ready? 2082 // FIXME: do we have to remove FdInfos from fds if they are not ready?
2059 tc.fds_len = pos->fds_len; 2083 tc.fds_len = pos->fds_len;
2060 tc.fds = pos->fds; 2084 tc.fds = pos->fds;
2061 for (unsigned int i = 0; i != pos->fds_len; ++i) 2085 for (unsigned int i = 0; i != pos->fds_len; ++i)
2062 { 2086 {
2063 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i]; 2087 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
2064 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)) 2088 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
2065 { 2089 {
2066 GNUNET_NETWORK_fdset_set_native(sh->rs, 2090 GNUNET_NETWORK_fdset_set_native (sh->rs,
2067 fdi->sock); 2091 fdi->sock);
2068 }
2069 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2070 {
2071 GNUNET_NETWORK_fdset_set_native(sh->ws,
2072 fdi->sock);
2073 }
2074 }
2075 tc.read_ready = sh->rs;
2076 tc.write_ready = sh->ws;
2077 LOG(GNUNET_ERROR_TYPE_DEBUG,
2078 "Running task %p\n",
2079 pos);
2080 GNUNET_assert(NULL != pos->callback);
2081 {
2082 struct GNUNET_AsyncScopeSave old_scope;
2083 if (pos->scope.have_scope)
2084 GNUNET_async_scope_enter(&pos->scope.scope_id, &old_scope);
2085 else
2086 GNUNET_async_scope_get(&old_scope);
2087 pos->callback(pos->callback_cls);
2088 GNUNET_async_scope_restore(&old_scope);
2089 }
2090 if (NULL != pos->fds)
2091 {
2092 int del_result = scheduler_driver->del(scheduler_driver->cls, pos);
2093 if (GNUNET_OK != del_result)
2094 {
2095 LOG(GNUNET_ERROR_TYPE_ERROR,
2096 "driver could not delete task %p\n", pos);
2097 GNUNET_assert(0);
2098 }
2099 }
2100 active_task = NULL;
2101 dump_backtrace(pos);
2102 destroy_task(pos);
2103 } 2092 }
2093 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2094 {
2095 GNUNET_NETWORK_fdset_set_native (sh->ws,
2096 fdi->sock);
2097 }
2098 }
2099 tc.read_ready = sh->rs;
2100 tc.write_ready = sh->ws;
2101 LOG (GNUNET_ERROR_TYPE_DEBUG,
2102 "Running task %p\n",
2103 pos);
2104 GNUNET_assert (NULL != pos->callback);
2105 {
2106 struct GNUNET_AsyncScopeSave old_scope;
2107 if (pos->scope.have_scope)
2108 GNUNET_async_scope_enter (&pos->scope.scope_id, &old_scope);
2109 else
2110 GNUNET_async_scope_get (&old_scope);
2111 pos->callback (pos->callback_cls);
2112 GNUNET_async_scope_restore (&old_scope);
2113 }
2114 if (NULL != pos->fds)
2115 {
2116 int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
2117 if (GNUNET_OK != del_result)
2118 {
2119 LOG (GNUNET_ERROR_TYPE_ERROR,
2120 "driver could not delete task %p\n", pos);
2121 GNUNET_assert (0);
2122 }
2123 }
2124 active_task = NULL;
2125 dump_backtrace (pos);
2126 destroy_task (pos);
2104 } 2127 }
2105 shutdown_if_no_lifeness(); 2128 }
2129 shutdown_if_no_lifeness ();
2106 if (0 == ready_count) 2130 if (0 == ready_count)
2107 { 2131 {
2108 scheduler_driver->set_wakeup(scheduler_driver->cls, 2132 scheduler_driver->set_wakeup (scheduler_driver->cls,
2109 get_timeout()); 2133 get_timeout ());
2110 return GNUNET_NO; 2134 return GNUNET_NO;
2111 } 2135 }
2112 scheduler_driver->set_wakeup(scheduler_driver->cls, 2136 scheduler_driver->set_wakeup (scheduler_driver->cls,
2113 GNUNET_TIME_absolute_get()); 2137 GNUNET_TIME_absolute_get ());
2114 return GNUNET_YES; 2138 return GNUNET_YES;
2115} 2139}
2116 2140
@@ -2147,62 +2171,62 @@ GNUNET_SCHEDULER_do_work(struct GNUNET_SCHEDULER_Handle *sh)
2147 * #GNUNET_SCHEDULER_driver_done 2171 * #GNUNET_SCHEDULER_driver_done
2148 */ 2172 */
2149struct GNUNET_SCHEDULER_Handle * 2173struct GNUNET_SCHEDULER_Handle *
2150GNUNET_SCHEDULER_driver_init(const struct GNUNET_SCHEDULER_Driver *driver) 2174GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver)
2151{ 2175{
2152 struct GNUNET_SCHEDULER_Handle *sh; 2176 struct GNUNET_SCHEDULER_Handle *sh;
2153 const struct GNUNET_DISK_FileHandle *pr; 2177 const struct GNUNET_DISK_FileHandle *pr;
2154 2178
2155 /* scheduler must not be running */ 2179 /* scheduler must not be running */
2156 GNUNET_assert(NULL == scheduler_driver); 2180 GNUNET_assert (NULL == scheduler_driver);
2157 GNUNET_assert(NULL == shutdown_pipe_handle); 2181 GNUNET_assert (NULL == shutdown_pipe_handle);
2158 /* general set-up */ 2182 /* general set-up */
2159 sh = GNUNET_new(struct GNUNET_SCHEDULER_Handle); 2183 sh = GNUNET_new (struct GNUNET_SCHEDULER_Handle);
2160 shutdown_pipe_handle = GNUNET_DISK_pipe(GNUNET_NO, 2184 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2161 GNUNET_NO, 2185 GNUNET_NO,
2162 GNUNET_NO, 2186 GNUNET_NO,
2163 GNUNET_NO); 2187 GNUNET_NO);
2164 GNUNET_assert(NULL != shutdown_pipe_handle); 2188 GNUNET_assert (NULL != shutdown_pipe_handle);
2165 pr = GNUNET_DISK_pipe_handle(shutdown_pipe_handle, 2189 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2166 GNUNET_DISK_PIPE_END_READ); 2190 GNUNET_DISK_PIPE_END_READ);
2167 my_pid = getpid(); 2191 my_pid = getpid ();
2168 scheduler_driver = driver; 2192 scheduler_driver = driver;
2169 2193
2170 /* install signal handlers */ 2194 /* install signal handlers */
2171 LOG(GNUNET_ERROR_TYPE_DEBUG, 2195 LOG (GNUNET_ERROR_TYPE_DEBUG,
2172 "Registering signal handlers\n"); 2196 "Registering signal handlers\n");
2173 sh->shc_int = GNUNET_SIGNAL_handler_install(SIGINT, 2197 sh->shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2174 &sighandler_shutdown);
2175 sh->shc_term = GNUNET_SIGNAL_handler_install(SIGTERM,
2176 &sighandler_shutdown); 2198 &sighandler_shutdown);
2177#if (SIGTERM != GNUNET_TERM_SIG) 2199 sh->shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2178 sh->shc_gterm = GNUNET_SIGNAL_handler_install(GNUNET_TERM_SIG,
2179 &sighandler_shutdown); 2200 &sighandler_shutdown);
2201#if (SIGTERM != GNUNET_TERM_SIG)
2202 sh->shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2203 &sighandler_shutdown);
2180#endif 2204#endif
2181 sh->shc_pipe = GNUNET_SIGNAL_handler_install(SIGPIPE, 2205 sh->shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2182 &sighandler_pipe); 2206 &sighandler_pipe);
2183 sh->shc_quit = GNUNET_SIGNAL_handler_install(SIGQUIT, 2207 sh->shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2208 &sighandler_shutdown);
2209 sh->shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2184 &sighandler_shutdown); 2210 &sighandler_shutdown);
2185 sh->shc_hup = GNUNET_SIGNAL_handler_install(SIGHUP,
2186 &sighandler_shutdown);
2187 2211
2188 /* Setup initial tasks */ 2212 /* Setup initial tasks */
2189 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT; 2213 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2190 current_lifeness = GNUNET_NO; 2214 current_lifeness = GNUNET_NO;
2191 install_parent_control_task = 2215 install_parent_control_task =
2192 GNUNET_SCHEDULER_add_now(&install_parent_control_handler, 2216 GNUNET_SCHEDULER_add_now (&install_parent_control_handler,
2193 NULL); 2217 NULL);
2194 shutdown_pipe_task = 2218 shutdown_pipe_task =
2195 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, 2219 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2196 pr, 2220 pr,
2197 &shutdown_pipe_cb, 2221 &shutdown_pipe_cb,
2198 NULL); 2222 NULL);
2199 current_lifeness = GNUNET_YES; 2223 current_lifeness = GNUNET_YES;
2200 scheduler_driver->set_wakeup(scheduler_driver->cls, 2224 scheduler_driver->set_wakeup (scheduler_driver->cls,
2201 get_timeout()); 2225 get_timeout ());
2202 /* begin main event loop */ 2226 /* begin main event loop */
2203 sh->rs = GNUNET_NETWORK_fdset_create(); 2227 sh->rs = GNUNET_NETWORK_fdset_create ();
2204 sh->ws = GNUNET_NETWORK_fdset_create(); 2228 sh->ws = GNUNET_NETWORK_fdset_create ();
2205 GNUNET_NETWORK_fdset_handle_set(sh->rs, pr); 2229 GNUNET_NETWORK_fdset_handle_set (sh->rs, pr);
2206 return sh; 2230 return sh;
2207} 2231}
2208 2232
@@ -2222,241 +2246,242 @@ GNUNET_SCHEDULER_driver_init(const struct GNUNET_SCHEDULER_Driver *driver)
2222 * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init 2246 * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
2223 */ 2247 */
2224void 2248void
2225GNUNET_SCHEDULER_driver_done(struct GNUNET_SCHEDULER_Handle *sh) 2249GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
2226{ 2250{
2227 GNUNET_assert(NULL == pending_head); 2251 GNUNET_assert (NULL == pending_head);
2228 GNUNET_assert(NULL == pending_timeout_head); 2252 GNUNET_assert (NULL == pending_timeout_head);
2229 GNUNET_assert(NULL == shutdown_head); 2253 GNUNET_assert (NULL == shutdown_head);
2230 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i) 2254 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i)
2231 { 2255 {
2232 GNUNET_assert(NULL == ready_head[i]); 2256 GNUNET_assert (NULL == ready_head[i]);
2233 } 2257 }
2234 GNUNET_NETWORK_fdset_destroy(sh->rs); 2258 GNUNET_NETWORK_fdset_destroy (sh->rs);
2235 GNUNET_NETWORK_fdset_destroy(sh->ws); 2259 GNUNET_NETWORK_fdset_destroy (sh->ws);
2236 2260
2237 /* uninstall signal handlers */ 2261 /* uninstall signal handlers */
2238 GNUNET_SIGNAL_handler_uninstall(sh->shc_int); 2262 GNUNET_SIGNAL_handler_uninstall (sh->shc_int);
2239 GNUNET_SIGNAL_handler_uninstall(sh->shc_term); 2263 GNUNET_SIGNAL_handler_uninstall (sh->shc_term);
2240#if (SIGTERM != GNUNET_TERM_SIG) 2264#if (SIGTERM != GNUNET_TERM_SIG)
2241 GNUNET_SIGNAL_handler_uninstall(sh->shc_gterm); 2265 GNUNET_SIGNAL_handler_uninstall (sh->shc_gterm);
2242#endif 2266#endif
2243 GNUNET_SIGNAL_handler_uninstall(sh->shc_pipe); 2267 GNUNET_SIGNAL_handler_uninstall (sh->shc_pipe);
2244 GNUNET_SIGNAL_handler_uninstall(sh->shc_quit); 2268 GNUNET_SIGNAL_handler_uninstall (sh->shc_quit);
2245 GNUNET_SIGNAL_handler_uninstall(sh->shc_hup); 2269 GNUNET_SIGNAL_handler_uninstall (sh->shc_hup);
2246 GNUNET_DISK_pipe_close(shutdown_pipe_handle); 2270 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2247 shutdown_pipe_handle = NULL; 2271 shutdown_pipe_handle = NULL;
2248 scheduler_driver = NULL; 2272 scheduler_driver = NULL;
2249 GNUNET_free(sh); 2273 GNUNET_free (sh);
2250} 2274}
2251 2275
2252 2276
2253static int 2277static int
2254select_loop(struct GNUNET_SCHEDULER_Handle *sh, 2278select_loop (struct GNUNET_SCHEDULER_Handle *sh,
2255 struct DriverContext *context) 2279 struct DriverContext *context)
2256{ 2280{
2257 struct GNUNET_NETWORK_FDSet *rs; 2281 struct GNUNET_NETWORK_FDSet *rs;
2258 struct GNUNET_NETWORK_FDSet *ws; 2282 struct GNUNET_NETWORK_FDSet *ws;
2259 int select_result; 2283 int select_result;
2260 2284
2261 GNUNET_assert(NULL != context); 2285 GNUNET_assert (NULL != context);
2262 rs = GNUNET_NETWORK_fdset_create(); 2286 rs = GNUNET_NETWORK_fdset_create ();
2263 ws = GNUNET_NETWORK_fdset_create(); 2287 ws = GNUNET_NETWORK_fdset_create ();
2264 while ((NULL != context->scheduled_head) || 2288 while ((NULL != context->scheduled_head) ||
2265 (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != context->timeout.abs_value_us)) 2289 (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
2290 context->timeout.abs_value_us))
2291 {
2292 LOG (GNUNET_ERROR_TYPE_DEBUG,
2293 "select timeout = %s\n",
2294 GNUNET_STRINGS_absolute_time_to_string (context->timeout));
2295
2296 GNUNET_NETWORK_fdset_zero (rs);
2297 GNUNET_NETWORK_fdset_zero (ws);
2298
2299 for (struct Scheduled *pos = context->scheduled_head;
2300 NULL != pos;
2301 pos = pos->next)
2266 { 2302 {
2267 LOG(GNUNET_ERROR_TYPE_DEBUG, 2303 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2268 "select timeout = %s\n", 2304 {
2269 GNUNET_STRINGS_absolute_time_to_string(context->timeout)); 2305 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2270 2306 }
2271 GNUNET_NETWORK_fdset_zero(rs); 2307 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2272 GNUNET_NETWORK_fdset_zero(ws); 2308 {
2273 2309 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2274 for (struct Scheduled *pos = context->scheduled_head; 2310 }
2275 NULL != pos; 2311 }
2276 pos = pos->next) 2312 struct GNUNET_TIME_Relative time_remaining =
2277 { 2313 GNUNET_TIME_absolute_get_remaining (context->timeout);
2278 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et)) 2314 if (NULL == scheduler_select)
2279 { 2315 {
2280 GNUNET_NETWORK_fdset_set_native(rs, pos->fdi->sock); 2316 select_result = GNUNET_NETWORK_socket_select (rs,
2281 } 2317 ws,
2282 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et)) 2318 NULL,
2283 { 2319 time_remaining);
2284 GNUNET_NETWORK_fdset_set_native(ws, pos->fdi->sock); 2320 }
2285 } 2321 else
2286 } 2322 {
2287 struct GNUNET_TIME_Relative time_remaining = 2323 select_result = scheduler_select (scheduler_select_cls,
2288 GNUNET_TIME_absolute_get_remaining(context->timeout); 2324 rs,
2289 if (NULL == scheduler_select) 2325 ws,
2290 { 2326 NULL,
2291 select_result = GNUNET_NETWORK_socket_select(rs, 2327 time_remaining);
2292 ws, 2328 }
2293 NULL, 2329 if (select_result == GNUNET_SYSERR)
2294 time_remaining); 2330 {
2295 } 2331 if (errno == EINTR)
2296 else 2332 continue;
2297 {
2298 select_result = scheduler_select(scheduler_select_cls,
2299 rs,
2300 ws,
2301 NULL,
2302 time_remaining);
2303 }
2304 if (select_result == GNUNET_SYSERR)
2305 {
2306 if (errno == EINTR)
2307 continue;
2308 2333
2309 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, 2334 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
2310 "select"); 2335 "select");
2311#if USE_LSOF 2336#if USE_LSOF
2312 char lsof[512]; 2337 char lsof[512];
2313 2338
2314 snprintf(lsof, 2339 snprintf (lsof,
2315 sizeof(lsof), 2340 sizeof(lsof),
2316 "lsof -p %d", 2341 "lsof -p %d",
2317 getpid()); 2342 getpid ());
2318 (void)close(1); 2343 (void) close (1);
2319 (void)dup2(2, 1); 2344 (void) dup2 (2, 1);
2320 if (0 != system(lsof)) 2345 if (0 != system (lsof))
2321 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 2346 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2322 "system"); 2347 "system");
2323#endif 2348#endif
2324#if DEBUG_FDS 2349#if DEBUG_FDS
2325 for (struct Scheduled *s = context->scheduled_head; 2350 for (struct Scheduled *s = context->scheduled_head;
2326 NULL != s; 2351 NULL != s;
2327 s = s->next) 2352 s = s->next)
2328 { 2353 {
2329 int flags = fcntl(s->fdi->sock, 2354 int flags = fcntl (s->fdi->sock,
2330 F_GETFD); 2355 F_GETFD);
2331 2356
2332 if ((flags == -1) && 2357 if ((flags == -1) &&
2333 (EBADF == errno)) 2358 (EBADF == errno))
2334 { 2359 {
2335 LOG(GNUNET_ERROR_TYPE_ERROR, 2360 LOG (GNUNET_ERROR_TYPE_ERROR,
2336 "Got invalid file descriptor %d!\n", 2361 "Got invalid file descriptor %d!\n",
2337 s->fdi->sock); 2362 s->fdi->sock);
2338#if EXECINFO 2363#if EXECINFO
2339 dump_backtrace(s->task); 2364 dump_backtrace (s->task);
2340#endif 2365#endif
2341 } 2366 }
2342 } 2367 }
2343#endif 2368#endif
2344 GNUNET_assert(0); 2369 GNUNET_assert (0);
2345 GNUNET_NETWORK_fdset_destroy(rs); 2370 GNUNET_NETWORK_fdset_destroy (rs);
2346 GNUNET_NETWORK_fdset_destroy(ws); 2371 GNUNET_NETWORK_fdset_destroy (ws);
2347 return GNUNET_SYSERR; 2372 return GNUNET_SYSERR;
2373 }
2374 if (select_result > 0)
2375 {
2376 for (struct Scheduled *pos = context->scheduled_head;
2377 NULL != pos;
2378 pos = pos->next)
2379 {
2380 int is_ready = GNUNET_NO;
2381
2382 if ((0 != (GNUNET_SCHEDULER_ET_IN & pos->et)) &&
2383 (GNUNET_YES ==
2384 GNUNET_NETWORK_fdset_test_native (rs,
2385 pos->fdi->sock)) )
2386 {
2387 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2388 is_ready = GNUNET_YES;
2348 } 2389 }
2349 if (select_result > 0) 2390 if ((0 != (GNUNET_SCHEDULER_ET_OUT & pos->et)) &&
2391 (GNUNET_YES ==
2392 GNUNET_NETWORK_fdset_test_native (ws,
2393 pos->fdi->sock)) )
2350 { 2394 {
2351 for (struct Scheduled *pos = context->scheduled_head; 2395 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2352 NULL != pos; 2396 is_ready = GNUNET_YES;
2353 pos = pos->next)
2354 {
2355 int is_ready = GNUNET_NO;
2356
2357 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et) &&
2358 GNUNET_YES ==
2359 GNUNET_NETWORK_fdset_test_native(rs,
2360 pos->fdi->sock))
2361 {
2362 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2363 is_ready = GNUNET_YES;
2364 }
2365 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et) &&
2366 GNUNET_YES ==
2367 GNUNET_NETWORK_fdset_test_native(ws,
2368 pos->fdi->sock))
2369 {
2370 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2371 is_ready = GNUNET_YES;
2372 }
2373 if (GNUNET_YES == is_ready)
2374 {
2375 GNUNET_SCHEDULER_task_ready(pos->task,
2376 pos->fdi);
2377 }
2378 }
2379 } 2397 }
2380 if (GNUNET_YES == GNUNET_SCHEDULER_do_work(sh)) 2398 if (GNUNET_YES == is_ready)
2381 { 2399 {
2382 LOG(GNUNET_ERROR_TYPE_DEBUG, 2400 GNUNET_SCHEDULER_task_ready (pos->task,
2383 "scheduler has more tasks ready!\n"); 2401 pos->fdi);
2384 } 2402 }
2403 }
2404 }
2405 if (GNUNET_YES == GNUNET_SCHEDULER_do_work (sh))
2406 {
2407 LOG (GNUNET_ERROR_TYPE_DEBUG,
2408 "scheduler has more tasks ready!\n");
2385 } 2409 }
2386 GNUNET_NETWORK_fdset_destroy(rs); 2410 }
2387 GNUNET_NETWORK_fdset_destroy(ws); 2411 GNUNET_NETWORK_fdset_destroy (rs);
2412 GNUNET_NETWORK_fdset_destroy (ws);
2388 return GNUNET_OK; 2413 return GNUNET_OK;
2389} 2414}
2390 2415
2391 2416
2392static int 2417static int
2393select_add(void *cls, 2418select_add (void *cls,
2394 struct GNUNET_SCHEDULER_Task *task, 2419 struct GNUNET_SCHEDULER_Task *task,
2395 struct GNUNET_SCHEDULER_FdInfo *fdi) 2420 struct GNUNET_SCHEDULER_FdInfo *fdi)
2396{ 2421{
2397 struct DriverContext *context = cls; 2422 struct DriverContext *context = cls;
2398 2423
2399 GNUNET_assert(NULL != context); 2424 GNUNET_assert (NULL != context);
2400 GNUNET_assert(NULL != task); 2425 GNUNET_assert (NULL != task);
2401 GNUNET_assert(NULL != fdi); 2426 GNUNET_assert (NULL != fdi);
2402 GNUNET_assert(0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) || 2427 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2403 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)); 2428 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2404 2429
2405 if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0)) 2430 if (! ((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2406 { 2431 {
2407 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */ 2432 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2408 return GNUNET_SYSERR; 2433 return GNUNET_SYSERR;
2409 } 2434 }
2410 2435
2411 struct Scheduled *scheduled = GNUNET_new(struct Scheduled); 2436 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2412 scheduled->task = task; 2437 scheduled->task = task;
2413 scheduled->fdi = fdi; 2438 scheduled->fdi = fdi;
2414 scheduled->et = fdi->et; 2439 scheduled->et = fdi->et;
2415 2440
2416 GNUNET_CONTAINER_DLL_insert(context->scheduled_head, 2441 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2417 context->scheduled_tail, 2442 context->scheduled_tail,
2418 scheduled); 2443 scheduled);
2419 return GNUNET_OK; 2444 return GNUNET_OK;
2420} 2445}
2421 2446
2422 2447
2423static int 2448static int
2424select_del(void *cls, 2449select_del (void *cls,
2425 struct GNUNET_SCHEDULER_Task *task) 2450 struct GNUNET_SCHEDULER_Task *task)
2426{ 2451{
2427 struct DriverContext *context; 2452 struct DriverContext *context;
2428 struct Scheduled *pos; 2453 struct Scheduled *pos;
2429 int ret; 2454 int ret;
2430 2455
2431 GNUNET_assert(NULL != cls); 2456 GNUNET_assert (NULL != cls);
2432 2457
2433 context = cls; 2458 context = cls;
2434 ret = GNUNET_SYSERR; 2459 ret = GNUNET_SYSERR;
2435 pos = context->scheduled_head; 2460 pos = context->scheduled_head;
2436 while (NULL != pos) 2461 while (NULL != pos)
2462 {
2463 struct Scheduled *next = pos->next;
2464 if (pos->task == task)
2437 { 2465 {
2438 struct Scheduled *next = pos->next; 2466 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2439 if (pos->task == task) 2467 context->scheduled_tail,
2440 { 2468 pos);
2441 GNUNET_CONTAINER_DLL_remove(context->scheduled_head, 2469 GNUNET_free (pos);
2442 context->scheduled_tail, 2470 ret = GNUNET_OK;
2443 pos);
2444 GNUNET_free(pos);
2445 ret = GNUNET_OK;
2446 }
2447 pos = next;
2448 } 2471 }
2472 pos = next;
2473 }
2449 return ret; 2474 return ret;
2450} 2475}
2451 2476
2452 2477
2453static void 2478static void
2454select_set_wakeup(void *cls, 2479select_set_wakeup (void *cls,
2455 struct GNUNET_TIME_Absolute dt) 2480 struct GNUNET_TIME_Absolute dt)
2456{ 2481{
2457 struct DriverContext *context = cls; 2482 struct DriverContext *context = cls;
2458 2483
2459 GNUNET_assert(NULL != context); 2484 GNUNET_assert (NULL != context);
2460 context->timeout = dt; 2485 context->timeout = dt;
2461} 2486}
2462 2487
@@ -2467,11 +2492,11 @@ select_set_wakeup(void *cls,
2467 * @return NULL on error 2492 * @return NULL on error
2468 */ 2493 */
2469struct GNUNET_SCHEDULER_Driver * 2494struct GNUNET_SCHEDULER_Driver *
2470GNUNET_SCHEDULER_driver_select() 2495GNUNET_SCHEDULER_driver_select ()
2471{ 2496{
2472 struct GNUNET_SCHEDULER_Driver *select_driver; 2497 struct GNUNET_SCHEDULER_Driver *select_driver;
2473 2498
2474 select_driver = GNUNET_new(struct GNUNET_SCHEDULER_Driver); 2499 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2475 2500
2476 select_driver->add = &select_add; 2501 select_driver->add = &select_add;
2477 select_driver->del = &select_del; 2502 select_driver->del = &select_del;
@@ -2494,14 +2519,14 @@ GNUNET_SCHEDULER_driver_select()
2494 * @param aid the asynchronous scope id to enter 2519 * @param aid the asynchronous scope id to enter
2495 */ 2520 */
2496void 2521void
2497GNUNET_SCHEDULER_begin_async_scope(struct GNUNET_AsyncScopeId *aid) 2522GNUNET_SCHEDULER_begin_async_scope (struct GNUNET_AsyncScopeId *aid)
2498{ 2523{
2499 struct GNUNET_AsyncScopeSave dummy_old_scope; 2524 struct GNUNET_AsyncScopeSave dummy_old_scope;
2500 2525
2501 GNUNET_assert(NULL != active_task); 2526 GNUNET_assert (NULL != active_task);
2502 /* Since we're in a task, the context will be automatically 2527 /* Since we're in a task, the context will be automatically
2503 restored by the scheduler. */ 2528 restored by the scheduler. */
2504 GNUNET_async_scope_enter(aid, &dummy_old_scope); 2529 GNUNET_async_scope_enter (aid, &dummy_old_scope);
2505} 2530}
2506 2531
2507 2532