My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mutex.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef _OSL_MUTEX_HXX_
30 #define _OSL_MUTEX_HXX_
31 
32 #ifdef __cplusplus
33 
34 #include <osl/mutex.h>
35 
36 
37 namespace osl
38 {
41  class Mutex {
42 
43  public:
49  {
50  mutex = osl_createMutex();
51  }
52 
57  {
58  osl_destroyMutex(mutex);
59  }
60 
66  {
67  return osl_acquireMutex(mutex);
68  }
69 
75  {
76  return osl_tryToAcquireMutex(mutex);
77  }
78 
84  {
85  return osl_releaseMutex(mutex);
86  }
87 
94  static Mutex * getGlobalMutex()
95  {
96  return (Mutex *)osl_getGlobalMutex();
97  }
98 
99  private:
100  oslMutex mutex;
101 
108  Mutex(const Mutex&);
109 
117 
121  Mutex& operator= (const Mutex&);
122 
126  Mutex& operator= (oslMutex);
127  };
128 
131  template<class T>
132  class Guard
133  {
134  private:
135  Guard( const Guard& );
136  const Guard& operator = ( const Guard& );
137 
138  protected:
139  T * pT;
140  public:
141 
144  Guard(T * pT_) : pT(pT_)
145  {
146  pT->acquire();
147  }
148 
151  Guard(T & t) : pT(&t)
152  {
153  pT->acquire();
154  }
155 
158  {
159  pT->release();
160  }
161  };
162 
165  template<class T>
167  {
168  private:
169  ClearableGuard( const ClearableGuard& );
170  const ClearableGuard& operator = ( const ClearableGuard& );
171  protected:
172  T * pT;
173  public:
174 
177  ClearableGuard(T * pT_) : pT(pT_)
178  {
179  pT->acquire();
180  }
181 
184  ClearableGuard(T & t) : pT(&t)
185  {
186  pT->acquire();
187  }
188 
192  {
193  if (pT)
194  pT->release();
195  }
196 
199  void clear()
200  {
201  if(pT)
202  {
203  pT->release();
204  pT = NULL;
205  }
206  }
207  };
208 
211  template< class T >
212  class ResettableGuard : public ClearableGuard< T >
213  {
214  private:
215  ResettableGuard(ResettableGuard &); // not defined
216  void operator =(ResettableGuard &); // not defined
217 
218  protected:
220  public:
223  ResettableGuard( T* pT_ ) :
224  ClearableGuard<T>( pT_ ),
225  pResetT( pT_ )
226  {}
227 
230  ResettableGuard( T& rT ) :
231  ClearableGuard<T>( rT ),
232  pResetT( &rT )
233  {}
234 
237  void reset()
238  {
239  if( pResetT )
240  {
241  this->pT = pResetT;
242  this->pT->acquire();
243  }
244  }
245  };
246 
250 
255  {
256  public:
259  virtual void SAL_CALL acquire() = 0;
260 
263  virtual sal_Bool SAL_CALL tryToAcquire() = 0;
264 
267  virtual void SAL_CALL release() = 0;
268 
269  protected:
271  virtual ~SolarMutex() {}
272  };
276 }
277 
278 #endif /* __cplusplus */
279 #endif /* _OSL_MUTEX_HXX_ */
280 
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */