This file is part of GNUnet
Copyright (C) 2009-2017 GNUnet e.V.
- GNUnet is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 3, or (at your
- option) any later version.
+ GNUnet is free software: you can redistribute it and/or modify it
+ under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
- You should have received a copy of the GNU General Public License
- along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
+ SPDX-License-Identifier: AGPL3.0-or-later
*/
/**
* @file util/scheduler.c
{
struct GNUNET_SCHEDULER_Task *pos;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "GNUNET_SCHEDULER_shutdown\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "GNUNET_SCHEDULER_shutdown\n");
if (NULL != install_parent_control_task)
{
GNUNET_SCHEDULER_cancel (install_parent_control_task);
int old_errno = errno; /* backup errno */
if (getpid () != my_pid)
- exit (1); /* we have fork'ed since the signal handler was created,
- * ignore the signal, see https://gnunet.org/vfork discussion */
+ _exit (1); /* we have fork'ed since the signal handler was created,
+ * ignore the signal, see https://gnunet.org/vfork discussion */
GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
(shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
&c, sizeof (c));
}
-void
+static void
shutdown_if_no_lifeness ()
{
struct GNUNET_SCHEDULER_Task *t;
}
-int
-select_loop (struct GNUNET_SCHEDULER_Handle *sh, struct DriverContext *context);
+static int
+select_loop (struct GNUNET_SCHEDULER_Handle *sh,
+ struct DriverContext *context);
/**
task_cls,
GNUNET_SCHEDULER_REASON_STARTUP,
GNUNET_SCHEDULER_PRIORITY_DEFAULT);
- select_loop (sh, &context);
+ select_loop (sh,
+ &context);
GNUNET_SCHEDULER_driver_done (sh);
GNUNET_free (driver);
}
GNUNET_DISK_file_read (pr, &c, sizeof (c));
/* mark all active tasks as ready due to shutdown */
GNUNET_SCHEDULER_shutdown ();
+ shutdown_pipe_task =
+ GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
+ pr,
+ &shutdown_pipe_cb,
+ NULL);
}
/* scheduler must be running */
GNUNET_assert (NULL != scheduler_driver);
- GNUNET_assert ( (NULL != active_task) ||
- (GNUNET_NO == task->lifeness) );
is_fd_task = (NULL != task->fds);
if (is_fd_task)
{
{
struct GNUNET_SCHEDULER_Task *t;
+ /* scheduler must be running */
+ GNUNET_assert (NULL != scheduler_driver);
GNUNET_assert (NULL != task);
- GNUNET_assert ((NULL != active_task) ||
- (GNUNET_SCHEDULER_REASON_STARTUP == reason));
t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
t->read_fd = -1;
t->write_fd = -1;
struct GNUNET_SCHEDULER_Task *pos;
struct GNUNET_SCHEDULER_Task *prev;
- GNUNET_assert (NULL != active_task);
+ /* scheduler must be running */
+ GNUNET_assert (NULL != scheduler_driver);
GNUNET_assert (NULL != task);
t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
t->callback = task;
{
struct GNUNET_SCHEDULER_Task *t;
- GNUNET_assert (NULL != active_task);
+ /* scheduler must be running */
+ GNUNET_assert (NULL != scheduler_driver);
GNUNET_assert (NULL != task);
t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
t->callback = task;
{
struct GNUNET_SCHEDULER_Task *t;
- GNUNET_assert (NULL != active_task);
+ /* scheduler must be running */
+ GNUNET_assert (NULL != scheduler_driver);
GNUNET_assert (NULL != task);
t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
init_fd_info (t,
/* scheduler must be running */
GNUNET_assert (NULL != scheduler_driver);
- GNUNET_assert (NULL != active_task);
GNUNET_assert (NULL != task);
int no_rs = (NULL == rs);
int no_ws = (NULL == ws);
/**
* Function called by external event loop implementations to tell the
* scheduler to run some of the tasks that are ready. Must be called
- * only after #GNUNET_SCHEDULER_driver_init has been called and before
+ * only after #GNUNET_SCHEDULER_driver_init has been called and before
* #GNUNET_SCHEDULER_driver_done is called.
* This function may return even though there are tasks left to run
* just to give other tasks a chance as well. If we return #GNUNET_YES,
if (timeout.abs_value_us > now.abs_value_us)
{
/**
- * The driver called this function before the current timeout was
- * reached (and no FD tasks are ready). This can happen in the
- * rare case when the system time is changed while the driver is
- * waiting for the timeout, so we handle this gracefully. It might
- * also be a programming error in the driver though.
+ * The event loop called this function before the current timeout was
+ * reached (and no FD tasks are ready). This is acceptable if
+ *
+ * - the system time was changed while the driver was waiting for
+ * the timeout
+ * - an external event loop called GNUnet API functions outside of
+ * the callbacks called in GNUNET_SCHEDULER_do_work and thus
+ * wasn't notified about the new timeout
+ *
+ * It might also mean we are busy-waiting because of a programming
+ * error in the external event loop.
*/
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- "GNUNET_SCHEDULER_do_work did not find any ready "
- "tasks and timeout has not been reached yet.");
- return GNUNET_NO;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "GNUNET_SCHEDULER_do_work did not find any ready "
+ "tasks and timeout has not been reached yet.\n");
+ }
+ else
+ {
+ /**
+ * the current timeout was reached but no ready tasks were found,
+ * internal scheduler error!
+ */
+ GNUNET_assert (0);
}
- /**
- * the current timeout was reached but no ready tasks were found,
- * internal scheduler error!
- */
- GNUNET_assert (0);
- }
-
- /* find out which task priority level we are going to
- process this time */
- max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
- GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
- /* yes, p>0 is correct, 0 is "KEEP" which should
- * always be an empty queue (see assertion)! */
- for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
- {
- pos = ready_head[p];
- if (NULL != pos)
- break;
}
- GNUNET_assert (NULL != pos); /* ready_count wrong? */
-
- /* process all tasks at this priority level, then yield */
- while (NULL != (pos = ready_head[p]))
+ else
{
- GNUNET_CONTAINER_DLL_remove (ready_head[p],
- ready_tail[p],
- pos);
- ready_count--;
- current_priority = pos->priority;
- current_lifeness = pos->lifeness;
- active_task = pos;
-#if PROFILE_DELAYS
- if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
- DELAY_THRESHOLD.rel_value_us)
+ /* find out which task priority level we are going to
+ process this time */
+ max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
+ GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
+ /* yes, p>0 is correct, 0 is "KEEP" which should
+ * always be an empty queue (see assertion)! */
+ for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Task %p took %s to be scheduled\n",
- pos,
- GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
- GNUNET_YES));
+ pos = ready_head[p];
+ if (NULL != pos)
+ break;
}
-#endif
- tc.reason = pos->reason;
- GNUNET_NETWORK_fdset_zero (sh->rs);
- GNUNET_NETWORK_fdset_zero (sh->ws);
- // FIXME: do we have to remove FdInfos from fds if they are not ready?
- tc.fds_len = pos->fds_len;
- tc.fds = pos->fds;
- for (unsigned int i = 0; i != pos->fds_len; ++i)
+ GNUNET_assert (NULL != pos); /* ready_count wrong? */
+
+ /* process all tasks at this priority level, then yield */
+ while (NULL != (pos = ready_head[p]))
{
- struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
- if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
+ GNUNET_CONTAINER_DLL_remove (ready_head[p],
+ ready_tail[p],
+ pos);
+ ready_count--;
+ current_priority = pos->priority;
+ current_lifeness = pos->lifeness;
+ active_task = pos;
+#if PROFILE_DELAYS
+ if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
+ DELAY_THRESHOLD.rel_value_us)
{
- GNUNET_NETWORK_fdset_set_native (sh->rs,
- fdi->sock);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Task %p took %s to be scheduled\n",
+ pos,
+ GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
+ GNUNET_YES));
}
- if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
+#endif
+ tc.reason = pos->reason;
+ GNUNET_NETWORK_fdset_zero (sh->rs);
+ GNUNET_NETWORK_fdset_zero (sh->ws);
+ // FIXME: do we have to remove FdInfos from fds if they are not ready?
+ tc.fds_len = pos->fds_len;
+ tc.fds = pos->fds;
+ for (unsigned int i = 0; i != pos->fds_len; ++i)
{
- GNUNET_NETWORK_fdset_set_native (sh->ws,
- fdi->sock);
+ struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
+ if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
+ {
+ GNUNET_NETWORK_fdset_set_native (sh->rs,
+ fdi->sock);
+ }
+ if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
+ {
+ GNUNET_NETWORK_fdset_set_native (sh->ws,
+ fdi->sock);
+ }
}
- }
- tc.read_ready = sh->rs;
- tc.write_ready = sh->ws;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Running task %p\n",
- pos);
- GNUNET_assert (NULL != pos->callback);
- pos->callback (pos->callback_cls);
- if (NULL != pos->fds)
- {
- int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
- if (GNUNET_OK != del_result)
+ tc.read_ready = sh->rs;
+ tc.write_ready = sh->ws;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Running task %p\n",
+ pos);
+ GNUNET_assert (NULL != pos->callback);
+ pos->callback (pos->callback_cls);
+ if (NULL != pos->fds)
{
- LOG (GNUNET_ERROR_TYPE_ERROR,
- "driver could not delete task %p\n", pos);
- GNUNET_assert (0);
+ int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
+ if (GNUNET_OK != del_result)
+ {
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ "driver could not delete task %p\n", pos);
+ GNUNET_assert (0);
+ }
}
+ active_task = NULL;
+ dump_backtrace (pos);
+ destroy_task (pos);
}
- active_task = NULL;
- dump_backtrace (pos);
- destroy_task (pos);
}
shutdown_if_no_lifeness ();
if (0 == ready_count)
* Function called by external event loop implementations to initialize
* the scheduler. An external implementation has to provide @a driver
* which contains callbacks for the scheduler (see definition of struct
- * #GNUNET_SCHEDULER_Driver) for instructing the external implementation
- * to watch for events. If it detects any event it is expected to call
- * #GNUNET_SCHEDULER_do_work to let the scheduler handle it. If an event
- * is related to a specific task (e.g. the scheduler gave instructions
- * to watch a file descriptor), the external implementation is expected
- * to mark that task ready before by calling #GNUNET_SCHEDULER_task_ready.
+ * #GNUNET_SCHEDULER_Driver). The callbacks are used to instruct the
+ * external implementation to watch for events. If it detects any of
+ * those events it is expected to call #GNUNET_SCHEDULER_do_work to let
+ * the scheduler handle it. If an event is related to a specific task
+ * (e.g. the scheduler gave instructions to watch a file descriptor),
+ * the external implementation is expected to mark that task ready
+ * before by calling #GNUNET_SCHEDULER_task_ready.
* This function has to be called before any tasks are scheduled and
- * before GNUNET_SCHEDULER_do_work is called for the first time. It
+ * before GNUNET_SCHEDULER_do_work is called for the first time. It
* allocates resources that have to be freed again by calling
* #GNUNET_SCHEDULER_driver_done.
*
- * This function installs the same signal handlers as
+ * This function installs the same signal handlers as
* #GNUNET_SCHEDULER_run. This means SIGTERM (and other similar signals)
* will induce a call to #GNUNET_SCHEDULER_shutdown during the next
* call to #GNUNET_SCHEDULER_do_work. As a result, SIGTERM causes all
GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver)
{
struct GNUNET_SCHEDULER_Handle *sh;
- struct GNUNET_SCHEDULER_Task tsk;
const struct GNUNET_DISK_FileHandle *pr;
- /* general set-up */
- GNUNET_assert (NULL == active_task);
+ /* scheduler must not be running */
+ GNUNET_assert (NULL == scheduler_driver);
GNUNET_assert (NULL == shutdown_pipe_handle);
+ /* general set-up */
sh = GNUNET_new (struct GNUNET_SCHEDULER_Handle);
shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
GNUNET_NO,
/* Setup initial tasks */
current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
current_lifeness = GNUNET_NO;
- memset (&tsk,
- 0,
- sizeof (tsk));
- active_task = &tsk;
install_parent_control_task =
GNUNET_SCHEDULER_add_now (&install_parent_control_handler,
NULL);
&shutdown_pipe_cb,
NULL);
current_lifeness = GNUNET_YES;
- active_task = NULL;
scheduler_driver->set_wakeup (scheduler_driver->cls,
get_timeout ());
/* begin main event loop */
* by external event loop implementations after the scheduler has
* shut down. This is the case if both of the following conditions
* are met:
- *
+ *
* - all tasks the scheduler has added through the driver's add
* callback have been removed again through the driver's del
* callback
*
* @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
*/
-void GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
+void
+GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
{
GNUNET_assert (NULL == pending_head);
GNUNET_assert (NULL == pending_timeout_head);
}
-int
-select_loop (struct GNUNET_SCHEDULER_Handle *sh, struct DriverContext *context)
+static int
+select_loop (struct GNUNET_SCHEDULER_Handle *sh,
+ struct DriverContext *context)
{
struct GNUNET_NETWORK_FDSet *rs;
struct GNUNET_NETWORK_FDSet *ws;
GNUNET_assert (NULL != context);
rs = GNUNET_NETWORK_fdset_create ();
ws = GNUNET_NETWORK_fdset_create ();
- while (NULL != context->scheduled_head ||
- GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != context->timeout.abs_value_us)
+ while ( (NULL != context->scheduled_head) ||
+ (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != context->timeout.abs_value_us) )
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"select timeout = %s\n",
GNUNET_NETWORK_fdset_zero (rs);
GNUNET_NETWORK_fdset_zero (ws);
- struct Scheduled *pos;
- for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
+
+ for (struct Scheduled *pos = context->scheduled_head;
+ NULL != pos;
+ pos = pos->next)
{
if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
{
if (errno == EINTR)
continue;
- LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
+ LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
+ "select");
#ifndef MINGW
#if USE_LSOF
char lsof[512];
- snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
+ snprintf (lsof,
+ sizeof (lsof),
+ "lsof -p %d",
+ getpid ());
(void) close (1);
(void) dup2 (2, 1);
if (0 != system (lsof))
#endif
#endif
#if DEBUG_FDS
- struct Scheduled *s;
- for (s = context->scheduled_head; NULL != s; s = s->next)
+ for (struct Scheduled *s = context->scheduled_head;
+ NULL != s;
+ s = s->next)
{
- int flags = fcntl (s->fdi->sock, F_GETFD);
- if ((flags == -1) && (errno == EBADF))
+ int flags = fcntl (s->fdi->sock,
+ F_GETFD);
+
+ if ( (flags == -1) &&
+ (EBADF == errno) )
{
LOG (GNUNET_ERROR_TYPE_ERROR,
"Got invalid file descriptor %d!\n",
s->fdi->sock);
+#if EXECINFO
+ dump_backtrace (s->task);
+#endif
}
}
#endif
}
if (select_result > 0)
{
- for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
+ for (struct Scheduled *pos = context->scheduled_head;
+ NULL != pos;
+ pos = pos->next)
{
int is_ready = GNUNET_NO;
+
if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et) &&
- GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, pos->fdi->sock))
+ GNUNET_YES ==
+ GNUNET_NETWORK_fdset_test_native (rs,
+ pos->fdi->sock))
{
pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
is_ready = GNUNET_YES;
}
if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et) &&
- GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, pos->fdi->sock))
+ GNUNET_YES ==
+ GNUNET_NETWORK_fdset_test_native (ws,
+ pos->fdi->sock))
{
pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
is_ready = GNUNET_YES;
}
if (GNUNET_YES == is_ready)
{
- GNUNET_SCHEDULER_task_ready (pos->task, pos->fdi);
+ GNUNET_SCHEDULER_task_ready (pos->task,
+ pos->fdi);
}
}
}
if (GNUNET_YES == GNUNET_SCHEDULER_do_work (sh))
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "scheduler has more tasks ready!\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "scheduler has more tasks ready!\n");
}
}
GNUNET_NETWORK_fdset_destroy (rs);
}
-int
+static int
select_add (void *cls,
struct GNUNET_SCHEDULER_Task *task,
struct GNUNET_SCHEDULER_FdInfo *fdi)
}
-int
+static int
select_del (void *cls,
struct GNUNET_SCHEDULER_Task *task)
{
}
-void
+static void
select_set_wakeup (void *cls,
struct GNUNET_TIME_Absolute dt)
{
struct DriverContext *context = cls;
-
+
GNUNET_assert (NULL != context);
context->timeout = dt;
}