]> git.lizzy.rs Git - minetest.git/blob - src/threads.h
Remove legacy unused define DIGGING_PARTICLES_AMOUNT
[minetest.git] / src / threads.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef THREADS_HEADER
21 #define THREADS_HEADER
22
23 //
24 // Determine which threading APIs we will use
25 //
26 #if __cplusplus >= 201103L
27         #define USE_CPP11_THREADS 1
28 #elif defined(_WIN32)
29         #define USE_WIN_THREADS 1
30 #else
31         #define USE_POSIX_THREADS 1
32 #endif
33
34 #if defined(_WIN32)
35         // Prefer critical section API because std::mutex is much slower on Windows
36         #define USE_WIN_MUTEX 1
37 #elif __cplusplus >= 201103L
38         #define USE_CPP11_MUTEX 1
39 #else
40         #define USE_POSIX_MUTEX 1
41 #endif
42
43 ///////////////
44
45
46 #if USE_CPP11_THREADS
47         #include <thread>
48 #elif USE_POSIX_THREADS
49         #include <pthread.h>
50 #else
51         #ifndef WIN32_LEAN_AND_MEAN
52                 #define WIN32_LEAN_AND_MEAN
53         #endif
54         #include <windows.h>
55 #endif
56
57 #include "threading/mutex.h"
58
59 //
60 // threadid_t, threadhandle_t
61 //
62 #if USE_CPP11_THREADS
63         typedef std::thread::id threadid_t;
64         typedef std::thread::native_handle_type threadhandle_t;
65 #elif USE_WIN_THREADS
66         typedef DWORD threadid_t;
67         typedef HANDLE threadhandle_t;
68 #elif USE_POSIX_THREADS
69         typedef pthread_t threadid_t;
70         typedef pthread_t threadhandle_t;
71 #endif
72
73 //
74 // ThreadStartFunc
75 //
76 #if USE_CPP11_THREADS || USE_POSIX_THREADS
77         typedef void *ThreadStartFunc(void *param);
78 #elif defined(_WIN32_WCE)
79         typedef DWORD ThreadStartFunc(LPVOID param);
80 #elif defined(_WIN32)
81         typedef DWORD WINAPI ThreadStartFunc(LPVOID param);
82 #endif
83
84
85 inline threadid_t thr_get_current_thread_id()
86 {
87 #if USE_CPP11_THREADS
88         return std::this_thread::get_id();
89 #elif USE_WIN_THREADS
90         return GetCurrentThreadId();
91 #elif USE_POSIX_THREADS
92         return pthread_self();
93 #endif
94 }
95
96 inline bool thr_compare_thread_id(threadid_t thr1, threadid_t thr2)
97 {
98 #if USE_POSIX_THREADS
99         return pthread_equal(thr1, thr2);
100 #else
101         return thr1 == thr2;
102 #endif
103 }
104
105 inline bool thr_is_current_thread(threadid_t thr)
106 {
107         return thr_compare_thread_id(thr_get_current_thread_id(), thr);
108 }
109
110 #endif