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"
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h" /* afs statistics */
20 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
21 void afs_osi_Wakeup(char *event);
22 void afs_osi_Sleep(char *event);
24 static char waitV, dummyV;
27 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
29 AFS_STATCNT(osi_InitWaitHandle);
30 achandle->proc = (caddr_t) 0;
34 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
38 AFS_STATCNT(osi_CancelWait);
39 proc = achandle->proc;
40 if (proc == 0) return;
41 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
42 afs_osi_Wakeup(&waitV);
46 * Waits for data on ahandle, or ams ms later. ahandle may be null.
47 * Returns 0 if timeout and EINTR if signalled.
49 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
52 afs_int32 endTime, tid;
53 struct timer_list *timer = NULL;
55 AFS_STATCNT(osi_Wait);
56 endTime = osi_Time() + (ams/1000);
58 ahandle->proc = (caddr_t) current;
63 code = osi_TimedSleep(&waitV, ams, aintok);
66 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
67 /* we've been signalled */
70 } while (osi_Time() < endTime);
77 typedef struct afs_event {
78 struct afs_event *next; /* next in hash chain */
79 char *event; /* lwp event: an address */
80 int refcount; /* Is it in use? */
81 int seq; /* Sequence number: this is incremented
82 by wakeup calls; wait will not return until
84 #if defined(AFS_LINUX24_ENV)
85 wait_queue_head_t cond;
87 struct wait_queue *cond;
92 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
93 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
94 int afs_evhashcnt = 0;
96 /* Get and initialize event structure corresponding to lwp event (i.e. address)
98 static afs_event_t *afs_getevent(char *event)
100 afs_event_t *evp, *newp = 0;
104 hashcode = afs_evhash(event);
105 evp = afs_evhasht[hashcode];
107 if (evp->event == event) {
111 if (evp->refcount == 0)
123 /* afs_addevent -- allocates a new event for the address. It isn't returned;
124 * instead, afs_getevent should be called again. Thus, the real effect of
125 * this routine is to add another event to the hash bucket for this
129 * Called with GLOCK held. However the function might drop
130 * GLOCK when it calls osi_AllocSmallSpace for allocating
131 * a new event (In Linux, the allocator drops GLOCK to avoid
135 static void afs_addevent(char *event)
141 hashcode = afs_evhash(event);
142 newp = osi_AllocSmallSpace(sizeof(afs_event_t));
144 newp->next = afs_evhasht[hashcode];
145 afs_evhasht[hashcode] = newp;
146 #if defined(AFS_LINUX24_ENV)
147 init_waitqueue_head(&newp->cond);
149 init_waitqueue(&newp->cond);
152 newp->event = &dummyV; /* Dummy address for new events */
156 #ifndef set_current_state
157 #define set_current_state(x) current->state = (x);
160 /* Release the specified event */
161 #define relevent(evp) ((evp)->refcount--)
165 * Waits for an event to be notified, returning early if a signal
166 * is received. Returns EINTR if signaled, and 0 otherwise.
168 int afs_osi_SleepSig(char *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);
194 if (signal_pending(current)) {
199 remove_wait_queue(&evp->cond, &wait);
200 set_current_state(TASK_RUNNING);
206 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals. */
207 void afs_osi_Sleep(char *event)
211 spin_lock_irq(¤t->sigmask_lock);
212 saved_set = current->blocked;
213 sigfillset(¤t->blocked);
214 recalc_sigpending(current);
215 spin_unlock_irq(¤t->sigmask_lock);
217 afs_osi_SleepSig(event);
219 spin_lock_irq(¤t->sigmask_lock);
220 current->blocked = saved_set;
221 recalc_sigpending(current);
222 spin_unlock_irq(¤t->sigmask_lock);
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
235 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
238 long ticks = (ams * HZ / 1000) + 1;
239 struct afs_event *evp;
240 #ifdef DECLARE_WAITQUEUE
241 DECLARE_WAITQUEUE(wait, current);
243 struct wait_queue wait = { current, NULL };
246 evp = afs_getevent(event);
249 evp = afs_getevent(event);
252 add_wait_queue(&evp->cond, &wait);
253 set_current_state(TASK_INTERRUPTIBLE);
254 /* always sleep TASK_INTERRUPTIBLE to keep load average
255 from artifically increasing. */
259 if (schedule_timeout(ticks))
262 schedule_timeout(ticks);
265 remove_wait_queue(&evp->cond, &wait);
266 set_current_state(TASK_RUNNING);
274 void afs_osi_Wakeup(char *event)
276 struct afs_event *evp;
278 evp = afs_getevent(event);
279 if (!evp) /* No sleepers */
282 if (evp->refcount > 1) {