5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1998 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "silcincludes.h"
23 #include "silcschedule_i.h"
25 /* Forward declarations */
26 typedef struct SilcTaskQueueStruct *SilcTaskQueue;
28 /* System specific routines. Implemented under unix/, win32/ and such. */
30 /* System specific select(). Returns same values as normal select(). */
31 int silc_select(SilcScheduleFd fds, SilcUInt32 fds_count,
32 struct timeval *timeout);
34 /* Initializes the platform specific scheduler. This for example initializes
35 the wakeup mechanism of the scheduler. In multi-threaded environment
36 the scheduler needs to be wakenup when tasks are added or removed from
37 the task queues. Returns context to the platform specific scheduler. */
38 void *silc_schedule_internal_init(SilcSchedule schedule);
40 /* Uninitializes the platform specific scheduler context. */
41 void silc_schedule_internal_uninit(void *context);
43 /* Wakes up the scheduler. This is platform specific routine */
44 void silc_schedule_internal_wakeup(void *context);
47 void silc_schedule_internal_signal_register(void *context,
49 SilcTaskCallback callback,
50 void *callback_context);
52 /* Unregister signal */
53 void silc_schedule_internal_signal_unregister(void *context,
55 SilcTaskCallback callback,
56 void *callback_context);
58 /* Mark signal to be called later. */
59 void silc_schedule_internal_signal_call(void *context, SilcUInt32 signal);
61 /* Call all signals */
62 void silc_schedule_internal_signals_call(void *context,
63 SilcSchedule schedule);
65 /* Block registered signals in scheduler. */
66 void silc_schedule_internal_signals_block(void *context);
68 /* Unblock registered signals in schedule. */
69 void silc_schedule_internal_signals_unblock(void *context);
71 /* Internal task management routines. */
73 static void silc_task_queue_alloc(SilcTaskQueue *queue);
74 static void silc_task_queue_free(SilcTaskQueue queue);
75 static SilcTask silc_task_find(SilcTaskQueue queue, SilcUInt32 fd);
76 static SilcTask silc_task_add(SilcTaskQueue queue, SilcTask newtask,
77 SilcTaskPriority priority);
78 static SilcTask silc_task_get_first(SilcTaskQueue queue, SilcTask first);
79 static SilcTask silc_task_add_timeout(SilcTaskQueue queue, SilcTask newtask,
80 SilcTaskPriority priority);
81 static int silc_schedule_task_remove(SilcTaskQueue queue, SilcTask task);
82 static int silc_schedule_task_timeout_compare(struct timeval *smaller,
83 struct timeval *bigger);
84 static void silc_task_del_by_context(SilcTaskQueue queue, void *context);
85 static void silc_task_del_by_callback(SilcTaskQueue queue,
86 SilcTaskCallback callback);
87 static void silc_task_del_by_fd(SilcTaskQueue queue, SilcUInt32 fd);
89 /* Returns the task queue by task type */
90 #define SILC_SCHEDULE_GET_QUEUE(type) \
91 (type == SILC_TASK_FD ? schedule->fd_queue : \
92 type == SILC_TASK_TIMEOUT ? schedule->timeout_queue : \
93 schedule->generic_queue)
95 /* Locks. These also blocks signals that we care about and thus guarantee
96 that while we are in scheduler no signals can happen. This way we can
97 synchronise signals with SILC Scheduler. */
98 #define SILC_SCHEDULE_LOCK(schedule) \
100 silc_schedule_internal_signals_block(schedule->internal); \
101 silc_mutex_lock(schedule->lock); \
103 #define SILC_SCHEDULE_UNLOCK(schedule) \
105 silc_mutex_unlock(schedule->lock); \
106 silc_schedule_internal_signals_unblock(schedule->internal); \
109 /* SILC Task object. Represents one task in the scheduler. */
110 struct SilcTaskStruct {
112 struct timeval timeout;
113 SilcTaskCallback callback;
116 SilcTaskPriority priority;
119 /* Pointers forming doubly linked circular list */
120 struct SilcTaskStruct *next;
121 struct SilcTaskStruct *prev;
124 /* SILC Task Queue object. The queue holds all the tasks in the scheduler.
125 There are always three task queues in the scheduler. One for non-timeout
126 tasks (fd tasks performing tasks over specified file descriptor),
127 one for timeout tasks and one for generic tasks. */
128 struct SilcTaskQueueStruct {
129 SilcTask task; /* Pointer to all tasks */
130 struct timeval timeout; /* Current timeout */
131 SILC_MUTEX_DEFINE(lock); /* Queue's lock */
135 SILC Scheduler structure.
137 This is the actual schedule object in SILC. Both SILC client and server
138 uses this same scheduler. Actually, this scheduler could be used by any
139 program needing scheduling.
141 Following short description of the fields:
143 SilcTaskQueue fd_queue
145 Task queue hook for non-timeout tasks. Usually this means that these
146 tasks perform different kind of I/O on file descriptors. File
147 descriptors are usually network sockets but they actually can be
148 any file descriptors. This hook is initialized in silc_schedule_init
149 function. Timeout tasks should not be added to this queue because
150 they will never expire.
152 SilcTaskQueue timeout_queue
154 Task queue hook for timeout tasks. This hook is reserved specificly
155 for tasks with timeout. Non-timeout tasks should not be added to this
156 queue because they will never get scheduled. This hook is also
157 initialized in silc_schedule_init function.
159 SilcTaskQueue generic_queue
161 Task queue hook for generic tasks. This hook is reserved specificly
162 for generic tasks, tasks that apply to all file descriptors, except
163 to those that have specificly registered a non-timeout task. This hook
164 is also initialized in silc_schedule_init function.
166 SilcScheduleFd fd_list
168 List of file descriptors the scheduler is supposed to be listenning.
169 This is updated internally.
174 Size of the fd_list list. There can be `max_fd' many tasks in
175 the scheduler at once. The `last_fd' is the last valid entry
178 struct timeval *timeout;
180 Pointer to the schedules next timeout. Value of this timeout is
181 automatically updated in the silc_schedule function.
185 Marks validity of the scheduler. This is a boolean value. When this
186 is false the scheduler is terminated and the program will end. This
187 set to true when the scheduler is initialized with silc_schedule_init
193 File descriptor sets for select(). These are automatically managed
194 by the scheduler and should not be touched otherwise.
198 System specific scheduler context.
200 SILC_MUTEX_DEFINE(lock)
206 TRUE when tasks has been registered from signals. Next round in
207 scheduler will call the callbacks when this is TRUE.
210 struct SilcScheduleStruct {
211 SilcTaskQueue fd_queue;
212 SilcTaskQueue timeout_queue;
213 SilcTaskQueue generic_queue;
214 SilcScheduleFd fd_list;
217 struct timeval *timeout;
220 SILC_MUTEX_DEFINE(lock);
225 /* Initializes the scheduler. This returns the scheduler context that
226 is given as arugment usually to all silc_schedule_* functions.
227 The `max_tasks' indicates the number of maximum tasks that the
228 scheduler can handle. */
230 SilcSchedule silc_schedule_init(int max_tasks)
232 SilcSchedule schedule;
234 SILC_LOG_DEBUG(("Initializing scheduler"));
236 schedule = silc_calloc(1, sizeof(*schedule));
238 /* Allocate three task queues, one for file descriptor based tasks,
239 one for timeout tasks and one for generic tasks. */
240 silc_task_queue_alloc(&schedule->fd_queue);
241 silc_task_queue_alloc(&schedule->timeout_queue);
242 silc_task_queue_alloc(&schedule->generic_queue);
247 /* Initialize the scheduler */
248 schedule->fd_list = silc_calloc(max_tasks, sizeof(*schedule->fd_list));
249 schedule->max_fd = max_tasks;
250 schedule->timeout = NULL;
251 schedule->valid = TRUE;
253 /* Allocate scheduler lock */
254 silc_mutex_alloc(&schedule->lock);
256 /* Initialize the platform specific scheduler. */
257 schedule->internal = silc_schedule_internal_init(schedule);
262 /* Uninitializes the schedule. This is called when the program is ready
263 to end. This removes all tasks and task queues. Returns FALSE if the
264 scheduler could not be uninitialized. This happens when the scheduler
265 is still valid and silc_schedule_stop has not been called. */
267 bool silc_schedule_uninit(SilcSchedule schedule)
269 SILC_LOG_DEBUG(("Uninitializing scheduler"));
271 if (schedule->valid == TRUE)
274 /* Unregister all tasks */
275 silc_schedule_task_remove(schedule->fd_queue, SILC_ALL_TASKS);
276 silc_schedule_task_remove(schedule->timeout_queue, SILC_ALL_TASKS);
277 silc_schedule_task_remove(schedule->generic_queue, SILC_ALL_TASKS);
279 /* Unregister all task queues */
280 silc_task_queue_free(schedule->fd_queue);
281 silc_task_queue_free(schedule->timeout_queue);
282 silc_task_queue_free(schedule->generic_queue);
284 silc_free(schedule->fd_list);
286 /* Uninit the platform specific scheduler. */
287 silc_schedule_internal_uninit(schedule->internal);
289 silc_mutex_free(schedule->lock);
294 /* Enlarge the capabilities of the scheduler to handle tasks to `max_tasks'. */
296 bool silc_schedule_reinit(SilcSchedule schedule, int max_tasks)
298 SILC_SCHEDULE_LOCK(schedule);
299 if (schedule->max_fd <= max_tasks)
301 schedule->fd_list = silc_realloc(schedule->fd_list,
302 (sizeof(*schedule->fd_list) * max_tasks));
303 schedule->max_fd = max_tasks;
304 SILC_SCHEDULE_UNLOCK(schedule);
308 /* Stops the schedule even if it is not supposed to be stopped yet.
309 After calling this, one should call silc_schedule_uninit (after the
310 silc_schedule has returned). */
312 void silc_schedule_stop(SilcSchedule schedule)
314 SILC_LOG_DEBUG(("Stopping scheduler"));
315 SILC_SCHEDULE_LOCK(schedule);
316 schedule->valid = FALSE;
317 SILC_SCHEDULE_UNLOCK(schedule);
320 /* Executes nontimeout tasks. It then checks whether any of ther fd tasks
321 was signaled by the silc_select. If some task was not signaled then
322 all generic tasks are executed for that task. The generic tasks are
323 never executed for task that has explicit fd task set. */
324 /* This holds the schedule->lock and the queue locks. */
326 static void silc_schedule_dispatch_nontimeout(SilcSchedule schedule)
329 int i, last_fd = schedule->last_fd;
332 for (i = 0; i <= last_fd; i++) {
333 if (schedule->fd_list[i].events == 0)
336 fd = schedule->fd_list[i].fd;
338 /* First check whether this fd has task in the fd queue */
339 silc_mutex_lock(schedule->fd_queue->lock);
340 task = silc_task_find(schedule->fd_queue, fd);
342 /* If the task was found then execute its callbacks. If not then
343 execute all generic tasks for that fd. */
345 /* Validity of the task is checked always before and after
346 execution beacuse the task might have been unregistered
347 in the callback function, ie. it is not valid anymore. */
349 /* Is the task ready for reading */
350 if (task->valid && schedule->fd_list[i].revents & SILC_TASK_READ) {
351 silc_mutex_unlock(schedule->fd_queue->lock);
352 SILC_SCHEDULE_UNLOCK(schedule);
353 task->callback(schedule, SILC_TASK_READ, task->fd, task->context);
354 SILC_SCHEDULE_LOCK(schedule);
355 silc_mutex_lock(schedule->fd_queue->lock);
358 /* Is the task ready for writing */
359 if (task->valid && schedule->fd_list[i].revents & SILC_TASK_WRITE) {
360 silc_mutex_unlock(schedule->fd_queue->lock);
361 SILC_SCHEDULE_UNLOCK(schedule);
362 task->callback(schedule, SILC_TASK_WRITE, task->fd, task->context);
363 SILC_SCHEDULE_LOCK(schedule);
364 silc_mutex_lock(schedule->fd_queue->lock);
368 silc_schedule_task_remove(schedule->fd_queue, task);
370 silc_mutex_unlock(schedule->fd_queue->lock);
372 /* Run generic tasks for this fd. */
374 silc_mutex_unlock(schedule->fd_queue->lock);
376 silc_mutex_lock(schedule->generic_queue->lock);
377 if (!schedule->generic_queue->task) {
378 silc_mutex_unlock(schedule->generic_queue->lock);
382 task = schedule->generic_queue->task;
384 /* Validity of the task is checked always before and after
385 execution beacuse the task might have been unregistered
386 in the callback function, ie. it is not valid anymore. */
388 /* Is the task ready for reading */
389 if (task->valid && schedule->fd_list[i].revents & SILC_TASK_READ) {
390 silc_mutex_unlock(schedule->generic_queue->lock);
391 SILC_SCHEDULE_UNLOCK(schedule);
392 task->callback(schedule, SILC_TASK_READ, fd, task->context);
393 SILC_SCHEDULE_LOCK(schedule);
394 silc_mutex_lock(schedule->generic_queue->lock);
397 /* Is the task ready for writing */
398 if (task->valid && schedule->fd_list[i].revents & SILC_TASK_WRITE) {
399 silc_mutex_unlock(schedule->generic_queue->lock);
400 SILC_SCHEDULE_UNLOCK(schedule);
401 task->callback(schedule, SILC_TASK_WRITE, fd, task->context);
402 SILC_SCHEDULE_LOCK(schedule);
403 silc_mutex_lock(schedule->generic_queue->lock);
407 /* Invalid (unregistered) tasks are removed from the
409 if (schedule->generic_queue->task == task->next) {
410 silc_schedule_task_remove(schedule->generic_queue, task);
411 silc_mutex_unlock(schedule->generic_queue->lock);
416 silc_schedule_task_remove(schedule->generic_queue, task);
420 /* Break if there isn't more tasks in the queue */
421 if (schedule->generic_queue->task == task->next)
427 silc_mutex_unlock(schedule->generic_queue->lock);
432 /* Executes all tasks whose timeout has expired. The task is removed from
433 the task queue after the callback function has returned. Also, invalid
434 tasks are removed here. We don't have to care about priorities because
435 tasks are already sorted in their priority order at the registration
437 /* This holds the schedule->lock and the schedule->timeout_queue->lock */
439 static void silc_schedule_dispatch_timeout(SilcSchedule schedule)
441 SilcTaskQueue queue = schedule->timeout_queue;
443 struct timeval curtime;
445 SILC_LOG_DEBUG(("Running timeout tasks"));
447 silc_gettimeofday(&curtime);
449 queue = schedule->timeout_queue;
450 if (queue && queue->task) {
453 /* Walk thorugh all tasks in the particular task queue and run all
454 the expired tasks. */
456 /* Execute the task if the timeout has expired */
457 if (silc_schedule_task_timeout_compare(&task->timeout, &curtime)) {
459 silc_mutex_unlock(queue->lock);
460 SILC_SCHEDULE_UNLOCK(schedule);
461 task->callback(schedule, SILC_TASK_EXPIRE, task->fd, task->context);
462 SILC_SCHEDULE_LOCK(schedule);
463 silc_mutex_lock(queue->lock);
466 /* Break if there isn't more tasks in the queue */
467 if (queue->task == task->next) {
468 silc_schedule_task_remove(queue, task);
474 /* Remove the task from queue */
475 silc_schedule_task_remove(queue, task->prev);
477 /* The timeout hasn't expired, check for next one */
479 /* Break if there isn't more tasks in the queue */
480 if (queue->task == task->next)
489 /* Calculates next timeout for select(). This is the timeout value
490 when at earliest some of the timeout tasks expire. If this is in the
491 past, they will be run now. */
492 /* This holds the schedule->lock and the schedule->timeout_queue->lock */
494 static void silc_schedule_select_timeout(SilcSchedule schedule)
496 SilcTaskQueue queue = schedule->timeout_queue;
498 struct timeval curtime;
500 /* Get the current time */
501 silc_gettimeofday(&curtime);
502 schedule->timeout = NULL;
504 /* First task in the task queue has always the smallest timeout. */
507 if (task && task->valid == TRUE) {
508 /* If the timeout is in past, we will run the task and all other
509 timeout tasks from the past. */
510 if (silc_schedule_task_timeout_compare(&task->timeout, &curtime)) {
511 silc_schedule_dispatch_timeout(schedule);
513 /* The task(s) has expired and doesn't exist on the task queue
514 anymore. We continue with new timeout. */
515 queue = schedule->timeout_queue;
517 if (task == NULL || task->valid == FALSE)
521 /* Calculate the next timeout for select() */
522 queue->timeout.tv_sec = task->timeout.tv_sec - curtime.tv_sec;
523 queue->timeout.tv_usec = task->timeout.tv_usec - curtime.tv_usec;
524 if (queue->timeout.tv_sec < 0)
525 queue->timeout.tv_sec = 0;
527 /* We wouldn't want to go under zero, check for it. */
528 if (queue->timeout.tv_usec < 0) {
529 queue->timeout.tv_sec -= 1;
530 if (queue->timeout.tv_sec < 0)
531 queue->timeout.tv_sec = 0;
532 queue->timeout.tv_usec += 1000000L;
535 /* We've got the timeout value */
538 /* Task is not valid, remove it and try next one. */
539 silc_schedule_task_remove(queue, task);
541 if (queue->task == NULL)
546 /* Save the timeout */
548 schedule->timeout = &queue->timeout;
549 SILC_LOG_DEBUG(("timeout: sec=%d, usec=%d", schedule->timeout->tv_sec,
550 schedule->timeout->tv_usec));
554 /* Runs the scheduler once and then returns. */
556 bool silc_schedule_one(SilcSchedule schedule, int timeout_usecs)
558 struct timeval timeout;
561 SILC_LOG_DEBUG(("In scheduler loop"));
563 if (!schedule->is_locked)
564 SILC_SCHEDULE_LOCK(schedule);
566 /* If the task queues aren't initialized or we aren't valid anymore
568 if ((!schedule->fd_queue && !schedule->timeout_queue
569 && !schedule->generic_queue) || schedule->valid == FALSE) {
570 SILC_LOG_DEBUG(("Scheduler not valid anymore, exiting"));
571 if (!schedule->is_locked)
572 SILC_SCHEDULE_UNLOCK(schedule);
576 /* Calculate next timeout for silc_select(). This is the timeout value
577 when at earliest some of the timeout tasks expire. */
578 silc_mutex_lock(schedule->timeout_queue->lock);
579 silc_schedule_select_timeout(schedule);
580 silc_mutex_unlock(schedule->timeout_queue->lock);
582 if (timeout_usecs >= 0) {
584 timeout.tv_usec = timeout_usecs;
585 schedule->timeout = &timeout;
588 SILC_SCHEDULE_UNLOCK(schedule);
590 /* Deliver signals if any has been set to be called */
591 if (schedule->signal_tasks) {
592 silc_schedule_internal_signals_call(schedule->internal, schedule);
593 schedule->signal_tasks = FALSE;
596 /* This is the main select(). The program blocks here until some
597 of the selected file descriptors change status or the selected
599 SILC_LOG_DEBUG(("Select"));
600 ret = silc_select(schedule->fd_list, schedule->last_fd + 1,
603 SILC_SCHEDULE_LOCK(schedule);
610 SILC_LOG_ERROR(("Error in select(): %s", strerror(errno)));
614 silc_mutex_lock(schedule->timeout_queue->lock);
615 silc_schedule_dispatch_timeout(schedule);
616 silc_mutex_unlock(schedule->timeout_queue->lock);
619 /* There is some data available now */
620 SILC_LOG_DEBUG(("Running non-timeout tasks"));
621 silc_schedule_dispatch_nontimeout(schedule);
625 if (!schedule->is_locked)
626 SILC_SCHEDULE_UNLOCK(schedule);
631 /* The SILC scheduler. This is actually the main routine in SILC programs.
632 When this returns the program is to be ended. Before this function can
633 be called, one must call silc_schedule_init function. */
635 void silc_schedule(SilcSchedule schedule)
637 SILC_LOG_DEBUG(("Running scheduler"));
639 if (schedule->valid == FALSE) {
640 SILC_LOG_ERROR(("Scheduler is not valid, stopping"));
644 SILC_SCHEDULE_LOCK(schedule);
645 schedule->is_locked = TRUE;
647 /* Start the scheduler loop */
648 while (silc_schedule_one(schedule, -1))
651 SILC_SCHEDULE_UNLOCK(schedule);
654 /* Wakes up the scheduler. This is used only in multi-threaded
655 environments where threads may add new tasks or remove old tasks
656 from task queues. This is called to wake up the scheduler in the
657 main thread so that it detects the changes in the task queues.
658 If threads support is not compiled in this function has no effect.
659 Implementation of this function is platform specific. */
661 void silc_schedule_wakeup(SilcSchedule schedule)
664 SILC_LOG_DEBUG(("Wakeup scheduler"));
665 SILC_SCHEDULE_LOCK(schedule);
666 silc_schedule_internal_wakeup(schedule->internal);
667 SILC_SCHEDULE_UNLOCK(schedule);
671 /* Add new task to the scheduler */
673 SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
674 SilcTaskCallback callback, void *context,
675 long seconds, long useconds,
677 SilcTaskPriority priority)
683 SILC_LOG_DEBUG(("Registering new task, fd=%d type=%d priority=%d", fd,
686 queue = SILC_SCHEDULE_GET_QUEUE(type);
688 /* If the task is generic task, we check whether this task has already
689 been registered. Generic tasks are registered only once and after that
690 the same task applies to all file descriptors to be registered. */
691 if (type == SILC_TASK_GENERIC) {
692 silc_mutex_lock(queue->lock);
695 SilcTask task = queue->task;
697 if ((task->callback == callback) && (task->context == context)) {
698 SILC_LOG_DEBUG(("Found matching generic task, using the match"));
700 silc_mutex_unlock(queue->lock);
702 /* Add the fd to be listened, the task found now applies to this
704 silc_schedule_set_listen_fd(schedule, fd, SILC_TASK_READ);
708 if (queue->task == task->next)
715 silc_mutex_unlock(queue->lock);
718 newtask = silc_calloc(1, sizeof(*newtask));
720 newtask->context = context;
721 newtask->callback = callback;
722 newtask->valid = TRUE;
723 newtask->priority = priority;
724 newtask->type = type;
725 newtask->next = newtask;
726 newtask->prev = newtask;
728 /* Create timeout if marked to be timeout task */
729 if (((seconds + useconds) > 0) && (type == SILC_TASK_TIMEOUT)) {
730 silc_gettimeofday(&newtask->timeout);
731 newtask->timeout.tv_sec += seconds + (useconds / 1000000L);
732 newtask->timeout.tv_usec += (useconds % 1000000L);
733 if (newtask->timeout.tv_usec > 999999L) {
734 newtask->timeout.tv_sec += 1;
735 newtask->timeout.tv_usec -= 1000000L;
740 /* If the task is non-timeout task we have to tell the scheduler that we
741 would like to have these tasks scheduled at some odd distant future. */
742 if (type != SILC_TASK_TIMEOUT)
743 silc_schedule_set_listen_fd(schedule, fd, SILC_TASK_READ);
745 silc_mutex_lock(queue->lock);
747 /* Is this first task of the queue? */
748 if (queue->task == NULL) {
749 queue->task = newtask;
750 silc_mutex_unlock(queue->lock);
755 newtask = silc_task_add_timeout(queue, newtask, priority);
757 newtask = silc_task_add(queue, newtask, priority);
759 silc_mutex_unlock(queue->lock);
764 /* Removes a task from the scheduler */
766 void silc_schedule_task_del(SilcSchedule schedule, SilcTask task)
768 SilcTaskQueue queue = SILC_SCHEDULE_GET_QUEUE(task->type);
770 /* Unregister all tasks */
771 if (task == SILC_ALL_TASKS) {
773 SILC_LOG_DEBUG(("Unregistering all tasks at once"));
775 silc_mutex_lock(queue->lock);
778 silc_mutex_unlock(queue->lock);
787 if (queue->task == next->next)
792 silc_mutex_unlock(queue->lock);
796 SILC_LOG_DEBUG(("Unregistering task"));
798 silc_mutex_lock(queue->lock);
800 /* Unregister the specific task */
804 silc_mutex_unlock(queue->lock);
807 /* Remove task by fd */
809 void silc_schedule_task_del_by_fd(SilcSchedule schedule, SilcUInt32 fd)
811 SILC_LOG_DEBUG(("Unregister task by fd %d", fd));
813 silc_task_del_by_fd(schedule->timeout_queue, fd);
814 silc_task_del_by_fd(schedule->fd_queue, fd);
817 /* Remove task by task callback. */
819 void silc_schedule_task_del_by_callback(SilcSchedule schedule,
820 SilcTaskCallback callback)
822 SILC_LOG_DEBUG(("Unregister task by callback"));
824 silc_task_del_by_callback(schedule->timeout_queue, callback);
825 silc_task_del_by_callback(schedule->fd_queue, callback);
826 silc_task_del_by_callback(schedule->generic_queue, callback);
829 /* Remove task by context. */
831 void silc_schedule_task_del_by_context(SilcSchedule schedule, void *context)
833 SILC_LOG_DEBUG(("Unregister task by context"));
835 silc_task_del_by_context(schedule->timeout_queue, context);
836 silc_task_del_by_context(schedule->fd_queue, context);
837 silc_task_del_by_context(schedule->generic_queue, context);
840 /* Sets a file descriptor to be listened by select() in scheduler. One can
841 call this directly if wanted. This can be called multiple times for
842 one file descriptor to set different iomasks. */
844 void silc_schedule_set_listen_fd(SilcSchedule schedule,
845 SilcUInt32 fd, SilcTaskEvent iomask)
850 SILC_SCHEDULE_LOCK(schedule);
852 for (i = 0; i < schedule->max_fd; i++)
853 if (schedule->fd_list[i].fd == fd) {
854 schedule->fd_list[i].fd = fd;
855 schedule->fd_list[i].events = iomask;
856 if (i > schedule->last_fd)
857 schedule->last_fd = i;
863 for (i = 0; i < schedule->max_fd; i++)
864 if (schedule->fd_list[i].events == 0) {
865 schedule->fd_list[i].fd = fd;
866 schedule->fd_list[i].events = iomask;
867 if (i > schedule->last_fd)
868 schedule->last_fd = i;
872 SILC_SCHEDULE_UNLOCK(schedule);
875 /* Removes a file descriptor from listen list. */
877 void silc_schedule_unset_listen_fd(SilcSchedule schedule, SilcUInt32 fd)
881 SILC_SCHEDULE_LOCK(schedule);
883 SILC_LOG_DEBUG(("Unset listen fd %d", fd));
885 for (i = 0; i < schedule->max_fd; i++)
886 if (schedule->fd_list[i].fd == fd) {
887 schedule->fd_list[i].fd = 0;
888 schedule->fd_list[i].events = 0;
889 if (schedule->last_fd == i)
890 schedule->last_fd = schedule->max_fd - 1;
894 SILC_SCHEDULE_UNLOCK(schedule);
897 /* Register a new signal */
899 void silc_schedule_signal_register(SilcSchedule schedule, SilcUInt32 signal,
900 SilcTaskCallback callback, void *context)
902 silc_schedule_internal_signal_register(schedule->internal, signal,
906 /* Unregister a new signal */
908 void silc_schedule_signal_unregister(SilcSchedule schedule, SilcUInt32 signal,
909 SilcTaskCallback callback, void *context)
911 silc_schedule_internal_signal_unregister(schedule->internal, signal,
915 /* Call signal indicated by `signal'. */
917 void silc_schedule_signal_call(SilcSchedule schedule, SilcUInt32 signal)
919 /* Mark that signals needs to be delivered later. */
920 silc_schedule_internal_signal_call(schedule->internal, signal);
921 schedule->signal_tasks = TRUE;
924 /* Allocates a newtask task queue into the scheduler */
926 static void silc_task_queue_alloc(SilcTaskQueue *queue)
928 *queue = silc_calloc(1, sizeof(**queue));
929 silc_mutex_alloc(&(*queue)->lock);
932 /* Free's a task queue. */
934 static void silc_task_queue_free(SilcTaskQueue queue)
936 silc_mutex_free(queue->lock);
940 /* Return task by its fd. */
942 static SilcTask silc_task_find(SilcTaskQueue queue, SilcUInt32 fd)
954 if (queue->task == next->next)
962 /* Adds a non-timeout task into the task queue. This function is used
963 by silc_task_register function. Returns a pointer to the registered
966 static SilcTask silc_task_add(SilcTaskQueue queue, SilcTask newtask,
967 SilcTaskPriority priority)
969 SilcTask task, next, prev;
971 /* Take the first task in the queue */
975 case SILC_TASK_PRI_LOW:
976 /* Lowest priority. The task is added at the end of the list. */
978 newtask->prev = prev;
979 newtask->next = task;
980 prev->next = newtask;
981 task->prev = newtask;
983 case SILC_TASK_PRI_NORMAL:
984 /* Normal priority. The task is added before lower priority tasks
985 but after tasks with higher priority. */
987 while(prev != task) {
988 if (prev->priority > SILC_TASK_PRI_LOW)
993 /* There are only lower priorities in the list, we will
994 sit before them and become the first task in the queue. */
996 newtask->prev = prev;
997 newtask->next = task;
998 task->prev = newtask;
999 prev->next = newtask;
1001 /* We are now the first task in queue */
1002 queue->task = newtask;
1004 /* Found a spot from the list, add the task to the list. */
1006 newtask->prev = prev;
1007 newtask->next = next;
1008 prev->next = newtask;
1009 next->prev = newtask;
1020 /* Return the timeout task with smallest timeout. */
1022 static SilcTask silc_task_get_first(SilcTaskQueue queue, SilcTask first)
1024 SilcTask prev, task;
1036 if (silc_schedule_task_timeout_compare(&prev->timeout, &task->timeout))
1045 /* Adds a timeout task into the task queue. This function is used by
1046 silc_task_register function. Returns a pointer to the registered
1047 task. Timeout tasks are sorted by their timeout value in ascending
1048 order. The priority matters if there are more than one task with
1051 static SilcTask silc_task_add_timeout(SilcTaskQueue queue, SilcTask newtask,
1052 SilcTaskPriority priority)
1054 SilcTask task, prev, next;
1056 /* Take the first task in the queue */
1059 /* Take last task from the list */
1063 case SILC_TASK_PRI_LOW:
1064 /* Lowest priority. The task is added at the end of the list. */
1065 while(prev != task) {
1067 /* If we have longer timeout than with the task head of us
1068 we have found our spot. */
1069 if (silc_schedule_task_timeout_compare(&prev->timeout,
1073 /* If we are equal size of timeout we will be after it. */
1074 if (!silc_schedule_task_timeout_compare(&newtask->timeout,
1078 /* We have shorter timeout, compare to next one. */
1081 /* Found a spot from the list, add the task to the list. */
1083 newtask->prev = prev;
1084 newtask->next = next;
1085 prev->next = newtask;
1086 next->prev = newtask;
1089 /* Check if we are going to be the first task in the queue */
1090 if (silc_schedule_task_timeout_compare(&prev->timeout,
1093 if (!silc_schedule_task_timeout_compare(&newtask->timeout,
1097 /* We are now the first task in queue */
1098 queue->task = newtask;
1101 case SILC_TASK_PRI_NORMAL:
1102 /* Normal priority. The task is added before lower priority tasks
1103 but after tasks with higher priority. */
1104 while(prev != task) {
1106 /* If we have longer timeout than with the task head of us
1107 we have found our spot. */
1108 if (silc_schedule_task_timeout_compare(&prev->timeout,
1112 /* If we are equal size of timeout, priority kicks in place. */
1113 if (!silc_schedule_task_timeout_compare(&newtask->timeout,
1115 if (prev->priority >= SILC_TASK_PRI_NORMAL)
1118 /* We have shorter timeout or higher priority, compare to next one. */
1121 /* Found a spot from the list, add the task to the list. */
1123 newtask->prev = prev;
1124 newtask->next = next;
1125 prev->next = newtask;
1126 next->prev = newtask;
1129 /* Check if we are going to be the first task in the queue */
1130 if (silc_schedule_task_timeout_compare(&prev->timeout,
1133 if (!silc_schedule_task_timeout_compare(&newtask->timeout,
1135 if (prev->priority >= SILC_TASK_PRI_NORMAL)
1138 /* We are now the first task in queue */
1139 queue->task = newtask;
1150 /* Removes (unregisters) a task from particular task queue. This function
1151 is used internally by scheduler. This must be called holding the
1154 static int silc_schedule_task_remove(SilcTaskQueue queue, SilcTask task)
1156 SilcTask first, old, next;
1158 if (!queue || !task)
1165 first = queue->task;
1167 /* Unregister all tasks in queue */
1168 if (task == SILC_ALL_TASKS) {
1169 SILC_LOG_DEBUG(("Removing all tasks at once"));
1174 silc_free(next->prev);
1183 SILC_LOG_DEBUG(("Removing task"));
1185 /* Unregister the task */
1189 SilcTask prev, next;
1196 if (prev == old && next == old)
1198 if (queue->task == old)
1199 queue->task = silc_task_get_first(queue, next);
1212 /* Compare two time values. If the first argument is smaller than the
1213 second this function returns TRUE. */
1215 static int silc_schedule_task_timeout_compare(struct timeval *smaller,
1216 struct timeval *bigger)
1218 if ((smaller->tv_sec < bigger->tv_sec) ||
1219 ((smaller->tv_sec == bigger->tv_sec) &&
1220 (smaller->tv_usec < bigger->tv_usec)))
1226 static void silc_task_del_by_fd(SilcTaskQueue queue, SilcUInt32 fd)
1230 silc_mutex_lock(queue->lock);
1233 silc_mutex_unlock(queue->lock);
1241 next->valid = FALSE;
1242 if (queue->task == next->next)
1247 silc_mutex_unlock(queue->lock);
1250 static void silc_task_del_by_callback(SilcTaskQueue queue,
1251 SilcTaskCallback callback)
1255 silc_mutex_lock(queue->lock);
1258 silc_mutex_unlock(queue->lock);
1265 if (next->callback == callback)
1266 next->valid = FALSE;
1267 if (queue->task == next->next)
1272 silc_mutex_unlock(queue->lock);
1275 static void silc_task_del_by_context(SilcTaskQueue queue, void *context)
1279 silc_mutex_lock(queue->lock);
1282 silc_mutex_unlock(queue->lock);
1289 if (next->context == context)
1290 next->valid = FALSE;
1291 if (queue->task == next->next)
1296 silc_mutex_unlock(queue->lock);