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
87 #ifdef AFS_DARWIN80_ENV
93 #ifdef AFS_DARWIN80_ENV
94 #define EVTLOCK_INIT(e) \
96 (e)->lck = lck_mtx_alloc_init(openafs_lck_grp, 0); \
99 #define EVTLOCK_LOCK(e) \
101 osi_Assert((e)->owner != current_thread()); \
102 lck_mtx_lock((e)->lck); \
103 osi_Assert((e)->owner == 0); \
104 (e)->owner = current_thread(); \
106 #define EVTLOCK_UNLOCK(e) \
108 osi_Assert((e)->owner == current_thread()); \
110 lck_mtx_unlock((e)->lck); \
112 #define EVTLOCK_DESTROY(e) lck_mtx_free((e)->lck, openafs_lck_grp)
114 #define EVTLOCK_INIT(e)
115 #define EVTLOCK_LOCK(e)
116 #define EVTLOCK_UNLOCK(e)
117 #define EVTLOCK_DESTROY(e)
120 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
121 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
122 int afs_evhashcnt = 0;
124 /* Get and initialize event structure corresponding to lwp event (i.e. address)
127 afs_getevent(char *event)
129 afs_event_t *evp, *oevp, *newp = 0;
133 hashcode = afs_evhash(event);
134 evp = afs_evhasht[hashcode];
137 if (evp->event == event) {
141 if (evp->refcount == 0)
147 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
149 newp->next = afs_evhasht[hashcode];
150 afs_evhasht[hashcode] = newp;
160 /* Release the specified event */
161 #ifdef AFS_DARWIN80_ENV
162 #define relevent(evp) \
164 osi_Assert((evp)->owner == current_thread()); \
167 lck_mtx_unlock((evp)->lck); \
170 #define relevent(evp) ((evp)->refcount--)
175 afs_osi_Sleep(void *event)
177 struct afs_event *evp;
180 evp = afs_getevent(event);
181 #ifdef AFS_DARWIN80_ENV
186 while (seq == evp->seq) {
187 #ifdef AFS_DARWIN80_ENV
189 msleep(event, evp->lck, PVFS, "afs_osi_Sleep", NULL);
190 evp->owner = current_thread();
194 #ifdef AFS_DARWIN14_ENV
195 /* this is probably safe for all versions, but testing is hard */
198 assert_wait((event_t) event, 0);
205 #ifdef AFS_DARWIN80_ENV
211 afs_osi_fullSigMask()
213 #ifndef AFS_DARWIN80_ENV
214 struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
216 /* Protect original sigmask */
217 if (!user_thread->uu_oldmask) {
218 /* Back up current sigmask */
219 user_thread->uu_oldmask = user_thread->uu_sigmask;
220 /* Mask all signals */
221 user_thread->uu_sigmask = ~(sigset_t)0;
227 afs_osi_fullSigRestore()
229 #ifndef AFS_DARWIN80_ENV
230 struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
232 /* Protect original sigmask */
233 if (user_thread->uu_oldmask) {
234 /* Restore original sigmask */
235 user_thread->uu_sigmask = user_thread->uu_oldmask;
236 /* Clear the oldmask */
237 user_thread->uu_oldmask = (sigset_t)0;
243 afs_osi_SleepSig(void *event)
245 afs_osi_Sleep(event);
252 * event - event to sleep on
253 * ams --- max sleep time in milliseconds
254 * aintok - 1 if should sleep interruptibly
256 * Returns 0 if timeout and EINTR if signalled.
259 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
262 struct afs_event *evp;
265 #ifdef AFS_DARWIN80_ENV
273 evp = afs_getevent(event);
276 #ifdef AFS_DARWIN80_ENV
278 prio = PCATCH | PPAUSE;
281 ts.tv_sec = ams / 1000;
282 ts.tv_nsec = (ams % 1000) * 1000000;
284 code = msleep(event, evp->lck, prio, "afs_osi_TimedSleep", &ts);
285 evp->owner = current_thread();
287 ticks = (ams * afs_hz) / 1000;
288 #ifdef AFS_DARWIN14_ENV
289 /* this is probably safe for all versions, but testing is hard. */
290 /* using tsleep instead of assert_wait/thread_set_timer/thread_block
291 * allows shutdown to work in 1.4 */
292 /* lack of PCATCH does *not* prevent signal delivery, neither does
293 * a low priority. We would need to deal with ERESTART here if we
294 * wanted to mess with p->p_sigmask, and messing with p_sigignore is
295 * not the way to go.... (someone correct me if I'm wrong)
298 prio = PCATCH | PPAUSE;
301 code = tsleep(event, prio, "afs_osi_TimedSleep", ticks);
303 assert_wait((event_t) event, aintok ? THREAD_ABORTSAFE : THREAD_UNINT);
304 thread_set_timer(ticks, NSEC_PER_SEC / hz);
314 #ifdef AFS_DARWIN80_ENV
322 afs_osi_Wakeup(void *event)
324 struct afs_event *evp;
327 evp = afs_getevent(event);
328 if (evp->refcount > 1) {
330 #ifdef AFS_DARWIN14_ENV
331 /* this is probably safe for all versions, but testing is hard. */
334 thread_wakeup((event_t) event);
343 shutdown_osisleep(void) {
344 struct afs_event *evp, *nevp, **pevpp;
346 for (i=0; i < HASHSIZE; i++) {
347 evp = afs_evhasht[i];
348 pevpp = &afs_evhasht[i];
352 if (evp->refcount == 0) {
353 EVTLOCK_DESTROY(evp);
355 osi_FreeSmallSpace(evp);