]> git.lizzy.rs Git - dragonfireclient.git/blob - src/threads.h
Add Valleys mapgen.
[dragonfireclient.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 API 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 ///////////////
35
36
37 #if USE_CPP11_THREADS
38         #include <thread>
39 #endif
40
41 #include "threading/mutex.h"
42
43 //
44 // threadid_t, threadhandle_t
45 //
46 #if USE_CPP11_THREADS
47         typedef std::thread::id threadid_t;
48         typedef std::thread::native_handle_type threadhandle_t;
49 #elif USE_WIN_THREADS
50         typedef DWORD threadid_t;
51         typedef HANDLE threadhandle_t;
52 #elif USE_POSIX_THREADS
53         typedef pthread_t threadid_t;
54         typedef pthread_t threadhandle_t;
55 #endif
56
57 //
58 // ThreadStartFunc
59 //
60 #if USE_CPP11_THREADS || USE_POSIX_THREADS
61         typedef void *ThreadStartFunc(void *param);
62 #elif defined(_WIN32_WCE)
63         typedef DWORD ThreadStartFunc(LPVOID param);
64 #elif defined(_WIN32)
65         typedef DWORD WINAPI ThreadStartFunc(LPVOID param);
66 #endif
67
68
69 inline threadid_t thr_get_current_thread_id()
70 {
71 #if USE_CPP11_THREADS
72         return std::this_thread::get_id();
73 #elif USE_WIN_THREADS
74         return GetCurrentThreadId();
75 #elif USE_POSIX_THREADS
76         return pthread_self();
77 #endif
78 }
79
80 inline bool thr_compare_thread_id(threadid_t thr1, threadid_t thr2)
81 {
82 #if USE_POSIX_THREADS
83         return pthread_equal(thr1, thr2);
84 #else
85         return thr1 == thr2;
86 #endif
87 }
88
89 inline bool thr_is_current_thread(threadid_t thr)
90 {
91         return thr_compare_thread_id(thr_get_current_thread_id(), thr);
92 }
93
94 #endif