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 */
15 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
16 void afs_osi_Wakeup(char *event);
17 void afs_osi_Sleep(char *event);
21 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
23 AFS_STATCNT(osi_InitWaitHandle);
24 achandle->proc = (caddr_t) 0;
28 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
32 AFS_STATCNT(osi_CancelWait);
33 proc = achandle->proc;
34 if (proc == 0) return;
35 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
36 afs_osi_Wakeup(&waitV);
40 * Waits for data on ahandle, or ams ms later. ahandle may be null.
41 * Returns 0 if timeout and EINTR if signalled.
43 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
46 afs_int32 endTime, tid;
48 AFS_STATCNT(osi_Wait);
49 endTime = osi_Time() + (ams/1000);
51 ahandle->proc = (caddr_t) curthread;
55 code = osi_TimedSleep(&waitV, ams, aintok);
57 if (code) break; /* if something happened, quit now */
58 /* if we we're cancelled, quit now */
59 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
60 /* we've been signalled */
63 } while (osi_Time() < endTime);
70 typedef struct afs_event {
71 struct afs_event *next; /* next in hash chain */
72 char *event; /* lwp event: an address */
73 int refcount; /* Is it in use? */
74 int seq; /* Sequence number: this is incremented
75 by wakeup calls; wait will not return until
77 kcondvar_t cond; /* Currently associated condition variable */
81 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
82 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
83 int afs_evhashcnt = 0;
85 /* Get and initialize event structure corresponding to lwp event (i.e. address)
87 static afs_event_t *afs_getevent(char *event)
89 afs_event_t *evp, *newp = 0;
93 hashcode = afs_evhash(event);
94 evp = afs_evhasht[hashcode];
96 if (evp->event == event) {
100 if (evp->refcount == 0)
105 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
107 newp->next = afs_evhasht[hashcode];
108 afs_evhasht[hashcode] = newp;
109 cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
117 /* Release the specified event */
118 #define relevent(evp) ((evp)->refcount--)
121 void afs_osi_Sleep(char *event)
123 struct afs_event *evp;
126 evp = afs_getevent(event);
128 while (seq == evp->seq) {
130 cv_wait(&evp->cond, &afs_global_lock);
138 * event - event to sleep on
139 * ams --- max sleep time in milliseconds
140 * aintok - 1 if should sleep interruptibly
142 * Returns 0 if timeout and EINTR if signalled.
144 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
147 struct afs_event *evp;
150 ticks = ( ams * afs_hz )/1000;
151 ticks = ticks + lbolt;
153 evp = afs_getevent(event);
157 if (cv_timedwait_sig(&evp->cond, &afs_global_lock, ticks) == -1)
160 cv_timedwait(&evp->cond, &afs_global_lock, ticks);
168 void afs_osi_Wakeup(char *event)
170 struct afs_event *evp;
172 evp = afs_getevent(event);
173 if (evp->refcount > 1) {
175 cv_broadcast(&evp->cond);