Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / perf / util / mutex.c
blobbca7f0717f355e16ade273a16c2620a4254957c7
1 // SPDX-License-Identifier: GPL-2.0
2 #include "mutex.h"
4 #include "debug.h"
5 #include <linux/string.h>
6 #include <errno.h>
8 static void check_err(const char *fn, int err)
10 char sbuf[STRERR_BUFSIZE];
12 if (err == 0)
13 return;
15 pr_err("%s error: '%s'\n", fn, str_error_r(err, sbuf, sizeof(sbuf)));
18 #define CHECK_ERR(err) check_err(__func__, err)
20 static void __mutex_init(struct mutex *mtx, bool pshared)
22 pthread_mutexattr_t attr;
24 CHECK_ERR(pthread_mutexattr_init(&attr));
26 #ifndef NDEBUG
27 /* In normal builds enable error checking, such as recursive usage. */
28 CHECK_ERR(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
29 #endif
30 if (pshared)
31 CHECK_ERR(pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
33 CHECK_ERR(pthread_mutex_init(&mtx->lock, &attr));
34 CHECK_ERR(pthread_mutexattr_destroy(&attr));
37 void mutex_init(struct mutex *mtx)
39 __mutex_init(mtx, /*pshared=*/false);
42 void mutex_init_pshared(struct mutex *mtx)
44 __mutex_init(mtx, /*pshared=*/true);
47 void mutex_destroy(struct mutex *mtx)
49 CHECK_ERR(pthread_mutex_destroy(&mtx->lock));
52 void mutex_lock(struct mutex *mtx)
53 NO_THREAD_SAFETY_ANALYSIS
55 CHECK_ERR(pthread_mutex_lock(&mtx->lock));
58 void mutex_unlock(struct mutex *mtx)
59 NO_THREAD_SAFETY_ANALYSIS
61 CHECK_ERR(pthread_mutex_unlock(&mtx->lock));
64 bool mutex_trylock(struct mutex *mtx)
66 int ret = pthread_mutex_trylock(&mtx->lock);
68 if (ret == 0)
69 return true; /* Lock acquired. */
71 if (ret == EBUSY)
72 return false; /* Lock busy. */
74 /* Print error. */
75 CHECK_ERR(ret);
76 return false;
79 static void __cond_init(struct cond *cnd, bool pshared)
81 pthread_condattr_t attr;
83 CHECK_ERR(pthread_condattr_init(&attr));
84 if (pshared)
85 CHECK_ERR(pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
87 CHECK_ERR(pthread_cond_init(&cnd->cond, &attr));
88 CHECK_ERR(pthread_condattr_destroy(&attr));
91 void cond_init(struct cond *cnd)
93 __cond_init(cnd, /*pshared=*/false);
96 void cond_init_pshared(struct cond *cnd)
98 __cond_init(cnd, /*pshared=*/true);
101 void cond_destroy(struct cond *cnd)
103 CHECK_ERR(pthread_cond_destroy(&cnd->cond));
106 void cond_wait(struct cond *cnd, struct mutex *mtx)
108 CHECK_ERR(pthread_cond_wait(&cnd->cond, &mtx->lock));
111 void cond_signal(struct cond *cnd)
113 CHECK_ERR(pthread_cond_signal(&cnd->cond));
116 void cond_broadcast(struct cond *cnd)
118 CHECK_ERR(pthread_cond_broadcast(&cnd->cond));