0026823: Configuration - use EGL on another platform without GLX
[occt.git] / src / Standard / Standard_Mutex.cxx
1 // Created on: 2006-04-13
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2006-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 // On Windows, function TryEnterCriticalSection has appeared in Windows NT
17 // and is surrounded by #ifdef in MS VC++ 7.1 headers.
18 // Thus to use it we need to define appropriate macro saying that we wil
19 // run on Windows NT 4.0 at least
20 #if ((defined(_WIN32) || defined(__WIN32__)) && !defined(_WIN32_WINNT))
21   #define _WIN32_WINNT 0x0400
22 #endif
23
24 #include <Standard_Mutex.hxx>
25 #include <Standard_OStream.hxx>
26
27 #include <errno.h>
28
29 //=============================================
30 // Standard_Mutex::Standard_Mutex
31 //=============================================
32
33 Standard_Mutex::Standard_Mutex () 
34 {
35 #if (defined(_WIN32) || defined(__WIN32__))
36   InitializeCriticalSection (&myMutex);
37 #else
38   pthread_mutexattr_t anAttr;
39   pthread_mutexattr_init (&anAttr);
40   pthread_mutexattr_settype (&anAttr, PTHREAD_MUTEX_RECURSIVE);
41   pthread_mutex_init (&myMutex, &anAttr);
42   pthread_mutexattr_destroy (&anAttr);
43 #endif
44 }
45
46 //=============================================
47 // Standard_Mutex::~Standard_Mutex
48 //=============================================
49
50 Standard_Mutex::~Standard_Mutex () 
51 {
52 #if (defined(_WIN32) || defined(__WIN32__))
53   DeleteCriticalSection (&myMutex);
54 #else
55   pthread_mutex_destroy (&myMutex);
56 #endif
57 }
58
59 //=============================================
60 // Standard_Mutex::Lock
61 //=============================================
62
63 void Standard_Mutex::Lock ()
64 {
65 #if (defined(_WIN32) || defined(__WIN32__))
66   EnterCriticalSection (&myMutex);
67 #else
68   pthread_mutex_lock (&myMutex);
69 #endif
70 }
71
72 //=============================================
73 // Standard_Mutex::TryLock
74 //=============================================
75
76 Standard_Boolean Standard_Mutex::TryLock ()
77 {
78 #if (defined(_WIN32) || defined(__WIN32__))
79   return (TryEnterCriticalSection (&myMutex) != 0);
80 #else
81   return (pthread_mutex_trylock (&myMutex) != EBUSY);
82 #endif
83 }
84
85 //=============================================
86 // Standard_Mutex::DestroyCallback
87 //=============================================
88
89 void Standard_Mutex::DestroyCallback ()
90 {
91   UnregisterCallback();
92   Unlock();
93 }