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"
21 #include "rx/rx_kcommon.h"
22 #include "rx_kmutex.h"
23 #include "rx/rx_kernel.h"
27 void afs_mutex_init(afs_kmutex_t *l)
29 #if defined(AFS_LINUX24_ENV)
37 void afs_mutex_enter(afs_kmutex_t *l)
41 osi_Panic("mutex_enter: 0x%x held by %d", l, l->owner);
42 l->owner = current->pid;
45 int afs_mutex_tryenter(afs_kmutex_t *l)
47 if (down_trylock(&l->sem))
49 l->owner = current->pid;
53 void afs_mutex_exit(afs_kmutex_t *l)
55 if (l->owner != current->pid)
56 osi_Panic("mutex_exit: 0x%x held by %d",
62 /* CV_WAIT and CV_TIMEDWAIT sleep until the specified event occurs, or, in the
63 * case of CV_TIMEDWAIT, until the specified timeout occurs.
64 * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
65 * can wake up, even if all signals are blocked
66 * - TODO: handle signals correctly by passing an indication back to the
67 * caller that the wait has been interrupted and the stack should be cleaned
68 * up preparatory to signal delivery
70 int afs_cv_wait(afs_kcondvar_t *cv, afs_kmutex_t *l, int sigok)
72 int isAFSGlocked = ISAFS_GLOCK();
74 #ifdef DECLARE_WAITQUEUE
75 DECLARE_WAITQUEUE(wait, current);
77 struct wait_queue wait = { current, NULL };
80 add_wait_queue(cv, &wait);
81 set_current_state(TASK_INTERRUPTIBLE);
83 if (isAFSGlocked) AFS_GUNLOCK();
87 spin_lock_irq(¤t->sigmask_lock);
88 saved_set = current->blocked;
89 sigfillset(¤t->blocked);
90 recalc_sigpending(current);
91 spin_unlock_irq(¤t->sigmask_lock);
95 remove_wait_queue(cv, &wait);
98 spin_lock_irq(¤t->sigmask_lock);
99 current->blocked = saved_set;
100 recalc_sigpending(current);
101 spin_unlock_irq(¤t->sigmask_lock);
104 if (isAFSGlocked) AFS_GLOCK();
107 return (sigok && signal_pending(current)) ? EINTR : 0;
110 void afs_cv_timedwait(afs_kcondvar_t *cv, afs_kmutex_t *l, int waittime)
112 int isAFSGlocked = ISAFS_GLOCK();
113 long t = waittime * HZ / 1000;
114 #ifdef DECLARE_WAITQUEUE
115 DECLARE_WAITQUEUE(wait, current);
117 struct wait_queue wait = { current, NULL };
120 add_wait_queue(cv, &wait);
121 set_current_state(TASK_INTERRUPTIBLE);
123 if (isAFSGlocked) AFS_GUNLOCK();
126 t = schedule_timeout(t);
127 remove_wait_queue(cv, &wait);
129 if (isAFSGlocked) AFS_GLOCK();