Whamcloud - gitweb
LU-2600 osd-zfs: batched object accounting
[fs/lustre-release.git] / libcfs / libcfs / user-lock.c
index a6d9973..fcba2cd 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.
@@ -16,8 +14,8 @@
  * in the LICENSE file that accompanied this code).
  *
  * You should have received a copy of the GNU General Public License
- * version 2 along with this program; If not, see [sun.com URL with a
- * copy of GPLv2].
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * 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.
+ *
+ * Copyright (c) 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #ifndef __KERNEL__
 
-#include <stdlib.h>
 #include <libcfs/libcfs.h>
 
 /*
  * Optional debugging (magic stamping and checking ownership) can be added.
  */
 
-#if 0
 /*
  * spin_lock
  *
  * - spin_lock_init(x)
  * - spin_lock(x)
+ * - spin_lock_nested(x, subclass)
  * - spin_unlock(x)
  * - spin_trylock(x)
  *
 
 void spin_lock_init(spinlock_t *lock)
 {
-        LASSERT(lock != NULL);
-        (void)lock;
+       LASSERT(lock != NULL);
+       (void)lock;
 }
 
 void spin_lock(spinlock_t *lock)
 {
-        (void)lock;
+       (void)lock;
 }
 
 void spin_unlock(spinlock_t *lock)
 {
-        (void)lock;
+       (void)lock;
 }
 
 int spin_trylock(spinlock_t *lock)
 {
-        (void)lock;
+       (void)lock;
        return 1;
 }
 
 void spin_lock_bh_init(spinlock_t *lock)
 {
-        LASSERT(lock != NULL);
-        (void)lock;
+       LASSERT(lock != NULL);
+       (void)lock;
 }
 
 void spin_lock_bh(spinlock_t *lock)
 {
-        LASSERT(lock != NULL);
-        (void)lock;
+       LASSERT(lock != NULL);
+       (void)lock;
 }
 
 void spin_unlock_bh(spinlock_t *lock)
 {
-        LASSERT(lock != NULL);
-        (void)lock;
+       LASSERT(lock != NULL);
+       (void)lock;
 }
 
 /*
@@ -119,41 +118,33 @@ void spin_unlock_bh(spinlock_t *lock)
  * - __down(x)
  * - __up(x)
  */
-struct semaphore {};
 
 void sema_init(struct semaphore *s, int val)
 {
-        LASSERT(s != NULL);
-        (void)s;
-        (void)val;
+       LASSERT(s != NULL);
+       (void)s;
+       (void)val;
 }
 
 void __down(struct semaphore *s)
 {
-        LASSERT(s != NULL);
-        (void)s;
+       LASSERT(s != NULL);
+       (void)s;
 }
 
-void __up(struct semaphore *s)
+int __down_interruptible(struct semaphore *s)
 {
-        LASSERT(s != NULL);
-        (void)s;
+       LASSERT(s != NULL);
+       (void)s;
+       return 0;
 }
 
-/*
- * Mutex:
- *
- * - init_mutex(x)
- * - init_mutex_locked(x)
- * - mutex_up(x)
- * - mutex_down(x)
- */
-
-#define mutex_up(s)                    __up(s)
-#define mutex_down(s)                  __down(s)
+void __up(struct semaphore *s)
+{
+       LASSERT(s != NULL);
+       (void)s;
+}
 
-#define init_mutex(x)                  sema_init(x, 1)
-#define init_mutex_locked(x)           sema_init(x, 0)
 
 /*
  * Completion:
@@ -162,26 +153,59 @@ void __up(struct semaphore *s)
  * - complete(c)
  * - wait_for_completion(c)
  */
-struct completion {};
+static wait_handler_t wait_handler;
 
+void init_completion_module(wait_handler_t handler)
+{
+       wait_handler = handler;
+}
+
+int call_wait_handler(int timeout)
+{
+       if (!wait_handler)
+               return -ENOSYS;
+       return wait_handler(timeout);
+}
+
+#ifndef HAVE_LIBPTHREAD
 void init_completion(struct completion *c)
 {
-        LASSERT(c != NULL);
-        (void)c;
+       LASSERT(c != NULL);
+       c->done = 0;
+       init_waitqueue_head(&c->wait);
+}
+
+void fini_completion(struct completion *c)
+{
 }
 
 void complete(struct completion *c)
 {
-        LASSERT(c != NULL);
-        (void)c;
+       LASSERT(c != NULL);
+       c->done  = 1;
+       wake_up(&c->wait);
 }
 
 void wait_for_completion(struct completion *c)
 {
-        LASSERT(c != NULL);
-        (void)c;
+       LASSERT(c != NULL);
+       do {
+               if (call_wait_handler(1000) < 0)
+                       break;
+       } while (c->done == 0);
 }
 
+int wait_for_completion_interruptible(struct completion *c)
+{
+       LASSERT(c != NULL);
+       do {
+               if (call_wait_handler(1000) < 0)
+                       break;
+       } while (c->done == 0);
+       return 0;
+}
+#endif /* HAVE_LIBPTHREAD */
+
 /*
  * rw_semaphore:
  *
@@ -192,60 +216,70 @@ void wait_for_completion(struct completion *c)
  * - down_write(x)
  * - up_write(x)
  */
