provide afs_osi_TimedSleep
[openafs.git] / src / afs / LINUX24 / 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
18 #if defined(FREEZER_H_EXISTS)
19 #include <linux/freezer.h>
20 #endif
21
22 static char waitV, dummyV;
23
24 void
25 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
26 {
27     AFS_STATCNT(osi_InitWaitHandle);
28     achandle->proc = (caddr_t) 0;
29 }
30
31 /* cancel osi_Wait */
32 void
33 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
34 {
35     caddr_t proc;
36
37     AFS_STATCNT(osi_CancelWait);
38     proc = achandle->proc;
39     if (proc == 0)
40         return;
41     achandle->proc = (caddr_t) 0;       /* so dude can figure out he was signalled */
42     afs_osi_Wakeup(&waitV);
43 }
44
45 /* afs_osi_Wait
46  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
47  * Returns 0 if timeout and EINTR if signalled.
48  */
49 int
50 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
51 {
52     afs_int32 endTime;
53     int code;
54
55     AFS_STATCNT(osi_Wait);
56     endTime = osi_Time() + (ams / 1000);
57     if (ahandle)
58         ahandle->proc = (caddr_t) current;
59
60     do {
61         AFS_ASSERT_GLOCK();
62         code = afs_osi_TimedSleep(&waitV, ams, 1);
63         if (code)
64             break;
65         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
66             /* we've been signalled */
67             break;
68         }
69     } while (osi_Time() < endTime);
70     return code;
71 }
72
73
74
75
76 typedef struct afs_event {
77     struct afs_event *next;     /* next in hash chain */
78     char *event;                /* lwp event: an address */
79     int refcount;               /* Is it in use? */
80     int seq;                    /* Sequence number: this is incremented
81                                  * by wakeup calls; wait will not return until
82                                  * it changes */
83 #if defined(AFS_LINUX24_ENV)
84     wait_queue_head_t cond;
85 #else
86     struct wait_queue *cond;
87 #endif
88 } afs_event_t;
89
90 #define HASHSIZE 128
91 afs_event_t *afs_evhasht[HASHSIZE];     /* Hash table for events */
92 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
93 int afs_evhashcnt = 0;
94
95 /* Get and initialize event structure corresponding to lwp event (i.e. address)
96  * */
97 static afs_event_t *
98 afs_getevent(char *event)
99 {
100     afs_event_t *evp, *newp = 0;
101     int hashcode;
102
103     AFS_ASSERT_GLOCK();
104     hashcode = afs_evhash(event);
105     evp = afs_evhasht[hashcode];
106     while (evp) {
107         if (evp->event == event) {
108             evp->refcount++;
109             return evp;
110         }
111         if (evp->refcount == 0)
112             newp = evp;
113         evp = evp->next;
114     }
115     if (!newp)
116         return NULL;
117
118     newp->event = event;
119     newp->refcount = 1;
120     return newp;
121 }
122
123 /* afs_addevent -- allocates a new event for the address.  It isn't returned;
124  *     instead, afs_getevent should be called again.  Thus, the real effect of
125  *     this routine is to add another event to the hash bucket for this
126  *     address.
127  *
128  * Locks:
129  *     Called with GLOCK held. However the function might drop
130  *     GLOCK when it calls osi_AllocSmallSpace for allocating
131  *     a new event (In Linux, the allocator drops GLOCK to avoid
132  *     a deadlock).
133  */
134
135 static void
136 afs_addevent(char *event)
137 {
138     int hashcode;
139     afs_event_t *newp;
140
141     AFS_ASSERT_GLOCK();
142     hashcode = afs_evhash(event);
143     newp = osi_linux_alloc(sizeof(afs_event_t), 0);
144     afs_evhashcnt++;
145     newp->next = afs_evhasht[hashcode];
146     afs_evhasht[hashcode] = newp;
147 #if defined(AFS_LINUX24_ENV)
148     init_waitqueue_head(&newp->cond);
149 #else
150     init_waitqueue(&newp->cond);
151 #endif
152     newp->seq = 0;
153     newp->event = &dummyV;      /* Dummy address for new events */
154     newp->refcount = 0;
155 }
156
157 #ifndef set_current_state
158 #define set_current_state(x)            current->state = (x);
159 #endif
160
161 /* Release the specified event */
162 #define relevent(evp) ((evp)->refcount--)
163
164 /* afs_osi_SleepSig
165  *
166  * Waits for an event to be notified, returning early if a signal
167  * is received.  Returns EINTR if signaled, and 0 otherwise.
168  */
169 int
170 afs_osi_SleepSig(void *event)
171 {
172     struct afs_event *evp;
173     int seq, retval;
174 #ifdef DECLARE_WAITQUEUE
175     DECLARE_WAITQUEUE(wait, current);
176 #else
177     struct wait_queue wait = { current, NULL };
178 #endif
179
180     evp = afs_getevent(event);
181     if (!evp) {
182         afs_addevent(event);
183         evp = afs_getevent(event);
184     }
185
186     seq = evp->seq;
187     retval = 0;
188
189     add_wait_queue(&evp->cond, &wait);
190     while (seq == evp->seq) {
191         set_current_state(TASK_INTERRUPTIBLE);
192         AFS_ASSERT_GLOCK();
193         AFS_GUNLOCK();
194         schedule();
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 (aintok) {
269         if (schedule_timeout(ticks))
270             code = EINTR;
271     } else
272         schedule_timeout(ticks);
273
274     AFS_GLOCK();
275     remove_wait_queue(&evp->cond, &wait);
276     set_current_state(TASK_RUNNING);
277
278     relevent(evp);
279
280     return code;
281 }
282
283
284 int
285 afs_osi_Wakeup(void *event)
286 {
287     int ret = 2;
288     struct afs_event *evp;
289
290     evp = afs_getevent(event);
291     if (!evp)                   /* No sleepers */
292         return 1;
293
294     if (evp->refcount > 1) {
295         evp->seq++;
296         wake_up(&evp->cond);
297         ret = 0;
298     }
299     relevent(evp);
300     return ret;
301 }