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 */
22 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
28 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
30 AFS_STATCNT(osi_InitWaitHandle);
31 achandle->proc = (caddr_t) 0;
36 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
40 AFS_STATCNT(osi_CancelWait);
41 proc = achandle->proc;
44 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
45 afs_osi_Wakeup(&waitV);
49 * Waits for data on ahandle, or ams ms later. ahandle may be null.
50 * Returns 0 if timeout and EINTR if signalled.
53 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
56 afs_int32 endTime, tid;
58 AFS_STATCNT(osi_Wait);
59 endTime = osi_Time() + (ams / 1000);
61 ahandle->proc = (caddr_t) curthreadp;
65 code = osi_TimedSleep(&waitV, ams, aintok);
68 break; /* if something happened, quit now */
69 /* if we we're cancelled, quit now */
70 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
71 /* we've been signalled */
74 } while (osi_Time() < endTime);
81 typedef struct afs_event {
82 struct afs_event *next; /* next in hash chain */
83 char *event; /* lwp event: an address */
84 int refcount; /* Is it in use? */
85 int seq; /* Sequence number: this is incremented
86 * by wakeup calls; wait will not return until
88 kcondvar_t cond; /* Currently associated condition variable */
92 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
93 #if (_MIPS_SZPTR == 64)
94 #define afs_evhash(event) (afs_uint32) ((((long)event)>>3) & (HASHSIZE-1));
96 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
98 int afs_evhashcnt = 0;
100 /* Get and initialize event structure corresponding to lwp event (i.e. address)
103 afs_getevent(char *event)
105 afs_event_t *evp, *newp = 0;
109 hashcode = afs_evhash(event);
110 evp = afs_evhasht[hashcode];
112 if (evp->event == event) {
116 if (evp->refcount == 0)
121 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
123 newp->next = afs_evhasht[hashcode];
124 afs_evhasht[hashcode] = newp;
125 cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
133 /* Release the specified event */
134 #define relevent(evp) ((evp)->refcount--)
138 afs_osi_Sleep(void *event)
140 struct afs_event *evp;
143 evp = afs_getevent(event);
145 while (seq == evp->seq) {
147 cv_wait(&evp->cond, &afs_global_lock);
153 afs_osi_SleepSig(void *event)
155 afs_osi_Sleep(event);
162 * event - event to sleep on
163 * ams --- max sleep time in milliseconds
164 * aintok - 1 if should sleep interruptibly
166 * Returns 0 if timeout and EINTR if signalled.
169 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
172 struct afs_event *evp;
173 struct timespec ticks;
175 ticks.tv_sec = ams / 1000;
176 ticks.tv_nsec = (ams - (ticks.tv_sec * 1000)) * 1000000;
179 evp = afs_getevent(event);
184 (&evp->cond, AFSD_PRI(), &afs_global_lock, 0, 0, &ticks,
185 (struct timespec *)0))
187 AFS_MUTEX_ENTER(&afs_global_lock);
189 cv_timedwait(&evp->cond, &afs_global_lock, ticks);
198 afs_osi_Wakeup(void *event)
201 struct afs_event *evp;
203 evp = afs_getevent(event);
204 if (evp->refcount > 1) {
206 cv_broadcast(&evp->cond);