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