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
11 #include <afsconfig.h>
12 #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 */
21 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
26 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
28 AFS_STATCNT(osi_InitWaitHandle);
29 achandle->proc = (caddr_t) 0;
34 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
38 AFS_STATCNT(osi_CancelWait);
39 proc = achandle->proc;
42 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
43 afs_osi_Wakeup(&waitV);
47 * Waits for data on ahandle, or ams ms later. ahandle may be null.
48 * Returns 0 if timeout and EINTR if signalled.
51 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
54 afs_int32 endTime, tid;
56 AFS_STATCNT(osi_Wait);
57 endTime = osi_Time() + (ams / 1000);
59 ahandle->proc = (caddr_t) curproc;
63 code = osi_TimedSleep(&waitV, ams, aintok);
66 break; /* if something happened, quit now */
67 /* if we we're cancelled, quit now */
68 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
69 /* we've been signalled */
72 } while (osi_Time() < endTime);
79 typedef struct afs_event {
80 struct afs_event *next; /* next in hash chain */
81 char *event; /* lwp event: an address */
82 int refcount; /* Is it in use? */
83 int seq; /* Sequence number: this is incremented
84 * by wakeup calls; wait will not return until
90 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
91 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
92 int afs_evhashcnt = 0;
94 /* Get and initialize event structure corresponding to lwp event (i.e. address)
97 afs_getevent(char *event)
99 afs_event_t *evp, *newp = 0;
103 hashcode = afs_evhash(event);
104 evp = afs_evhasht[hashcode];
106 if (evp->event == event) {
110 if (evp->refcount == 0)
115 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
117 newp->next = afs_evhasht[hashcode];
118 afs_evhasht[hashcode] = newp;
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 assert_wait((vm_offset_t) (&evp->cond), 0);
149 afs_osi_SleepSig(void *event)
151 afs_osi_Sleep(event);
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.
165 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
168 struct afs_event *evp;
171 ticks = (ams * afs_hz) / 1000;
174 evp = afs_getevent(event);
176 assert_wait((vm_offset_t) (&evp->cond), aintok);
178 thread_set_timeout(ticks);
181 /* if (current_thread()->wait_result != THREAD_AWAKENED)
190 afs_osi_Wakeup(void *event)
193 struct afs_event *evp;
195 evp = afs_getevent(event);
196 if (evp->refcount > 1) {
198 thread_wakeup((vm_offset_t) (&evp->cond));