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 test_ti_thread_flag(current->thread_info, TIF_FREEZE)
212 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
213 refrigerator(PF_FREEZE);
220 if (signal_pending(current)) {
225 remove_wait_queue(&evp->cond, &wait);
226 set_current_state(TASK_RUNNING);
232 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
233 * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
234 * can wake up, even if all signals are blocked
235 * - TODO: handle signals correctly by passing an indication back to the
236 * caller that the wait has been interrupted and the stack should be cleaned
237 * up preparatory to signal delivery
240 afs_osi_Sleep(void *event)
245 saved_set = current->blocked;
246 sigfillset(¤t->blocked);
247 RECALC_SIGPENDING(current);
250 afs_osi_SleepSig(event);
253 current->blocked = saved_set;
254 RECALC_SIGPENDING(current);
261 * event - event to sleep on
262 * ams --- max sleep time in milliseconds
263 * aintok - 1 if should sleep interruptibly
265 * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
269 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
272 long ticks = (ams * HZ / 1000) + 1;
273 struct afs_event *evp;
274 #ifdef DECLARE_WAITQUEUE
275 DECLARE_WAITQUEUE(wait, current);
277 struct wait_queue wait = { current, NULL };
280 evp = afs_getevent(event);
283 evp = afs_getevent(event);
286 add_wait_queue(&evp->cond, &wait);
287 set_current_state(TASK_INTERRUPTIBLE);
288 /* always sleep TASK_INTERRUPTIBLE to keep load average
289 * from artifically increasing. */
293 if (schedule_timeout(ticks))
296 schedule_timeout(ticks);
297 #ifdef AFS_LINUX26_ENV
301 current->flags & PF_FREEZE
303 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
306 test_ti_thread_flag(current->thread_info, TIF_FREEZE)
310 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
311 refrigerator(PF_FREEZE);
319 remove_wait_queue(&evp->cond, &wait);
320 set_current_state(TASK_RUNNING);
329 afs_osi_Wakeup(void *event)
332 struct afs_event *evp;
334 evp = afs_getevent(event);
335 if (!evp) /* No sleepers */
338 if (evp->refcount > 1) {