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"
16 #include "afs/sysincludes.h" /* Standard vendor system headers */
17 #include "afsincludes.h" /* Afs-based standard headers */
18 #include "afs/afs_stats.h" /* afs statistics */
20 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
25 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
27 AFS_STATCNT(osi_InitWaitHandle);
28 achandle->proc = (caddr_t) 0;
33 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
37 AFS_STATCNT(osi_CancelWait);
38 proc = achandle->proc;
41 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
42 afs_osi_Wakeup(&waitV);
46 * Waits for data on ahandle, or ams ms later. ahandle may be null.
47 * Returns 0 if timeout and EINTR if signalled.
50 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
53 afs_int32 endTime, tid;
55 AFS_STATCNT(osi_Wait);
56 endTime = osi_Time() + (ams / 1000);
58 ahandle->proc = (caddr_t) curthread;
62 code = osi_TimedSleep(&waitV, ams, aintok);
65 break; /* if something happened, quit now */
66 /* if we we're cancelled, quit now */
67 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
68 /* we've been signalled */
71 } while (osi_Time() < endTime);
78 typedef struct afs_event {
79 struct afs_event *next; /* next in hash chain */
80 char *event; /* lwp event: an address */
81 int refcount; /* Is it in use? */
82 int seq; /* Sequence number: this is incremented
83 * by wakeup calls; wait will not return until
85 kcondvar_t cond; /* Currently associated condition variable */
89 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
90 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
91 int afs_evhashcnt = 0;
93 /* Get and initialize event structure corresponding to lwp event (i.e. address)
96 afs_getevent(char *event)
98 afs_event_t *evp, *newp = 0;
102 hashcode = afs_evhash(event);
103 evp = afs_evhasht[hashcode];
105 if (evp->event == event) {
109 if (evp->refcount == 0)
114 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
116 newp->next = afs_evhasht[hashcode];
117 afs_evhasht[hashcode] = newp;
118 cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
126 /* Release the specified event */
127 #define relevent(evp) ((evp)->refcount--)
131 afs_osi_Sleep(void *event)
133 struct afs_event *evp;
136 evp = afs_getevent(event);
138 while (seq == evp->seq) {
140 cv_wait(&evp->cond, &afs_global_lock);
146 afs_osi_SleepSig(void *event)
148 struct afs_event *evp;
151 evp = afs_getevent(event);
153 while (seq == evp->seq) {
155 if (cv_wait_sig(&evp->cond, &afs_global_lock) == 0) {
167 * event - event to sleep on
168 * ams --- max sleep time in milliseconds
169 * aintok - 1 if should sleep interruptibly
171 * Returns 0 if timeout and EINTR if signalled.
174 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
177 struct afs_event *evp;
180 ticks = (ams * afs_hz) / 1000;
181 ticks = ticks + lbolt;
183 evp = afs_getevent(event);
187 if (cv_timedwait_sig(&evp->cond, &afs_global_lock, ticks) == 0)
190 cv_timedwait(&evp->cond, &afs_global_lock, ticks);
199 afs_osi_Wakeup(void *event)
202 struct afs_event *evp;
204 evp = afs_getevent(event);
205 if (evp->refcount > 1) {
207 cv_broadcast(&evp->cond);