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 <afsconfig.h>
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h" /* afs statistics */
20 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
21 void afs_osi_Wakeup(char *event);
22 void afs_osi_Sleep(char *event);
27 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
29 AFS_STATCNT(osi_InitWaitHandle);
30 achandle->proc = (caddr_t) 0;
34 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
38 AFS_STATCNT(osi_CancelWait);
39 proc = achandle->proc;
40 if (proc == 0) return;
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.
49 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
52 afs_int32 endTime, tid;
53 struct proc *p=current_proc();
55 AFS_STATCNT(osi_Wait);
56 endTime = osi_Time() + (ams/1000);
58 ahandle->proc = (caddr_t)p;
62 code = osi_TimedSleep(&waitV, ams, aintok);
64 if (code) break; /* if something happened, quit now */
65 /* if we we're cancelled, quit now */
66 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
67 /* we've been signalled */
70 } while (osi_Time() < endTime);
76 typedef struct afs_event {
77 struct afs_event *next; /* next in hash chain */
78 char *event; /* lwp event: an address */
79 int refcount; /* Is it in use? */
80 int seq; /* Sequence number: this is incremented
81 by wakeup calls; wait will not return until
86 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
87 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
88 int afs_evhashcnt = 0;
90 /* Get and initialize event structure corresponding to lwp event (i.e. address)
92 static afs_event_t *afs_getevent(char *event)
94 afs_event_t *evp, *newp = 0;
98 hashcode = afs_evhash(event);
99 evp = afs_evhasht[hashcode];
101 if (evp->event == event) {
105 if (evp->refcount == 0)
110 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
112 newp->next = afs_evhasht[hashcode];
113 afs_evhasht[hashcode] = newp;
121 /* Release the specified event */
122 #define relevent(evp) ((evp)->refcount--)
125 void afs_osi_Sleep(char *event)
127 struct afs_event *evp;
130 evp = afs_getevent(event);
132 while (seq == evp->seq) {
134 assert_wait((event_t)event, 0);
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;
157 ticks = ( ams * afs_hz )/1000;
160 evp = afs_getevent(event);
162 assert_wait((event_t)event, aintok ? THREAD_ABORTSAFE : 0);
164 thread_set_timer(ticks, NSEC_PER_SEC / hz);
167 #if 0 /* thread_t structure only available if MACH_KERNEL_PRIVATE */
168 if (current_thread()->wait_result != THREAD_AWAKENED)
180 void afs_osi_Wakeup(char *event)
182 struct afs_event *evp;
184 evp = afs_getevent(event);
185 if (evp->refcount > 1) {
187 thread_wakeup((event_t)event);