~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/mutex.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include "mutex.h"
  3 
  4 #include "debug.h"
  5 #include <linux/string.h>
  6 #include <errno.h>
  7 
  8 static void check_err(const char *fn, int err)
  9 {
 10         char sbuf[STRERR_BUFSIZE];
 11 
 12         if (err == 0)
 13                 return;
 14 
 15         pr_err("%s error: '%s'\n", fn, str_error_r(err, sbuf, sizeof(sbuf)));
 16 }
 17 
 18 #define CHECK_ERR(err) check_err(__func__, err)
 19 
 20 static void __mutex_init(struct mutex *mtx, bool pshared)
 21 {
 22         pthread_mutexattr_t attr;
 23 
 24         CHECK_ERR(pthread_mutexattr_init(&attr));
 25 
 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));
 32 
 33         CHECK_ERR(pthread_mutex_init(&mtx->lock, &attr));
 34         CHECK_ERR(pthread_mutexattr_destroy(&attr));
 35 }
 36 
 37 void mutex_init(struct mutex *mtx)
 38 {
 39         __mutex_init(mtx, /*pshared=*/false);
 40 }
 41 
 42 void mutex_init_pshared(struct mutex *mtx)
 43 {
 44         __mutex_init(mtx, /*pshared=*/true);
 45 }
 46 
 47 void mutex_destroy(struct mutex *mtx)
 48 {
 49         CHECK_ERR(pthread_mutex_destroy(&mtx->lock));
 50 }
 51 
 52 void mutex_lock(struct mutex *mtx)
 53         NO_THREAD_SAFETY_ANALYSIS
 54 {
 55         CHECK_ERR(pthread_mutex_lock(&mtx->lock));
 56 }
 57 
 58 void mutex_unlock(struct mutex *mtx)
 59         NO_THREAD_SAFETY_ANALYSIS
 60 {
 61         CHECK_ERR(pthread_mutex_unlock(&mtx->lock));
 62 }
 63 
 64 bool mutex_trylock(struct mutex *mtx)
 65 {
 66         int ret = pthread_mutex_trylock(&mtx->lock);
 67 
 68         if (ret == 0)
 69                 return true; /* Lock acquired. */
 70 
 71         if (ret == EBUSY)
 72                 return false; /* Lock busy. */
 73 
 74         /* Print error. */
 75         CHECK_ERR(ret);
 76         return false;
 77 }
 78 
 79 static void __cond_init(struct cond *cnd, bool pshared)
 80 {
 81         pthread_condattr_t attr;
 82 
 83         CHECK_ERR(pthread_condattr_init(&attr));
 84         if (pshared)
 85                 CHECK_ERR(pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
 86 
 87         CHECK_ERR(pthread_cond_init(&cnd->cond, &attr));
 88         CHECK_ERR(pthread_condattr_destroy(&attr));
 89 }
 90 
 91 void cond_init(struct cond *cnd)
 92 {
 93         __cond_init(cnd, /*pshared=*/false);
 94 }
 95 
 96 void cond_init_pshared(struct cond *cnd)
 97 {
 98         __cond_init(cnd, /*pshared=*/true);
 99 }
100 
101 void cond_destroy(struct cond *cnd)
102 {
103         CHECK_ERR(pthread_cond_destroy(&cnd->cond));
104 }
105 
106 void cond_wait(struct cond *cnd, struct mutex *mtx)
107 {
108         CHECK_ERR(pthread_cond_wait(&cnd->cond, &mtx->lock));
109 }
110 
111 void cond_signal(struct cond *cnd)
112 {
113         CHECK_ERR(pthread_cond_signal(&cnd->cond));
114 }
115 
116 void cond_broadcast(struct cond *cnd)
117 {
118         CHECK_ERR(pthread_cond_broadcast(&cnd->cond));
119 }
120 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php