updates.
[crypto.git] / lib / silcutil / unix / silcunixschedule.c
1 /*
2
3   silcunixschedule.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1998 - 2001 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /* $Id$ */
21
22 #include "silcincludes.h"
23
24 /* Calls normal select() system call. */
25
26 int silc_select(int n, fd_set *readfds, fd_set *writefds,
27                 fd_set *exceptfds, struct timeval *timeout)
28 {
29   return select(n, readfds, writefds, exceptfds, timeout);
30 }
31
32 #ifdef SILC_THREADS
33
34 /* Internal wakeup context. */
35 typedef struct {
36   int wakeup_pipe[2];
37   SilcTask wakeup_task;
38 } *SilcUnixWakeup;
39
40 SILC_TASK_CALLBACK(silc_schedule_wakeup_cb)
41 {
42   SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
43   unsigned char c;
44
45   read(wakeup->wakeup_pipe[0], &c, 1);
46 }
47
48 #endif /* SILC_THREADS */
49
50 /* Initializes the wakeup of the scheduler. In multi-threaded environment
51    the scheduler needs to be wakenup when tasks are added or removed from
52    the task queues. This will initialize the wakeup for the scheduler.
53    Any tasks that needs to be registered must be registered to the `queue'.
54    It is quaranteed that the scheduler will automatically free any
55    registered tasks in this queue. This is system specific routine. */
56
57 void *silc_schedule_wakeup_init(void *queue)
58 {
59 #ifdef SILC_THREADS
60   SilcUnixWakeup wakeup;
61
62   wakeup = silc_calloc(1, sizeof(*wakeup));
63
64   if (pipe(wakeup->wakeup_pipe)) {
65     silc_free(wakeup);
66     return NULL;
67   }
68
69   wakeup->wakeup_task = silc_task_register(queue, wakeup->wakeup_pipe[0],
70                                            silc_schedule_wakeup_cb, wakeup,
71                                            0, 0, SILC_TASK_FD, 
72                                            SILC_TASK_PRI_NORMAL);
73   if (!wakeup->wakeup_task) {
74     close(wakeup->wakeup_pipe[0]);
75     close(wakeup->wakeup_pipe[1]);
76     silc_free(wakeup);
77     return NULL;
78   }
79
80   return (void *)wakeup;
81 #endif
82   return NULL;
83 }
84
85 /* Uninitializes the system specific wakeup. */
86
87 void silc_schedule_wakeup_uninit(void *context)
88 {
89 #ifdef SILC_THREADS
90   SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
91
92   if (!wakeup)
93     return;
94
95   close(wakeup->wakeup_pipe[0]);
96   close(wakeup->wakeup_pipe[1]);
97   silc_free(wakeup);
98 #endif
99 }
100
101 /* Wakes up the scheduler */
102
103 void silc_schedule_wakeup_internal(void *context)
104 {
105 #ifdef SILC_THREADS
106   SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
107
108   if (!wakeup)
109     return;
110
111   write(wakeup->wakeup_pipe[1], "!", 1);
112 #endif
113 }