84ec10e6fd647c2aea2d0107dd48eea6cb80fb1d
[occt.git] / src / Standard / Standard_Mutex.cxx
1 // Created on: 2006-04-13
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2006-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 // On Windows, function TryEnterCriticalSection has appeared in Windows NT
22 // and is surrounded by #ifdef in MS VC++ 7.1 headers.
23 // Thus to use it we need to define appropriate macro saying that we wil
24 // run on Windows NT 4.0 at least
25 #if defined(WNT) && ! defined(_WIN32_WINNT)
26 #define _WIN32_WINNT 0x0400
27 #endif
28
29 #include <Standard_Mutex.hxx>
30 #include <Standard_OStream.hxx>
31
32 //=============================================
33 // Standard_Mutex::Standard_Mutex
34 //=============================================
35
36 Standard_Mutex::Standard_Mutex () 
37 {
38 #ifdef WNT
39   InitializeCriticalSection( &myMutex );
40 #else
41   pthread_mutex_init( &myMutex, 0 );
42 #endif
43 }
44
45 //=============================================
46 // Standard_Mutex::~Standard_Mutex
47 //=============================================
48
49 Standard_Mutex::~Standard_Mutex () 
50 {
51 #ifdef WNT
52   DeleteCriticalSection( &myMutex );
53 #else
54   pthread_mutex_destroy( &myMutex );
55 #endif
56 }
57
58 //=============================================
59 // Standard_Mutex::Lock
60 //=============================================
61
62 void Standard_Mutex::Lock ()
63 {
64 #ifdef WNT
65   EnterCriticalSection( &myMutex );
66 #else
67   pthread_mutex_lock( &myMutex );
68 #endif
69 }
70
71 //=============================================
72 // Standard_Mutex::TryLock
73 //=============================================
74
75 Standard_Boolean Standard_Mutex::TryLock ()
76 {
77 #ifdef WNT
78   return ( TryEnterCriticalSection( &myMutex ) != 0 );
79 #else
80   return ( pthread_mutex_trylock( &myMutex ) != EBUSY );
81 #endif
82 }
83
84 //=============================================
85 // Standard_Mutex::DestroyCallback
86 //=============================================
87
88 void Standard_Mutex::DestroyCallback ()
89 {
90   UnregisterCallback();
91   Unlock();
92 }