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
11 #include <afsconfig.h>
12 #include "afs/param.h"
15 #include "afs/sysincludes.h" /* Standard vendor system headers */
16 #include "afsincludes.h" /* Afs-based standard headers */
17 #include "afs/afs_stats.h" /* afs statistics */
19 #ifndef AFS_FBSD50_ENV
20 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
26 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
28 AFS_STATCNT(osi_InitWaitHandle);
30 cv_init(&achandle->wh_condvar, "afscondvar");
31 achandle->wh_inited = 1;
33 achandle->proc = NULL;
39 * I can't tell -- is this supposed to be cv_signal() or cv_waitq_remove()?
40 * Or perhaps cv_broadcast()?
41 * Assuming cv_signal() is the desired meaning. -GAW
44 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
46 #ifndef AFS_FBSD50_ENV
50 AFS_STATCNT(osi_CancelWait);
53 /* XXX should not be necessary */
54 if (!achandle->wh_inited)
57 cv_signal(&achandle->wh_condvar);
59 proc = achandle->proc;
62 achandle->proc = NULL; /* so dude can figure out he was signalled */
63 afs_osi_Wakeup(&waitV);
68 * Waits for data on ahandle, or ams ms later. ahandle may be null.
69 * Returns 0 if timeout and EINTR if signalled.
72 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
82 AFS_STATCNT(osi_Wait);
84 tv.tv_sec = ams / 1000;
85 tv.tv_usec = (ams % 1000) * 1000;
89 if (ahandle == NULL) {
90 /* This is nasty and evil and rude. */
91 code = msleep(&tv, &afs_global_mtx, (aintok ? PPAUSE|PCATCH : PVFS),
94 if (!ahandle->wh_inited)
95 afs_osi_InitWaitHandle(ahandle); /* XXX should not be needed */
98 code = cv_timedwait_sig(&ahandle->wh_condvar, &afs_global_mtx,
101 code = cv_timedwait(&ahandle->wh_condvar, &afs_global_mtx, ticks);
104 endTime = osi_Time() + (ams / 1000);
106 ahandle->proc = (caddr_t) curproc;
109 code = osi_TimedSleep(&waitV, ams, aintok);
111 break; /* if something happened, quit now */
112 /* if we we're cancelled, quit now */
113 if (ahandle && (ahandle->proc == NULL)) {
114 /* we've been signalled */
117 } while (osi_Time() < endTime);
123 * All this gluck should probably also be replaced with CVs.
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
136 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
137 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
138 int afs_evhashcnt = 0;
140 /* Get and initialize event structure corresponding to lwp event (i.e. address)
143 afs_getevent(char *event)
145 afs_event_t *evp, *newp = 0;
149 hashcode = afs_evhash(event);
150 evp = afs_evhasht[hashcode];
152 if (evp->event == event) {
156 if (evp->refcount == 0)
161 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
163 newp->next = afs_evhasht[hashcode];
164 afs_evhasht[hashcode] = newp;
172 /* Release the specified event */
173 #define relevent(evp) ((evp)->refcount--)
177 afs_osi_Sleep(void *event)
179 struct afs_event *evp;
182 evp = afs_getevent(event);
184 while (seq == evp->seq) {
186 #ifdef AFS_FBSD50_ENV
187 msleep(event, &afs_global_mtx, PVFS, "afsslp", 0);
190 tsleep(event, PVFS, "afs_osi_Sleep", 0);
198 afs_osi_SleepSig(void *event)
200 afs_osi_Sleep(event);
204 #ifndef AFS_FBSD50_ENV
208 * event - event to sleep on
209 * ams --- max sleep time in milliseconds
210 * aintok - 1 if should sleep interruptibly
212 * Returns 0 if timeout and EINTR if signalled.
215 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
218 struct afs_event *evp;
222 ticks = (ams * afs_hz) / 1000;
225 evp = afs_getevent(event);
229 prio = PCATCH | PPAUSE;
232 code = tsleep(event, prio, "afs_osi_TimedSleep", ticks);
239 #endif /* not AFS_FBSD50_ENV */
242 afs_osi_Wakeup(void *event)
245 struct afs_event *evp;
247 evp = afs_getevent(event);
248 if (evp->refcount > 1) {