Panda3D
mutexHolder.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 mutexHolder.I
10  * @author drose
11  * @date 2002-08-09
12  */
13 
14 /**
15  *
16  */
17 INLINE MutexHolder::
18 MutexHolder(const Mutex &mutex) {
19 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
20  _mutex = &mutex;
21  _mutex->acquire();
22 #endif
23 }
24 
25 /**
26  * This variant on the constructor accepts the current thread as a parameter,
27  * if it is already known, as an optimization.
28  */
29 INLINE MutexHolder::
30 MutexHolder(const Mutex &mutex, Thread *current_thread) {
31 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
32  _mutex = &mutex;
33  // Actually, the regular Mutex class doesn't need the current thread
34  // parameter at the moment. So not actually an optimization. But we keep
35  // this method because it causes a symmetry with ReMutexHolder.
36  _mutex->acquire(/*current_thread*/);
37 #endif
38 }
39 
40 /**
41  * If the MutexHolder constructor is given a pointer to a Mutex object
42  * (instead of an actual object), it will first check to see if the pointer is
43  * NULL, and allocate a new Mutex if it is. This is intended as a convenience
44  * for functions that may need to reference a Mutex at static init time, when
45  * it is impossible to guarantee ordering of initializers.
46  */
47 INLINE MutexHolder::
48 MutexHolder(Mutex *&mutex) {
49 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
50  if (mutex == nullptr) {
51  mutex = new Mutex;
52  }
53  _mutex = mutex;
54  _mutex->acquire();
55 #endif
56 }
57 
58 /**
59  *
60  */
61 INLINE MutexHolder::
62 ~MutexHolder() {
63 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
64  _mutex->release();
65 #endif
66 }
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:38
void acquire() const
Grabs the mutex if it is available.
Definition: mutexDirect.I:55
A thread; that is, a lightweight process.
Definition: thread.h:46