3f692a9767db1f2f663432655bb22915d62e73f6
[shibboleth/cpp-xmltooling.git] / xmltooling / Lockable.cpp
1 /*
2  *  Copyright 2009-2010 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * Lockable.cpp
19  * 
20  * Locking abstraction.
21  */
22
23 #include "internal.h"
24 #include "Lockable.h"
25
26 using namespace xmltooling;
27
28 Lockable::Lockable()
29 {
30 }
31
32 Lockable::~Lockable()
33 {
34 }
35         
36 Locker::Locker(Lockable* lockee, bool lock)
37 {
38     if (lockee && lock)
39         m_lockee = lockee->lock();
40     else
41         m_lockee = lockee;
42 }
43
44 void Locker::assign(Lockable* lockee, bool lock)
45 {
46     if (m_lockee)
47         m_lockee->unlock();
48     m_lockee = nullptr;
49     if (lockee && lock)
50         m_lockee = lockee->lock();
51     else
52         m_lockee = lockee;
53 }
54
55 Locker::~Locker()
56 {
57     if (m_lockee)
58         m_lockee->unlock();
59 }