2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include "afs/param.h"
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"
19 static char waitV, dummyV;
22 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
24 AFS_STATCNT(osi_InitWaitHandle);
25 achandle->proc = (caddr_t) 0;
30 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
34 AFS_STATCNT(osi_CancelWait);
35 proc = achandle->proc;
38 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
39 afs_osi_Wakeup(&waitV);
43 * Waits for data on ahandle, or ams ms later. ahandle may be null.
44 * Returns 0 if timeout and EINTR if signalled.
47 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
52 AFS_STATCNT(osi_Wait);
53 endTime = osi_Time() + (ams / 1000);
55 ahandle->proc = (caddr_t) current;
59 code = afs_osi_TimedSleep(&waitV, ams, 1);
62 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
63 /* we've been signalled */
66 } while (osi_Time() < endTime);
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;
74 /* Get and initialize event structure corresponding to lwp event (i.e. address)
77 afs_getevent(char *event)
79 afs_event_t *evp, *newp = 0;
83 hashcode = afs_evhash(event);
84 evp = afs_evhasht[hashcode];
86 if (evp->event == event) {
90 if (evp->refcount == 0)
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
108 * Called with GLOCK held.
112 afs_addevent(char *event)
118 hashcode = afs_evhash(event);
119 newp = kzalloc(sizeof(afs_event_t), GFP_NOFS);
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 */
127 #ifndef set_current_state
128 #define set_current_state(x) current->state = (x)
131 /* Release the specified event */
132 #define relevent(evp) ((evp)->refcount--)
136 * Waits for an event to be notified, returning early if a signal
137 * is received. Returns EINTR if signaled, and 0 otherwise.
140 afs_osi_SleepSig(void *event)
142 struct afs_event *evp;
146 evp = afs_getevent(event);
149 evp = afs_getevent(event);
156 code = wait_event_freezable(evp->cond, seq != evp->seq);
159 if (code == -ERESTARTSYS)
168 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
169 * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
170 * can wake up, even if all signals are blocked
171 * - TODO: handle signals correctly by passing an indication back to the
172 * caller that the wait has been interrupted and the stack should be cleaned
173 * up preparatory to signal delivery
176 afs_osi_Sleep(void *event)
181 saved_set = current->blocked;
182 sigfillset(¤t->blocked);
183 RECALC_SIGPENDING(current);
186 afs_osi_SleepSig(event);
189 current->blocked = saved_set;
190 RECALC_SIGPENDING(current);
194 /* afs_osi_TimedSleep
197 * event - event to sleep on
198 * ams --- max sleep time in milliseconds
199 * aintok - 1 if should sleep interruptibly
201 * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
205 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
208 long ticks = (ams * HZ / 1000) + 1;
209 struct afs_event *evp;
212 evp = afs_getevent(event);
215 evp = afs_getevent(event);
221 code = wait_event_freezable_timeout(evp->cond, evp->seq != seq, ticks);
223 if (code == -ERESTARTSYS)
235 afs_osi_Wakeup(void *event)
238 struct afs_event *evp;
240 evp = afs_getevent(event);
241 if (!evp) /* No sleepers */
244 if (evp->refcount > 1) {