-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmingw.thread.h
133 lines (128 loc) · 3.77 KB
/
mingw.thread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* @file mingw.thread.h
* @brief std::thread implementation for MinGW
*
* (c) 2014 by Mega Limited, Wellsford, New Zealand
*
* This file is part of the standard threads implementation for MinGW.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright GNU LGPL version 2.1 License.
*
* You should have received a copy of the license along with this
* program.
*/
#ifndef WIN32STDTHREAD_H
#define WIN32STDTHREAD_H
#include <windows.h>
#include <functional>
#include <memory>
#include <chrono>
#include <system_error>
#include <process.h>
//instead of INVALID_HANDLE_VALUE _beginthreadex returns 0
#define _STD_THREAD_INVALID_HANDLE 0
namespace std
{
class thread
{
public:
class id
{
DWORD mId;
void clear() {mId = 0;}
friend class thread;
public:
id(DWORD aId=0):mId(aId){}
bool operator==(const id& other) const {return mId == other.mId;}
};
protected:
HANDLE mHandle;
id mThreadId;
public:
typedef HANDLE native_handle_type;
id get_id() const noexcept {return mThreadId;}
native_handle_type native_handle() const {return mHandle;}
thread(): mHandle(_STD_THREAD_INVALID_HANDLE){}
thread(thread& other)
:mHandle(other.mHandle), mThreadId(other.mThreadId)
{
other.mHandle = _STD_THREAD_INVALID_HANDLE;
other.mThreadId.clear();
}
template<class Function, class... Args>
explicit thread(Function&& f, Args&&... args)
{
typedef decltype(std::bind(f, args...)) Call;
Call* call = new Call(std::bind(f, args...));
mHandle = (HANDLE)_beginthreadex(NULL, 0, threadfunc<Call>,
(LPVOID)call, 0, (unsigned*)&(mThreadId.mId));
}
template <class Call>
static unsigned int __stdcall threadfunc(void* arg)
{
std::unique_ptr<Call> upCall(static_cast<Call*>(arg));
(*upCall)();
return (unsigned long)0;
}
bool joinable() const {return mHandle != _STD_THREAD_INVALID_HANDLE;}
void join()
{
if (get_id() == GetCurrentThreadId())
throw system_error(EDEADLK, generic_category());
if (mHandle == _STD_THREAD_INVALID_HANDLE)
throw system_error(ESRCH, generic_category());
if (!joinable())
throw system_error(EINVAL, generic_category());
WaitForSingleObject(mHandle, INFINITE);
CloseHandle(mHandle);
mHandle = _STD_THREAD_INVALID_HANDLE;
mThreadId.clear();
}
~thread()
{
if (joinable())
std::terminate();
}
thread& operator=(const thread&) = delete;
thread& operator=(thread&& other) noexcept
{
if (joinable())
std::terminate();
swap(std::forward<thread>(other));
return *this;
}
void swap(thread&& other) noexcept
{
std::swap(mHandle, other.mHandle);
std::swap(mThreadId.mId, other.mThreadId.mId);
}
static unsigned int hardware_concurrency() noexcept {return 1;}
void detach()
{
if (!joinable())
throw system_error();
mHandle = _STD_THREAD_INVALID_HANDLE;
mThreadId.clear();
}
};
namespace this_thread
{
inline thread::id get_id() {return thread::id(GetCurrentThreadId());}
inline void yield() {Sleep(0);}
template< class Rep, class Period >
void sleep_for( const std::chrono::duration<Rep,Period>& sleep_duration)
{
Sleep(chrono::duration_cast<chrono::milliseconds>(sleep_duration).count());
}
template <class Clock, class Duration>
void sleep_until(const std::chrono::time_point<Clock,Duration>& sleep_time)
{
sleep_for(sleep_time-Clock::now());
}
}
}
#endif // WIN32STDTHREAD_H