First pass at better signal handling:
[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("$Header$");
14
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h"   /* afs statistics */
18
19
20 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
21 void afs_osi_Wakeup(char *event);
22 void afs_osi_Sleep(char *event);
23
24 static char waitV;
25
26
27 void 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 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
35 {
36     caddr_t proc;
37
38     AFS_STATCNT(osi_CancelWait);
39     proc = achandle->proc;
40     if (proc == 0) return;
41     achandle->proc = (caddr_t) 0;   /* so dude can figure out he was signalled */
42     afs_osi_Wakeup(&waitV);
43 }
44
45 /* afs_osi_Wait
46  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
47  * Returns 0 if timeout and EINTR if signalled.
48  */
49 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
50 {
51     int code;
52     afs_int32 endTime, tid;
53     struct proc *p=current_proc();
54
55     AFS_STATCNT(osi_Wait);
56     endTime = osi_Time() + (ams/1000);
57     if (ahandle)
58         ahandle->proc = (caddr_t)p;
59     do {
60         AFS_ASSERT_GLOCK();
61         code = 0;
62         code = osi_TimedSleep(&waitV, ams, aintok);
63
64         if (code) break;        /* if something happened, quit now */
65         /* if we we're cancelled, quit now */
66         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
67             /* we've been signalled */
68             break;
69         }
70     } while (osi_Time() < endTime);
71     return code;
72 }
73
74
75
76 typedef struct afs_event {
77     struct afs_event *next;     /* next in hash chain */
78     char *event;                /* lwp event: an address */
79     int refcount;               /* Is it in use? */
80     int seq;                    /* Sequence number: this is incremented
81                                    by wakeup calls; wait will not return until
82                                    it changes */
83 } afs_event_t;
84
85 #define HASHSIZE 128
86 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
87 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
88 int afs_evhashcnt = 0;
89
90 /* Get and initialize event structure corresponding to lwp event (i.e. address)
91  * */
92 static afs_event_t *afs_getevent(char *event)
93 {
94     afs_event_t *evp, *newp = 0;
95     int hashcode;
96
97     AFS_ASSERT_GLOCK();
98     hashcode = afs_evhash(event);
99     evp = afs_evhasht[hashcode];
100     while (evp) {
101         if (evp->event == event) {
102             evp->refcount++;
103             return evp;
104         }
105         if (evp->refcount == 0)
106             newp = evp;
107         evp = evp->next;
108     }
109     if (!newp) {
110         newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
111         afs_evhashcnt++;
112         newp->next = afs_evhasht[hashcode];
113         afs_evhasht[hashcode] = newp;
114         newp->seq = 0;
115     }
116     newp->event = event;
117     newp->refcount = 1;
118     return newp;
119 }
120
121 /* Release the specified event */
122 #define relevent(evp) ((evp)->refcount--)
123
124
125 void afs_osi_Sleep(char *event)
126 {
127     struct afs_event *evp;
128     int seq;
129
130     evp = afs_getevent(event);
131     seq = evp->seq;
132     while (seq == evp->seq) {
133         AFS_ASSERT_GLOCK();
134         AFS_GUNLOCK();
135 #ifdef AFS_DARWIN14_ENV
136         /* this is probably safe for all versions, but testing is hard */
137         sleep(event, PVFS);
138 #else
139         assert_wait((event_t)event, 0);
140         thread_block(0);
141 #endif
142         AFS_GLOCK();
143     }
144     relevent(evp);
145 }
146
147 int afs_osi_SleepSig(char *event)
148 {
149     afs_osi_Sleep(event);
150     return 0;
151 }
152
153 /* osi_TimedSleep
154  * 
155  * Arguments:
156  * event - event to sleep on
157  * ams --- max sleep time in milliseconds
158  * aintok - 1 if should sleep interruptibly
159  *
160  * Returns 0 if timeout and EINTR if signalled.
161  */
162 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
163 {
164     int code = 0;
165     struct afs_event *evp;
166     int ticks,seq;
167     int prio;
168
169     ticks = ( ams * afs_hz )/1000;
170
171
172     evp = afs_getevent(event);
173     seq=evp->seq;
174     AFS_GUNLOCK();
175 #ifdef AFS_DARWIN14_ENV
176     /* this is probably safe for all versions, but testing is hard. */
177     /* using tsleep instead of assert_wait/thread_set_timer/thread_block
178        allows shutdown to work in 1.4 */
179     /* lack of PCATCH does *not* prevent signal delivery, neither does 
180        a low priority. We would need to deal with ERESTART here if we 
181        wanted to mess with p->p_sigmask, and messing with p_sigignore is
182        not the way to go.... (someone correct me if I'm wrong)
183     */
184     if (aintok)
185        prio=PCATCH|PPAUSE;
186     else
187        prio=PVFS;
188     code=tsleep(event, prio, "afs_osi_TimedSleep", ticks);
189 #else 
190     assert_wait((event_t)event, aintok ? THREAD_ABORTSAFE : THREAD_UNINT);
191     thread_set_timer(ticks, NSEC_PER_SEC / hz);
192     thread_block(0);
193     code=0;
194 #endif
195     AFS_GLOCK();
196     if (seq == evp->seq)
197         code = EINTR;
198     
199     relevent(evp);
200     return code;
201 }
202
203
204 void afs_osi_Wakeup(char *event)
205 {
206     struct afs_event *evp;
207     
208     evp = afs_getevent(event);
209     if (evp->refcount > 1) {
210         evp->seq++;    
211 #ifdef AFS_DARWIN14_ENV
212     /* this is probably safe for all versions, but testing is hard. */
213         wakeup(event);
214 #else
215         thread_wakeup((event_t)event);
216 #endif
217     }
218     relevent(evp);
219 }