Re: [PATCH v1 3/4] perf mutex and cond: Updated files mutex.h & mutex.c

From: Namhyung Kim
Date: Wed Jul 27 2022 - 18:43:16 EST


On Wed, Jul 27, 2022 at 4:20 AM <gpavithrasha@xxxxxxxxx> wrote:
>
> From: pavithra <gpavithrasha@xxxxxxxxx>
>
> Added new struct and corresponding
> functions to wrap usage of pthread_cond_t.
> Added a new function for mutex_trylock-similar to
> mutex_lock.
>
> Signed-off-by: pavithra <gpavithrasha@xxxxxxxxx>
> ---
> tools/perf/util/mutex.c | 37 ++++++++++++++++++++++++++++++++-----
> tools/perf/util/mutex.h | 13 +++++++++++--
> 2 files changed, 43 insertions(+), 7 deletions(-)
>
> diff --git a/tools/perf/util/mutex.c b/tools/perf/util/mutex.c
> index b7264a1438c4..9dc37a3f374f 100644
> --- a/tools/perf/util/mutex.c
> +++ b/tools/perf/util/mutex.c
> @@ -1,8 +1,8 @@
> #include <mutex.h>
> #include <pthread.h>
>
> -//to avoid the warning : implicit declaration of BUG_ON,
> -//we add the following 2 headers.
> +/*to avoid the warning : implicit declaration of BUG_ON*/
> +/*we add the following 2 headers*/
> #include <linux/compiler.h>
> #include <linux/kernel.h>
>
> @@ -11,14 +11,15 @@ void mutex_init(struct mutex *mtx)
> pthread_mutexattr_t lock_attr;
> pthread_mutexattr_init(&lock_attr);
> pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_ERRORCHECK);
> -BUG_ON(pthread_mutex_init(&mtx->lock, &lock_attr));
> -//on success, returns 0.
> +/*pthread_mutex_init:on success, returns 0*/
> +BUG_ON(pthread_mutex_init(&mtx->lock, &lock_attr));
> pthread_mutexattr_destroy(&lock_attr);
> }
>
> void mutex_destroy(struct mutex *mtx)
> {
> -BUG_ON(pthread_mutex_destroy(&mtx->lock)); //on success, returns 0.
> +/*pthread_mutex_destroy:on success, returns 0*/
> +BUG_ON(pthread_mutex_destroy(&mtx->lock));

Above changes should belong to the patch 1.

> }
>
> void mutex_lock(struct mutex *mtx)
> @@ -30,3 +31,29 @@ void mutex_unlock(struct mutex *mtx)
> {
> BUG_ON(pthread_mutex_unlock(&mtx->lock) != 0);
> }
> +
> +bool mutex_trylock(struct mutex *mtx)
> +{
> +return pthread_mutex_trylock(&mtx->lock)!=0;
> +}
> +
> +void cond_wait(struct cond *cnd, struct mutex *mtx)
> +{
> +BUG_ON(pthread_cond_wait(&cnd->cond, &mtx->lock) != 0);
> +}
> +
> +void cond_signal(struct cond *cnd)
> +{
> +BUG_ON(pthread_cond_signal(&cnd->cond) != 0);
> +}
> +
> +void cond_init(struct cond *cnd)
> +{
> +pthread_condattr_t attr;
> +
> +pthread_condattr_init(&attr);
> +
> +/*pthread_cond_init:on success, returns 0*/
> +BUG_ON(pthread_cond_init(&cnd->cond, &attr));

Please be consistent with != 0.

Thanks,
Namhyung


> +pthread_condattr_destroy(&attr);
> +}
> diff --git a/tools/perf/util/mutex.h b/tools/perf/util/mutex.h
> index ab2ebb98b24a..f1b4aaa151be 100644
> --- a/tools/perf/util/mutex.h
> +++ b/tools/perf/util/mutex.h
> @@ -1,15 +1,24 @@
> #ifndef __PERF_MUTEX_H
> -#define _PERF_MUTEX_H
> +#define __PERF_MUTEX_H
>
> #include <pthread.h>
> +#include <stdbool.h>
>
> struct mutex {
> pthread_mutex_t lock;
> };
>
> +struct cond {
> +pthread_cond_t cond;
> +};
> +
> void mutex_lock(struct mutex *mtx);
> void mutex_unlock(struct mutex *mtx);
> +bool mutex_trylock(struct mutex *mtx);
> void mutex_init(struct mutex *mtx);
> void mutex_destroy(struct mutex *mtx);
>
> -#endif /* _PERF_MUTEX_H */
> +void cond_wait(struct cond *cnd, struct mutex *mtx);
> +void cond_signal(struct cond *cnd);
> +void cond_init(struct cond *cnd);
> +#endif /* __PERF_MUTEX_H */
> --
> 2.25.1
>