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 */
21 #if defined(AFS_GLOBAL_SUNLOCK)
22 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
25 void afs_osi_Wakeup(char *event);
26 void afs_osi_Sleep(char *event);
28 static char waitV, dummyV;
30 #if ! defined(AFS_GLOBAL_SUNLOCK)
32 /* call procedure aproc with arock as an argument, in ams milliseconds */
33 static struct timer_list *afs_osi_CallProc(void *aproc, void *arock, int ams)
35 struct timer_list *timer = NULL;
37 timer = (struct timer_list*)osi_Alloc(sizeof(struct timer_list));
40 timer->expires = (ams*afs_hz)/1000 + 1;
41 timer->data = (unsigned long)arock;
42 timer->function = aproc;
48 /* cancel a timeout, whether or not it has already occurred */
49 static int afs_osi_CancelProc(struct timer_list *timer)
53 osi_Free(timer, sizeof(struct timer_list));
60 AFS_STATCNT(WaitHack);
66 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
68 AFS_STATCNT(osi_InitWaitHandle);
69 achandle->proc = (caddr_t) 0;
73 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
77 AFS_STATCNT(osi_CancelWait);
78 proc = achandle->proc;
79 if (proc == 0) return;
80 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
81 afs_osi_Wakeup(&waitV);
85 * Waits for data on ahandle, or ams ms later. ahandle may be null.
86 * Returns 0 if timeout and EINTR if signalled.
88 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
91 afs_int32 endTime, tid;
92 struct timer_list *timer = NULL;
94 AFS_STATCNT(osi_Wait);
95 endTime = osi_Time() + (ams/1000);
97 ahandle->proc = (caddr_t) current;
101 #if defined(AFS_GLOBAL_SUNLOCK)
102 code = osi_TimedSleep(&waitV, ams, 1);
108 timer = afs_osi_CallProc(AfsWaitHack, (char *) current, ams);
109 afs_osi_Sleep(&waitV);
110 afs_osi_CancelProc(timer);
111 #endif /* AFS_GLOBAL_SUNLOCK */
112 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
113 /* we've been signalled */
116 } while (osi_Time() < endTime);
123 typedef struct afs_event {
124 struct afs_event *next; /* next in hash chain */
125 char *event; /* lwp event: an address */
126 int refcount; /* Is it in use? */
127 int seq; /* Sequence number: this is incremented
128 by wakeup calls; wait will not return until
130 #if defined(AFS_LINUX24_ENV)
131 wait_queue_head_t cond;
133 struct wait_queue *cond;
138 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
139 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
140 int afs_evhashcnt = 0;
142 /* Get and initialize event structure corresponding to lwp event (i.e. address)
144 static afs_event_t *afs_getevent(char *event)
146 afs_event_t *evp, *newp = 0;
150 hashcode = afs_evhash(event);
151 evp = afs_evhasht[hashcode];
153 if (evp->event == event) {
157 if (evp->refcount == 0)
169 /* afs_addevent -- allocates a new event for the address. It isn't returned;
170 * instead, afs_getevent should be called again. Thus, the real effect of
171 * this routine is to add another event to the hash bucket for this
175 * Called with GLOCK held. However the function might drop
176 * GLOCK when it calls osi_AllocSmallSpace for allocating
177 * a new event (In Linux, the allocator drops GLOCK to avoid
181 static void afs_addevent(char *event)
187 hashcode = afs_evhash(event);
188 newp = osi_AllocSmallSpace(sizeof(afs_event_t));
190 newp->next = afs_evhasht[hashcode];
191 afs_evhasht[hashcode] = newp;
192 #if defined(AFS_LINUX24_ENV)
193 init_waitqueue_head(&newp->cond);
195 init_waitqueue(&newp->cond);
198 newp->event = &dummyV; /* Dummy address for new events */
203 /* Release the specified event */
204 #define relevent(evp) ((evp)->refcount--)
208 * Waits for an event to be notified, returning early if a signal
209 * is received. Returns EINTR if signaled, and 0 otherwise.
211 int afs_osi_SleepSig(char *event)
213 struct afs_event *evp;
216 evp = afs_getevent(event);
218 /* Can't block because allocating a new event would require dropping
219 * the GLOCK, which may cause us to miss the wakeup. So call the
220 * allocator then return immediately. We'll find the new event next
221 * time around without dropping the GLOCK. */
229 while (seq == evp->seq) {
232 interruptible_sleep_on(&evp->cond);
234 if (signal_pending(current)) {
243 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals. */
244 void afs_osi_Sleep(char *event)
248 spin_lock_irq(¤t->sigmask_lock);
249 saved_set = current->blocked;
250 sigfillset(¤t->blocked);
251 recalc_sigpending(current);
252 spin_unlock_irq(¤t->sigmask_lock);
254 afs_osi_SleepSig(event);
256 spin_lock_irq(¤t->sigmask_lock);
257 current->blocked = saved_set;
258 recalc_sigpending(current);
259 spin_unlock_irq(¤t->sigmask_lock);
265 * event - event to sleep on
266 * ams --- max sleep time in milliseconds
267 * aintok - 1 if should sleep interruptibly
269 * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
272 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
274 long t = ams * HZ / 1000;
275 struct afs_event *evp;
277 evp = afs_getevent(event);
279 /* Can't block because allocating a new event would require dropping
280 * the GLOCK, which may cause us to miss the wakeup. So call the
281 * allocator then return immediately. We'll find the new event next
282 * time around without dropping the GLOCK. */
289 t = interruptible_sleep_on_timeout(&evp->cond, t);
291 t = sleep_on_timeout(&evp->cond, t);
296 return t ? EINTR : 0;
300 void afs_osi_Wakeup(char *event)
302 struct afs_event *evp;
304 evp = afs_getevent(event);
305 if (!evp) /* No sleepers */
308 if (evp->refcount > 1) {