Optimize updateFastFaceRow processing by removing some TileSpec copy (#5678)
[oweals/minetest.git] / src / threading / mutex.cpp
1 /*
2 This file is a part of the JThread package, which contains some object-
3 oriented thread wrappers for different thread implementations.
4
5 Copyright (c) 2000-2006  Jori Liesenborgs (jori.liesenborgs@gmail.com)
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "threads.h"
27
28 #ifndef USE_CPP11_MUTEX
29
30 #include "threading/mutex.h"
31
32 #include <cassert>
33
34 #define UNUSED(expr) do { (void)(expr); } while (0)
35
36 Mutex::Mutex()
37 {
38         init_mutex(false);
39 }
40
41
42 Mutex::Mutex(bool recursive)
43 {
44         init_mutex(recursive);
45 }
46
47 void Mutex::init_mutex(bool recursive)
48 {
49 #if USE_WIN_MUTEX
50         // Windows critical sections are recursive by default
51         UNUSED(recursive);
52
53         InitializeCriticalSection(&mutex);
54 #else
55         pthread_mutexattr_t attr;
56         pthread_mutexattr_init(&attr);
57
58         if (recursive)
59                 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
60
61         int ret = pthread_mutex_init(&mutex, &attr);
62         assert(!ret);
63         UNUSED(ret);
64
65         pthread_mutexattr_destroy(&attr);
66 #endif
67 }
68
69 Mutex::~Mutex()
70 {
71 #if USE_WIN_MUTEX
72         DeleteCriticalSection(&mutex);
73 #else
74         int ret = pthread_mutex_destroy(&mutex);
75         assert(!ret);
76         UNUSED(ret);
77 #endif
78 }
79
80 void Mutex::lock()
81 {
82 #if USE_WIN_MUTEX
83         EnterCriticalSection(&mutex);
84 #else
85         int ret = pthread_mutex_lock(&mutex);
86         assert(!ret);
87         UNUSED(ret);
88 #endif
89 }
90
91 bool Mutex::try_lock()
92 {
93 #if USE_WIN_MUTEX
94         return TryEnterCriticalSection(&mutex) != 0;
95 #else
96         return pthread_mutex_trylock(&mutex) == 0;
97 #endif
98 }
99
100 void Mutex::unlock()
101 {
102 #if USE_WIN_MUTEX
103         LeaveCriticalSection(&mutex);
104 #else
105         int ret = pthread_mutex_unlock(&mutex);
106         assert(!ret);
107         UNUSED(ret);
108 #endif
109 }
110
111 RecursiveMutex::RecursiveMutex()
112         : Mutex(true)
113 {}
114
115 #endif // C++11
116