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);
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;
102 #if defined(AFS_GLOBAL_SUNLOCK)
103 code = osi_TimedSleep(&waitV, ams, 1);
106 flush_signals(current);
110 timer = afs_osi_CallProc(AfsWaitHack, (char *) current, ams);
111 afs_osi_Sleep(&waitV);
112 afs_osi_CancelProc(timer);
113 #endif /* AFS_GLOBAL_SUNLOCK */
114 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
115 /* we've been signalled */
118 } while (osi_Time() < endTime);
125 typedef struct afs_event {
126 struct afs_event *next; /* next in hash chain */
127 char *event; /* lwp event: an address */
128 int refcount; /* Is it in use? */
129 int seq; /* Sequence number: this is incremented
130 by wakeup calls; wait will not return until
132 #if defined(AFS_LINUX24_ENV)
133 wait_queue_head_t cond;
135 struct wait_queue *cond;
140 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
141 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
142 int afs_evhashcnt = 0;
144 /* Get and initialize event structure corresponding to lwp event (i.e. address)
146 static afs_event_t *afs_getevent(char *event)
148 afs_event_t *evp, *newp = 0;
152 hashcode = afs_evhash(event);
153 evp = afs_evhasht[hashcode];
155 if (evp->event == event) {
159 if (evp->refcount == 0)
164 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
166 newp->next = afs_evhasht[hashcode];
167 afs_evhasht[hashcode] = newp;
168 #if defined(AFS_LINUX24_ENV)
169 init_waitqueue_head(&newp->cond);
171 init_waitqueue(&newp->cond);
180 /* Release the specified event */
181 #define relevent(evp) ((evp)->refcount--)
184 void afs_osi_Sleep(char *event)
186 struct afs_event *evp;
189 evp = afs_getevent(event);
191 while (seq == evp->seq) {
194 interruptible_sleep_on(&evp->cond);
203 * event - event to sleep on
204 * ams --- max sleep time in milliseconds
205 * aintok - 1 if should sleep interruptibly
207 * Returns 0 if timeout and EINTR if signalled.
209 * While the Linux kernel still has a global lock, we can use the standard
210 * sleep calls and drop our locks early. The kernel lock will protect us
211 * until we get to sleep.
213 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
215 long t = ams * HZ / 1000;
216 struct afs_event *evp;
218 evp = afs_getevent(event);
222 t = interruptible_sleep_on_timeout(&evp->cond, t);
224 t = sleep_on_timeout(&evp->cond, t);
227 return t ? EINTR : 0;
231 void afs_osi_Wakeup(char *event)
233 struct afs_event *evp;
235 evp = afs_getevent(event);
236 if (evp->refcount > 1) {