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"
14 #include "afs/sysincludes.h" /* Standard vendor system headers */
15 #include "afsincludes.h" /* Afs-based standard headers */
16 #include "afs/afs_stats.h" /* afs statistics */
19 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
25 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
27 AFS_STATCNT(osi_InitWaitHandle);
28 achandle->proc = (caddr_t) 0;
33 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
37 AFS_STATCNT(osi_CancelWait);
38 proc = achandle->proc;
41 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
42 afs_osi_Wakeup(&waitV);
46 * Waits for data on ahandle, or ams ms later. ahandle may be null.
47 * Returns 0 if timeout and EINTR if signalled.
50 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
53 afs_int32 endTime, tid;
54 struct proc *p = current_proc();
56 AFS_STATCNT(osi_Wait);
57 endTime = osi_Time() + (ams / 1000);
59 ahandle->proc = (caddr_t) p;
63 code = osi_TimedSleep(&waitV, ams, aintok);
66 break; /* if something happened, quit now */
67 /* if we we're cancelled, quit now */
68 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
69 /* we've been signalled */
72 } while (osi_Time() < endTime);
78 typedef struct afs_event {
79 struct afs_event *next; /* next in hash chain */
80 char *event; /* lwp event: an address */
81 int refcount; /* Is it in use? */
82 int seq; /* Sequence number: this is incremented
83 * by wakeup calls; wait will not return until
85 #ifdef AFS_DARWIN80_ENV
91 #ifdef AFS_DARWIN80_ENV
92 #define EVTLOCK_INIT(e) \
94 (e)->lck = lck_mtx_alloc_init(openafs_lck_grp, 0); \
97 #define EVTLOCK_LOCK(e) \
99 osi_Assert((e)->owner != current_thread()); \
100 lck_mtx_lock((e)->lck); \
101 osi_Assert((e)->owner == 0); \
102 (e)->owner = current_thread(); \
104 #define EVTLOCK_UNLOCK(e) \
106 osi_Assert((e)->owner == current_thread()); \
108 lck_mtx_unlock((e)->lck); \
110 #define EVTLOCK_DESTROY(e) lck_mtx_free((e)->lck, openafs_lck_grp)
112 #define EVTLOCK_INIT(e)
113 #define EVTLOCK_LOCK(e)
114 #define EVTLOCK_UNLOCK(e)
115 #define EVTLOCK_DESTROY(e)
118 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
119 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
120 int afs_evhashcnt = 0;
122 /* Get and initialize event structure corresponding to lwp event (i.e. address)
125 afs_getevent(char *event)
127 afs_event_t *evp, *oevp, *newp = 0;
131 hashcode = afs_evhash(event);
132 evp = afs_evhasht[hashcode];
135 if (evp->event == event) {
139 if (evp->refcount == 0)
145 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
147 newp->next = afs_evhasht[hashcode];
148 afs_evhasht[hashcode] = newp;
158 /* Release the specified event */
159 #ifdef AFS_DARWIN80_ENV
160 #define relevent(evp) \
162 osi_Assert((evp)->owner == current_thread()); \
165 lck_mtx_unlock((evp)->lck); \
168 #define relevent(evp) ((evp)->refcount--)
173 afs_osi_Sleep(void *event)
175 struct afs_event *evp;
178 evp = afs_getevent(event);
179 #ifdef AFS_DARWIN80_ENV
184 while (seq == evp->seq) {
185 #ifdef AFS_DARWIN80_ENV
187 msleep(event, evp->lck, PVFS, "afs_osi_Sleep", NULL);
188 evp->owner = current_thread();
192 #ifdef AFS_DARWIN14_ENV
193 /* this is probably safe for all versions, but testing is hard */
196 assert_wait((event_t) event, 0);
203 #ifdef AFS_DARWIN80_ENV
209 afs_osi_fullSigMask()
211 #ifndef AFS_DARWIN80_ENV
212 struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
214 /* Protect original sigmask */
215 if (!user_thread->uu_oldmask) {
216 /* Back up current sigmask */
217 user_thread->uu_oldmask = user_thread->uu_sigmask;
218 /* Mask all signals */
219 user_thread->uu_sigmask = ~(sigset_t)0;
225 afs_osi_fullSigRestore()
227 #ifndef AFS_DARWIN80_ENV
228 struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
230 /* Protect original sigmask */
231 if (user_thread->uu_oldmask) {
232 /* Restore original sigmask */
233 user_thread->uu_sigmask = user_thread->uu_oldmask;
234 /* Clear the oldmask */
235 user_thread->uu_oldmask = (sigset_t)0;
241 afs_osi_SleepSig(void *event)
243 afs_osi_Sleep(event);
250 * event - event to sleep on
251 * ams --- max sleep time in milliseconds
252 * aintok - 1 if should sleep interruptibly
254 * Returns 0 if timeout and EINTR if signalled.
257 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
260 struct afs_event *evp;
263 #ifdef AFS_DARWIN80_ENV
271 evp = afs_getevent(event);
274 #ifdef AFS_DARWIN80_ENV
276 prio = PCATCH | PPAUSE;
279 ts.tv_sec = ams / 1000;
280 ts.tv_nsec = (ams % 1000) * 1000000;
282 code = msleep(event, evp->lck, prio, "afs_osi_TimedSleep", &ts);
283 evp->owner = current_thread();
285 ticks = (ams * afs_hz) / 1000;
286 #ifdef AFS_DARWIN14_ENV
287 /* this is probably safe for all versions, but testing is hard. */
288 /* using tsleep instead of assert_wait/thread_set_timer/thread_block
289 * allows shutdown to work in 1.4 */
290 /* lack of PCATCH does *not* prevent signal delivery, neither does
291 * a low priority. We would need to deal with ERESTART here if we
292 * wanted to mess with p->p_sigmask, and messing with p_sigignore is
293 * not the way to go.... (someone correct me if I'm wrong)
296 prio = PCATCH | PPAUSE;
299 code = tsleep(event, prio, "afs_osi_TimedSleep", ticks);
301 assert_wait((event_t) event, aintok ? THREAD_ABORTSAFE : THREAD_UNINT);
302 thread_set_timer(ticks, NSEC_PER_SEC / hz);
312 #ifdef AFS_DARWIN80_ENV
320 afs_osi_Wakeup(void *event)
322 struct afs_event *evp;
325 evp = afs_getevent(event);
326 if (evp->refcount > 1) {
328 #ifdef AFS_DARWIN14_ENV
329 /* this is probably safe for all versions, but testing is hard. */
332 thread_wakeup((event_t) event);
341 shutdown_osisleep(void) {
342 struct afs_event *evp, *nevp, **pevpp;
344 for (i=0; i < HASHSIZE; i++) {
345 evp = afs_evhasht[i];
346 pevpp = &afs_evhasht[i];
350 if (evp->refcount == 0) {
351 EVTLOCK_DESTROY(evp);
353 osi_FreeSmallSpace(evp);