linux-updates-20060309
[openafs.git] / src / rx / LINUX / rx_kmutex.h
index fa5e15a..6ea4faf 100644 (file)
@@ -16,7 +16,7 @@
 #ifndef RX_KMUTEX_H_
 #define RX_KMUTEX_H_
 
-#include "../rx/rx_kernel.h"   /* for osi_Panic() */
+#include "rx/rx_kernel.h"      /* for osi_Panic() */
 
 /* AFS_GLOBAL_RXLOCK_KERNEL is defined so that the busy tq code paths are
  * used. The thread can sleep when sending packets.
 #define        AFS_GLOBAL_RXLOCK_KERNEL 1
 
 
-#ifdef CONFIG_SMP
 #define RX_ENABLE_LOCKS 1
 
 #ifndef _LINUX_CODA_FS_I
 #define _LINUX_CODA_FS_I
-struct coda_inode_info {};
+struct coda_inode_info {
+};
+#endif
+#include <linux/version.h>
+#include <linux/wait.h>
+#include <linux/sched.h>
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
+#include <linux/mutex.h>
+#else
+#include <asm/semaphore.h>
 #endif
-#include "linux/wait.h"
-#include "linux/sched.h"
 
 typedef struct afs_kmutex {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
+    struct mutex mutex;
+#else
     struct semaphore sem;
+#endif
     int owner;
 } afs_kmutex_t;
 
-#if defined(AFS_LINUX24_ENV)
-typedef wait_queue_head_t afs_kcondvar_t;
-#else
-typedef struct wait_queue *afs_kcondvar_t;
+#ifndef set_current_state
+#define set_current_state(X) current->state=X
 #endif
 
-static inline int MUTEX_ISMINE(afs_kmutex_t *l)
-{
-    return l->owner == current->pid;
-}
-
-
-static inline void afs_mutex_init(afs_kmutex_t *l)
-{
+typedef struct afs_kcondvar {
+    int seq;
 #if defined(AFS_LINUX24_ENV)
-    init_MUTEX(&l->sem);
+    wait_queue_head_t waitq;
 #else
-    l->sem = MUTEX;
+    struct wait_queue *waitq;
 #endif
-    l->owner = 0;
-}
-#define MUTEX_INIT(a,b,c,d) afs_mutex_init(a)
-
-#define MUTEX_DESTROY(a)
+} afs_kcondvar_t;
 
-static inline void MUTEX_ENTER(afs_kmutex_t *l)
+static inline int
+MUTEX_ISMINE(afs_kmutex_t * l)
 {
-    down(&l->sem);
-    if (l->owner)
-       osi_Panic("mutex_enter: 0x%x held by %d", l, l->owner);
-    l->owner = current->pid;
-}
-                                                             
-/* And how to do a good tryenter? */
-static inline int MUTEX_TRYENTER(afs_kmutex_t *l)
-{
-    if (!l->owner) {
-       MUTEX_ENTER(l);
-       return 1;
-    }
-    else
-       return 0;
+    return l->owner == current->pid;
 }
 
-static inline void MUTEX_EXIT(afs_kmutex_t *l)
-{
-    if (l->owner != current->pid)
-       osi_Panic("mutex_exit: 0x%x held by %d",
-                 l, l->owner);
-    l->owner = 0;
-    up(&l->sem);
-}
+#define MUTEX_INIT(a,b,c,d)    afs_mutex_init(a)
+#define MUTEX_DESTROY(a)
+#define MUTEX_ENTER            afs_mutex_enter
+#define MUTEX_TRYENTER         afs_mutex_tryenter
+#define MUTEX_EXIT             afs_mutex_exit
 
 #if defined(AFS_LINUX24_ENV)
-#define CV_INIT(cv,b,c,d) init_waitqueue_head((wait_queue_head_t *)(cv))
+#define CV_INIT(cv,b,c,d)       do { (cv)->seq = 0; init_waitqueue_head(&(cv)->waitq); } while (0)
 #else
-#define CV_INIT(cv,b,c,d) init_waitqueue((struct wait_queue**)(cv))
+#define CV_INIT(cv,b,c,d)      do { (cv)->seq = 0; init_waitqueue(&(cv)->waitq); } while (0)
 #endif
 #define CV_DESTROY(cv)
+#define CV_WAIT_SIG(cv, m)     afs_cv_wait(cv, m, 1)
+#define CV_WAIT(cv, m)         afs_cv_wait(cv, m, 0)
+#define CV_TIMEDWAIT           afs_cv_timedwait
 
-/* CV_WAIT and CV_TIMEDWAIT rely on the fact that the Linux kernel has
- * a global lock. Thus we can safely drop our locks before calling the
- * kernel sleep services.
- */
-static inline int CV_WAIT(afs_kcondvar_t *cv, afs_kmutex_t *l)
-{
-    int isAFSGlocked = ISAFS_GLOCK(); 
-
-    if (isAFSGlocked) AFS_GUNLOCK();
-    MUTEX_EXIT(l);
-
-#if defined(AFS_LINUX24_ENV)
-    interruptible_sleep_on((wait_queue_head_t *)cv);
-#else
-    interruptible_sleep_on((struct wait_queue**)cv);
-#endif
-
-    MUTEX_ENTER(l);
-    if (isAFSGlocked) AFS_GLOCK();
-
-    return 0;
-}
-
-static inline int CV_TIMEDWAIT(afs_kcondvar_t *cv, afs_kmutex_t *l, int waittime)
-{
-    int isAFSGlocked = ISAFS_GLOCK();
-    long t = waittime * HZ / 1000;
-
-    if (isAFSGlocked) AFS_GUNLOCK();
-    MUTEX_EXIT(l);
-    
-#if defined(AFS_LINUX24_ENV)
-    t = interruptible_sleep_on_timeout((wait_queue_head_t *)cv, t);
-#else
-    t = interruptible_sleep_on_timeout((struct wait_queue**)cv, t);
-#endif
-    
-    MUTEX_ENTER(l);
-    if (isAFSGlocked) AFS_GLOCK();
-
-    return 0;
-}
-
+#define CV_SIGNAL(cv)          do { ++(cv)->seq; wake_up(&(cv)->waitq); } while (0)
 #if defined(AFS_LINUX24_ENV)
-#define CV_SIGNAL(cv) wake_up((wait_queue_head_t *)cv)
-#define CV_BROADCAST(cv) wake_up((wait_queue_head_t *)cv)
-#else
-#define CV_SIGNAL(cv) wake_up((struct wait_queue**)cv)
-#define CV_BROADCAST(cv) wake_up((struct wait_queue**)cv)
-#endif
-
+#define CV_BROADCAST(cv)       do { ++(cv)->seq; wake_up_all(&(cv)->waitq); } while (0)
 #else
-
-#define MUTEX_ISMINE(a)
-#define osirx_AssertMine(addr, msg)
-
-#define MUTEX_DESTROY(a)
-#define MUTEX_ENTER(a)
-#define MUTEX_TRYENTER(a) 1
-#define MUTEX_EXIT(a)  
-#define MUTEX_INIT(a,b,c,d) 
-#define CV_INIT(a,b,c,d)
-#define CV_DESTROY(a)
+#define CV_BROADCAST(cv)       do { ++(cv)->seq; wake_up(&(cv)->waitq); } while (0)
 #endif
 
-/* Since we're using the RX listener daemon, we don't need to hold off
- * interrupts.
- */
-#define SPLVAR
-#define NETPRI
-#define USERPRI
-
 #endif /* RX_KMUTEX_H_ */