[PATCH 2/6] multipathd: track waiters for mutex_lock

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



use the uatomic operations to track how many threads are waiting in
lock() for mutex_locks. This will be used by a later patch.

Signed-off-by: Benjamin Marzinski <bmarzins@xxxxxxxxxx>
---
 libmultipath/lock.h | 16 ++++++++++++++++
 multipathd/main.c   |  2 +-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/libmultipath/lock.h b/libmultipath/lock.h
index d7b779e7..20ca77e6 100644
--- a/libmultipath/lock.h
+++ b/libmultipath/lock.h
@@ -2,17 +2,28 @@
 #define _LOCK_H
 
 #include <pthread.h>
+#include <urcu/uatomic.h>
+#include <stdbool.h>
 
 typedef void (wakeup_fn)(void);
 
 struct mutex_lock {
 	pthread_mutex_t mutex;
 	wakeup_fn *wakeup;
+	int waiters; /* uatomic access only */
 };
 
+static inline void init_lock(struct mutex_lock *a)
+{
+	pthread_mutex_init(&a->mutex, NULL);
+	uatomic_set(&a->waiters, 0);
+}
+
 static inline void lock(struct mutex_lock *a)
 {
+	uatomic_inc(&a->waiters);
 	pthread_mutex_lock(&a->mutex);
+	uatomic_dec(&a->waiters);
 }
 
 static inline int trylock(struct mutex_lock *a)
@@ -30,6 +41,11 @@ static inline void __unlock(struct mutex_lock *a)
 	pthread_mutex_unlock(&a->mutex);
 }
 
+static inline bool lock_has_waiters(struct mutex_lock *a)
+{
+	return (uatomic_read(&a->waiters) > 0);
+}
+
 #define lock_cleanup_pop(a) pthread_cleanup_pop(1)
 
 void cleanup_lock (void * data);
diff --git a/multipathd/main.c b/multipathd/main.c
index 2f2b9d4c..71079113 100644
--- a/multipathd/main.c
+++ b/multipathd/main.c
@@ -2940,7 +2940,7 @@ init_vecs (void)
 	if (!vecs)
 		return NULL;
 
-	pthread_mutex_init(&vecs->lock.mutex, NULL);
+	init_lock(&vecs->lock);
 
 	return vecs;
 }
-- 
2.17.2

--
dm-devel mailing list
dm-devel@xxxxxxxxxx
https://listman.redhat.com/mailman/listinfo/dm-devel




[Index of Archives]     [DM Crypt]     [Fedora Desktop]     [ATA RAID]     [Fedora Marketing]     [Fedora Packaging]     [Fedora SELinux]     [Yosemite Discussion]     [KDE Users]     [Fedora Docs]

  Powered by Linux