Panda3D
mutexPosixImpl.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file mutexPosixImpl.I
10  * @author drose
11  * @date 2006-02-10
12  */
13 
14 /**
15  *
16  */
17 constexpr MutexPosixImpl::
18 MutexPosixImpl() noexcept : _lock(PTHREAD_MUTEX_INITIALIZER) {
19 }
20 
21 /**
22  *
23  */
24 INLINE MutexPosixImpl::
25 ~MutexPosixImpl() {
26  TAU_PROFILE("MutexPosixImpl::~MutexPosixImpl", " ", TAU_USER);
27  int result = pthread_mutex_destroy(&_lock);
28  assert(result == 0);
29 }
30 
31 /**
32  *
33  */
34 INLINE void MutexPosixImpl::
35 lock() {
36  TAU_PROFILE("void MutexPosixImpl::lock", " ", TAU_USER);
37  int result = pthread_mutex_lock(&_lock);
38  assert(result == 0);
39 }
40 
41 /**
42  *
43  */
44 INLINE bool MutexPosixImpl::
45 try_lock() {
46  TAU_PROFILE("bool MutexPosixImpl::try_lock", " ", TAU_USER);
47  int result = pthread_mutex_trylock(&_lock);
48  assert(result == 0 || result == EBUSY);
49  return (result == 0);
50 }
51 
52 /**
53  *
54  */
55 INLINE void MutexPosixImpl::
56 unlock() {
57  TAU_PROFILE("void MutexPosixImpl::unlock", " ", TAU_USER);
58  int result = pthread_mutex_unlock(&_lock);
59  assert(result == 0);
60 }
61 
62 /**
63  *
64  */
65 #ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
66 constexpr ReMutexPosixImpl::
67 ReMutexPosixImpl() noexcept : _lock(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) {
68 }
69 #else
70 INLINE ReMutexPosixImpl::
71 ReMutexPosixImpl() {
72  TAU_PROFILE("ReMutexPosixImpl::ReMutexPosixImpl", " ", TAU_USER);
73  pthread_mutexattr_t attr;
74  pthread_mutexattr_init(&attr);
75  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
76  int result = pthread_mutex_init(&_lock, &attr);
77  pthread_mutexattr_destroy(&attr);
78  assert(result == 0);
79 }
80 #endif
81 
82 /**
83  *
84  */
85 INLINE ReMutexPosixImpl::
86 ~ReMutexPosixImpl() {
87  TAU_PROFILE("ReMutexPosixImpl::~ReMutexPosixImpl", " ", TAU_USER);
88  int result = pthread_mutex_destroy(&_lock);
89  assert(result == 0);
90 }
91 
92 /**
93  *
94  */
95 INLINE void ReMutexPosixImpl::
96 lock() {
97  TAU_PROFILE("void ReMutexPosixImpl::lock", " ", TAU_USER);
98  int result = pthread_mutex_lock(&_lock);
99  assert(result == 0);
100 }
101 
102 /**
103  *
104  */
105 INLINE bool ReMutexPosixImpl::
106 try_lock() {
107  TAU_PROFILE("bool ReMutexPosixImpl::try_lock", " ", TAU_USER);
108  int result = pthread_mutex_trylock(&_lock);
109  assert(result == 0 || result == EBUSY);
110  return (result == 0);
111 }
112 
113 /**
114  *
115  */
116 INLINE void ReMutexPosixImpl::
117 unlock() {
118  TAU_PROFILE("void ReMutexPosixImpl::unlock", " ", TAU_USER);
119  int result = pthread_mutex_unlock(&_lock);
120  assert(result == 0);
121 }