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 */
18 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
23 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
25 AFS_STATCNT(osi_InitWaitHandle);
26 achandle->proc = (caddr_t) 0;
31 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
35 AFS_STATCNT(osi_CancelWait);
36 proc = achandle->proc;
39 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
40 afs_osi_Wakeup(&waitV);
44 * Waits for data on ahandle, or ams ms later. ahandle may be null.
45 * Returns 0 if timeout and EINTR if signalled.
48 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
51 afs_int32 endTime, tid;
53 AFS_STATCNT(osi_Wait);
54 endTime = osi_Time() + (ams / 1000);
56 ahandle->proc = (caddr_t) curthread;
60 code = osi_TimedSleep(&waitV, ams, aintok);
63 break; /* if something happened, quit now */
64 /* if we we're cancelled, quit now */
65 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
66 /* we've been signalled */
69 } while (osi_Time() < endTime);
76 typedef struct afs_event {
77 struct afs_event *next; /* next in hash chain */
78 char *event; /* lwp event: an address */
79 int refcount; /* Is it in use? */
80 int seq; /* Sequence number: this is incremented
81 * by wakeup calls; wait will not return until
83 kcondvar_t cond; /* Currently associated condition variable */
87 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
88 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
89 int afs_evhashcnt = 0;
91 /* Get and initialize event structure corresponding to lwp event (i.e. address)
94 afs_getevent(char *event)
96 afs_event_t *evp, *newp = 0;
100 hashcode = afs_evhash(event);
101 evp = afs_evhasht[hashcode];
103 if (evp->event == event) {
107 if (evp->refcount == 0)
112 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
114 newp->next = afs_evhasht[hashcode];
115 afs_evhasht[hashcode] = newp;
116 cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
124 /* Release the specified event */
125 #define relevent(evp) ((evp)->refcount--)
129 afs_osi_Sleep(void *event)
131 struct afs_event *evp;
134 evp = afs_getevent(event);
136 while (seq == evp->seq) {
138 cv_wait(&evp->cond, &afs_global_lock);
144 afs_osi_SleepSig(void *event)
146 struct afs_event *evp;
149 evp = afs_getevent(event);
151 while (seq == evp->seq) {
153 if (cv_wait_sig(&evp->cond, &afs_global_lock) == 0) {
165 * event - event to sleep on
166 * ams --- max sleep time in milliseconds
167 * aintok - 1 if should sleep interruptibly
169 * Returns 0 if timeout and EINTR if signalled.
172 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
175 struct afs_event *evp;
178 ticks = (ams * afs_hz) / 1000;
179 ticks = ticks + lbolt;
181 evp = afs_getevent(event);
185 if (cv_timedwait_sig(&evp->cond, &afs_global_lock, ticks) == 0)
188 cv_timedwait(&evp->cond, &afs_global_lock, ticks);
197 afs_osi_Wakeup(void *event)
200 struct afs_event *evp;
202 evp = afs_getevent(event);
203 if (evp->refcount > 1) {
205 cv_broadcast(&evp->cond);