3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #ifndef THREADS_HEADER
21 #define THREADS_HEADER
24 // Determine which threading APIs we will use
26 #if __cplusplus >= 201103L
27 #define USE_CPP11_THREADS 1
29 #define USE_WIN_THREADS 1
31 #define USE_POSIX_THREADS 1
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
40 #define USE_POSIX_MUTEX 1
48 #elif USE_POSIX_THREADS
51 #ifndef WIN32_LEAN_AND_MEAN
52 #define WIN32_LEAN_AND_MEAN
57 #include "threading/mutex.h"
60 // threadid_t, threadhandle_t
63 typedef std::thread::id threadid_t;
64 typedef std::thread::native_handle_type threadhandle_t;
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;
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);
81 typedef DWORD WINAPI ThreadStartFunc(LPVOID param);
85 inline threadid_t thr_get_current_thread_id()
88 return std::this_thread::get_id();
90 return GetCurrentThreadId();
91 #elif USE_POSIX_THREADS
92 return pthread_self();
96 inline bool thr_compare_thread_id(threadid_t thr1, threadid_t thr2)
99 return pthread_equal(thr1, thr2);
105 inline bool thr_is_current_thread(threadid_t thr)
107 return thr_compare_thread_id(thr_get_current_thread_id(), thr);