Panda3D
 All Classes Functions Variables Enumerations
mutexSpinlockImpl.I
1 // Filename: mutexSpinlockImpl.I
2 // Created by: drose (11Apr06)
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: MutexSpinlockImpl::Constructor
18 // Access: Public
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE MutexSpinlockImpl::
22 MutexSpinlockImpl() {
23  _lock = 0;
24 }
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: MutexSpinlockImpl::Destructor
28 // Access: Public
29 // Description:
30 ////////////////////////////////////////////////////////////////////
31 INLINE MutexSpinlockImpl::
32 ~MutexSpinlockImpl() {
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: MutexSpinlockImpl::acquire
37 // Access: Public
38 // Description:
39 ////////////////////////////////////////////////////////////////////
40 INLINE void MutexSpinlockImpl::
41 acquire() {
42  if (!try_acquire()) {
43  do_lock();
44  }
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: MutexSpinlockImpl::try_acquire
49 // Access: Public
50 // Description:
51 ////////////////////////////////////////////////////////////////////
52 INLINE bool MutexSpinlockImpl::
53 try_acquire() {
54  return (AtomicAdjust::compare_and_exchange(_lock, 0, 1) == 0);
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: MutexSpinlockImpl::release
59 // Access: Public
60 // Description:
61 ////////////////////////////////////////////////////////////////////
62 INLINE void MutexSpinlockImpl::
63 release() {
64  AtomicAdjust::set(_lock, 0);
65 }
static Integer set(Integer &var, Integer new_value)
Atomically changes the indicated variable and returns the original value.
static Integer compare_and_exchange(Integer &mem, Integer old_value, Integer new_value)
Atomic compare and exchange.