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

Martin Wilck martin.wilck at suse.com
Wed Aug 17 07:15:25 UTC 2022


On Sat, 2022-07-30 at 00:12 -0500, Benjamin Marzinski wrote:
> 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 at redhat.com>

After discussion about atomic ops:

Reviewed-by: Martin Wilck <mwilck at suse.com>


> ---
>  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;
>  }



More information about the dm-devel mailing list