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