-struct rw_semaphore {};
 
 void init_rwsem(struct rw_semaphore *s)
 {
-        LASSERT(s != NULL);
-        (void)s;
+       LASSERT(s != NULL);
+       (void)s;
 }
 
 void down_read(struct rw_semaphore *s)
 {
-        LASSERT(s != NULL);
-        (void)s;
+       LASSERT(s != NULL);
+       (void)s;
 }
 
 int down_read_trylock(struct rw_semaphore *s)
 {
-        LASSERT(s != NULL);
-        (void)s;
+       LASSERT(s != NULL);
+       (void)s;
        return 1;
 }
 
 void down_write(struct rw_semaphore *s)
 {
-        LASSERT(s != NULL);
-        (void)s;
+       LASSERT(s != NULL);
+       (void)s;
+}
+
+void downgrade_write(struct rw_semaphore *s)
+{
+       LASSERT(s != NULL);
+       (void)s;
 }
 
 int down_write_trylock(struct rw_semaphore *s)
 {
-        LASSERT(s != NULL);
-        (void)s;
+       LASSERT(s != NULL);
+       (void)s;
        return 1;
 }
 
 void up_read(struct rw_semaphore *s)
 {
-        LASSERT(s != NULL);
-        (void)s;
+       LASSERT(s != NULL);
+       (void)s;
 }
 
 void up_write(struct rw_semaphore *s)
 {
-        LASSERT(s != NULL);
-        (void)s;
+       LASSERT(s != NULL);
+       (void)s;
+}
+
+void fini_rwsem(struct rw_semaphore *s)
+{
+       LASSERT(s != NULL);
+       (void)s;
 }
-#endif
 
 #ifdef HAVE_LIBPTHREAD
 
 /*
- * Completion
+ * Multi-threaded user space completion
  */
 
-void cfs_init_completion(struct cfs_completion *c)
+void init_completion(struct completion *c)
 {
         LASSERT(c != NULL);
         c->c_done = 0;
@@ -253,14 +287,14 @@ void cfs_init_completion(struct cfs_completion *c)
         pthread_cond_init(&c->c_cond, NULL);
 }
 
-void cfs_fini_completion(struct cfs_completion *c)
+void fini_completion(struct completion *c)
 {
         LASSERT(c != NULL);
         pthread_mutex_destroy(&c->c_mut);
         pthread_cond_destroy(&c->c_cond);
 }
 
-void cfs_complete(struct cfs_completion *c)
+void complete(struct completion *c)
 {
         LASSERT(c != NULL);
         pthread_mutex_lock(&c->c_mut);
@@ -269,7 +303,7 @@ void cfs_complete(struct cfs_completion *c)
         pthread_mutex_unlock(&c->c_mut);
 }
 
-void cfs_wait_for_completion(struct cfs_completion *c)
+void wait_for_completion(struct completion *c)
 {
         LASSERT(c != NULL);
         pthread_mutex_lock(&c->c_mut);
@@ -280,12 +314,12 @@ void cfs_wait_for_completion(struct cfs_completion *c)
 }
 
 /*
- * atomic primitives
+ * Multi-threaded user space atomic primitives
  */
 
 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
 
-int cfs_atomic_read(cfs_atomic_t *a)
+int mt_atomic_read(mt_atomic_t *a)
 {
         int r;
 
@@ -295,14 +329,14 @@ int cfs_atomic_read(cfs_atomic_t *a)
         return r;
 }
 
-void cfs_atomic_set(cfs_atomic_t *a, int b)
+void mt_atomic_set(mt_atomic_t *a, int b)
 {
         pthread_mutex_lock(&atomic_guard_lock);
         a->counter = b;
         pthread_mutex_unlock(&atomic_guard_lock);
 }
 
-int cfs_atomic_dec_and_test(cfs_atomic_t *a)
+int mt_atomic_dec_and_test(mt_atomic_t *a)
 {
         int r;
 
@@ -312,20 +346,20 @@ int cfs_atomic_dec_and_test(cfs_atomic_t *a)
         return (r == 0);
 }
 
-void cfs_atomic_inc(cfs_atomic_t *a)
+void mt_atomic_inc(mt_atomic_t *a)
 {
         pthread_mutex_lock(&atomic_guard_lock);
         ++a->counter;
         pthread_mutex_unlock(&atomic_guard_lock);
 }
 
-void cfs_atomic_dec(cfs_atomic_t *a)
+void mt_atomic_dec(mt_atomic_t *a)
 {
         pthread_mutex_lock(&atomic_guard_lock);
         --a->counter;
         pthread_mutex_unlock(&atomic_guard_lock);
 }
-void cfs_atomic_add(int b, cfs_atomic_t *a)
+void mt_atomic_add(int b, mt_atomic_t *a)
 
 {
         pthread_mutex_lock(&atomic_guard_lock);
@@ -333,7 +367,7 @@ void cfs_atomic_add(int b, cfs_atomic_t *a)
         pthread_mutex_unlock(&atomic_guard_lock);
 }
 
-void cfs_atomic_sub(int b, cfs_atomic_t *a)
+void mt_atomic_sub(int b, mt_atomic_t *a)
 {
         pthread_mutex_lock(&atomic_guard_lock);
         a->counter -= b;