Whamcloud - gitweb
LU-56 libcfs: implementation of cpu partition
[fs/lustre-release.git] / libcfs / libcfs / winnt / winnt-lock.c
index 2b57e7f..0d79b53 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -26,7 +24,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  */
 /*
 #include <libcfs/libcfs.h>
 
 
-#if _X86_
+#if defined(_X86_)
 
 void __declspec (naked) FASTCALL
-atomic_add(
+cfs_atomic_add(
     int i,
-    atomic_t *v
+    cfs_atomic_t *v
     )
 {
     // ECX = i
@@ -58,9 +56,9 @@ atomic_add(
 }
 
 void __declspec (naked) FASTCALL
-atomic_sub(
+cfs_atomic_sub(
     int i,
-    atomic_t *v
+    cfs_atomic_t *v
    ) 
 {
     // ECX = i
@@ -73,8 +71,8 @@ atomic_sub(
 }
 
 void __declspec (naked) FASTCALL
-atomic_inc(
-    atomic_t *v
+cfs_atomic_inc(
+    cfs_atomic_t *v
     )
 {
     //InterlockedIncrement((PULONG)(&((v)->counter)));
@@ -88,8 +86,8 @@ atomic_inc(
 }
 
 void __declspec (naked) FASTCALL
-atomic_dec(
-    atomic_t *v
+cfs_atomic_dec(
+    cfs_atomic_t *v
     )
 {
     // ECX = v ; [ECX][0] = v->counter
@@ -101,9 +99,9 @@ atomic_dec(
 }
 
 int __declspec (naked) FASTCALL 
-atomic_sub_and_test(
+cfs_atomic_sub_and_test(
     int i,
-    atomic_t *v
+    cfs_atomic_t *v
     )
 {
 
@@ -119,8 +117,8 @@ atomic_sub_and_test(
 }
 
 int __declspec (naked) FASTCALL
-atomic_inc_and_test(
-    atomic_t *v
+cfs_atomic_inc_and_test(
+    cfs_atomic_t *v
     )
 {
     // ECX = v ; [ECX][0] = v->counter
@@ -134,8 +132,8 @@ atomic_inc_and_test(
 }
 
 int __declspec (naked) FASTCALL
-atomic_dec_and_test(
-    atomic_t *v
+cfs_atomic_dec_and_test(
+    cfs_atomic_t *v
     )
 {
     // ECX = v ; [ECX][0] = v->counter
@@ -148,46 +146,46 @@ atomic_dec_and_test(
     }
 }
 
-#else
+#elif defined(_AMD64_)
 
 void FASTCALL
-atomic_add(
+cfs_atomic_add(
     int i,
-    atomic_t *v
+    cfs_atomic_t *v
     )
 {
     InterlockedExchangeAdd( (PULONG)(&((v)->counter)) , (LONG) (i));
 }
 
 void FASTCALL
-atomic_sub(
+cfs_atomic_sub(
     int i,
-    atomic_t *v
+    cfs_atomic_t *v
    ) 
 {
     InterlockedExchangeAdd( (PULONG)(&((v)->counter)) , (LONG) (-1*i));
 }
 
 void FASTCALL
-atomic_inc(
-    atomic_t *v
+cfs_atomic_inc(
+    cfs_atomic_t *v
     )
 {
    InterlockedIncrement((PULONG)(&((v)->counter)));
 }
 
 void FASTCALL
-atomic_dec(
-    atomic_t *v
+cfs_atomic_dec(
+    cfs_atomic_t *v
     )
 {
     InterlockedDecrement((PULONG)(&((v)->counter)));
 }
 
 int FASTCALL 
-atomic_sub_and_test(
+cfs_atomic_sub_and_test(
     int i,
-    atomic_t *v
+    cfs_atomic_t *v
     )
 {
     int counter, result;
@@ -206,8 +204,8 @@ atomic_sub_and_test(
 }
 
 int FASTCALL
-atomic_inc_and_test(
-    atomic_t *v
+cfs_atomic_inc_and_test(
+    cfs_atomic_t *v
     )
 {
     int counter, result;
@@ -226,8 +224,8 @@ atomic_inc_and_test(
 }
 
 int FASTCALL
-atomic_dec_and_test(
-    atomic_t *v
+cfs_atomic_dec_and_test(
+    cfs_atomic_t *v
     )
 {
     int counter, result;
@@ -235,7 +233,7 @@ atomic_dec_and_test(
     do {
 
         counter = v->counter;
-        result = counter + 1;
+        result = counter - 1;
 
     } while ( InterlockedCompareExchange(
                 &(v->counter),
@@ -245,8 +243,62 @@ atomic_dec_and_test(
     return (result == 0);
 }
 
+#else
+
+#error CPU arch type isn't specified.
+
 #endif
 
+/**
+ * atomic_add_return - add integer and return
+ * \param v pointer of type atomic_t
+ * \param i integer value to add
+ *
+ * Atomically adds \a i to \a v and returns \a i + \a v
+ */
+int FASTCALL cfs_atomic_add_return(int i, cfs_atomic_t *v)
+{
+    int counter, result;
+
+    do {
+
+        counter = v->counter;
+        result = counter + i;
+
+    } while ( InterlockedCompareExchange(
+                &(v->counter),
+                result,
+                counter) !=  counter);
+
+    return result;
+
+}
+
+/**
+ * atomic_sub_return - subtract integer and return
+ * \param v pointer of type atomic_t
+ * \param i integer value to subtract
+ *
+ * Atomically subtracts \a i from \a v and returns \a v - \a i
+ */
+int FASTCALL cfs_atomic_sub_return(int i, cfs_atomic_t *v)
+{
+       return cfs_atomic_add_return(-i, v);
+}
+
+int FASTCALL cfs_atomic_dec_and_lock(cfs_atomic_t *v, cfs_spinlock_t *lock)
+{
+    if (cfs_atomic_read(v) != 1) {
+        return 0;
+    }
+
+       cfs_spin_lock(lock);
+       if (cfs_atomic_dec_and_test(v))
+               return 1;
+       cfs_spin_unlock(lock);
+       return 0;
+}
+
 
 /*
  * rw spinlock
@@ -254,19 +306,19 @@ atomic_dec_and_test(
 
 
 void
-rwlock_init(rwlock_t * rwlock)
+cfs_rwlock_init(cfs_rwlock_t * rwlock)
 {
-    spin_lock_init(&rwlock->guard);
+    cfs_spin_lock_init(&rwlock->guard);
     rwlock->count = 0;
 }
 
 void
-rwlock_fini(rwlock_t * rwlock)
+cfs_rwlock_fini(cfs_rwlock_t * rwlock)
 {
 }
 
 void
-read_lock(rwlock_t * rwlock)
+cfs_read_lock(cfs_rwlock_t * rwlock)
 {
     cfs_task_t * task = cfs_current();
     PTASK_SLOT   slot = NULL;
@@ -283,18 +335,18 @@ read_lock(rwlock_t * rwlock)
     slot->irql = KeRaiseIrqlToDpcLevel();
 
     while (TRUE) {
-           spin_lock(&rwlock->guard);
+           cfs_spin_lock(&rwlock->guard);
         if (rwlock->count >= 0)
             break;
-        spin_unlock(&rwlock->guard);
+        cfs_spin_unlock(&rwlock->guard);
     }
 
        rwlock->count++;
-       spin_unlock(&rwlock->guard);
+       cfs_spin_unlock(&rwlock->guard);
 }
 
 void
-read_unlock(rwlock_t * rwlock)
+cfs_read_unlock(cfs_rwlock_t * rwlock)
 {
     cfs_task_t * task = cfs_current();
     PTASK_SLOT   slot = NULL;
@@ -308,19 +360,19 @@ read_unlock(rwlock_t * rwlock)
     slot = CONTAINING_RECORD(task, TASK_SLOT, task);
     ASSERT(slot->Magic == TASKSLT_MAGIC);
    
-    spin_lock(&rwlock->guard);
+    cfs_spin_lock(&rwlock->guard);
        ASSERT(rwlock->count > 0);
     rwlock->count--;
     if (rwlock < 0) {
         cfs_enter_debugger();
     }
-       spin_unlock(&rwlock->guard);
+       cfs_spin_unlock(&rwlock->guard);
 
     KeLowerIrql(slot->irql);
 }
 
 void
-write_lock(rwlock_t * rwlock)
+cfs_write_lock(cfs_rwlock_t * rwlock)
 {
     cfs_task_t * task = cfs_current();
     PTASK_SLOT   slot = NULL;
@@ -337,18 +389,18 @@ write_lock(rwlock_t * rwlock)
     slot->irql = KeRaiseIrqlToDpcLevel();
 
     while (TRUE) {
-           spin_lock(&rwlock->guard);
+           cfs_spin_lock(&rwlock->guard);
         if (rwlock->count == 0)
             break;
-        spin_unlock(&rwlock->guard);
+        cfs_spin_unlock(&rwlock->guard);
     }
 
        rwlock->count = -1;
-       spin_unlock(&rwlock->guard);
+       cfs_spin_unlock(&rwlock->guard);
 }
 
 void
-write_unlock(rwlock_t * rwlock)
+cfs_write_unlock(cfs_rwlock_t * rwlock)
 {
     cfs_task_t * task = cfs_current();
     PTASK_SLOT   slot = NULL;
@@ -362,10 +414,10 @@ write_unlock(rwlock_t * rwlock)
     slot = CONTAINING_RECORD(task, TASK_SLOT, task);
     ASSERT(slot->Magic == TASKSLT_MAGIC);
    
-    spin_lock(&rwlock->guard);
+    cfs_spin_lock(&rwlock->guard);
        ASSERT(rwlock->count == -1);
     rwlock->count = 0;
-       spin_unlock(&rwlock->guard);
+       cfs_spin_unlock(&rwlock->guard);
 
     KeLowerIrql(slot->irql);
 }