0495164414d13c155ea0eae4983654a6a9b2cc9a
[openafs.git] / src / afs / DARWIN / osi_sleep.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 RCSID
14     ("$Header$");
15
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 */
19
20
21 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
22
23 static char waitV;
24
25
26 void
27 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
28 {
29     AFS_STATCNT(osi_InitWaitHandle);
30     achandle->proc = (caddr_t) 0;
31 }
32
33 /* cancel osi_Wait */
34 void
35 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
36 {
37     caddr_t proc;
38
39     AFS_STATCNT(osi_CancelWait);
40     proc = achandle->proc;
41     if (proc == 0)
42         return;
43     achandle->proc = (caddr_t) 0;       /* so dude can figure out he was signalled */
44     afs_osi_Wakeup(&waitV);
45 }
46
47 /* afs_osi_Wait
48  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
49  * Returns 0 if timeout and EINTR if signalled.
50  */
51 int
52 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
53 {
54     int code;
55     afs_int32 endTime, tid;
56     struct proc *p = current_proc();
57
58     AFS_STATCNT(osi_Wait);
59     endTime = osi_Time() + (ams / 1000);
60     if (ahandle)
61         ahandle->proc = (caddr_t) p;
62     do {
63         AFS_ASSERT_GLOCK();
64         code = 0;
65         code = osi_TimedSleep(&waitV, ams, aintok);
66
67         if (code)
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 */
72             break;
73         }
74     } while (osi_Time() < endTime);
75     return code;
76 }
77
78
79
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
86                                  * it changes */
87 #ifdef AFS_DARWIN80_ENV
88    lck_mtx_t *lck;
89    thread_t owner;
90 #endif
91 } afs_event_t;
92
93 #ifdef AFS_DARWIN80_ENV
94 #define EVTLOCK_INIT(e) \
95     do { \
96         (e)->lck = lck_mtx_alloc_init(openafs_lck_grp, 0); \
97         (e)->owner = 0; \
98     } while (0)
99 #define EVTLOCK_LOCK(e) \
100     do { \
101         osi_Assert((e)->owner != current_thread()); \
102         lck_mtx_lock((e)->lck); \
103         osi_Assert((e)->owner == 0); \
104         (e)->owner = current_thread(); \
105     } while (0)
106 #define EVTLOCK_UNLOCK(e) \
107     do { \
108         osi_Assert((e)->owner == current_thread()); \
109         (e)->owner = 0; \
110         lck_mtx_unlock((e)->lck); \
111     } while (0)
112 #define EVTLOCK_DESTROY(e) lck_mtx_free((e)->lck, openafs_lck_grp)
113 #else
114 #define EVTLOCK_INIT(e)
115 #define EVTLOCK_LOCK(e)
116 #define EVTLOCK_UNLOCK(e)
117 #define EVTLOCK_DESTROY(e)
118 #endif
119 #define HASHSIZE 128
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;
123
124 /* Get and initialize event structure corresponding to lwp event (i.e. address)
125  * */
126 static afs_event_t *
127 afs_getevent(char *event)
128 {
129     afs_event_t *evp, *oevp, *newp = 0;
130     int hashcode;
131
132     AFS_ASSERT_GLOCK();
133     hashcode = afs_evhash(event);
134     evp = afs_evhasht[hashcode];
135     while (evp) {
136         EVTLOCK_LOCK(evp);
137         if (evp->event == event) {
138             evp->refcount++;
139             return evp;
140         }
141         if (evp->refcount == 0)
142             newp = evp;
143         EVTLOCK_UNLOCK(evp);
144         evp = evp->next;
145     }
146     if (!newp) {
147         newp = (afs_event_t *) osi_AllocSmallSpace(sizeof(afs_event_t));
148         afs_evhashcnt++;
149         newp->next = afs_evhasht[hashcode];
150         afs_evhasht[hashcode] = newp;
151         newp->seq = 0;
152         EVTLOCK_INIT(newp);
153     }
154     EVTLOCK_LOCK(newp);
155     newp->event = event;
156     newp->refcount = 1;
157     return newp;
158 }
159
160 /* Release the specified event */
161 #ifdef AFS_DARWIN80_ENV
162 #define relevent(evp) \
163     do { \
164         osi_Assert((evp)->owner == current_thread()); \
165         (evp)->refcount--; \
166         (evp)->owner = 0; \
167         lck_mtx_unlock((evp)->lck); \
168     } while (0)
169 #else
170 #define relevent(evp) ((evp)->refcount--)
171 #endif
172
173
174 void
175 afs_osi_Sleep(void *event)
176 {
177     struct afs_event *evp;
178     int seq;
179
180     evp = afs_getevent(event);
181 #ifdef AFS_DARWIN80_ENV
182      AFS_ASSERT_GLOCK();
183      AFS_GUNLOCK();
184 #endif
185     seq = evp->seq;
186     while (seq == evp->seq) {
187 #ifdef AFS_DARWIN80_ENV
188         evp->owner = 0;
189         msleep(event, evp->lck, PVFS, "afs_osi_Sleep", NULL);
190         evp->owner = current_thread();
191 #else
192         AFS_ASSERT_GLOCK();
193         AFS_GUNLOCK();
194 #ifdef AFS_DARWIN14_ENV
195         /* this is probably safe for all versions, but testing is hard */
196         sleep(event, PVFS);
197 #else
198         assert_wait((event_t) event, 0);
199         thread_block(0);
200 #endif
201         AFS_GLOCK();
202 #endif
203     }
204     relevent(evp);
205 #ifdef AFS_DARWIN80_ENV
206     AFS_GLOCK();
207 #endif
208 }
209
210 void 
211 afs_osi_fullSigMask()
212 {
213 #ifndef AFS_DARWIN80_ENV
214     struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
215        
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;
222     }
223 #endif
224 }
225
226 void 
227 afs_osi_fullSigRestore()
228 {
229 #ifndef AFS_DARWIN80_ENV
230     struct uthread *user_thread = (struct uthread *)get_bsdthread_info(current_act());
231        
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;
238     }
239 #endif
240 }
241
242 int
243 afs_osi_SleepSig(void *event)
244 {
245     afs_osi_Sleep(event);
246     return 0;
247 }
248
249 /* osi_TimedSleep
250  * 
251  * Arguments:
252  * event - event to sleep on
253  * ams --- max sleep time in milliseconds
254  * aintok - 1 if should sleep interruptibly
255  *
256  * Returns 0 if timeout and EINTR if signalled.
257  */
258 static int
259 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
260 {
261     int code = 0;
262     struct afs_event *evp;
263     int seq;
264     int prio;
265 #ifdef AFS_DARWIN80_ENV
266     struct timespec ts;
267 #else
268     int ticks;
269 #endif
270
271
272
273     evp = afs_getevent(event);
274     seq = evp->seq;
275     AFS_GUNLOCK();
276 #ifdef AFS_DARWIN80_ENV
277     if (aintok)
278         prio = PCATCH | PPAUSE;
279     else
280         prio = PVFS;
281     ts.tv_sec = ams / 1000;
282     ts.tv_nsec = (ams % 1000) * 1000000;
283     evp->owner = 0;
284     code = msleep(event, evp->lck, prio, "afs_osi_TimedSleep", &ts);
285     evp->owner = current_thread();
286 #else
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)
296      */
297     if (aintok)
298         prio = PCATCH | PPAUSE;
299     else
300         prio = PVFS;
301     code = tsleep(event, prio, "afs_osi_TimedSleep", ticks);
302 #else
303     assert_wait((event_t) event, aintok ? THREAD_ABORTSAFE : THREAD_UNINT);
304     thread_set_timer(ticks, NSEC_PER_SEC / hz);
305     thread_block(0);
306     code = 0;
307 #endif
308     AFS_GLOCK();
309 #endif
310     if (seq == evp->seq)
311         code = EINTR;
312
313     relevent(evp);
314 #ifdef AFS_DARWIN80_ENV
315     AFS_GLOCK();
316 #endif
317     return code;
318 }
319
320
321 int
322 afs_osi_Wakeup(void *event)
323 {
324     struct afs_event *evp;
325     int ret = 1;
326
327     evp = afs_getevent(event);
328     if (evp->refcount > 1) {
329         evp->seq++;
330 #ifdef AFS_DARWIN14_ENV
331         /* this is probably safe for all versions, but testing is hard. */
332         wakeup(event);
333 #else
334         thread_wakeup((event_t) event);
335 #endif
336         ret = 0;
337     }
338     relevent(evp);
339     return ret;
340 }
341
342 void
343 shutdown_osisleep(void) {
344     struct afs_event *evp, *nevp, **pevpp;
345     int i;
346     for (i=0; i < HASHSIZE; i++) {
347         evp = afs_evhasht[i];
348         pevpp = &afs_evhasht[i];
349         while (evp) {
350             EVTLOCK_LOCK(evp);
351             nevp = evp->next;
352             if (evp->refcount == 0) {
353                 EVTLOCK_DESTROY(evp);
354                 *pevpp = evp->next;
355                 osi_FreeSmallSpace(evp);
356                 afs_evhashcnt--;
357             } else {
358                 EVTLOCK_UNLOCK(evp);
359                 pevpp = &evp->next;
360             }
361             evp = nevp;
362         }
363     }
364 }
365