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