/* There is some data available now */
SILC_LOG_DEBUG(("Running fd tasks"));
silc_schedule_dispatch_fd(schedule);
+
+ /* If timeout was very short, dispatch also timeout tasks */
+ if (schedule->has_timeout && schedule->timeout.tv_sec == 0 &&
+ schedule->timeout.tv_usec < 50000)
+ silc_schedule_dispatch_timeout(schedule, FALSE);
continue;
} else {
return schedule->app_context;
}
+/* Set notify callback */
+
+void silc_schedule_set_notify(SilcSchedule schedule,
+ SilcTaskNotifyCb notify, void *context)
+{
+ schedule->notify = notify;
+ schedule->notify_context = context;
+}
+
/* Add new task to the scheduler */
SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
task = (SilcTask)ttask;
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, TRUE, task, FALSE, 0, 0, seconds, useconds,
+ schedule->notify_context);
+
} else if (silc_likely(type == SILC_TASK_FD)) {
SilcTaskFd ftask;
task = (SilcTask)ftask;
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, TRUE, task, TRUE, ftask->fd,
+ SILC_TASK_READ, 0, 0, schedule->notify_context);
+
} else if (silc_unlikely(type == SILC_TASK_SIGNAL)) {
SILC_SCHEDULE_UNLOCK(schedule);
schedule_ops.signal_register(schedule, schedule->internal, fd,
/* Delete from fd queue */
silc_hash_table_list(schedule->fd_queue, &htl);
- while (silc_hash_table_get(&htl, NULL, (void *)&task))
+ while (silc_hash_table_get(&htl, NULL, (void *)&task)) {
task->valid = FALSE;
+
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, FALSE, task, TRUE,
+ ((SilcTaskFd)task)->fd, 0, 0, 0,
+ schedule->notify_context);
+ }
silc_hash_table_list_reset(&htl);
/* Delete from timeout queue */
silc_list_start(schedule->timeout_queue);
- while ((task = (SilcTask)silc_list_get(schedule->timeout_queue))
- != SILC_LIST_END)
+ while ((task = (SilcTask)silc_list_get(schedule->timeout_queue))) {
task->valid = FALSE;
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, FALSE, task, FALSE, 0, 0, 0, 0,
+ schedule->notify_context);
+ }
+
SILC_SCHEDULE_UNLOCK(schedule);
return TRUE;
}
SILC_LOG_DEBUG(("Unregistering task %p", task));
SILC_SCHEDULE_LOCK(schedule);
task->valid = FALSE;
+
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, FALSE, task, !task->type, 0, 0, 0, 0,
+ schedule->notify_context);
SILC_SCHEDULE_UNLOCK(schedule);
return TRUE;
(void *)&task))) {
SILC_LOG_DEBUG(("Deleting task %p", task));
task->valid = FALSE;
+
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, FALSE, task, TRUE, fd, 0, 0, 0,
+ schedule->notify_context);
ret = TRUE;
}
while (silc_hash_table_get(&htl, NULL, (void *)&task)) {
if (task->callback == callback) {
task->valid = FALSE;
+
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, FALSE, task, TRUE,
+ ((SilcTaskFd)task)->fd, 0, 0, 0,
+ schedule->notify_context);
ret = TRUE;
}
}
while ((task = (SilcTask)silc_list_get(list))) {
if (task->callback == callback) {
task->valid = FALSE;
+
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, FALSE, task, FALSE, 0, 0, 0, 0,
+ schedule->notify_context);
ret = TRUE;
}
}
while (silc_hash_table_get(&htl, NULL, (void *)&task)) {
if (task->context == context) {
task->valid = FALSE;
+
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, FALSE, task, TRUE,
+ ((SilcTaskFd)task)->fd, 0, 0, 0,
+ schedule->notify_context);
ret = TRUE;
}
}
silc_list_start(list);
while ((task = (SilcTask)silc_list_get(list))) {
if (task->context == context) {
- ret = TRUE;
task->valid = FALSE;
+
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, FALSE, task, FALSE, 0, 0, 0, 0,
+ schedule->notify_context);
+ ret = TRUE;
}
}
while ((task = (SilcTask)silc_list_get(list))) {
if (task->callback == callback && task->context == context) {
task->valid = FALSE;
+
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, FALSE, task, FALSE, 0, 0, 0, 0,
+ schedule->notify_context);
ret = TRUE;
}
}
task->revents = mask;
silc_schedule_dispatch_fd(schedule);
}
+
+ /* Call notify callback */
+ if (schedule->notify)
+ schedule->notify(schedule, TRUE, (SilcTask)task,
+ TRUE, task->fd, mask, 0, 0,
+ schedule->notify_context);
}
SILC_SCHEDULE_UNLOCK(schedule);
SilcTaskEvent type, SilcUInt32 fd,
void *context);
+/****f* silcutil/SilcScheduleAPI/SilcTaskNotifyCb
+ *
+ * SYNOPSIS
+ *
+ * typedef void (*SilcTaskNotifyCb)(SilcSchedule schedule,
+ * SilcBool added, SilcTask task,
+ * SilcBool fd_task, SilcUInt32 fd,
+ * SilcTaskEvent event,
+ * long seconds, long useconds,
+ * void *context);
+ *
+ * DESCRIPTION
+ *
+ * Task notify callback. Callback of this type can be set to scheduler
+ * by calling silc_schedule_set_notify and will be called whenever new
+ * task is added or old task is removed. If `added' is TRUE then `task'
+ * is added to scheduler. If `added' is FALSE then `task' will be removed
+ * from the scheduler. If `fd_task' is TRUE the `task' is file descriptor
+ * task and has `fd' is its file descriptor. If `fd_task' is FALSE then
+ * the task is timeout task and `seconds' and `useconds' specify the
+ * timeout. The `context' is the context given to silc_schedule_set_notify.
+ *
+ * NOTES
+ *
+ * The `schedule' is locked while this callback is called. This means that
+ * new tasks cannot be added or removed inside this callback.
+ *
+ * When timeout task expires this callback is not called. This is called
+ * only when task is explicitly deleted from the scheduler. Note that,
+ * when timeout task expires it is removed from the scheduler and `task'
+ * will become invalid.
+ *
+ * If fd task changes its events, this will be called as if it was a new
+ * task with different `event' mask.
+ *
+ ***/
+typedef void (*SilcTaskNotifyCb)(SilcSchedule schedule,
+ SilcBool added, SilcTask task,
+ SilcBool fd_task, SilcUInt32 fd,
+ SilcTaskEvent event,
+ long seconds, long useconds,
+ void *app_context);
+
/* Macros */
/****d* silcutil/SilcScheduleAPI/SILC_ALL_TASKS
*
* DESCRIPTION
*
- * Generic macro to define task callback functions. This defines a
- * static function with name `func' as a task callback function.
+ * Generic macro to declare task callback functions. This defines a
+ * function with name `func' as a task callback function.
*
* SOURCE
*/
***/
void *silc_schedule_get_context(SilcSchedule schedule);
+/****f* silcutil/SilcScheduleAPI/silc_schedule_set_notify
+ *
+ * SYNOPSIS
+ *
+ * void silc_schedule_set_notify(SilcSchedule schedule,
+ * SilcTaskNotifyCb notify, void *context);
+ *
+ * DESCRIPTION
+ *
+ * Set notify callback to scheduler. The `notify' will be called whenever
+ * task is added to or deleted from scheduler.
+ *
+ ***/
+void silc_schedule_set_notify(SilcSchedule schedule,
+ SilcTaskNotifyCb notify, void *context);
+
/****f* silcutil/SilcScheduleAPI/silc_schedule_task_add_fd
*
* SYNOPSIS
* Add timeout task to scheduler. The `callback' will be called once
* the specified timeout has elapsed. The task will be removed from the
* scheduler automatically once the task expires. The event returned
- * to the `callback' is SILC_TASK_EXPIRE. The task added with zero (0)
+ * to the `callback' is SILC_TASK_EXPIRE. A task added with zero (0)
* timeout will be executed immediately next time tasks are scheduled.
*
***/
(void)read(internal->wakeup_pipe[0], &c, 1);
}
+SILC_TASK_CALLBACK(silc_schedule_wakeup_init)
+{
+ SilcUnixScheduler internal = schedule->internal;
+
+ internal->wakeup_task =
+ silc_schedule_task_add(schedule, internal->wakeup_pipe[0],
+ silc_schedule_wakeup_cb, internal,
+ 0, 0, SILC_TASK_FD);
+ if (!internal->wakeup_task) {
+ SILC_LOG_WARNING(("Could not add a wakeup task, threads won't work"));
+ close(internal->wakeup_pipe[0]);
+ return;
+ }
+ silc_schedule_internal_schedule_fd(schedule, internal,
+ (SilcTaskFd)internal->wakeup_task,
+ SILC_TASK_READ);
+}
#endif /* SILC_THREADS */
/* Initializes the platform specific scheduler. This for example initializes
return NULL;
}
- internal->wakeup_task =
- silc_schedule_task_add(schedule, internal->wakeup_pipe[0],
- silc_schedule_wakeup_cb, internal,
- 0, 0, SILC_TASK_FD);
- if (!internal->wakeup_task) {
- SILC_LOG_ERROR(("Could not add a wakeup task, threads won't work"));
- close(internal->wakeup_pipe[0]);
- close(internal->wakeup_pipe[1]);
- silc_free(internal);
- return NULL;
- }
- silc_schedule_internal_schedule_fd(schedule, internal,
- (SilcTaskFd)internal->wakeup_task,
- SILC_TASK_READ);
+ silc_schedule_task_add_timeout(schedule, silc_schedule_wakeup_init,
+ internal, 0, 0);
#endif /* SILC_THREADS */
internal->app_context = app_context;