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 "afsincludes.h" /* Afs-based standard headers */
17 #include "afs/afs_stats.h" /* afs statistics */
19 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
23 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
25 AFS_STATCNT(osi_InitWaitHandle);
26 achandle->proc = (caddr_t) 0;
30 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
34 AFS_STATCNT(osi_CancelWait);
35 proc = achandle->proc;
36 if (proc == 0) return;
37 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
38 afs_osi_Wakeup(&waitV);
42 * Waits for data on ahandle, or ams ms later. ahandle may be null.
43 * Returns 0 if timeout and EINTR if signalled.
45 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
48 afs_int32 endTime, tid;
50 AFS_STATCNT(osi_Wait);
51 endTime = osi_Time() + (ams/1000);
53 ahandle->proc = (caddr_t) curthread;
57 code = osi_TimedSleep(&waitV, ams, aintok);
59 if (code) break; /* if something happened, quit now */
60 /* if we we're cancelled, quit now */
61 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
62 /* we've been signalled */
65 } while (osi_Time() < endTime);
72 typedef struct afs_event {
73 struct afs_event *next; /* next in hash chain */
74 char *event; /* lwp event: an address */
75 int refcount; /* Is it in use? */
76 int seq; /* Sequence number: this is incremented
77 by wakeup calls; wait will not return until
79 kcondvar_t cond; /* Currently associated condition variable */
83 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
84 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
85 int afs_evhashcnt = 0;
87 /* Get and initialize event structure corresponding to lwp event (i.e. address)
89 static afs_event_t *afs_getevent(char *event)
91 afs_event_t *evp, *newp = 0;
95 hashcode = afs_evhash(event);
96 evp = afs_evhasht[hashcode];
98 if (evp->event == event) {
102 if (evp->refcount == 0)
107 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
109 newp->next = afs_evhasht[hashcode];
110 afs_evhasht[hashcode] = newp;
111 cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
119 /* Release the specified event */
120 #define relevent(evp) ((evp)->refcount--)
123 void afs_osi_Sleep(void *event)
125 struct afs_event *evp;
128 evp = afs_getevent(event);
130 while (seq == evp->seq) {
132 cv_wait(&evp->cond, &afs_global_lock);
137 int afs_osi_SleepSig(void *event)
139 struct afs_event *evp;
142 evp = afs_getevent(event);
144 while (seq == evp->seq) {
146 if (cv_wait_sig(&evp->cond, &afs_global_lock) == 0) {
158 * event - event to sleep on
159 * ams --- max sleep time in milliseconds
160 * aintok - 1 if should sleep interruptibly
162 * Returns 0 if timeout and EINTR if signalled.
164 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
167 struct afs_event *evp;
170 ticks = ( ams * afs_hz )/1000;
171 ticks = ticks + lbolt;
173 evp = afs_getevent(event);
177 if (cv_timedwait_sig(&evp->cond, &afs_global_lock, ticks) == 0)
180 cv_timedwait(&evp->cond, &afs_global_lock, ticks);
188 int afs_osi_Wakeup(void *event)
191 struct afs_event *evp;
193 evp = afs_getevent(event);
194 if (evp->refcount > 1) {
196 cv_broadcast(&evp->cond);