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