DEVEL15-linux-warning-reduction-20090318
[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 #if defined(FREEZER_H_EXISTS)
21 #include <linux/freezer.h>
22 #endif
23
24 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
25
26 static char waitV, dummyV;
27
28 void
29 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
30 {
31     AFS_STATCNT(osi_InitWaitHandle);
32     achandle->proc = (caddr_t) 0;
33 }
34
35 /* cancel osi_Wait */
36 void
37 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
38 {
39     caddr_t proc;
40
41     AFS_STATCNT(osi_CancelWait);
42     proc = achandle->proc;
43     if (proc == 0)
44         return;
45     achandle->proc = (caddr_t) 0;       /* so dude can figure out he was signalled */
46     afs_osi_Wakeup(&waitV);
47 }
48
49 /* afs_osi_Wait
50  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
51  * Returns 0 if timeout and EINTR if signalled.
52  */
53 int
54 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
55 {
56     afs_int32 endTime;
57     int code;
58
59     AFS_STATCNT(osi_Wait);
60     endTime = osi_Time() + (ams / 1000);
61     if (ahandle)
62         ahandle->proc = (caddr_t) current;
63
64     do {
65         AFS_ASSERT_GLOCK();
66         code = osi_TimedSleep(&waitV, ams, 1);
67         if (code)
68             break;
69         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
70             /* we've been signalled */
71             break;
72         }
73     } while (osi_Time() < endTime);
74     return code;
75 }
76
77
78
79
80 typedef struct afs_event {
81     struct afs_event *next;     /* next in hash chain */
82     char *event;                /* lwp event: an address */
83     int refcount;               /* Is it in use? */
84     int seq;                    /* Sequence number: this is incremented
85                                  * by wakeup calls; wait will not return until
86                                  * it changes */
87 #if defined(AFS_LINUX24_ENV)
88     wait_queue_head_t cond;
89 #else
90     struct wait_queue *cond;
91 #endif
92 } afs_event_t;
93
94 #define HASHSIZE 128
95 afs_event_t *afs_evhasht[HASHSIZE];     /* Hash table for events */
96 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
97 int afs_evhashcnt = 0;
98
99 /* Get and initialize event structure corresponding to lwp event (i.e. address)
100  * */
101 static afs_event_t *
102 afs_getevent(char *event)
103 {
104     afs_event_t *evp, *newp = 0;
105     int hashcode;
106
107     AFS_ASSERT_GLOCK();
108     hashcode = afs_evhash(event);
109     evp = afs_evhasht[hashcode];
110     while (evp) {
111         if (evp->event == event) {
112             evp->refcount++;
113             return evp;
114         }
115         if (evp->refcount == 0)
116             newp = evp;
117         evp = evp->next;
118     }
119     if (!newp)
120         return NULL;
121
122     newp->event = event;
123     newp->refcount = 1;
124     return newp;
125 }
126
127 /* afs_addevent -- allocates a new event for the address.  It isn't returned;
128  *     instead, afs_getevent should be called again.  Thus, the real effect of
129  *     this routine is to add another event to the hash bucket for this
130  *     address.
131  *
132  * Locks:
133  *     Called with GLOCK held. However the function might drop
134  *     GLOCK when it calls osi_AllocSmallSpace for allocating
135  *     a new event (In Linux, the allocator drops GLOCK to avoid
136  *     a deadlock).
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 = osi_linux_alloc(sizeof(afs_event_t), 0);
148     afs_evhashcnt++;
149     newp->next = afs_evhasht[hashcode];
150     afs_evhasht[hashcode] = newp;
151 #if defined(AFS_LINUX24_ENV)
152     init_waitqueue_head(&newp->cond);
153 #else
154     init_waitqueue(&newp->cond);
155 #endif
156     newp->seq = 0;
157     newp->event = &dummyV;      /* Dummy address for new events */
158     newp->refcount = 0;
159 }
160
161 #ifndef set_current_state
162 #define set_current_state(x)            current->state = (x);
163 #endif
164
165 /* Release the specified event */
166 #define relevent(evp) ((evp)->refcount--)
167
168 /* afs_osi_SleepSig
169  *
170  * Waits for an event to be notified, returning early if a signal
171  * is received.  Returns EINTR if signaled, and 0 otherwise.
172  */
173 int
174 afs_osi_SleepSig(void *event)
175 {
176     struct afs_event *evp;
177     int seq, retval;
178 #ifdef DECLARE_WAITQUEUE
179     DECLARE_WAITQUEUE(wait, current);
180 #else
181     struct wait_queue wait = { current, NULL };
182 #endif
183
184     evp = afs_getevent(event);
185     if (!evp) {
186         afs_addevent(event);
187         evp = afs_getevent(event);
188     }
189
190     seq = evp->seq;
191     retval = 0;
192
193     add_wait_queue(&evp->cond, &wait);
194     while (seq == evp->seq) {
195         set_current_state(TASK_INTERRUPTIBLE);
196         AFS_ASSERT_GLOCK();
197         AFS_GUNLOCK();
198         schedule();
199 #ifdef AFS_LINUX26_ENV
200 #ifdef CONFIG_PM
201         if (
202 #ifdef PF_FREEZE
203             current->flags & PF_FREEZE
204 #else
205 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
206             !current->todo
207 #else
208 #if defined(STRUCT_TASK_STRUCT_HAS_THREAD_INFO)
209             test_ti_thread_flag(current->thread_info, TIF_FREEZE)
210 #else
211             test_ti_thread_flag(task_thread_info(current), TIF_FREEZE)
212 #endif
213 #endif
214 #endif
215             )
216 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
217             refrigerator(PF_FREEZE);
218 #else
219             refrigerator();
220 #endif
221 #endif
222 #endif
223         AFS_GLOCK();
224         if (signal_pending(current)) {
225             retval = EINTR;
226             break;
227         }
228     }
229     remove_wait_queue(&evp->cond, &wait);
230     set_current_state(TASK_RUNNING);
231
232     relevent(evp);
233     return retval;
234 }
235
236 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
237  * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
238  *   can wake up, even if all signals are blocked
239  * - TODO: handle signals correctly by passing an indication back to the
240  *   caller that the wait has been interrupted and the stack should be cleaned
241  *   up preparatory to signal delivery
242  */
243 void
244 afs_osi_Sleep(void *event)
245 {
246     sigset_t saved_set;
247
248     SIG_LOCK(current);
249     saved_set = current->blocked;
250     sigfillset(&current->blocked);
251     RECALC_SIGPENDING(current);
252     SIG_UNLOCK(current);
253
254     afs_osi_SleepSig(event);
255
256     SIG_LOCK(current);
257     current->blocked = saved_set;
258     RECALC_SIGPENDING(current);
259     SIG_UNLOCK(current);
260 }
261
262 /* osi_TimedSleep
263  * 
264  * Arguments:
265  * event - event to sleep on
266  * ams --- max sleep time in milliseconds
267  * aintok - 1 if should sleep interruptibly
268  *
269  * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
270  * have raced.
271  */
272 static int
273 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
274 {
275     int code = 0;
276     long ticks = (ams * HZ / 1000) + 1;
277     struct afs_event *evp;
278 #ifdef DECLARE_WAITQUEUE
279     DECLARE_WAITQUEUE(wait, current);
280 #else
281     struct wait_queue wait = { current, NULL };
282 #endif
283
284     evp = afs_getevent(event);
285     if (!evp) {
286         afs_addevent(event);
287         evp = afs_getevent(event);
288     }
289
290     add_wait_queue(&evp->cond, &wait);
291     set_current_state(TASK_INTERRUPTIBLE);
292     /* always sleep TASK_INTERRUPTIBLE to keep load average
293      * from artifically increasing. */
294     AFS_GUNLOCK();
295
296     if (aintok) {
297         if (schedule_timeout(ticks))
298             code = EINTR;
299     } else
300         schedule_timeout(ticks);
301 #ifdef AFS_LINUX26_ENV
302 #ifdef CONFIG_PM
303     if (
304 #ifdef PF_FREEZE
305             current->flags & PF_FREEZE
306 #else
307 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
308             !current->todo
309 #else
310 #if defined(STRUCT_TASK_STRUCT_HAS_THREAD_INFO)
311             test_ti_thread_flag(current->thread_info, TIF_FREEZE)
312 #else
313             test_ti_thread_flag(task_thread_info(current), TIF_FREEZE)
314 #endif
315 #endif
316 #endif
317             )
318 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
319         refrigerator(PF_FREEZE);
320 #else
321         refrigerator();
322 #endif
323 #endif
324 #endif
325
326     AFS_GLOCK();
327     remove_wait_queue(&evp->cond, &wait);
328     set_current_state(TASK_RUNNING);
329
330     relevent(evp);
331
332     return code;
333 }
334
335
336 int
337 afs_osi_Wakeup(void *event)
338 {
339     int ret = 2;
340     struct afs_event *evp;
341
342     evp = afs_getevent(event);
343     if (!evp)                   /* No sleepers */
344         return 1;
345
346     if (evp->refcount > 1) {
347         evp->seq++;
348         wake_up(&evp->cond);
349         ret = 0;
350     }
351     relevent(evp);
352     return ret;
353 }