1 /* Copyright (C) 1995 Transarc Corporation - All rights reserved. */
3 * (C) COPYRIGHT IBM CORPORATION 1987, 1988
4 * LICENSED MATERIALS - PROPERTY OF IBM
7 #include "../afs/param.h" /* Should be always first */
8 #include "../afs/sysincludes.h" /* Standard vendor system headers */
9 #include "../afs/afsincludes.h" /* Afs-based standard headers */
10 #include "../afs/afs_stats.h" /* afs statistics */
14 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
15 void afs_osi_Wakeup(char *event);
16 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) u.u_procp;
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
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;
116 /* Release the specified event */
117 #define relevent(evp) ((evp)->refcount--)
120 void afs_osi_Sleep(char *event)
122 struct afs_event *evp;
125 evp = afs_getevent(event);
127 while (seq == evp->seq) {
129 assert_wait((vm_offset_t)(&evp->cond), 0);
140 * event - event to sleep on
141 * ams --- max sleep time in milliseconds
142 * aintok - 1 if should sleep interruptibly
144 * Returns 0 if timeout and EINTR if signalled.
146 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
149 struct afs_event *evp;
152 ticks = ( ams * afs_hz )/1000;
155 evp = afs_getevent(event);
157 assert_wait((vm_offset_t)(&evp->cond), aintok);
159 thread_set_timeout(ticks);
162 if (current_thread()->wait_result != THREAD_AWAKENED)
170 void afs_osi_Wakeup(char *event)
172 struct afs_event *evp;
174 evp = afs_getevent(event);
175 if (evp->refcount > 1) {
177 thread_wakeup((vm_offset_t)(&evp->cond));