-
Notifications
You must be signed in to change notification settings - Fork 3
/
tinythread_t.h
147 lines (113 loc) · 3.84 KB
/
tinythread_t.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Copyright (c) 2012 Jared Duke
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef _TINYTHREAD_T_H_
#define _TINYTHREAD_T_H_
#include "tinythread.h"
#if defined(_TTHREAD_POSIX_)
#include <unistd.h>
#include <map>
#elif defined(_TTHREAD_WIN32_)
#include <process.h>
#endif
#include <memory>
/// @file
namespace tthread {
/// Thread class with template argument support.
class threadt : public thread {
public:
threadt() : thread() { }
threadt(threadt&& other) : thread(std::move(other)) { }
threadt& operator=(threadt&& other) {
thread::swap(std::move(other));
return *this;
}
template< typename thread_func_t >
threadt(thread_func_t func) : thread() {
init(std::move(func));
}
#if defined(_TTHREAD_VARIADIC_)
template< typename thread_func_t, typename Args... >
threadt(thread_func_t func, Args... args) : thread() {
init(std::bind(std::move(func), std::move(args)...));
}
#endif
protected:
_TTHREAD_DISABLE_ASSIGNMENT(threadt);
template< typename thread_func_t >
void init(thread_func_t func);
template< typename thread_func_t >
struct _thread_start_info_t {
thread_func_t mFunction; ///< Handle to the function to be executed.
thread::data_ptr mThreadData; ///< Pointer to the thread data.
_thread_start_info_t(thread_func_t&& func, thread::data_ptr threadData)
: mFunction(std::move(func)), mThreadData(threadData) { }
};
template< class thread_func_t >
#if defined(_TTHREAD_WIN32_)
static unsigned WINAPI wrapper_function(void* aArg);
#elif defined(_TTHREAD_POSIX_)
static void* wrapper_function(void* aArg);
#endif
};
template< typename thread_func_t >
void threadt::init(thread_func_t func) {
// Serialize access to this thread structure
lock_guard<mutex> guard(mData->mMutex);
// Fill out the thread startup information (passed to the thread wrapper,
// which will eventually free it)
typedef _thread_start_info_t<thread_func_t> thread_info;
std::unique_ptr<thread_info> ti( new thread_info(std::move(func), mData) );
thread_data& data = *mData;
// The thread is now alive
data.mNotAThread = false;
// Create the thread
#if defined(_TTHREAD_WIN32_)
data.mHandle = (HANDLE) _beginthreadex(0, 0, wrapper_function<thread_func_t>, (void*) ti.get(), 0, &data.mWin32ThreadID);
#elif defined(_TTHREAD_POSIX_)
if (pthread_create(&data.mHandle, NULL, wrapper_function<thread_func_t>, (void*) ti.get()) != 0)
data.mHandle = 0;
#endif
// Did we fail to create the thread?
if (!data.mHandle) {
data.mNotAThread = true;
} else {
// Release ownership of the thread info object
ti.release();
}
}
template< class thread_func_t >
#if defined(_TTHREAD_WIN32_)
unsigned WINAPI threadt::wrapper_function(void* aArg)
#elif defined(_TTHREAD_POSIX_)
void* threadt::wrapper_function(void* aArg)
#endif
{
typedef _thread_start_info_t<thread_func_t> thread_info;
// Get thread startup information
std::unique_ptr<thread_info> ti((thread_info*)aArg);
try {
ti->mFunction();
} catch (...) {
std::terminate();
}
lock_guard<mutex> guard(ti->mThreadData->mMutex);
ti->mThreadData->mNotAThread = true;
return 0;
}
}
#endif // _TINYTHREAD_T_H_