Block all signals while sleeping; this prevents 100% cpu usage
[openafs.git] / src / afs / LINUX / 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
21 #if defined(AFS_GLOBAL_SUNLOCK)
22 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
23 #endif
24
25 void afs_osi_Wakeup(char *event);
26 void afs_osi_Sleep(char *event);
27
28 static char waitV, dummyV;
29
30 #if ! defined(AFS_GLOBAL_SUNLOCK)
31
32 /* call procedure aproc with arock as an argument, in ams milliseconds */
33 static struct timer_list *afs_osi_CallProc(void *aproc, void *arock, int ams)
34 {
35     struct timer_list *timer = NULL;
36     
37     timer = (struct timer_list*)osi_Alloc(sizeof(struct timer_list));
38     if (timer) {
39         init_timer(timer);
40         timer->expires = (ams*afs_hz)/1000 + 1;
41         timer->data = (unsigned long)arock;
42         timer->function = aproc;
43         add_timer(timer);
44     }
45     return timer;
46 }
47
48 /* cancel a timeout, whether or not it has already occurred */
49 static int afs_osi_CancelProc(struct timer_list *timer)
50 {
51     if (timer) {
52         del_timer(timer);
53         osi_Free(timer, sizeof(struct timer_list));
54     }
55     return 0;
56 }
57
58 static AfsWaitHack()
59 {
60     AFS_STATCNT(WaitHack);
61     wakeup(&waitV);
62 }
63
64 #endif
65
66 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
67 {
68     AFS_STATCNT(osi_InitWaitHandle);
69     achandle->proc = (caddr_t) 0;
70 }
71
72 /* cancel osi_Wait */
73 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
74 {
75     caddr_t proc;
76
77     AFS_STATCNT(osi_CancelWait);
78     proc = achandle->proc;
79     if (proc == 0) return;
80     achandle->proc = (caddr_t) 0;   /* so dude can figure out he was signalled */
81     afs_osi_Wakeup(&waitV);
82 }
83
84 /* afs_osi_Wait
85  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
86  * Returns 0 if timeout and EINTR if signalled.
87  */
88 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
89 {
90     int code;
91     afs_int32 endTime, tid;
92     struct timer_list *timer = NULL;
93
94     AFS_STATCNT(osi_Wait);
95     endTime = osi_Time() + (ams/1000);
96     if (ahandle)
97         ahandle->proc = (caddr_t) current;
98
99     AFS_ASSERT_GLOCK();
100     do {
101 #if     defined(AFS_GLOBAL_SUNLOCK)
102         code = osi_TimedSleep(&waitV, ams, 1);
103         if (code == EINTR) {
104                 if (aintok) 
105                     return EINTR;
106         }
107 #else
108         timer = afs_osi_CallProc(AfsWaitHack, (char *) current, ams);
109         afs_osi_Sleep(&waitV);
110         afs_osi_CancelProc(timer);
111 #endif /* AFS_GLOBAL_SUNLOCK */
112         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
113             /* we've been signalled */
114             return EINTR;
115         }
116     } while (osi_Time() < endTime);
117     return 0;
118 }
119
120
121
122
123 typedef struct afs_event {
124     struct afs_event *next;     /* next in hash chain */
125     char *event;                /* lwp event: an address */
126     int refcount;               /* Is it in use? */
127     int seq;                    /* Sequence number: this is incremented
128                                    by wakeup calls; wait will not return until
129                                    it changes */
130 #if defined(AFS_LINUX24_ENV)
131     wait_queue_head_t cond;
132 #else
133     struct wait_queue *cond;
134 #endif
135 } afs_event_t;
136
137 #define HASHSIZE 128
138 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
139 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
140 int afs_evhashcnt = 0;
141
142 /* Get and initialize event structure corresponding to lwp event (i.e. address)
143  * */
144 static afs_event_t *afs_getevent(char *event)
145 {
146     afs_event_t *evp, *newp = 0;
147     int hashcode;
148
149     AFS_ASSERT_GLOCK();
150     hashcode = afs_evhash(event);
151     evp = afs_evhasht[hashcode];
152     while (evp) {
153         if (evp->event == event) {
154             evp->refcount++;
155             return evp;
156         }
157         if (evp->refcount == 0)
158             newp = evp;
159         evp = evp->next;
160     }
161     if (!newp)
162         return NULL;
163
164     newp->event = event;
165     newp->refcount = 1;
166     return newp;
167 }
168
169 /* afs_addevent -- allocates a new event for the address.  It isn't returned;
170  *     instead, afs_getevent should be called again.  Thus, the real effect of
171  *     this routine is to add another event to the hash bucket for this
172  *     address.
173  *
174  * Locks:
175  *     Called with GLOCK held. However the function might drop
176  *     GLOCK when it calls osi_AllocSmallSpace for allocating
177  *     a new event (In Linux, the allocator drops GLOCK to avoid
178  *     a deadlock).
179  */
180
181 static void afs_addevent(char *event)
182 {
183     int hashcode;
184     afs_event_t *newp;
185     
186     AFS_ASSERT_GLOCK();
187     hashcode = afs_evhash(event);
188     newp = osi_AllocSmallSpace(sizeof(afs_event_t));
189     afs_evhashcnt++;
190     newp->next = afs_evhasht[hashcode];
191     afs_evhasht[hashcode] = newp;
192 #if defined(AFS_LINUX24_ENV)
193     init_waitqueue_head(&newp->cond);
194 #else
195     init_waitqueue(&newp->cond);
196 #endif
197     newp->seq = 0;
198     newp->event = &dummyV;  /* Dummy address for new events */
199     newp->refcount = 0;
200 }
201
202
203 /* Release the specified event */
204 #define relevent(evp) ((evp)->refcount--)
205
206 /* afs_osi_Sleep -- waits for an event to be notified. */
207
208 void afs_osi_Sleep(char *event)
209 {
210     struct afs_event *evp;
211     int seq;
212
213     evp = afs_getevent(event);
214     if (!evp) {
215         /* Can't block because allocating a new event would require dropping
216          * the GLOCK, which may cause us to miss the wakeup.  So call the
217          * allocator then return immediately.  We'll find the new event next
218          * time around without dropping the GLOCK. */
219         afs_addevent(event);
220         return;
221     }
222
223     seq = evp->seq;
224
225     while (seq == evp->seq) {
226         sigset_t saved_set;
227
228         AFS_ASSERT_GLOCK();
229         AFS_GUNLOCK();
230         spin_lock_irq(&current->sigmask_lock);
231         saved_set = current->blocked;
232         sigfillset(&current->blocked);
233         recalc_sigpending(current);
234         spin_unlock_irq(&current->sigmask_lock);
235
236         interruptible_sleep_on(&evp->cond);
237
238         spin_lock_irq(&current->sigmask_lock);
239         current->blocked = saved_set;
240         recalc_sigpending(current);
241         spin_unlock_irq(&current->sigmask_lock);
242         AFS_GLOCK();
243     }
244     relevent(evp);
245 }
246
247 /* osi_TimedSleep
248  * 
249  * Arguments:
250  * event - event to sleep on
251  * ams --- max sleep time in milliseconds
252  * aintok - 1 if should sleep interruptibly
253  *
254  * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
255  * have raced.
256  */
257 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
258 {
259     long t = ams * HZ / 1000;
260     struct afs_event *evp;
261
262     evp = afs_getevent(event);
263     if (!evp) {
264         /* Can't block because allocating a new event would require dropping
265          * the GLOCK, which may cause us to miss the wakeup.  So call the
266          * allocator then return immediately.  We'll find the new event next
267          * time around without dropping the GLOCK. */
268         afs_addevent(event);
269         return EAGAIN;
270     }
271
272     AFS_GUNLOCK();
273     if (aintok)
274         t = interruptible_sleep_on_timeout(&evp->cond, t);
275     else
276         t = sleep_on_timeout(&evp->cond, t);
277     AFS_GLOCK();
278
279     relevent(evp);
280
281     return t ? EINTR : 0;
282 }
283
284
285 void afs_osi_Wakeup(char *event)
286 {
287     struct afs_event *evp;
288
289     evp = afs_getevent(event);
290     if (!evp)                          /* No sleepers */
291         return;
292
293     if (evp->refcount > 1) {
294         evp->seq++;    
295         wake_up(&evp->cond);
296     }
297     relevent(evp);
298 }