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 #if defined(FREEZER_H_EXISTS)
21 #include <linux/freezer.h>
24 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
26 static char waitV, dummyV;
29 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
31 AFS_STATCNT(osi_InitWaitHandle);
32 achandle->proc = (caddr_t) 0;
37 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
41 AFS_STATCNT(osi_CancelWait);
42 proc = achandle->proc;
45 achandle->proc = (caddr_t) 0; /* so dude can figure out he was signalled */
46 afs_osi_Wakeup(&waitV);
50 * Waits for data on ahandle, or ams ms later. ahandle may be null.
51 * Returns 0 if timeout and EINTR if signalled.
54 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
59 AFS_STATCNT(osi_Wait);
60 endTime = osi_Time() + (ams / 1000);
62 ahandle->proc = (caddr_t) current;
66 code = osi_TimedSleep(&waitV, ams, 1);
69 if (ahandle && (ahandle->proc == (caddr_t) 0)) {
70 /* we've been signalled */
73 } while (osi_Time() < endTime);
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
87 #if defined(AFS_LINUX24_ENV)
88 wait_queue_head_t cond;
90 struct wait_queue *cond;
95 afs_event_t *afs_evhasht[HASHSIZE]; /* Hash table for events */
96 #define afs_evhash(event) (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
97 int afs_evhashcnt = 0;
99 /* Get and initialize event structure corresponding to lwp event (i.e. address)
102 afs_getevent(char *event)
104 afs_event_t *evp, *newp = 0;
108 hashcode = afs_evhash(event);
109 evp = afs_evhasht[hashcode];
111 if (evp->event == event) {
115 if (evp->refcount == 0)
127 /* afs_addevent -- allocates a new event for the address. It isn't returned;
128 * instead, afs_getevent should be called again. Thus, the real effect of
129 * this routine is to add another event to the hash bucket for this
133 * Called with GLOCK held. However the function might drop
134 * GLOCK when it calls osi_AllocSmallSpace for allocating
135 * a new event (In Linux, the allocator drops GLOCK to avoid
140 afs_addevent(char *event)
146 hashcode = afs_evhash(event);
147 newp = osi_linux_alloc(sizeof(afs_event_t), 0);
149 newp->next = afs_evhasht[hashcode];
150 afs_evhasht[hashcode] = newp;
151 #if defined(AFS_LINUX24_ENV)
152 init_waitqueue_head(&newp->cond);
154 init_waitqueue(&newp->cond);
157 newp->event = &dummyV; /* Dummy address for new events */
161 #ifndef set_current_state
162 #define set_current_state(x) current->state = (x);
165 /* Release the specified event */
166 #define relevent(evp) ((evp)->refcount--)
170 * Waits for an event to be notified, returning early if a signal
171 * is received. Returns EINTR if signaled, and 0 otherwise.
174 afs_osi_SleepSig(void *event)
176 struct afs_event *evp;
178 #ifdef DECLARE_WAITQUEUE
179 DECLARE_WAITQUEUE(wait, current);
181 struct wait_queue wait = { current, NULL };
184 evp = afs_getevent(event);
187 evp = afs_getevent(event);
193 add_wait_queue(&evp->cond, &wait);
194 while (seq == evp->seq) {
195 set_current_state(TASK_INTERRUPTIBLE);
199 #ifdef AFS_LINUX26_ENV
203 current->flags & PF_FREEZE
205 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
208 #if defined(STRUCT_TASK_STRUCT_HAS_THREAD_INFO)
209 test_ti_thread_flag(current->thread_info, TIF_FREEZE)
211 test_ti_thread_flag(task_thread_info(current), TIF_FREEZE)
216 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
217 refrigerator(PF_FREEZE);
224 if (signal_pending(current)) {
229 remove_wait_queue(&evp->cond, &wait);
230 set_current_state(TASK_RUNNING);
236 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
237 * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
238 * can wake up, even if all signals are blocked
239 * - TODO: handle signals correctly by passing an indication back to the
240 * caller that the wait has been interrupted and the stack should be cleaned
241 * up preparatory to signal delivery
244 afs_osi_Sleep(void *event)
249 saved_set = current->blocked;
250 sigfillset(¤t->blocked);
251 RECALC_SIGPENDING(current);
254 afs_osi_SleepSig(event);
257 current->blocked = saved_set;
258 RECALC_SIGPENDING(current);
265 * event - event to sleep on
266 * ams --- max sleep time in milliseconds
267 * aintok - 1 if should sleep interruptibly
269 * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
273 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
276 long ticks = (ams * HZ / 1000) + 1;
277 struct afs_event *evp;
278 #ifdef DECLARE_WAITQUEUE
279 DECLARE_WAITQUEUE(wait, current);
281 struct wait_queue wait = { current, NULL };
284 evp = afs_getevent(event);
287 evp = afs_getevent(event);
290 add_wait_queue(&evp->cond, &wait);
291 set_current_state(TASK_INTERRUPTIBLE);
292 /* always sleep TASK_INTERRUPTIBLE to keep load average
293 * from artifically increasing. */
297 if (schedule_timeout(ticks))
300 schedule_timeout(ticks);
301 #ifdef AFS_LINUX26_ENV
305 current->flags & PF_FREEZE
307 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
310 #if defined(STRUCT_TASK_STRUCT_HAS_THREAD_INFO)
311 test_ti_thread_flag(current->thread_info, TIF_FREEZE)
313 test_ti_thread_flag(task_thread_info(current), TIF_FREEZE)
318 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
319 refrigerator(PF_FREEZE);
327 remove_wait_queue(&evp->cond, &wait);
328 set_current_state(TASK_RUNNING);
337 afs_osi_Wakeup(void *event)
340 struct afs_event *evp;
342 evp = afs_getevent(event);
343 if (!evp) /* No sleepers */
346 if (evp->refcount > 1) {