@@ -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);
@@ -2940,7 +2940,7 @@ init_vecs (void)
if (!vecs)
return NULL;
- pthread_mutex_init(&vecs->lock.mutex, NULL);
+ init_lock(&vecs->lock);
return vecs;
}
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@redhat.com> --- libmultipath/lock.h | 16 ++++++++++++++++ multipathd/main.c | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-)