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
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));
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);
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.
*/
LOG (GNUNET_ERROR_TYPE_DEBUG,
"GNUNET_SCHEDULER_do_work did not find any ready "
"tasks and timeout has not been reached yet.\n");
- return GNUNET_NO;
}
- /**
- * 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;
+ else
+ {
+ /**
+ * the current timeout was reached but no ready tasks were found,
+ * internal scheduler error!
+ */
+ GNUNET_assert (0);
+ }
}
- 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)
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 */
*
* @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
+static int
select_del (void *cls,
struct GNUNET_SCHEDULER_Task *task)
{
}
-void
+static void
select_set_wakeup (void *cls,
struct GNUNET_TIME_Absolute dt)
{