Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Purpose: MutexHolder class. Helper class for controlling the acquisition 4 : * and release of a Mutex based on current context. 5 : * 6 : ****************************************************************************** 7 : * Copyright (c) 2009 8 : * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada. 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : #ifndef INCLUDE_CORE_MUTEXHOLDER_H 29 : #define INCLUDE_CORE_MUTEXHOLDER_H 30 : 31 : #include "pcidsk_mutex.h" 32 : 33 : namespace PCIDSK 34 : { 35 : /************************************************************************/ 36 : /* MutexHolder */ 37 : /************************************************************************/ 38 : class PCIDSK_DLL MutexHolder 39 : { 40 : public: 41 17750 : MutexHolder( Mutex *mutexIn ) 42 17750 : { 43 17750 : this->mutex = mutexIn; 44 17750 : if( mutex != nullptr ) 45 17750 : mutex->Acquire(); 46 17750 : } 47 17750 : ~MutexHolder() 48 17750 : { 49 17750 : if( mutex ) 50 17742 : mutex->Release(); 51 17750 : } 52 : 53 8 : void Release() 54 : { 55 8 : if (mutex) 56 : { 57 8 : mutex->Release(); 58 8 : mutex = nullptr; 59 : } 60 8 : } 61 : 62 : private: 63 : Mutex *mutex; 64 : 65 : }; 66 : 67 : } //end namespace PCIDSK 68 : 69 : #endif // INCLUDE_CORE_MUTEXHOLDER_H