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"
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 */
20 #include <linux/freezer.h>
22 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
24 static char waitV, dummyV;
27 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
29 AFS_STATCNT(osi_InitWaitHandle);
30 achandle->proc = (caddr_t) 0;
35 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
39 AFS_STATCNT(osi_CancelWait);
40 proc = achandle->proc;
43 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
44 afs_osi_Wakeup(&waitV);
48 * Waits for data on ahandle, or ams ms later. ahandle may be null.
49 * Returns 0 if timeout and EINTR if signalled.
52 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
57 AFS_STATCNT(osi_Wait);
58 endTime = osi_Time() + (ams / 1000);
60 ahandle->proc = (caddr_t) current;
64 code = osi_TimedSleep(&waitV, ams, 1);
67 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
68 /* we've been signalled */
71 } 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 #if defined(AFS_LINUX24_ENV)
86 wait_queue_head_t cond;
88 struct wait_queue *cond;
93 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
94 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
95 int afs_evhashcnt = 0;
97 /* Get and initialize event structure corresponding to lwp event (i.e. address)
100 afs_getevent(char *event)
102 afs_event_t *evp, *newp = 0;
106 hashcode = afs_evhash(event);
107 evp = afs_evhasht[hashcode];
109 if (evp->event == event) {
113 if (evp->refcount == 0)
125 /* afs_addevent -- allocates a new event for the address. It isn't returned;
126 * instead, afs_getevent should be called again. Thus, the real effect of
127 * this routine is to add another event to the hash bucket for this
131 * Called with GLOCK held. However the function might drop
132 * GLOCK when it calls osi_AllocSmallSpace for allocating
133 * a new event (In Linux, the allocator drops GLOCK to avoid
138 afs_addevent(char *event)
144 hashcode = afs_evhash(event);
145 newp = osi_linux_alloc(sizeof(afs_event_t), 0);
147 newp->next = afs_evhasht[hashcode];
148 afs_evhasht[hashcode] = newp;
149 #if defined(AFS_LINUX24_ENV)
150 init_waitqueue_head(&newp->cond);
152 init_waitqueue(&newp->cond);
155 newp->event = &dummyV; /* Dummy address for new events */
159 #ifndef set_current_state
160 #define set_current_state(x) current->state = (x);
163 /* Release the specified event */
164 #define relevent(evp) ((evp)->refcount--)
168 * Waits for an event to be notified, returning early if a signal
169 * is received. Returns EINTR if signaled, and 0 otherwise.
172 afs_osi_SleepSig(void *event)
174 struct afs_event *evp;
176 #ifdef DECLARE_WAITQUEUE
177 DECLARE_WAITQUEUE(wait, current);
179 struct wait_queue wait = { current, NULL };
182 evp = afs_getevent(event);
185 evp = afs_getevent(event);
191 add_wait_queue(&evp->cond, &wait);
192 while (seq == evp->seq) {
193 set_current_state(TASK_INTERRUPTIBLE);
197 #ifdef AFS_LINUX26_ENV
201 current->flags & PF_FREEZE
203 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
206 test_ti_thread_flag(current->thread_info, TIF_FREEZE)
210 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
211 refrigerator(PF_FREEZE);
218 if (signal_pending(current)) {
223 remove_wait_queue(&evp->cond, &wait);
224 set_current_state(TASK_RUNNING);
230 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
231 * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
232 * can wake up, even if all signals are blocked
233 * - TODO: handle signals correctly by passing an indication back to the
234 * caller that the wait has been interrupted and the stack should be cleaned
235 * up preparatory to signal delivery
238 afs_osi_Sleep(void *event)
243 saved_set = current->blocked;
244 sigfillset(¤t->blocked);
245 RECALC_SIGPENDING(current);
248 afs_osi_SleepSig(event);
251 current->blocked = saved_set;
252 RECALC_SIGPENDING(current);
259 * event - event to sleep on
260 * ams --- max sleep time in milliseconds
261 * aintok - 1 if should sleep interruptibly
263 * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
267 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
270 long ticks = (ams * HZ / 1000) + 1;
271 struct afs_event *evp;
272 #ifdef DECLARE_WAITQUEUE
273 DECLARE_WAITQUEUE(wait, current);
275 struct wait_queue wait = { current, NULL };
278 evp = afs_getevent(event);
281 evp = afs_getevent(event);
284 add_wait_queue(&evp->cond, &wait);
285 set_current_state(TASK_INTERRUPTIBLE);
286 /* always sleep TASK_INTERRUPTIBLE to keep load average
287 * from artifically increasing. */
291 if (schedule_timeout(ticks))
294 schedule_timeout(ticks);
295 #ifdef AFS_LINUX26_ENV
299 current->flags & PF_FREEZE
301 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
304 test_ti_thread_flag(current->thread_info, TIF_FREEZE)
308 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
309 refrigerator(PF_FREEZE);
317 remove_wait_queue(&evp->cond, &wait);
318 set_current_state(TASK_RUNNING);
327 afs_osi_Wakeup(void *event)
330 struct afs_event *evp;
332 evp = afs_getevent(event);
333 if (!evp) /* No sleepers */
336 if (evp->refcount > 1) {