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);
73 typedef struct afs_event {
74 struct afs_event *next; /* next in hash chain */
75 char *event; /* lwp event: an address */
76 int refcount; /* Is it in use? */
77 int seq; /* Sequence number: this is incremented
78 * by wakeup calls; wait will not return until
80 wait_queue_head_t cond;
84 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
85 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
86 int afs_evhashcnt = 0;
89 osi_event_shutdown(void)
93 for (i=0;i<HASHSIZE;i++) {
94 while (afs_evhasht[i] != NULL) {
95 afs_event_t *tmp = afs_evhasht[i];
96 afs_evhasht[i] = tmp->next;
102 /* Get and initialize event structure corresponding to lwp event (i.e. address)
105 afs_getevent(char *event)
107 afs_event_t *evp, *newp = 0;
111 hashcode = afs_evhash(event);
112 evp = afs_evhasht[hashcode];
114 if (evp->event == event) {
118 if (evp->refcount == 0)
130 /* afs_addevent -- allocates a new event for the address. It isn't returned;
131 * instead, afs_getevent should be called again. Thus, the real effect of
132 * this routine is to add another event to the hash bucket for this
136 * Called with GLOCK held.
140 afs_addevent(char *event)
146 hashcode = afs_evhash(event);
147 newp = kzalloc(sizeof(afs_event_t), GFP_NOFS);
149 newp->next = afs_evhasht[hashcode];
150 afs_evhasht[hashcode] = newp;
151 init_waitqueue_head(&newp->cond);
152 newp->event = &dummyV; /* Dummy address for new events */
155 #ifndef set_current_state
156 #define set_current_state(x) current->state = (x);
159 /* Release the specified event */
160 #define relevent(evp) ((evp)->refcount--)
164 * Waits for an event to be notified, returning early if a signal
165 * is received. Returns EINTR if signaled, and 0 otherwise.
168 afs_osi_SleepSig(void *event)
170 struct afs_event *evp;
172 #ifdef DECLARE_WAITQUEUE
173 DECLARE_WAITQUEUE(wait, current);
175 struct wait_queue wait = { current, NULL };
178 evp = afs_getevent(event);
181 evp = afs_getevent(event);
187 add_wait_queue(&evp->cond, &wait);
188 while (seq == evp->seq) {
189 set_current_state(TASK_INTERRUPTIBLE);
196 if (signal_pending(current)) {
201 remove_wait_queue(&evp->cond, &wait);
202 set_current_state(TASK_RUNNING);
208 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
209 * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
210 * can wake up, even if all signals are blocked
211 * - TODO: handle signals correctly by passing an indication back to the
212 * caller that the wait has been interrupted and the stack should be cleaned
213 * up preparatory to signal delivery
216 afs_osi_Sleep(void *event)
221 saved_set = current->blocked;
222 sigfillset(¤t->blocked);
223 RECALC_SIGPENDING(current);
226 afs_osi_SleepSig(event);
229 current->blocked = saved_set;
230 RECALC_SIGPENDING(current);
234 /* afs_osi_TimedSleep
237 * event - event to sleep on
238 * ams --- max sleep time in milliseconds
239 * aintok - 1 if should sleep interruptibly
241 * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
245 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
248 long ticks = (ams * HZ / 1000) + 1;
249 struct afs_event *evp;
250 #ifdef DECLARE_WAITQUEUE
251 DECLARE_WAITQUEUE(wait, current);
253 struct wait_queue wait = { current, NULL };
256 evp = afs_getevent(event);
259 evp = afs_getevent(event);
262 add_wait_queue(&evp->cond, &wait);
263 set_current_state(TASK_INTERRUPTIBLE);
264 /* always sleep TASK_INTERRUPTIBLE to keep load average
265 * from artifically increasing. */
268 if (schedule_timeout(ticks)) {
276 remove_wait_queue(&evp->cond, &wait);
277 set_current_state(TASK_RUNNING);
286 afs_osi_Wakeup(void *event)
289 struct afs_event *evp;
291 evp = afs_getevent(event);
292 if (!evp) /* No sleepers */
295 if (evp->refcount > 1) {