2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file xmltooling/Lockable.h
20 * Locking abstraction.
23 #ifndef __xmltooling_lockable_h__
24 #define __xmltooling_lockable_h__
26 #include <xmltooling/base.h>
28 namespace xmltooling {
31 * Abstract mixin interface for interfaces that support locking
33 class XMLTOOL_API Lockable
41 * Lock the associated object for exclusive access.
43 * @return a pointer to the object being locked
45 virtual Lockable* lock()=0;
48 * Unlock the associated object from exclusive access.
50 virtual void unlock()=0;
54 * RAII wrapper for lockable objects to ensure lock release
56 class XMLTOOL_API Locker
58 MAKE_NONCOPYABLE(Locker);
61 * Optionally locks an object and stores it for later release.
63 * @param lockee pointer to an object to hold, and optionally lock
64 * @param lock true iff object is not yet locked
66 Locker(Lockable* lockee=nullptr, bool lock=true);
69 * Optionally locks an object and stores it for later release.
70 * If an object is already held, it is unlocked and detached.
72 * @param lockee pointer to an object to hold, and optionally lock
73 * @param lock true iff object is not yet locked
75 void assign(Lockable* lockee=nullptr, bool lock=true);
78 * Destructor releases lock on held pointer, if any.
88 #endif /* __xmltooling_lockable_h__ */