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"
16 #include "afs/sysincludes.h" /* Standard vendor system headers */
17 #include "afsincludes.h" /* Afs-based standard headers */
18 #include "afs/afs_stats.h" /* afs statistics */
21 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
27 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
29 AFS_STATCNT(osi_InitWaitHandle);
30 achandle->proc = (caddr_t) 0;
35 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
39 AFS_STATCNT(osi_CancelWait);
40 proc = achandle->proc;
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.
52 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
55 afs_int32 endTime, tid;
56 struct proc *p = current_proc();
58 AFS_STATCNT(osi_Wait);
59 endTime = osi_Time() + (ams / 1000);
61 ahandle->proc = (caddr_t) p;
65 code = osi_TimedSleep(&waitV, ams, aintok);
68 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);
80 typedef struct afs_event {
81 struct afs_event *next; /* next in hash chain */
82 char *event; /* lwp event: an address */
83 int refcount; /* Is it in use? */
84 int seq; /* Sequence number: this is incremented
85 * by wakeup calls; wait will not return until
90 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
91 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
92 int afs_evhashcnt = 0;
94 /* Get and initialize event structure corresponding to lwp event (i.e. address)
97 afs_getevent(char *event)
99 afs_event_t *evp, *newp = 0;
103 hashcode = afs_evhash(event);
104 evp = afs_evhasht[hashcode];
106 if (evp->event == event) {
110 if (evp->refcount == 0)
115 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
117 newp->next = afs_evhasht[hashcode];
118 afs_evhasht[hashcode] = newp;
126 /* Release the specified event */
127 #define relevent(evp) ((evp)->refcount--)
131 afs_osi_Sleep(void *event)
133 struct afs_event *evp;
136 evp = afs_getevent(event);
138 while (seq == evp->seq) {
141 #ifdef AFS_DARWIN80_ENV
142 msleep(event, NULL, PVFS, "afs_osi_Sleep", NULL);
144 #ifdef AFS_DARWIN14_ENV
145 /* this is probably safe for all versions, but testing is hard */
148 assert_wait((event_t) event, 0);
158 afs_osi_fullSigMask()
160 #ifndef AFS_DARWIN80_ENV
161 struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
163 /* Protect original sigmask */
164 if (!user_thread->uu_oldmask) {
165 /* Back up current sigmask */
166 user_thread->uu_oldmask = user_thread->uu_sigmask;
167 /* Mask all signals */
168 user_thread->uu_sigmask = ~(sigset_t)0;
174 afs_osi_fullSigRestore()
176 #ifndef AFS_DARWIN80_ENV
177 struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
179 /* Protect original sigmask */
180 if (user_thread->uu_oldmask) {
181 /* Restore original sigmask */
182 user_thread->uu_sigmask = user_thread->uu_oldmask;
183 /* Clear the oldmask */
184 user_thread->uu_oldmask = (sigset_t)0;
190 afs_osi_SleepSig(void *event)
192 afs_osi_Sleep(event);
199 * event - event to sleep on
200 * ams --- max sleep time in milliseconds
201 * aintok - 1 if should sleep interruptibly
203 * Returns 0 if timeout and EINTR if signalled.
206 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
209 struct afs_event *evp;
212 #ifdef AFS_DARWIN80_ENV
220 evp = afs_getevent(event);
223 #ifdef AFS_DARWIN80_ENV
225 prio = PCATCH | PPAUSE;
228 ts.tv_sec = ams / 1000;
229 ts.tv_nsec = (ams % 1000) * 1000000;
230 code = msleep(event, NULL, prio, "afs_osi_TimedSleep", &ts);
232 ticks = (ams * afs_hz) / 1000;
233 #ifdef AFS_DARWIN14_ENV
234 /* this is probably safe for all versions, but testing is hard. */
235 /* using tsleep instead of assert_wait/thread_set_timer/thread_block
236 * allows shutdown to work in 1.4 */
237 /* lack of PCATCH does *not* prevent signal delivery, neither does
238 * a low priority. We would need to deal with ERESTART here if we
239 * wanted to mess with p->p_sigmask, and messing with p_sigignore is
240 * not the way to go.... (someone correct me if I'm wrong)
243 prio = PCATCH | PPAUSE;
246 code = tsleep(event, prio, "afs_osi_TimedSleep", ticks);
248 assert_wait((event_t) event, aintok ? THREAD_ABORTSAFE : THREAD_UNINT);
249 thread_set_timer(ticks, NSEC_PER_SEC / hz);
264 afs_osi_Wakeup(void *event)
266 struct afs_event *evp;
269 evp = afs_getevent(event);
270 if (evp->refcount > 1) {
272 #ifdef AFS_DARWIN14_ENV
273 /* this is probably safe for all versions, but testing is hard. */
276 thread_wakeup((event_t) event);