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 <afsconfig.h>
11 #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);
25 static void AfsWaitHack(struct trb *trb)
27 AFS_STATCNT(WaitHack);
29 e_clear_wait(trb->func_data, THREAD_TIMED_OUT);
32 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
34 AFS_STATCNT(osi_InitWaitHandle);
35 achandle->proc = (caddr_t) 0;
39 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
43 AFS_STATCNT(osi_CancelWait);
44 proc = achandle->proc;
45 if (proc == 0) return;
46 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
47 afs_osi_Wakeup(&waitV);
51 * Waits for data on ahandle, or ams ms later. ahandle may be null.
52 * Returns 0 if timeout and EINTR if signalled.
54 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
57 afs_int32 endTime, tid;
59 AFS_STATCNT(osi_Wait);
60 endTime = osi_Time() + (ams/1000);
62 ahandle->proc = (caddr_t) thread_self();
66 code = osi_TimedSleep(&waitV, ams, aintok);
68 if (code) break; /* if something happened, quit now */
69 /* if we we're cancelled, quit now */
70 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
71 /* we've been signalled */
74 } while (osi_Time() < endTime);
81 typedef struct afs_event {
82 struct afs_event *next; /* next in hash chain */
83 char *event; /* lwp event: an address */
84 int refcount; /* Is it in use? */
85 int seq; /* Sequence number: this is incremented
86 by wakeup calls; wait will not return until
92 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
93 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
94 int afs_evhashcnt = 0;
96 /* Get and initialize event structure corresponding to lwp event (i.e. address)
98 static afs_event_t *afs_getevent(char *event)
100 afs_event_t *evp, *newp = 0;
104 hashcode = afs_evhash(event);
105 evp = afs_evhasht[hashcode];
107 if (evp->event == event) {
111 if (evp->refcount == 0)
116 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
118 newp->next = afs_evhasht[hashcode];
119 afs_evhasht[hashcode] = newp;
120 newp->cond = EVENT_NULL;
128 /* Release the specified event */
129 #define relevent(evp) ((evp)->refcount--)
132 void afs_osi_Sleep(void *event)
134 struct afs_event *evp;
137 evp = afs_getevent(event);
139 while (seq == evp->seq) {
141 e_assert_wait(&evp->cond, 0);
149 int 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.
164 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
167 struct afs_event *evp;
168 struct timestruc_t ticks;
172 ticks.tv_sec = ams / 1000;
173 ticks.tv_nsec = (ams - (ticks.tv_sec * 1000) ) * 1000000;
176 evp = afs_getevent(event);
180 osi_Panic("talloc returned NULL");
182 trb->func = AfsWaitHack;
183 trb->eventlist = EVENT_NULL;
184 trb->ipri = INTTIMER;
185 trb->func_data = thread_self();
186 trb->timeout.it_value = ticks;
188 e_assert_wait(&evp->cond, aintok);
191 rc = e_block_thread();
193 if (rc == THREAD_INTERRUPTED)
203 int afs_osi_Wakeup(void *event)
206 struct afs_event *evp;
208 evp = afs_getevent(event);
209 if (evp->refcount > 1) {
211 e_wakeup(&evp->cond);