Linux CM: Use kernel allocator directly for events
[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
71
72
73 typedef struct afs_event {
74     struct afs_event *next;     /* next in hash chain */
75     char *event;                /* lwp event: an address */
76     int refcount;               /* Is it in use? */
77     int seq;                    /* Sequence number: this is incremented
78                                  * by wakeup calls; wait will not return until
79                                  * it changes */
80     wait_queue_head_t cond;
81 } afs_event_t;
82
83 #define HASHSIZE 128
84 afs_event_t *afs_evhasht[HASHSIZE];     /* Hash table for events */
85 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
86 int afs_evhashcnt = 0;
87
88 void
89 osi_event_shutdown(void)
90 {
91    int i;
92
93    for (i=0;i<HASHSIZE;i++) {
94         while (afs_evhasht[i] != NULL) {
95             afs_event_t *tmp = afs_evhasht[i];
96             afs_evhasht[i] = tmp->next;
97             kfree(tmp);
98         }
99    }
100 }
101
102 /* Get and initialize event structure corresponding to lwp event (i.e. address)
103  * */
104 static afs_event_t *
105 afs_getevent(char *event)
106 {
107     afs_event_t *evp, *newp = 0;
108     int hashcode;
109
110     AFS_ASSERT_GLOCK();
111     hashcode = afs_evhash(event);
112     evp = afs_evhasht[hashcode];
113     while (evp) {
114         if (evp->event == event) {
115             evp->refcount++;
116             return evp;
117         }
118         if (evp->refcount == 0)
119             newp = evp;
120         evp = evp->next;
121     }
122     if (!newp)
123         return NULL;
124
125     newp->event = event;
126     newp->refcount = 1;
127     return newp;
128 }
129
130 /* afs_addevent -- allocates a new event for the address.  It isn't returned;
131  *     instead, afs_getevent should be called again.  Thus, the real effect of
132  *     this routine is to add another event to the hash bucket for this
133  *     address.
134  *
135  * Locks:
136  *     Called with GLOCK held.
137  */
138
139 static void
140 afs_addevent(char *event)
141 {
142     int hashcode;
143     afs_event_t *newp;
144
145     AFS_ASSERT_GLOCK();
146     hashcode = afs_evhash(event);
147     newp = kzalloc(sizeof(afs_event_t), GFP_NOFS);
148     afs_evhashcnt++;
149     newp->next = afs_evhasht[hashcode];
150     afs_evhasht[hashcode] = newp;
151     init_waitqueue_head(&newp->cond);
152     newp->event = &dummyV;      /* Dummy address for new events */
153 }
154
155 #ifndef set_current_state
156 #define set_current_state(x)            current->state = (x);
157 #endif
158
159 /* Release the specified event */
160 #define relevent(evp) ((evp)->refcount--)
161
162 /* afs_osi_SleepSig
163  *
164  * Waits for an event to be notified, returning early if a signal
165  * is received.  Returns EINTR if signaled, and 0 otherwise.
166  */
167 int
168 afs_osi_SleepSig(void *event)
169 {
170     struct afs_event *evp;
171     int seq, retval;
172 #ifdef DECLARE_WAITQUEUE
173     DECLARE_WAITQUEUE(wait, current);
174 #else
175     struct wait_queue wait = { current, NULL };
176 #endif
177
178     evp = afs_getevent(event);
179     if (!evp) {
180         afs_addevent(event);
181         evp = afs_getevent(event);
182     }
183
184     seq = evp->seq;
185     retval = 0;
186
187     add_wait_queue(&evp->cond, &wait);
188     while (seq == evp->seq) {
189         set_current_state(TASK_INTERRUPTIBLE);
190         AFS_ASSERT_GLOCK();
191         AFS_GUNLOCK();
192         schedule();
193         afs_try_to_freeze();
194
195         AFS_GLOCK();
196         if (signal_pending(current)) {
197             retval = EINTR;
198             break;
199         }
200     }
201     remove_wait_queue(&evp->cond, &wait);
202     set_current_state(TASK_RUNNING);
203
204     relevent(evp);
205     return retval;
206 }
207
208 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
209  * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
210  *   can wake up, even if all signals are blocked
211  * - TODO: handle signals correctly by passing an indication back to the
212  *   caller that the wait has been interrupted and the stack should be cleaned
213  *   up preparatory to signal delivery
214  */
215 void
216 afs_osi_Sleep(void *event)
217 {
218     sigset_t saved_set;
219
220     SIG_LOCK(current);
221     saved_set = current->blocked;
222     sigfillset(&current->blocked);
223     RECALC_SIGPENDING(current);
224     SIG_UNLOCK(current);
225
226     afs_osi_SleepSig(event);
227
228     SIG_LOCK(current);
229     current->blocked = saved_set;
230     RECALC_SIGPENDING(current);
231     SIG_UNLOCK(current);
232 }
233
234 /* afs_osi_TimedSleep
235  * 
236  * Arguments:
237  * event - event to sleep on
238  * ams --- max sleep time in milliseconds
239  * aintok - 1 if should sleep interruptibly
240  *
241  * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
242  * have raced.
243  */
244 int
245 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
246 {
247     int code = 0;
248     long ticks = (ams * HZ / 1000) + 1;
249     struct afs_event *evp;
250 #ifdef DECLARE_WAITQUEUE
251     DECLARE_WAITQUEUE(wait, current);
252 #else
253     struct wait_queue wait = { current, NULL };
254 #endif
255
256     evp = afs_getevent(event);
257     if (!evp) {
258         afs_addevent(event);
259         evp = afs_getevent(event);
260     }
261
262     add_wait_queue(&evp->cond, &wait);
263     set_current_state(TASK_INTERRUPTIBLE);
264     /* always sleep TASK_INTERRUPTIBLE to keep load average
265      * from artifically increasing. */
266     AFS_GUNLOCK();
267
268     if (schedule_timeout(ticks)) {
269         if (aintok)
270             code = EINTR;
271     }
272
273     afs_try_to_freeze();
274
275     AFS_GLOCK();
276     remove_wait_queue(&evp->cond, &wait);
277     set_current_state(TASK_RUNNING);
278
279     relevent(evp);
280
281     return code;
282 }
283
284
285 int
286 afs_osi_Wakeup(void *event)
287 {
288     int ret = 2;
289     struct afs_event *evp;
290
291     evp = afs_getevent(event);
292     if (!evp)                   /* No sleepers */
293         return 1;
294
295     if (evp->refcount > 1) {
296         evp->seq++;
297         wake_up(&evp->cond);
298         ret = 0;
299     }
300     relevent(evp);
301     return ret;
302 }