#include "gnunet_scheduler_lib.h"
#include "gnunet_signal_lib.h"
#include "gnunet_time_lib.h"
+#ifdef LINUX
+#include "execinfo.h"
+#define EXECINFO GNUNET_NO
+#define MAX_TRACE_DEPTH 50
+#endif
#define DEBUG_TASKS GNUNET_NO
struct GNUNET_NETWORK_FDSet *read_set;
/**
- * Set of file descriptors this task is waiting
- * for for writing. Once ready, this is updated
- * to reflect the set of file descriptors ready
- * for operation.
+ * Set of file descriptors this task is waiting for for writing.
+ * Once ready, this is updated to reflect the set of file
+ * descriptors ready for operation.
*/
struct GNUNET_NETWORK_FDSet *write_set;
*/
enum GNUNET_SCHEDULER_Priority priority;
+#if EXECINFO
+ /**
+ * Array of strings which make up a backtrace from the point when this
+ * task was scheduled (essentially, who scheduled the task?)
+ */
+ char **backtrace_strings;
+
+ /**
+ * Size of the backtrace_strings array
+ */
+ int num_backtrace_strings;
+#endif
+
};
*/
struct Task *pending;
+ /**
+ * ID of the task that is running right now.
+ */
+ struct Task *active_task;
+
/**
* List of tasks ready to run right now,
* grouped by importance.
if (pos->write_set != NULL)
GNUNET_NETWORK_fdset_add (ws, pos->write_set);
if (pos->reason != 0)
- *timeout = GNUNET_TIME_UNIT_ZERO;
+ *timeout = GNUNET_TIME_UNIT_ZERO;
pos = pos->next;
}
}
const struct GNUNET_NETWORK_FDSet *rs,
const struct GNUNET_NETWORK_FDSet *ws)
{
- if (now.value >= task->timeout.value)
+ if (now.value >= task->timeout.value)
task->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
(rs != NULL) && (set_overlaps (rs, task->read_set)))
static void
queue_ready_task (struct GNUNET_SCHEDULER_Handle *handle, struct Task *task)
{
- task->next = handle->ready[check_priority (task->priority)];
- handle->ready[check_priority (task->priority)] = task;
+ enum GNUNET_SCHEDULER_Priority p = task->priority;
+ if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+ p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
+ task->next = handle->ready[check_priority (p)];
+ handle->ready[check_priority (p)] = task;
handle->ready_count++;
}
{
#if DEBUG_TASKS
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Checking readyness of task: %llu / %p\n",
+ "Checking readiness of task: %llu / %p\n",
pos->id, pos->callback_cls);
#endif
next = pos->next;
GNUNET_SCHEDULER_shutdown (struct GNUNET_SCHEDULER_Handle *sched)
{
struct Task *pos;
+ int i;
pos = sched->pending;
while (pos != NULL)
{
pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
/* we don't move the task into the ready queue yet; check_ready
- will do that later, possibly adding additional
- readyness-factors */
+ will do that later, possibly adding additional
+ readiness-factors */
pos = pos->next;
}
+ for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
+ {
+ pos = sched->ready[i];
+ while (pos != NULL)
+ {
+ pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
+ /* we don't move the task into the ready queue yet; check_ready
+ will do that later, possibly adding additional
+ readiness-factors */
+ pos = pos->next;
+ }
+ }
}
GNUNET_NETWORK_fdset_destroy (t->read_set);
if (NULL != t->write_set)
GNUNET_NETWORK_fdset_destroy (t->write_set);
+#if EXECINFO
+ GNUNET_free (t->backtrace_strings);
+#endif
GNUNET_free (t);
}
GNUNET_assert (pos != NULL); /* ready_count wrong? */
sched->ready[p] = pos->next;
sched->ready_count--;
- sched->current_priority = p;
- GNUNET_assert (pos->priority == p);
+ sched->current_priority = pos->priority;
+ sched->active_task = pos;
tc.sched = sched;
tc.reason = pos->reason;
tc.read_ready = pos->read_set;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Running task: %llu / %p\n", pos->id, pos->callback_cls);
#endif
+ sched->active_task = NULL;
destroy_task (pos);
sched->tasks_run++;
}
while ((sched->pending == NULL) || (p == GNUNET_SCHEDULER_PRIORITY_URGENT));
}
-#ifndef MINGW
/**
* Pipe used to communicate shutdown via signal.
*/
(sigpipe, GNUNET_DISK_PIPE_END_WRITE), &c,
sizeof (c));
}
-#endif
/**
struct GNUNET_SIGNAL_Context *shc_hup;
unsigned long long last_tr;
unsigned int busy_wait_warning;
-#ifndef MINGW
const struct GNUNET_DISK_FileHandle *pr;
-#endif
char c;
rs = GNUNET_NETWORK_fdset_create ();
ws = GNUNET_NETWORK_fdset_create ();
-#ifndef MINGW
GNUNET_assert (sigpipe == NULL);
sigpipe = GNUNET_DISK_pipe (GNUNET_NO);
GNUNET_assert (sigpipe != NULL);
pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
GNUNET_assert (pr != NULL);
- shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
+ shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
+#ifndef MINGW
shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
- shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
+ shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
#endif
memset (&sched, 0, sizeof (sched));
sched.current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
GNUNET_SCHEDULER_add_continuation (&sched,
task,
- task_cls,
- GNUNET_SCHEDULER_REASON_STARTUP);
+ task_cls,
+ GNUNET_SCHEDULER_REASON_STARTUP);
last_tr = 0;
busy_wait_warning = 0;
- while ( (sched.pending != NULL) || (sched.ready_count > 0) )
+ while ((sched.pending != NULL) || (sched.ready_count > 0))
{
GNUNET_NETWORK_fdset_zero (rs);
GNUNET_NETWORK_fdset_zero (ws);
timeout = GNUNET_TIME_UNIT_ZERO;
}
ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
-#ifndef MINGW
+ if (ret == GNUNET_SYSERR)
+ {
+ if (errno == EINTR)
+ continue;
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
+ break;
+ }
if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
- {
- /* consume the signal */
- GNUNET_DISK_file_read (pr, &c, sizeof(c));
- /* mark all active tasks as ready due to shutdown */
- GNUNET_SCHEDULER_shutdown (&sched);
- }
-#endif
+ {
+ /* consume the signal */
+ GNUNET_DISK_file_read (pr, &c, sizeof (c));
+ /* mark all active tasks as ready due to shutdown */
+ GNUNET_SCHEDULER_shutdown (&sched);
+ }
if (last_tr == sched.tasks_run)
{
busy_wait_warning++;
_("Looks like we're busy waiting...\n"));
sleep (1); /* mitigate */
}
- if (ret == GNUNET_SYSERR)
- {
- if (errno == EINTR)
- continue;
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
- break;
- }
check_ready (&sched, rs, ws);
run_ready (&sched);
}
-#ifndef MINGW
GNUNET_SIGNAL_handler_uninstall (shc_int);
GNUNET_SIGNAL_handler_uninstall (shc_term);
+#ifndef MINGW
GNUNET_SIGNAL_handler_uninstall (shc_quit);
GNUNET_SIGNAL_handler_uninstall (shc_hup);
+#endif
GNUNET_DISK_pipe_close (sigpipe);
sigpipe = NULL;
-#endif
GNUNET_NETWORK_fdset_destroy (rs);
GNUNET_NETWORK_fdset_destroy (ws);
}
+/**
+ * Obtain the reason code for why the current task was
+ * started. Will return the same value as
+ * the GNUNET_SCHEDULER_TaskContext's reason field.
+ *
+ * @param sched scheduler to query
+ * @return reason(s) why the current task is run
+ */
+enum GNUNET_SCHEDULER_Reason
+GNUNET_SCHEDULER_get_reason (struct GNUNET_SCHEDULER_Handle *sched)
+{
+ return sched->active_task->reason;
+}
+
/**
* Get information about the current load of this scheduler. Use this
enum GNUNET_SCHEDULER_Reason reason)
{
struct Task *t;
-
+#if EXECINFO
+ void *backtrace_array[50];
+#endif
t = GNUNET_malloc (sizeof (struct Task));
+#if EXECINFO
+ t->num_backtrace_strings = backtrace(backtrace_array, 50);
+ t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
+#endif
t->callback = task;
t->callback_cls = task_cls;
t->id = ++sched->last_id;
* @param sched scheduler to use
* @param prerequisite_task run this task after the task with the given
* task identifier completes (and any of our other
- * conditions, such as delay, read or write-readyness
+ * conditions, such as delay, read or write-readiness
* are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
* on completion of other tasks (this will cause the task to run as
* soon as possible).
GNUNET_SCHEDULER_TaskIdentifier
GNUNET_SCHEDULER_add_after (struct GNUNET_SCHEDULER_Handle *sched,
GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
- GNUNET_SCHEDULER_Task task,
- void *task_cls)
+ GNUNET_SCHEDULER_Task task, void *task_cls)
{
- return GNUNET_SCHEDULER_add_select (sched,
- GNUNET_SCHEDULER_PRIORITY_KEEP,
+ return GNUNET_SCHEDULER_add_select (sched,
+ GNUNET_SCHEDULER_PRIORITY_KEEP,
prerequisite_task,
GNUNET_TIME_UNIT_ZERO,
NULL, NULL, task, task_cls);
* only valid until "task" is started!
*/
GNUNET_SCHEDULER_TaskIdentifier
-GNUNET_SCHEDULER_add_with_priority (struct GNUNET_SCHEDULER_Handle *sched,
- enum GNUNET_SCHEDULER_Priority prio,
- GNUNET_SCHEDULER_Task task,
- void *task_cls)
+GNUNET_SCHEDULER_add_with_priority (struct GNUNET_SCHEDULER_Handle * sched,
+ enum GNUNET_SCHEDULER_Priority prio,
+ GNUNET_SCHEDULER_Task task,
+ void *task_cls)
{
- return GNUNET_SCHEDULER_add_select (sched,
- prio,
+ return GNUNET_SCHEDULER_add_select (sched,
+ prio,
GNUNET_SCHEDULER_NO_TASK,
GNUNET_TIME_UNIT_ZERO,
NULL, NULL, task, task_cls);
* only valid until "task" is started!
*/
GNUNET_SCHEDULER_TaskIdentifier
-GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle *sched,
+GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle * sched,
struct GNUNET_TIME_Relative delay,
- GNUNET_SCHEDULER_Task task,
- void *task_cls)
+ GNUNET_SCHEDULER_Task task, void *task_cls)
{
return GNUNET_SCHEDULER_add_select (sched,
- GNUNET_SCHEDULER_PRIORITY_KEEP,
+ GNUNET_SCHEDULER_PRIORITY_KEEP,
GNUNET_SCHEDULER_NO_TASK, delay,
NULL, NULL, task, task_cls);
}
+
+/**
+ * Schedule a new task to be run as soon as possible. The task
+ * will be run with the priority of the calling task.
+ *
+ * @param sched scheduler to use
+ * @param task main function of the task
+ * @param task_cls closure of task
+ * @return unique task identifier for the job
+ * only valid until "task" is started!
+ */
+GNUNET_SCHEDULER_TaskIdentifier
+GNUNET_SCHEDULER_add_now (struct GNUNET_SCHEDULER_Handle *sched,
+ GNUNET_SCHEDULER_Task task,
+ void *task_cls)
+{
+ return GNUNET_SCHEDULER_add_select (sched,
+ GNUNET_SCHEDULER_PRIORITY_KEEP,
+ GNUNET_SCHEDULER_NO_TASK,
+ GNUNET_TIME_UNIT_ZERO,
+ NULL, NULL, task, task_cls);
+}
+
+
+
/**
* Schedule a new task to be run with a specified delay or when the
* specified file descriptor is ready for reading. The delay can be
* only valid until "task" is started!
*/
GNUNET_SCHEDULER_TaskIdentifier
-GNUNET_SCHEDULER_add_read_net (struct GNUNET_SCHEDULER_Handle *sched,
- struct GNUNET_TIME_Relative delay,
- struct GNUNET_NETWORK_Handle *rfd,
- GNUNET_SCHEDULER_Task task,
- void *task_cls)
+GNUNET_SCHEDULER_add_read_net (struct GNUNET_SCHEDULER_Handle * sched,
+ struct GNUNET_TIME_Relative delay,
+ struct GNUNET_NETWORK_Handle * rfd,
+ GNUNET_SCHEDULER_Task task, void *task_cls)
{
struct GNUNET_NETWORK_FDSet *rs;
GNUNET_SCHEDULER_TaskIdentifier ret;
GNUNET_assert (rfd != NULL);
rs = GNUNET_NETWORK_fdset_create ();
GNUNET_NETWORK_fdset_set (rs, rfd);
- ret = GNUNET_SCHEDULER_add_select (sched,
- GNUNET_SCHEDULER_PRIORITY_KEEP,
- GNUNET_SCHEDULER_NO_TASK,
- delay,
- rs, NULL, task, task_cls);
+ ret = GNUNET_SCHEDULER_add_select (sched,
+ GNUNET_SCHEDULER_PRIORITY_KEEP,
+ GNUNET_SCHEDULER_NO_TASK,
+ delay, rs, NULL, task, task_cls);
GNUNET_NETWORK_fdset_destroy (rs);
return ret;
}
* only valid until "task" is started!
*/
GNUNET_SCHEDULER_TaskIdentifier
-GNUNET_SCHEDULER_add_write_net (struct GNUNET_SCHEDULER_Handle *sched,
- struct GNUNET_TIME_Relative delay,
- struct GNUNET_NETWORK_Handle *wfd,
- GNUNET_SCHEDULER_Task task,
- void *task_cls)
+GNUNET_SCHEDULER_add_write_net (struct GNUNET_SCHEDULER_Handle * sched,
+ struct GNUNET_TIME_Relative delay,
+ struct GNUNET_NETWORK_Handle * wfd,
+ GNUNET_SCHEDULER_Task task, void *task_cls)
{
struct GNUNET_NETWORK_FDSet *ws;
GNUNET_SCHEDULER_TaskIdentifier ret;
GNUNET_assert (wfd != NULL);
ws = GNUNET_NETWORK_fdset_create ();
GNUNET_NETWORK_fdset_set (ws, wfd);
- ret = GNUNET_SCHEDULER_add_select (sched,
- GNUNET_SCHEDULER_PRIORITY_KEEP,
+ ret = GNUNET_SCHEDULER_add_select (sched,
+ GNUNET_SCHEDULER_PRIORITY_KEEP,
GNUNET_SCHEDULER_NO_TASK, delay,
NULL, ws, task, task_cls);
GNUNET_NETWORK_fdset_destroy (ws);
* only valid until "task" is started!
*/
GNUNET_SCHEDULER_TaskIdentifier
-GNUNET_SCHEDULER_add_read_file (struct GNUNET_SCHEDULER_Handle *sched,
- struct GNUNET_TIME_Relative delay,
- const struct GNUNET_DISK_FileHandle *rfd,
- GNUNET_SCHEDULER_Task task,
- void *task_cls)
+GNUNET_SCHEDULER_add_read_file (struct GNUNET_SCHEDULER_Handle * sched,
+ struct GNUNET_TIME_Relative delay,
+ const struct GNUNET_DISK_FileHandle * rfd,
+ GNUNET_SCHEDULER_Task task, void *task_cls)
{
struct GNUNET_NETWORK_FDSet *rs;
GNUNET_SCHEDULER_TaskIdentifier ret;
rs = GNUNET_NETWORK_fdset_create ();
GNUNET_NETWORK_fdset_handle_set (rs, rfd);
ret = GNUNET_SCHEDULER_add_select (sched,
- GNUNET_SCHEDULER_PRIORITY_KEEP,
- GNUNET_SCHEDULER_NO_TASK, delay,
+ GNUNET_SCHEDULER_PRIORITY_KEEP,
+ GNUNET_SCHEDULER_NO_TASK, delay,
rs, NULL, task, task_cls);
GNUNET_NETWORK_fdset_destroy (rs);
return ret;
* only valid until "task" is started!
*/
GNUNET_SCHEDULER_TaskIdentifier
-GNUNET_SCHEDULER_add_write_file (struct GNUNET_SCHEDULER_Handle *sched,
- struct GNUNET_TIME_Relative delay,
- const struct GNUNET_DISK_FileHandle *wfd,
- GNUNET_SCHEDULER_Task task,
- void *task_cls)
+GNUNET_SCHEDULER_add_write_file (struct GNUNET_SCHEDULER_Handle * sched,
+ struct GNUNET_TIME_Relative delay,
+ const struct GNUNET_DISK_FileHandle * wfd,
+ GNUNET_SCHEDULER_Task task, void *task_cls)
{
struct GNUNET_NETWORK_FDSet *ws;
GNUNET_SCHEDULER_TaskIdentifier ret;
GNUNET_assert (wfd != NULL);
ws = GNUNET_NETWORK_fdset_create ();
GNUNET_NETWORK_fdset_handle_set (ws, wfd);
- ret = GNUNET_SCHEDULER_add_select (sched,
- GNUNET_SCHEDULER_PRIORITY_KEEP,
- GNUNET_SCHEDULER_NO_TASK,
- delay,
- NULL, ws, task, task_cls);
+ ret = GNUNET_SCHEDULER_add_select (sched,
+ GNUNET_SCHEDULER_PRIORITY_KEEP,
+ GNUNET_SCHEDULER_NO_TASK,
+ delay, NULL, ws, task, task_cls);
GNUNET_NETWORK_fdset_destroy (ws);
return ret;
}
* @param prio how important is this task?
* @param prerequisite_task run this task after the task with the given
* task identifier completes (and any of our other
- * conditions, such as delay, read or write-readyness
+ * conditions, such as delay, read or write-readiness
* are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
* on completion of other tasks.
* @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
* only valid until "task" is started!
*/
GNUNET_SCHEDULER_TaskIdentifier
-GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle *sched,
+GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle * sched,
enum GNUNET_SCHEDULER_Priority prio,
GNUNET_SCHEDULER_TaskIdentifier
prerequisite_task,
struct GNUNET_TIME_Relative delay,
const struct GNUNET_NETWORK_FDSet * rs,
- const struct GNUNET_NETWORK_FDSet * ws,
- GNUNET_SCHEDULER_Task task,
- void *task_cls)
+ const struct GNUNET_NETWORK_FDSet * ws,
+ GNUNET_SCHEDULER_Task task, void *task_cls)
{
struct Task *t;
+#if EXECINFO
+ void *backtrace_array[MAX_TRACE_DEPTH];
+#endif
+ GNUNET_assert (NULL != task);
t = GNUNET_malloc (sizeof (struct Task));
t->callback = task;
t->callback_cls = task_cls;
+#if EXECINFO
+ t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
+ t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
+#endif
if (rs != NULL)
{
t->read_set = GNUNET_NETWORK_fdset_create ();