-#if 1
- /* new, optimized version */
- struct Task *t;
- struct Task *pos;
- struct Task *prev;
-#if EXECINFO
- void *backtrace_array[MAX_TRACE_DEPTH];
-#endif
-
- GNUNET_assert (active_task != NULL);
- 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
- t->read_fd = -1;
- t->write_fd = -1;
- t->id = ++last_id;
-#if PROFILE_DELAYS
- t->start_time = GNUNET_TIME_absolute_get ();
-#endif
- t->timeout = GNUNET_TIME_relative_to_absolute (delay);
- t->priority = current_priority;
- t->lifeness = current_lifeness;
- /* try tail first (optimization in case we are
- appending to a long list of tasks with timeouts) */
- prev = pending_timeout_last;
- if (prev != NULL)
- {
- if (prev->timeout.abs_value > t->timeout.abs_value)
- prev = NULL;
- else
- pos = prev->next; /* heuristic success! */
- }
- if (prev == NULL)
- {
- /* heuristic failed, do traversal of timeout list */
- pos = pending_timeout;
- }
- while ( (pos != NULL) &&
- ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
- (pos->reason != 0) ) )
- {
- prev = pos;
- pos = pos->next;
- }
- if (prev == NULL)
- pending_timeout = t;
- else
- prev->next = t;
- t->next = pos;
- /* hyper-optimization... */
- pending_timeout_last = t;
-
-#if DEBUG_TASKS
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Adding task: %llu / %p\n", t->id, t->callback_cls);
-#endif
-#if EXECINFO
- int i;
-
- for (i=0;i<t->num_backtrace_strings;i++)
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Task %llu trace %d: %s\n",
- t->id,
- i,
- t->backtrace_strings[i]);
-#endif
- return t->id;
-
-#else
- /* unoptimized version */
- return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
- GNUNET_SCHEDULER_NO_TASK, delay,
- NULL, NULL, task, task_cls);
-#endif