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"
16 #include "../afs/sysincludes.h" /* Standard vendor system headers */
17 #include "../afs/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);
23 void afs_osi_Wakeup(char *event);
24 void afs_osi_Sleep(char *event);
29 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
31 AFS_STATCNT(osi_InitWaitHandle);
32 achandle->proc = (caddr_t) 0;
36 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
40 AFS_STATCNT(osi_CancelWait);
41 proc = achandle->proc;
42 if (proc == 0) return;
43 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
44 afs_osi_Wakeup(&waitV);
48 * Waits for data on ahandle, or ams ms later. ahandle may be null.
49 * Returns 0 if timeout and EINTR if signalled.
51 int 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);
65 if (code) break; /* if something happened, quit now */
66 /* if we we're cancelled, quit now */
67 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
68 /* we've been signalled */
71 } while (osi_Time() < endTime);
78 typedef struct afs_event {
79 struct afs_event *next; /* next in hash chain */
80 char *event; /* lwp event: an address */
81 int refcount; /* Is it in use? */
82 int seq; /* Sequence number: this is incremented
83 by wakeup calls; wait will not return until
89 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
90 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
91 int afs_evhashcnt = 0;
93 /* Get and initialize event structure corresponding to lwp event (i.e. address)
95 static afs_event_t *afs_getevent(char *event)
97 afs_event_t *evp, *newp = 0;
101 hashcode = afs_evhash(event);
102 evp = afs_evhasht[hashcode];
104 if (evp->event == event) {
108 if (evp->refcount == 0)
113 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
115 newp->next = afs_evhasht[hashcode];
116 afs_evhasht[hashcode] = newp;
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 assert_wait((vm_offset_t)(&evp->cond), 0);
148 * event - event to sleep on
149 * ams --- max sleep time in milliseconds
150 * aintok - 1 if should sleep interruptibly
152 * Returns 0 if timeout and EINTR if signalled.
154 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
157 struct afs_event *evp;
160 ticks = ( ams * afs_hz )/1000;
163 evp = afs_getevent(event);
165 assert_wait((vm_offset_t)(&evp->cond), aintok);
167 thread_set_timeout(ticks);
170 /* if (current_thread()->wait_result != THREAD_AWAKENED)
178 void afs_osi_Wakeup(char *event)
180 struct afs_event *evp;
182 evp = afs_getevent(event);
183 if (evp->refcount > 1) {
185 thread_wakeup((vm_offset_t)(&evp->cond));