1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 #ifndef _SYNCOBJECTS_HXX_
30 #define _SYNCOBJECTS_HXX_
31 
32 //------------------------------------------------------------------------
33 // includes
34 //------------------------------------------------------------------------
35 
36 #include <windows.h>
37 
38 //------------------------------------------------------------------------
39 // a simple helper template for automatic locking/unlocking
40 //------------------------------------------------------------------------
41 
42 template< class LOCK >
43 class CLockGuard
44 {
45 public:
46 	CLockGuard( LOCK* aLock ) :
47 		m_pLock( aLock )
48 	{
49 		m_pLock->Lock( );
50 	}
51 
52 	~CLockGuard( )
53 	{
54 		m_pLock->Unlock( );
55 	}
56 
57 private:
58 	LOCK* m_pLock;
59 };
60 
61 //------------------------------------------------------------------------
62 // a interface base class for different locking sub classes
63 //------------------------------------------------------------------------
64 
65 class CSyncObject
66 {
67 public:
68 	virtual ~CSyncObject( ) = 0;
69 
70 	virtual int Lock( )     = 0;
71 	virtual int Unlock( )   = 0;
72 };
73 
74 //------------------------------------------------------------------------
75 // if no synchronization is necessary this class will be used
76 // declaring the functions as inline safes runtime overhead
77 //------------------------------------------------------------------------
78 
79 class CNullLock
80 {
81 public:
82 	inline virtual ~CNullLock ( ) {};
83 	inline virtual int Lock( )    {};
84 	inline virtual int Unlock()   {};
85 };
86 
87 //------------------------------------------------------------------------
88 // a minimal wrapper for a win32 critical section
89 //------------------------------------------------------------------------
90 
91 class CCriticalSection : public CSyncObject
92 {
93 public:
94 	CCriticalSection( );
95 	virtual ~CCriticalSection( );
96 
97 	// both functions return always 0
98 	// because the win32 critsec functions
99 	// don't return any return code
100 	virtual int Lock( );
101 	virtual int Unlock( );
102 
103 private:
104 	CRITICAL_SECTION m_critSec;
105 };
106 
107 
108 typedef CLockGuard< CSyncObject > SyncObjLockGuard_t;
109 
110 #endif
111