3a9cda952fe6b5ec59103a3f60dee08f203547d1
[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 RCSID
14     ("$Header$");
15
16 #include "afs/sysincludes.h"    /* Standard vendor system headers */
17 #include "afsincludes.h"        /* Afs-based standard headers */
18 #include "afs/afs_stats.h"      /* afs statistics */
19
20 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
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 = 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 #ifdef AFS_LINUX26_ENV
196 #ifdef CONFIG_PM
197         if (
198 #ifdef PF_FREEZE
199             current->flags & PF_FREEZE
200 #else
201             !current->todo
202 #endif
203             )
204 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
205             refrigerator(PF_FREEZE);
206 #else
207             refrigerator();
208 #endif
209 #endif
210 #endif
211         AFS_GLOCK();
212         if (signal_pending(current)) {
213             retval = EINTR;
214             break;
215         }
216     }
217     remove_wait_queue(&evp->cond, &wait);
218     set_current_state(TASK_RUNNING);
219
220     relevent(evp);
221     return retval;
222 }
223
224 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
225  * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
226  *   can wake up, even if all signals are blocked
227  * - TODO: handle signals correctly by passing an indication back to the
228  *   caller that the wait has been interrupted and the stack should be cleaned
229  *   up preparatory to signal delivery
230  */
231 void
232 afs_osi_Sleep(void *event)
233 {
234     sigset_t saved_set;
235
236     SIG_LOCK(current);
237     saved_set = current->blocked;
238     sigfillset(&current->blocked);
239     RECALC_SIGPENDING(current);
240     SIG_UNLOCK(current);
241
242     afs_osi_SleepSig(event);
243
244     SIG_LOCK(current);
245     current->blocked = saved_set;
246     RECALC_SIGPENDING(current);
247     SIG_UNLOCK(current);
248 }
249
250 /* osi_TimedSleep
251  * 
252  * Arguments:
253  * event - event to sleep on
254  * ams --- max sleep time in milliseconds
255  * aintok - 1 if should sleep interruptibly
256  *
257  * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
258  * have raced.
259  */
260 static int
261 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
262 {
263     int code = 0;
264     long ticks = (ams * HZ / 1000) + 1;
265     struct afs_event *evp;
266 #ifdef DECLARE_WAITQUEUE
267     DECLARE_WAITQUEUE(wait, current);
268 #else
269     struct wait_queue wait = { current, NULL };
270 #endif
271
272     evp = afs_getevent(event);
273     if (!evp) {
274         afs_addevent(event);
275         evp = afs_getevent(event);
276     }
277
278     add_wait_queue(&evp->cond, &wait);
279     set_current_state(TASK_INTERRUPTIBLE);
280     /* always sleep TASK_INTERRUPTIBLE to keep load average
281      * from artifically increasing. */
282     AFS_GUNLOCK();
283
284     if (aintok) {
285         if (schedule_timeout(ticks))
286             code = EINTR;
287     } else
288         schedule_timeout(ticks);
289 #ifdef AFS_LINUX26_ENV
290 #ifdef CONFIG_PM
291     if (
292 #ifdef PF_FREEZE
293             current->flags & PF_FREEZE
294 #else
295             !current->todo
296 #endif
297             )
298 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
299         refrigerator(PF_FREEZE);
300 #else
301         refrigerator();
302 #endif
303 #endif
304 #endif
305
306     AFS_GLOCK();
307     remove_wait_queue(&evp->cond, &wait);
308     set_current_state(TASK_RUNNING);
309
310     relevent(evp);
311
312     return code;
313 }
314
315
316 int
317 afs_osi_Wakeup(void *event)
318 {
319     int ret = 2;
320     struct afs_event *evp;
321
322     evp = afs_getevent(event);
323     if (!evp)                   /* No sleepers */
324         return 1;
325
326     if (evp->refcount > 1) {
327         evp->seq++;
328         wake_up(&evp->cond);
329         ret = 0;
330     }
331     relevent(evp);
332     return ret;
333 }