]> git.lizzy.rs Git - dragonfireclient.git/blob - src/threading/mutex.h
Fix shift key producing space in console (#5777)
[dragonfireclient.git] / src / threading / mutex.h
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 #ifndef THREADING_MUTEX_H
27 #define THREADING_MUTEX_H
28
29 #include "threads.h"
30
31 #if USE_CPP11_MUTEX
32         #include <mutex>
33         using Mutex = std::mutex;
34         using RecursiveMutex = std::recursive_mutex;
35 #else
36
37 #if USE_WIN_MUTEX
38         #ifndef _WIN32_WINNT
39                 #define _WIN32_WINNT 0x0501
40         #endif
41         #ifndef WIN32_LEAN_AND_MEAN
42                 #define WIN32_LEAN_AND_MEAN
43         #endif
44         #include <windows.h>
45 #else
46         #include <pthread.h>
47 #endif
48
49 #include "util/basic_macros.h"
50
51 class Mutex
52 {
53 public:
54         Mutex();
55         ~Mutex();
56         void lock();
57         void unlock();
58
59         bool try_lock();
60
61 protected:
62         Mutex(bool recursive);
63         void init_mutex(bool recursive);
64 private:
65 #if USE_WIN_MUTEX
66         CRITICAL_SECTION mutex;
67 #else
68         pthread_mutex_t mutex;
69 #endif
70
71         DISABLE_CLASS_COPY(Mutex);
72 };
73
74 class RecursiveMutex : public Mutex
75 {
76 public:
77         RecursiveMutex();
78
79         DISABLE_CLASS_COPY(RecursiveMutex);
80 };
81
82 #endif // C++11
83
84 #endif