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 */
21 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
23 AFS_STATCNT(osi_InitWaitHandle);
24 achandle->proc = (caddr_t) 0;
29 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
33 AFS_STATCNT(osi_CancelWait);
34 proc = achandle->proc;
37 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
38 afs_osi_Wakeup(&waitV);
42 * Waits for data on ahandle, or ams ms later. ahandle may be null.
43 * Returns 0 if timeout and EINTR if signalled.
46 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
49 afs_int32 endTime, tid;
50 struct proc *p = current_proc();
52 AFS_STATCNT(osi_Wait);
53 endTime = osi_Time() + (ams / 1000);
55 ahandle->proc = (caddr_t) p;
59 code = afs_osi_TimedSleep(&waitV, ams, aintok);
62 break; /* if something happened, quit now */
63 /* if we we're cancelled, quit now */
64 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
65 /* we've been signalled */
68 } while (osi_Time() < endTime);
74 typedef struct afs_event {
75 struct afs_event *next; /* next in hash chain */
76 char *event; /* lwp event: an address */
77 int refcount; /* Is it in use? */
78 int seq; /* Sequence number: this is incremented
79 * by wakeup calls; wait will not return until
81 #ifdef AFS_DARWIN80_ENV
87 #ifdef AFS_DARWIN80_ENV
88 #define EVTLOCK_INIT(e) \
90 (e)->lck = lck_mtx_alloc_init(openafs_lck_grp, 0); \
93 #define EVTLOCK_LOCK(e) \
95 osi_Assert((e)->owner != current_thread()); \
96 lck_mtx_lock((e)->lck); \
97 osi_Assert((e)->owner == 0); \
98 (e)->owner = current_thread(); \
100 #define EVTLOCK_UNLOCK(e) \
102 osi_Assert((e)->owner == current_thread()); \
104 lck_mtx_unlock((e)->lck); \
106 #define EVTLOCK_DESTROY(e) lck_mtx_free((e)->lck, openafs_lck_grp)
108 #define EVTLOCK_INIT(e)
109 #define EVTLOCK_LOCK(e)
110 #define EVTLOCK_UNLOCK(e)
111 #define EVTLOCK_DESTROY(e)
114 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
115 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
116 int afs_evhashcnt = 0;
118 /* Get and initialize event structure corresponding to lwp event (i.e. address)
121 afs_getevent(char *event)
123 afs_event_t *evp, *oevp, *newp = 0;
127 hashcode = afs_evhash(event);
128 evp = afs_evhasht[hashcode];
131 if (evp->event == event) {
135 if (evp->refcount == 0)
141 newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
143 newp->next = afs_evhasht[hashcode];
144 afs_evhasht[hashcode] = newp;
154 /* Release the specified event */
155 #ifdef AFS_DARWIN80_ENV
156 #define relevent(evp) \
158 osi_Assert((evp)->owner == current_thread()); \
161 lck_mtx_unlock((evp)->lck); \
164 #define relevent(evp) ((evp)->refcount--)
169 afs_osi_Sleep(void *event)
171 struct afs_event *evp;
174 evp = afs_getevent(event);
175 #ifdef AFS_DARWIN80_ENV
180 while (seq == evp->seq) {
181 #ifdef AFS_DARWIN80_ENV
183 msleep(event, evp->lck, PVFS, "afs_osi_Sleep", NULL);
184 evp->owner = current_thread();
188 /* this is probably safe for all versions, but testing is hard */
194 #ifdef AFS_DARWIN80_ENV
200 afs_osi_fullSigMask()
202 #ifndef AFS_DARWIN80_ENV
203 struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
205 /* Protect original sigmask */
206 if (!user_thread->uu_oldmask) {
207 /* Back up current sigmask */
208 user_thread->uu_oldmask = user_thread->uu_sigmask;
209 /* Mask all signals */
210 user_thread->uu_sigmask = ~(sigset_t)0;
216 afs_osi_fullSigRestore()
218 #ifndef AFS_DARWIN80_ENV
219 struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
221 /* Protect original sigmask */
222 if (user_thread->uu_oldmask) {
223 /* Restore original sigmask */
224 user_thread->uu_sigmask = user_thread->uu_oldmask;
225 /* Clear the oldmask */
226 user_thread->uu_oldmask = (sigset_t)0;
232 afs_osi_SleepSig(void *event)
234 afs_osi_Sleep(event);
238 /* afs_osi_TimedSleep
241 * event - event to sleep on
242 * ams --- max sleep time in milliseconds
243 * aintok - 1 if should sleep interruptibly
245 * Returns 0 if timeout and EINTR if signalled.
248 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
251 struct afs_event *evp;
254 #ifdef AFS_DARWIN80_ENV
262 evp = afs_getevent(event);
265 #ifdef AFS_DARWIN80_ENV
267 prio = PCATCH | PPAUSE;
270 ts.tv_sec = ams / 1000;
271 ts.tv_nsec = (ams % 1000) * 1000000;
273 code = msleep(event, evp->lck, prio, "afs_osi_TimedSleep", &ts);
274 evp->owner = current_thread();
276 ticks = (ams * afs_hz) / 1000;
277 /* this is probably safe for all versions, but testing is hard. */
278 /* using tsleep instead of assert_wait/thread_set_timer/thread_block
279 * allows shutdown to work in 1.4 */
280 /* lack of PCATCH does *not* prevent signal delivery, neither does
281 * a low priority. We would need to deal with ERESTART here if we
282 * wanted to mess with p->p_sigmask, and messing with p_sigignore is
283 * not the way to go.... (someone correct me if I'm wrong)
286 prio = PCATCH | PPAUSE;
289 code = tsleep(event, prio, "afs_osi_TimedSleep", ticks);
296 #ifdef AFS_DARWIN80_ENV
304 afs_osi_Wakeup(void *event)
306 struct afs_event *evp;
309 evp = afs_getevent(event);
310 if (evp->refcount > 1) {
312 /* this is probably safe for all versions, but testing is hard. */
321 shutdown_osisleep(void) {
322 struct afs_event *evp, *nevp, **pevpp;
324 for (i=0; i < HASHSIZE; i++) {
325 evp = afs_evhasht[i];
326 pevpp = &afs_evhasht[i];
330 if (evp->refcount == 0) {
331 EVTLOCK_DESTROY(evp);
333 osi_FreeSmallSpace(evp);