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 "../afs/sysincludes.h" /* Standard vendor system headers */
12 #include "../afs/afsincludes.h" /* Afs-based standard headers */
13 #include "../afs/afs_stats.h" /* afs statistics */
17 #if defined(AFS_GLOBAL_SUNLOCK)
18 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
19 void afs_osi_Wakeup(char *event);
20 void afs_osi_Sleep(char *event);
25 #if ! defined(AFS_GLOBAL_SUNLOCK)
27 /* call procedure aproc with arock as an argument, in ams milliseconds */
28 static struct timer_list *afs_osi_CallProc(void *aproc, void *arock, int ams)
30 struct timer_list *timer = NULL;
32 timer = (struct timer_list*)osi_Alloc(sizeof(struct timer_list));
35 timer->expires = (ams*afs_hz)/1000 + 1;
36 timer->data = (unsigned long)arock;
37 timer->function = aproc;
43 /* cancel a timeout, whether or not it has already occurred */
44 static int afs_osi_CancelProc(struct timer_list *timer)
48 osi_Free(timer, sizeof(struct timer_list));
55 AFS_STATCNT(WaitHack);
61 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
63 AFS_STATCNT(osi_InitWaitHandle);
64 achandle->proc = (caddr_t) 0;
68 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
72 AFS_STATCNT(osi_CancelWait);
73 proc = achandle->proc;
74 if (proc == 0) return;
75 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
76 afs_osi_Wakeup(&waitV);
80 * Waits for data on ahandle, or ams ms later. ahandle may be null.
81 * Returns 0 if timeout and EINTR if signalled.
83 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
86 afs_int32 endTime, tid;
87 struct timer_list *timer = NULL;
89 AFS_STATCNT(osi_Wait);
90 endTime = osi_Time() + (ams/1000);
92 ahandle->proc = (caddr_t) current;
97 #if defined(AFS_GLOBAL_SUNLOCK)
98 code = osi_TimedSleep(&waitV, ams, 1);
101 flush_signals(current);
105 timer = afs_osi_CallProc(AfsWaitHack, (char *) current, ams);
106 afs_osi_Sleep(&waitV);
107 afs_osi_CancelProc(timer);
108 #endif /* AFS_GLOBAL_SUNLOCK */
109 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
110 /* we've been signalled */
113 } while (osi_Time() < endTime);
120 typedef struct afs_event {
121 struct afs_event *next; /* next in hash chain */
122 char *event; /* lwp event: an address */
123 int refcount; /* Is it in use? */
124 int seq; /* Sequence number: this is incremented
125 by wakeup calls; wait will not return until
127 #if defined(AFS_LINUX24_ENV)
128 wait_queue_head_t cond;
130 struct wait_queue *cond;
135 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
136 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
137 int afs_evhashcnt = 0;
139 /* Get and initialize event structure corresponding to lwp event (i.e. address)
141 static afs_event_t *afs_getevent(char *event)
143 afs_event_t *evp, *newp = 0;
147 hashcode = afs_evhash(event);
148 evp = afs_evhasht[hashcode];
150 if (evp->event == event) {
154 if (evp->refcount == 0)
159 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
161 newp->next = afs_evhasht[hashcode];
162 afs_evhasht[hashcode] = newp;
163 #if defined(AFS_LINUX24_ENV)
164 init_waitqueue_head(&newp->cond);
166 init_waitqueue(&newp->cond);
175 /* Release the specified event */
176 #define relevent(evp) ((evp)->refcount--)
179 void afs_osi_Sleep(char *event)
181 struct afs_event *evp;
184 evp = afs_getevent(event);
186 while (seq == evp->seq) {
189 interruptible_sleep_on(&evp->cond);
198 * event - event to sleep on
199 * ams --- max sleep time in milliseconds
200 * aintok - 1 if should sleep interruptibly
202 * Returns 0 if timeout and EINTR if signalled.
204 * While the Linux kernel still has a global lock, we can use the standard
205 * sleep calls and drop our locks early. The kernel lock will protect us
206 * until we get to sleep.
208 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
210 long t = ams * HZ / 1000;
211 struct afs_event *evp;
213 evp = afs_getevent(event);
217 t = interruptible_sleep_on_timeout(&evp->cond, t);
219 t = sleep_on_timeout(&evp->cond, t);
222 return t ? EINTR : 0;
226 void afs_osi_Wakeup(char *event)
228 struct afs_event *evp;
230 evp = afs_getevent(event);
231 if (evp->refcount > 1) {