Whamcloud - gitweb
b=12302
[fs/lustre-release.git] / lnet / libcfs / user-lock.c
index a1a6779..c521dc7 100644 (file)
@@ -37,6 +37,8 @@
 
 #include <stdlib.h>
 #include <libcfs/libcfs.h>
+#include <libcfs/kp30.h>
+
 /*
  * Optional debugging (magic stamping and checking ownership) can be added.
  */
@@ -223,6 +225,110 @@ void up_write(struct rw_semaphore *s)
 }
 #endif
 
+#ifdef HAVE_LIBPTHREAD
+
+/*
+ * Completion
+ */
+
+void cfs_init_completion(struct cfs_completion *c)
+{
+        LASSERT(c != NULL);
+        c->c_done = 0;
+        pthread_mutex_init(&c->c_mut, NULL);
+        pthread_cond_init(&c->c_cond, NULL);
+}
+
+void cfs_fini_completion(struct cfs_completion *c)
+{
+        LASSERT(c != NULL);
+        pthread_mutex_destroy(&c->c_mut);
+        pthread_cond_destroy(&c->c_cond);
+}
+
+void cfs_complete(struct cfs_completion *c)
+{
+        LASSERT(c != NULL);
+        pthread_mutex_lock(&c->c_mut);
+        c->c_done++;
+        pthread_cond_signal(&c->c_cond);
+        pthread_mutex_unlock(&c->c_mut);
+}
+
+void cfs_wait_for_completion(struct cfs_completion *c)
+{
+        LASSERT(c != NULL);
+        pthread_mutex_lock(&c->c_mut);
+        while (c->c_done == 0)
+                pthread_cond_wait(&c->c_cond, &c->c_mut);
+        c->c_done--;
+        pthread_mutex_unlock(&c->c_mut);
+}
+
+/*
+ * atomic primitives
+ */
+
+static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
+
+int cfs_atomic_read(cfs_atomic_t *a)
+{
+        int r;
+
+        pthread_mutex_lock(&atomic_guard_lock);
+        r = a->counter;
+        pthread_mutex_unlock(&atomic_guard_lock);
+        return r;
+}
+
+void cfs_atomic_set(cfs_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 r;
+
+        pthread_mutex_lock(&atomic_guard_lock);
+        r = --a->counter;
+        pthread_mutex_unlock(&atomic_guard_lock);
+        return (r == 0);
+}
+
+void cfs_atomic_inc(cfs_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)
+{
+        pthread_mutex_lock(&atomic_guard_lock);
+        --a->counter;
+        pthread_mutex_unlock(&atomic_guard_lock);
+}
+void cfs_atomic_add(int b, cfs_atomic_t *a)
+
+{
+        pthread_mutex_lock(&atomic_guard_lock);
+        a->counter += b;
+        pthread_mutex_unlock(&atomic_guard_lock);
+}
+
+void cfs_atomic_sub(int b, cfs_atomic_t *a)
+{
+        pthread_mutex_lock(&atomic_guard_lock);
+        a->counter -= b;
+        pthread_mutex_unlock(&atomic_guard_lock);
+}
+
+#endif /* HAVE_LIBPTHREAD */
+
+
 /* !__KERNEL__ */
 #endif