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;
88 /* Get and initialize event structure corresponding to lwp event (i.e. address)
91 afs_getevent(char *event)
93 afs_event_t *evp, *newp = 0;
97 hashcode = afs_evhash(event);
98 evp = afs_evhasht[hashcode];
100 if (evp->event == event) {
104 if (evp->refcount == 0)
116 /* afs_addevent -- allocates a new event for the address. It isn't returned;
117 * instead, afs_getevent should be called again. Thus, the real effect of
118 * this routine is to add another event to the hash bucket for this
122 * Called with GLOCK held. However the function might drop
123 * GLOCK when it calls osi_AllocSmallSpace for allocating
124 * a new event (In Linux, the allocator drops GLOCK to avoid
129 afs_addevent(char *event)
135 hashcode = afs_evhash(event);
136 newp = osi_linux_alloc(sizeof(afs_event_t), 0);
138 newp->next = afs_evhasht[hashcode];
139 afs_evhasht[hashcode] = newp;
140 init_waitqueue_head(&newp->cond);
142 newp->event = &dummyV; /* Dummy address for new events */
146 #ifndef set_current_state
147 #define set_current_state(x) current->state = (x);
150 /* Release the specified event */
151 #define relevent(evp) ((evp)->refcount--)
155 * Waits for an event to be notified, returning early if a signal
156 * is received. Returns EINTR if signaled, and 0 otherwise.
159 afs_osi_SleepSig(void *event)
161 struct afs_event *evp;
163 #ifdef DECLARE_WAITQUEUE
164 DECLARE_WAITQUEUE(wait, current);
166 struct wait_queue wait = { current, NULL };
169 evp = afs_getevent(event);
172 evp = afs_getevent(event);
178 add_wait_queue(&evp->cond, &wait);
179 while (seq == evp->seq) {
180 set_current_state(TASK_INTERRUPTIBLE);
187 if (signal_pending(current)) {
192 remove_wait_queue(&evp->cond, &wait);
193 set_current_state(TASK_RUNNING);
199 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
200 * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
201 * can wake up, even if all signals are blocked
202 * - TODO: handle signals correctly by passing an indication back to the
203 * caller that the wait has been interrupted and the stack should be cleaned
204 * up preparatory to signal delivery
207 afs_osi_Sleep(void *event)
212 saved_set = current->blocked;
213 sigfillset(¤t->blocked);
214 RECALC_SIGPENDING(current);
217 afs_osi_SleepSig(event);
220 current->blocked = saved_set;
221 RECALC_SIGPENDING(current);
225 /* afs_osi_TimedSleep
228 * event - event to sleep on
229 * ams --- max sleep time in milliseconds
230 * aintok - 1 if should sleep interruptibly
232 * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
236 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
239 long ticks = (ams * HZ / 1000) + 1;
240 struct afs_event *evp;
241 #ifdef DECLARE_WAITQUEUE
242 DECLARE_WAITQUEUE(wait, current);
244 struct wait_queue wait = { current, NULL };
247 evp = afs_getevent(event);
250 evp = afs_getevent(event);
253 add_wait_queue(&evp->cond, &wait);
254 set_current_state(TASK_INTERRUPTIBLE);
255 /* always sleep TASK_INTERRUPTIBLE to keep load average
256 * from artifically increasing. */
259 if (schedule_timeout(ticks)) {
267 remove_wait_queue(&evp->cond, &wait);
268 set_current_state(TASK_RUNNING);
277 afs_osi_Wakeup(void *event)
280 struct afs_event *evp;
282 evp = afs_getevent(event);
283 if (!evp) /* No sleepers */
286 if (evp->refcount > 1) {