xref: /linux/tools/perf/util/rwsem.c (revision 151ebcf0797b1a3ba53c8843dc21748c80e098c7)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "util.h"
3 #include "rwsem.h"
4 
5 #if RWS_ERRORCHECK
6 #include "mutex.h"
7 #endif
8 
9 int init_rwsem(struct rw_semaphore *sem)
10 {
11 #if RWS_ERRORCHECK
12 	mutex_init(&sem->mtx);
13 	return 0;
14 #else
15 	return pthread_rwlock_init(&sem->lock, NULL);
16 #endif
17 }
18 
19 int exit_rwsem(struct rw_semaphore *sem)
20 {
21 #if RWS_ERRORCHECK
22 	mutex_destroy(&sem->mtx);
23 	return 0;
24 #else
25 	return pthread_rwlock_destroy(&sem->lock);
26 #endif
27 }
28 
29 int down_read(struct rw_semaphore *sem)
30 {
31 #if RWS_ERRORCHECK
32 	mutex_lock(&sem->mtx);
33 	return 0;
34 #else
35 	return perf_singlethreaded ? 0 : pthread_rwlock_rdlock(&sem->lock);
36 #endif
37 }
38 
39 int up_read(struct rw_semaphore *sem)
40 {
41 #if RWS_ERRORCHECK
42 	mutex_unlock(&sem->mtx);
43 	return 0;
44 #else
45 	return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock);
46 #endif
47 }
48 
49 int down_write(struct rw_semaphore *sem)
50 {
51 #if RWS_ERRORCHECK
52 	mutex_lock(&sem->mtx);
53 	return 0;
54 #else
55 	return perf_singlethreaded ? 0 : pthread_rwlock_wrlock(&sem->lock);
56 #endif
57 }
58 
59 int up_write(struct rw_semaphore *sem)
60 {
61 #if RWS_ERRORCHECK
62 	mutex_unlock(&sem->mtx);
63 	return 0;
64 #else
65 	return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock);
66 #endif
67 }
68