b311480e |
1 | // Created on: 2006-04-13 |
2 | // Created by: Andrey BETENEV |
973c2be1 |
3 | // Copyright (c) 2006-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
7fd59977 |
16 | #include <Standard_Mutex.hxx> |
17 | #include <Standard_OStream.hxx> |
18 | |
d8d01f6e |
19 | #include <errno.h> |
20 | |
7fd59977 |
21 | //============================================= |
22 | // Standard_Mutex::Standard_Mutex |
23 | //============================================= |
24 | |
25 | Standard_Mutex::Standard_Mutex () |
26 | { |
49f38e37 |
27 | #if (defined(_WIN32) || defined(__WIN32__)) |
28 | InitializeCriticalSection (&myMutex); |
7fd59977 |
29 | #else |
49f38e37 |
30 | pthread_mutexattr_t anAttr; |
31 | pthread_mutexattr_init (&anAttr); |
32 | pthread_mutexattr_settype (&anAttr, PTHREAD_MUTEX_RECURSIVE); |
33 | pthread_mutex_init (&myMutex, &anAttr); |
34 | pthread_mutexattr_destroy (&anAttr); |
7fd59977 |
35 | #endif |
36 | } |
37 | |
38 | //============================================= |
39 | // Standard_Mutex::~Standard_Mutex |
40 | //============================================= |
41 | |
42 | Standard_Mutex::~Standard_Mutex () |
43 | { |
49f38e37 |
44 | #if (defined(_WIN32) || defined(__WIN32__)) |
45 | DeleteCriticalSection (&myMutex); |
7fd59977 |
46 | #else |
49f38e37 |
47 | pthread_mutex_destroy (&myMutex); |
7fd59977 |
48 | #endif |
49 | } |
50 | |
51 | //============================================= |
52 | // Standard_Mutex::Lock |
53 | //============================================= |
54 | |
55 | void Standard_Mutex::Lock () |
56 | { |
49f38e37 |
57 | #if (defined(_WIN32) || defined(__WIN32__)) |
58 | EnterCriticalSection (&myMutex); |
7fd59977 |
59 | #else |
49f38e37 |
60 | pthread_mutex_lock (&myMutex); |
7fd59977 |
61 | #endif |
62 | } |
63 | |
64 | //============================================= |
65 | // Standard_Mutex::TryLock |
66 | //============================================= |
67 | |
68 | Standard_Boolean Standard_Mutex::TryLock () |
69 | { |
49f38e37 |
70 | #if (defined(_WIN32) || defined(__WIN32__)) |
71 | return (TryEnterCriticalSection (&myMutex) != 0); |
7fd59977 |
72 | #else |
49f38e37 |
73 | return (pthread_mutex_trylock (&myMutex) != EBUSY); |
7fd59977 |
74 | #endif |
75 | } |
76 | |
77 | //============================================= |
78 | // Standard_Mutex::DestroyCallback |
79 | //============================================= |
80 | |
81 | void Standard_Mutex::DestroyCallback () |
82 | { |
83 | UnregisterCallback(); |
84 | Unlock(); |
85 | } |