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
11 * rx_kmutex.c - mutex and condition variable macros for kernel environment.
13 * Linux implementation.
16 #include <afsconfig.h>
17 #include "afs/param.h"
20 #include "rx/rx_kcommon.h"
21 #include "rx_kmutex.h"
22 #include "rx/rx_kernel.h"
25 afs_mutex_init(afs_kmutex_t * l)
27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
28 mutex_init(&l->mutex);
29 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
38 afs_mutex_enter(afs_kmutex_t * l)
40 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
41 mutex_lock(&l->mutex);
46 osi_Panic("mutex_enter: 0x%lx held by %d", (unsigned long)l, l->owner);
47 l->owner = current->pid;
51 afs_mutex_tryenter(afs_kmutex_t * l)
53 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
54 if (mutex_trylock(&l->mutex) == 0)
56 if (down_trylock(&l->sem))
59 l->owner = current->pid;
64 afs_mutex_exit(afs_kmutex_t * l)
66 if (l->owner != current->pid)
67 osi_Panic("mutex_exit: 0x%lx held by %d", (unsigned long)l, l->owner);
69 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
70 mutex_unlock(&l->mutex);
76 /* CV_WAIT and CV_TIMEDWAIT sleep until the specified event occurs, or, in the
77 * case of CV_TIMEDWAIT, until the specified timeout occurs.
78 * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
79 * can wake up, even if all signals are blocked
80 * - TODO: handle signals correctly by passing an indication back to the
81 * caller that the wait has been interrupted and the stack should be cleaned
82 * up preparatory to signal delivery
85 afs_cv_wait(afs_kcondvar_t * cv, afs_kmutex_t * l, int sigok)
87 int seq, isAFSGlocked = ISAFS_GLOCK();
89 #ifdef DECLARE_WAITQUEUE
90 DECLARE_WAITQUEUE(wait, current);
92 struct wait_queue wait = { current, NULL };
94 sigemptyset(&saved_set);
97 set_current_state(TASK_INTERRUPTIBLE);
98 add_wait_queue(&cv->waitq, &wait);
106 saved_set = current->blocked;
107 sigfillset(¤t->blocked);
108 RECALC_SIGPENDING(current);
112 while(seq == cv->seq) {
117 current->flags & PF_FREEZE
119 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
122 #if defined(STRUCT_TASK_STRUCT_HAS_THREAD_INFO)
123 test_ti_thread_flag(current->thread_info, TIF_FREEZE)
125 test_ti_thread_flag(task_thread_info(current), TIF_FREEZE)
130 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
131 refrigerator(PF_FREEZE);
135 set_current_state(TASK_INTERRUPTIBLE);
139 remove_wait_queue(&cv->waitq, &wait);
140 set_current_state(TASK_RUNNING);
144 current->blocked = saved_set;
145 RECALC_SIGPENDING(current);
153 return (sigok && signal_pending(current)) ? EINTR : 0;
157 afs_cv_timedwait(afs_kcondvar_t * cv, afs_kmutex_t * l, int waittime)
159 int seq, isAFSGlocked = ISAFS_GLOCK();
160 long t = waittime * HZ / 1000;
161 #ifdef DECLARE_WAITQUEUE
162 DECLARE_WAITQUEUE(wait, current);
164 struct wait_queue wait = { current, NULL };
168 set_current_state(TASK_INTERRUPTIBLE);
169 add_wait_queue(&cv->waitq, &wait);
175 while(seq == cv->seq) {
176 t = schedule_timeout(t);
177 if (!t) /* timeout */
181 remove_wait_queue(&cv->waitq, &wait);
182 set_current_state(TASK_RUNNING);