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 typedef struct afs_event {
71 struct afs_event *next; /* next in hash chain */
72 char *event; /* lwp event: an address */
73 int refcount; /* Is it in use? */
74 int seq; /* Sequence number: this is incremented
75 * by wakeup calls; wait will not return until
77 wait_queue_head_t cond;
81 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
82 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
83 int afs_evhashcnt = 0;
86 osi_event_shutdown(void)
90 for (i=0;i<HASHSIZE;i++) {
91 while (afs_evhasht[i] != NULL) {
92 afs_event_t *tmp = afs_evhasht[i];
93 afs_evhasht[i] = tmp->next;
99 /* Get and initialize event structure corresponding to lwp event (i.e. address)
102 afs_getevent(char *event)
104 afs_event_t *evp, *newp = 0;
108 hashcode = afs_evhash(event);
109 evp = afs_evhasht[hashcode];
111 if (evp->event == event) {
115 if (evp->refcount == 0)
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
133 * Called with GLOCK held.
137 afs_addevent(char *event)
143 hashcode = afs_evhash(event);
144 newp = kzalloc(sizeof(afs_event_t), GFP_NOFS);
146 newp->next = afs_evhasht[hashcode];
147 afs_evhasht[hashcode] = newp;
148 init_waitqueue_head(&newp->cond);
149 newp->event = &dummyV; /* Dummy address for new events */
152 #ifndef set_current_state
153 #define set_current_state(x) current->state = (x);
156 /* Release the specified event */
157 #define relevent(evp) ((evp)->refcount--)
161 * Waits for an event to be notified, returning early if a signal
162 * is received. Returns EINTR if signaled, and 0 otherwise.
165 afs_osi_SleepSig(void *event)
167 struct afs_event *evp;
171 evp = afs_getevent(event);
174 evp = afs_getevent(event);
181 code = wait_event_freezable(evp->cond, seq != evp->seq);
184 if (code == -ERESTARTSYS)
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
201 afs_osi_Sleep(void *event)
206 saved_set = current->blocked;
207 sigfillset(¤t->blocked);
208 RECALC_SIGPENDING(current);
211 afs_osi_SleepSig(event);
214 current->blocked = saved_set;
215 RECALC_SIGPENDING(current);
219 /* afs_osi_TimedSleep
222 * event - event to sleep on
223 * ams --- max sleep time in milliseconds
224 * aintok - 1 if should sleep interruptibly
226 * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
230 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
233 long ticks = (ams * HZ / 1000) + 1;
234 struct afs_event *evp;
237 evp = afs_getevent(event);
240 evp = afs_getevent(event);
246 code = wait_event_freezable_timeout(evp->cond, evp->seq != seq, ticks);
248 if (code == -ERESTARTSYS)
260 afs_osi_Wakeup(void *event)
263 struct afs_event *evp;
265 evp = afs_getevent(event);
266 if (!evp) /* No sleepers */
269 if (evp->refcount > 1) {