Panda3D
mutexPosixImpl.I
1 // Filename: mutexPosixImpl.I
2 // Created by: drose (10Feb06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: MutexPosixImpl::Constructor
18 // Access: Public
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE MutexPosixImpl::
22 MutexPosixImpl() {
23  TAU_PROFILE("MutexPosixImpl::MutexPosixImpl", " ", TAU_USER);
24  pthread_mutexattr_t attr;
25  pthread_mutexattr_init(&attr);
26  // The symbol PTHREAD_MUTEX_DEFAULT isn't always available?
27  // pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
28  int result = pthread_mutex_init(&_lock, &attr);
29  pthread_mutexattr_destroy(&attr);
30  assert(result == 0);
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: MutexPosixImpl::Destructor
35 // Access: Public
36 // Description:
37 ////////////////////////////////////////////////////////////////////
38 INLINE MutexPosixImpl::
39 ~MutexPosixImpl() {
40  TAU_PROFILE("MutexPosixImpl::~MutexPosixImpl", " ", TAU_USER);
41  int result = pthread_mutex_destroy(&_lock);
42  assert(result == 0);
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: MutexPosixImpl::acquire
47 // Access: Public
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 INLINE void MutexPosixImpl::
51 acquire() {
52  TAU_PROFILE("void MutexPosixImpl::acquire", " ", TAU_USER);
53  int result = pthread_mutex_lock(&_lock);
54  assert(result == 0);
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: MutexPosixImpl::try_acquire
59 // Access: Public
60 // Description:
61 ////////////////////////////////////////////////////////////////////
62 INLINE bool MutexPosixImpl::
63 try_acquire() {
64  TAU_PROFILE("bool MutexPosixImpl::try_acquire", " ", TAU_USER);
65  int result = pthread_mutex_trylock(&_lock);
66  assert(result == 0 || result == EBUSY);
67  return (result == 0);
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: MutexPosixImpl::release
72 // Access: Public
73 // Description:
74 ////////////////////////////////////////////////////////////////////
75 INLINE void MutexPosixImpl::
76 release() {
77  TAU_PROFILE("void MutexPosixImpl::release", " ", TAU_USER);
78  int result = pthread_mutex_unlock(&_lock);
79  assert(result == 0);
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: MutexPosixImpl::get_posix_lock
84 // Access: Public
85 // Description: Returns the underlying Posix lock handle.
86 ////////////////////////////////////////////////////////////////////
87 INLINE pthread_mutex_t *MutexPosixImpl::
88 get_posix_lock() {
89  return &_lock;
90 }
91 
92 ////////////////////////////////////////////////////////////////////
93 // Function: ReMutexPosixImpl::Constructor
94 // Access: Public
95 // Description:
96 ////////////////////////////////////////////////////////////////////
97 INLINE ReMutexPosixImpl::
98 ReMutexPosixImpl() {
99  TAU_PROFILE("ReMutexPosixImpl::ReMutexPosixImpl", " ", TAU_USER);
100  pthread_mutexattr_t attr;
101  pthread_mutexattr_init(&attr);
102  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
103  int result = pthread_mutex_init(&_lock, &attr);
104  pthread_mutexattr_destroy(&attr);
105  assert(result == 0);
106 }
107 
108 ////////////////////////////////////////////////////////////////////
109 // Function: ReMutexPosixImpl::Destructor
110 // Access: Public
111 // Description:
112 ////////////////////////////////////////////////////////////////////
113 INLINE ReMutexPosixImpl::
114 ~ReMutexPosixImpl() {
115  TAU_PROFILE("ReMutexPosixImpl::~ReMutexPosixImpl", " ", TAU_USER);
116  int result = pthread_mutex_destroy(&_lock);
117  assert(result == 0);
118 }
119 
120 ////////////////////////////////////////////////////////////////////
121 // Function: ReMutexPosixImpl::acquire
122 // Access: Public
123 // Description:
124 ////////////////////////////////////////////////////////////////////
125 INLINE void ReMutexPosixImpl::
126 acquire() {
127  TAU_PROFILE("void ReMutexPosixImpl::acquire", " ", TAU_USER);
128  int result = pthread_mutex_lock(&_lock);
129  assert(result == 0);
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: ReMutexPosixImpl::try_acquire
134 // Access: Public
135 // Description:
136 ////////////////////////////////////////////////////////////////////
137 INLINE bool ReMutexPosixImpl::
138 try_acquire() {
139  TAU_PROFILE("bool ReMutexPosixImpl::try_acquire", " ", TAU_USER);
140  int result = pthread_mutex_trylock(&_lock);
141  assert(result == 0 || result == EBUSY);
142  return (result == 0);
143 }
144 
145 ////////////////////////////////////////////////////////////////////
146 // Function: ReMutexPosixImpl::release
147 // Access: Public
148 // Description:
149 ////////////////////////////////////////////////////////////////////
150 INLINE void ReMutexPosixImpl::
151 release() {
152  TAU_PROFILE("void ReMutexPosixImpl::release", " ", TAU_USER);
153  int result = pthread_mutex_unlock(&_lock);
154  assert(result == 0);
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: ReMutexPosixImpl::get_posix_lock
159 // Access: Public
160 // Description: Returns the underlying Posix lock handle.
161 ////////////////////////////////////////////////////////////////////
162 INLINE pthread_mutex_t *ReMutexPosixImpl::
163 get_posix_lock() {
164  return &_lock;
165 }