1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 25 #ifndef _SYNCOBJECTS_HXX_ 26 #define _SYNCOBJECTS_HXX_ 27 28 //------------------------------------------------------------------------ 29 // includes 30 //------------------------------------------------------------------------ 31 32 #include <windows.h> 33 34 //------------------------------------------------------------------------ 35 // a simple helper template for automatic locking/unlocking 36 //------------------------------------------------------------------------ 37 38 template< class LOCK > 39 class CLockGuard 40 { 41 public: CLockGuard(LOCK * aLock)42 CLockGuard( LOCK* aLock ) : 43 m_pLock( aLock ) 44 { 45 m_pLock->Lock( ); 46 } 47 ~CLockGuard()48 ~CLockGuard( ) 49 { 50 m_pLock->Unlock( ); 51 } 52 53 private: 54 LOCK* m_pLock; 55 }; 56 57 //------------------------------------------------------------------------ 58 // a interface base class for different locking sub classes 59 //------------------------------------------------------------------------ 60 61 class CSyncObject 62 { 63 public: 64 virtual ~CSyncObject( ) = 0; 65 66 virtual int Lock( ) = 0; 67 virtual int Unlock( ) = 0; 68 }; 69 70 //------------------------------------------------------------------------ 71 // if no synchronization is necessary this class will be used 72 // declaring the functions as inline safes runtime overhead 73 //------------------------------------------------------------------------ 74 75 class CNullLock 76 { 77 public: ~CNullLock()78 inline virtual ~CNullLock ( ) {}; Lock()79 inline virtual int Lock( ) {}; Unlock()80 inline virtual int Unlock() {}; 81 }; 82 83 //------------------------------------------------------------------------ 84 // a minimal wrapper for a win32 critical section 85 //------------------------------------------------------------------------ 86 87 class CCriticalSection : public CSyncObject 88 { 89 public: 90 CCriticalSection( ); 91 virtual ~CCriticalSection( ); 92 93 // both functions return always 0 94 // because the win32 critsec functions 95 // don't return any return code 96 virtual int Lock( ); 97 virtual int Unlock( ); 98 99 private: 100 CRITICAL_SECTION m_critSec; 101 }; 102 103 104 typedef CLockGuard< CSyncObject > SyncObjLockGuard_t; 105 106 #endif 107