rx/rxkad: Move rxkad initialisation into rxkad
[openafs.git] / src / afs / LINUX / osi_sleep.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afsincludes.h"        /* Afs-based standard headers */
16 #include "afs/afs_stats.h"      /* afs statistics */
17 #include "osi_compat.h"
18
19 static char waitV, dummyV;
20
21 void
22 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
23 {
24     AFS_STATCNT(osi_InitWaitHandle);
25     achandle->proc = (caddr_t) 0;
26 }
27
28 /* cancel osi_Wait */
29 void
30 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
31 {
32     caddr_t proc;
33
34     AFS_STATCNT(osi_CancelWait);
35     proc = achandle->proc;
36     if (proc == 0)
37         return;
38     achandle->proc = (caddr_t) 0;       /* so dude can figure out he was signalled */
39     afs_osi_Wakeup(&waitV);
40 }
41
42 /* afs_osi_Wait
43  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
44  * Returns 0 if timeout and EINTR if signalled.
45  */
46 int
47 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
48 {
49     afs_int32 endTime;
50     int code;
51
52     AFS_STATCNT(osi_Wait);
53     endTime = osi_Time() + (ams / 1000);
54     if (ahandle)
55         ahandle->proc = (caddr_t) current;
56
57     do {
58         AFS_ASSERT_GLOCK();
59         code = afs_osi_TimedSleep(&waitV, ams, 1);
60         if (code)
61             break;
62         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
63             /* we've been signalled */
64             break;
65         }
66     } while (osi_Time() < endTime);
67     return code;
68 }
69
70 typedef struct afs_event {
71     struct afs_event *next;     /* next in hash chain */
72     char *event;                /* lwp event: an address */
73     int refcount;               /* Is it in use? */
74     int seq;                    /* Sequence number: this is incremented
75                                  * by wakeup calls; wait will not return until
76                                  * it changes */
77     wait_queue_head_t cond;
78 } afs_event_t;
79
80 #define HASHSIZE 128
81 afs_event_t *afs_evhasht[HASHSIZE];     /* Hash table for events */
82 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
83 int afs_evhashcnt = 0;
84
85 void
86 osi_event_shutdown(void)
87 {
88    int i;
89
90    for (i=0;i<HASHSIZE;i++) {
91         while (afs_evhasht[i] != NULL) {
92             afs_event_t *tmp = afs_evhasht[i];
93             afs_evhasht[i] = tmp->next;
94             kfree(tmp);
95         }
96    }
97 }
98
99 /* Get and initialize event structure corresponding to lwp event (i.e. address)
100  * */
101 static afs_event_t *
102 afs_getevent(char *event)
103 {
104     afs_event_t *evp, *newp = 0;
105     int hashcode;
106
107     AFS_ASSERT_GLOCK();
108     hashcode = afs_evhash(event);
109     evp = afs_evhasht[hashcode];
110     while (evp) {
111         if (evp->event == event) {
112             evp->refcount++;
113             return evp;
114         }
115         if (evp->refcount == 0)
116             newp = evp;
117         evp = evp->next;
118     }
119     if (!newp)
120         return NULL;
121
122     newp->event = event;
123     newp->refcount = 1;
124     return newp;
125 }
126
127 /* afs_addevent -- allocates a new event for the address.  It isn't returned;
128  *     instead, afs_getevent should be called again.  Thus, the real effect of
129  *     this routine is to add another event to the hash bucket for this
130  *     address.
131  *
132  * Locks:
133  *     Called with GLOCK held.
134  */
135
136 static void
137 afs_addevent(char *event)
138 {
139     int hashcode;
140     afs_event_t *newp;
141
142     AFS_ASSERT_GLOCK();
143     hashcode = afs_evhash(event);
144     newp = kzalloc(sizeof(afs_event_t), GFP_NOFS);
145     afs_evhashcnt++;
146     newp->next = afs_evhasht[hashcode];
147     afs_evhasht[hashcode] = newp;
148     init_waitqueue_head(&newp->cond);
149     newp->event = &dummyV;      /* Dummy address for new events */
150 }
151
152 #ifndef set_current_state
153 #define set_current_state(x)            current->state = (x);
154 #endif
155
156 /* Release the specified event */
157 #define relevent(evp) ((evp)->refcount--)
158
159 /* afs_osi_SleepSig
160  *
161  * Waits for an event to be notified, returning early if a signal
162  * is received.  Returns EINTR if signaled, and 0 otherwise.
163  */
164 int
165 afs_osi_SleepSig(void *event)
166 {
167     struct afs_event *evp;
168     int seq, retval;
169     int code;
170
171     evp = afs_getevent(event);
172     if (!evp) {
173         afs_addevent(event);
174         evp = afs_getevent(event);
175     }
176
177     seq = evp->seq;
178     retval = 0;
179
180     AFS_GUNLOCK();
181     code = wait_event_freezable(evp->cond, seq != evp->seq);
182     AFS_GLOCK();
183
184     if (code == -ERESTARTSYS)
185         code = EINTR;
186     else
187         code = -code;
188
189     relevent(evp);
190     return code;
191 }
192
193 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
194  * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
195  *   can wake up, even if all signals are blocked
196  * - TODO: handle signals correctly by passing an indication back to the
197  *   caller that the wait has been interrupted and the stack should be cleaned
198  *   up preparatory to signal delivery
199  */
200 void
201 afs_osi_Sleep(void *event)
202 {
203     sigset_t saved_set;
204
205     SIG_LOCK(current);
206     saved_set = current->blocked;
207     sigfillset(&current->blocked);
208     RECALC_SIGPENDING(current);
209     SIG_UNLOCK(current);
210
211     afs_osi_SleepSig(event);
212
213     SIG_LOCK(current);
214     current->blocked = saved_set;
215     RECALC_SIGPENDING(current);
216     SIG_UNLOCK(current);
217 }
218
219 /* afs_osi_TimedSleep
220  * 
221  * Arguments:
222  * event - event to sleep on
223  * ams --- max sleep time in milliseconds
224  * aintok - 1 if should sleep interruptibly
225  *
226  * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
227  * have raced.
228  */
229 int
230 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
231 {
232     int code = 0;
233     long ticks = (ams * HZ / 1000) + 1;
234     struct afs_event *evp;
235     int seq;
236
237     evp = afs_getevent(event);
238     if (!evp) {
239         afs_addevent(event);
240         evp = afs_getevent(event);
241     }
242
243     seq = evp->seq;
244
245     AFS_GUNLOCK();
246     code = wait_event_freezable_timeout(evp->cond, evp->seq != seq, ticks);
247     AFS_GLOCK();
248     if (code == -ERESTARTSYS)
249         code = EINTR;
250     else
251         code = -code;
252
253     relevent(evp);
254
255     return code;
256 }
257
258
259 int
260 afs_osi_Wakeup(void *event)
261 {
262     int ret = 2;
263     struct afs_event *evp;
264
265     evp = afs_getevent(event);
266     if (!evp)                   /* No sleepers */
267         return 1;
268
269     if (evp->refcount > 1) {
270         evp->seq++;
271         wake_up(&evp->cond);
272         ret = 0;
273     }
274     relevent(evp);
275     return ret;
276 }