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 "../afs/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);
22 void afs_osi_Wakeup(char *event);
23 void afs_osi_Sleep(char *event);
27 static void AfsWaitHack(struct trb *trb)
29 AFS_STATCNT(WaitHack);
31 e_clear_wait(trb->func_data, THREAD_TIMED_OUT);
34 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
36 AFS_STATCNT(osi_InitWaitHandle);
37 achandle->proc = (caddr_t) 0;
41 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
45 AFS_STATCNT(osi_CancelWait);
46 proc = achandle->proc;
47 if (proc == 0) return;
48 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
49 afs_osi_Wakeup(&waitV);
53 * Waits for data on ahandle, or ams ms later. ahandle may be null.
54 * Returns 0 if timeout and EINTR if signalled.
56 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
59 afs_int32 endTime, tid;
61 AFS_STATCNT(osi_Wait);
62 endTime = osi_Time() + (ams/1000);
64 ahandle->proc = (caddr_t) thread_self();
68 code = osi_TimedSleep(&waitV, ams, aintok);
70 if (code) break; /* if something happened, quit now */
71 /* if we we're cancelled, quit now */
72 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
73 /* we've been signalled */
76 } while (osi_Time() < endTime);
83 typedef struct afs_event {
84 struct afs_event *next; /* next in hash chain */
85 char *event; /* lwp event: an address */
86 int refcount; /* Is it in use? */
87 int seq; /* Sequence number: this is incremented
88 by wakeup calls; wait will not return until
94 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
95 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
96 int afs_evhashcnt = 0;
98 /* Get and initialize event structure corresponding to lwp event (i.e. address)
100 static afs_event_t *afs_getevent(char *event)
102 afs_event_t *evp, *newp = 0;
106 hashcode = afs_evhash(event);
107 evp = afs_evhasht[hashcode];
109 if (evp->event == event) {
113 if (evp->refcount == 0)
118 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
120 newp->next = afs_evhasht[hashcode];
121 afs_evhasht[hashcode] = newp;
122 newp->cond = EVENT_NULL;
130 /* Release the specified event */
131 #define relevent(evp) ((evp)->refcount--)
134 void afs_osi_Sleep(char *event)
136 struct afs_event *evp;
139 evp = afs_getevent(event);
141 while (seq == evp->seq) {
143 e_assert_wait(&evp->cond, 0);
154 * event - event to sleep on
155 * ams --- max sleep time in milliseconds
156 * aintok - 1 if should sleep interruptibly
158 * Returns 0 if timeout and EINTR if signalled.
160 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
163 struct afs_event *evp;
164 struct timestruc_t ticks;
168 ticks.tv_sec = ams / 1000;
169 ticks.tv_nsec = (ams - (ticks.tv_sec * 1000) ) * 1000000;
172 evp = afs_getevent(event);
176 osi_Panic("talloc returned NULL");
178 trb->func = AfsWaitHack;
179 trb->eventlist = EVENT_NULL;
180 trb->ipri = INTTIMER;
181 trb->func_data = thread_self();
182 trb->timeout.it_value = ticks;
184 e_assert_wait(&evp->cond, aintok);
187 rc = e_block_thread();
189 if (rc == THREAD_INTERRUPTED)
199 void afs_osi_Wakeup(char *event)
201 struct afs_event *evp;
203 evp = afs_getevent(event);
204 if (evp->refcount > 1) {
206 e_wakeup(&evp->cond);