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 "../afs/param.h" /* Should be always first */
11 #include "../afs/sysincludes.h" /* Standard vendor system headers */
12 #include "../afs/afsincludes.h" /* Afs-based standard headers */
13 #include "../afs/afs_stats.h" /* afs statistics */
17 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
18 void afs_osi_Wakeup(char *event);
19 void afs_osi_Sleep(char *event);
24 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
26 AFS_STATCNT(osi_InitWaitHandle);
27 achandle->proc = (caddr_t) 0;
31 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
35 AFS_STATCNT(osi_CancelWait);
36 proc = achandle->proc;
37 if (proc == 0) return;
38 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
39 afs_osi_Wakeup(&waitV);
43 * Waits for data on ahandle, or ams ms later. ahandle may be null.
44 * Returns 0 if timeout and EINTR if signalled.
46 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
49 afs_int32 endTime, tid;
51 AFS_STATCNT(osi_Wait);
52 endTime = osi_Time() + (ams/1000);
54 ahandle->proc = (caddr_t) curthreadp;
58 code = osi_TimedSleep(&waitV, ams, aintok);
60 if (code) break; /* if something happened, quit now */
61 /* if we we're cancelled, quit now */
62 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
63 /* we've been signalled */
66 } while (osi_Time() < endTime);
73 typedef struct afs_event {
74 struct afs_event *next; /* next in hash chain */
75 char *event; /* lwp event: an address */
76 int refcount; /* Is it in use? */
77 int seq; /* Sequence number: this is incremented
78 by wakeup calls; wait will not return until
80 kcondvar_t cond; /* Currently associated condition variable */
84 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
85 #if (_MIPS_SZPTR == 64)
86 #define afs_evhash(event) (afs_uint32) ((((long)event)>>3) & (HASHSIZE-1));
88 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
90 int afs_evhashcnt = 0;
92 /* Get and initialize event structure corresponding to lwp event (i.e. address)
94 static afs_event_t *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--)
128 void afs_osi_Sleep(char *event)
130 struct afs_event *evp;
133 evp = afs_getevent(event);
135 while (seq == evp->seq) {
137 cv_wait(&evp->cond, &afs_global_lock);
145 * event - event to sleep on
146 * ams --- max sleep time in milliseconds
147 * aintok - 1 if should sleep interruptibly
149 * Returns 0 if timeout and EINTR if signalled.
151 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
154 struct afs_event *evp;
155 struct timespec ticks;
157 ticks.tv_sec = ams / 1000;
158 ticks.tv_nsec = (ams - (ticks.tv_sec * 1000) ) * 1000000;
161 evp = afs_getevent(event);
165 if (sv_timedwait_sig(&evp->cond, AFSD_PRI(), &afs_global_lock,
166 0, 0, &ticks, (struct timespec*)0))
168 AFS_MUTEX_ENTER(&afs_global_lock);
170 cv_timedwait(&evp->cond, &afs_global_lock, ticks);
178 void afs_osi_Wakeup(char *event)
180 struct afs_event *evp;
182 evp = afs_getevent(event);
183 if (evp->refcount > 1) {
185 cv_broadcast(&evp->cond);