| 1 | /* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | // |
|---|
| 16 | // ScopedLock and ReverseScopedLock templates |
|---|
| 17 | // ~~~~~~~ |
|---|
| 18 | // |
|---|
| 19 | #ifndef _ScopedLock_ |
|---|
| 20 | #define _ScopedLock_ |
|---|
| 21 | |
|---|
| 22 | namespace OpenThreads{ |
|---|
| 23 | |
|---|
| 24 | template <class M> class ScopedLock |
|---|
| 25 | { |
|---|
| 26 | private: |
|---|
| 27 | M& m_lock; |
|---|
| 28 | ScopedLock(const ScopedLock&); // prevent copy |
|---|
| 29 | ScopedLock& operator=(const ScopedLock&); // prevent assign |
|---|
| 30 | public: |
|---|
| 31 | explicit ScopedLock(M& m):m_lock(m) {m_lock.lock();} |
|---|
| 32 | ~ScopedLock(){m_lock.unlock();} |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | template <class M> class ReverseScopedLock |
|---|
| 36 | { |
|---|
| 37 | private: |
|---|
| 38 | M& m_lock; |
|---|
| 39 | ReverseScopedLock(const ReverseScopedLock&); // prevent copy |
|---|
| 40 | ReverseScopedLock& operator=(const ReverseScopedLock&); // prevent assign |
|---|
| 41 | public: |
|---|
| 42 | explicit ReverseScopedLock(M& m):m_lock(m) {m_lock.unlock();} |
|---|
| 43 | ~ReverseScopedLock(){m_lock.lock();} |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | template <class M> class ScopedPointerLock |
|---|
| 48 | { |
|---|
| 49 | private: |
|---|
| 50 | M* m_lock; |
|---|
| 51 | ScopedPointerLock(const ScopedPointerLock&); // prevent copy |
|---|
| 52 | ScopedPointerLock& operator=(const ScopedPointerLock&); // prevent assign |
|---|
| 53 | public: |
|---|
| 54 | explicit ScopedPointerLock(M* m):m_lock(m) { if (m_lock) m_lock->lock();} |
|---|
| 55 | ~ScopedPointerLock(){ if (m_lock) m_lock->unlock();} |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | template <class M> class ReverseScopedPointerLock |
|---|
| 59 | { |
|---|
| 60 | private: |
|---|
| 61 | M* m_lock; |
|---|
| 62 | ReverseScopedPointerLock(const ReverseScopedPointerLock&); // prevent copy |
|---|
| 63 | ReverseScopedPointerLock& operator=(const ReverseScopedPointerLock&); // prevent assign |
|---|
| 64 | public: |
|---|
| 65 | explicit ReverseScopedPointerLock(M* m):m_lock(m) { if (m_lock) m_lock->unlock();} |
|---|
| 66 | ~ReverseScopedPointerLock(){ if (m_lock) m_lock->lock();} |
|---|
| 67 | }; |
|---|
| 68 | |
|---|
| 69 | } |
|---|
| 70 | #endif |
|---|