aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/common/src/std_thread.h
blob: 0580221b2f8bdb836b7fa4e59a5dc462cb17ea1b (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#ifndef STD_THREAD_H_
#define STD_THREAD_H_

#define GCC_VER(x,y,z)	((x) * 10000 + (y) * 100 + (z))
#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)

#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__
// GCC 4.4 provides <thread>
#ifndef _GLIBCXX_USE_SCHED_YIELD
#define _GLIBCXX_USE_SCHED_YIELD
#endif
#include <thread>
#else

// partial std::thread implementation for win32/pthread

#include <algorithm>

#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__)
#define USE_RVALUE_REFERENCES
#endif

#ifdef __APPLE__
#import <Foundation/NSAutoreleasePool.h>
#endif

#if defined(_WIN32)
// WIN32

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#if defined(_MSC_VER) && defined(_MT)
// When linking with LIBCMT (the multithreaded C library), Microsoft recommends
// using _beginthreadex instead of CreateThread.
#define USE_BEGINTHREADEX
#include <process.h>
#endif

#ifdef USE_BEGINTHREADEX
#define THREAD_ID unsigned
#define THREAD_RETURN unsigned __stdcall
#else
#define THREAD_ID DWORD
#define THREAD_RETURN DWORD WINAPI
#endif
#define THREAD_HANDLE HANDLE

#else
// PTHREAD

#include <unistd.h>

#ifndef _POSIX_THREADS
#error unsupported platform (no pthreads?)
#endif

#include <pthread.h>

#define THREAD_ID pthread_t
#define THREAD_HANDLE pthread_t
#define THREAD_RETURN void*

#endif

namespace std
{

class thread
{
public:
    typedef THREAD_HANDLE native_handle_type;

    class id
    {
        friend class thread;
    public:
        id() : m_thread(0) {}
        id(THREAD_ID _id) : m_thread(_id) {}

        bool operator==(const id& rhs) const
        {
            return m_thread == rhs.m_thread;
        }

        bool operator!=(const id& rhs) const
        {
            return !(*this == rhs);
        }

        bool operator<(const id& rhs) const
        {
            return m_thread < rhs.m_thread;
        }

    private:
        THREAD_ID m_thread;
    };

    // no variadic template support in msvc
    //template <typename C, typename... A>
    //thread(C&& func, A&&... args);

    template <typename C>
    thread(C func)
    {
        StartThread(new Func<C>(func));
    }

    template <typename C, typename A>
    thread(C func, A arg)
    {
        StartThread(new FuncArg<C, A>(func, arg));
    }

    thread() /*= default;*/ {}

#ifdef USE_RVALUE_REFERENCES
    thread(const thread&) /*= delete*/;

    thread(thread&& other)
    {
#else
    thread(const thread& t)
    {
        // ugly const_cast to get around lack of rvalue references
        thread& other = const_cast<thread&>(t);
#endif
        swap(other);
    }

#ifdef USE_RVALUE_REFERENCES
    thread& operator=(const thread&) /*= delete*/;

    thread& operator=(thread&& other)
    {
#else
    thread& operator=(const thread& t)
    {
        // ugly const_cast to get around lack of rvalue references
        thread& other = const_cast<thread&>(t);
#endif
        if (joinable())
            detach();
        swap(other);
        return *this;
    }

    ~thread()
    {
        if (joinable())
            detach();
    }

    bool joinable() const
    {
        return m_id != id();
    }

    id get_id() const
    {
        return m_id;
    }

    native_handle_type native_handle()
    {
#ifdef _WIN32
        return m_handle;
#else
        return m_id.m_thread;
#endif
    }

    void join()
    {
#ifdef _WIN32
        WaitForSingleObject(m_handle, INFINITE);
        detach();
#else
        pthread_join(m_id.m_thread, NULL);
        m_id = id();
#endif
    }

    void detach()
    {
#ifdef _WIN32
        CloseHandle(m_handle);
#else
        pthread_detach(m_id.m_thread);
#endif
        m_id = id();
    }

    void swap(thread& other)
    {
        std::swap(m_id, other.m_id);
#ifdef _WIN32
        std::swap(m_handle, other.m_handle);
#endif
    }

    static unsigned hardware_concurrency()
    {
#ifdef _WIN32
        SYSTEM_INFO sysinfo;
        GetSystemInfo(&sysinfo);
        return static_cast<unsigned>(sysinfo.dwNumberOfProcessors);
#else
        return 0;
#endif
    }

private:
    id m_id;

#ifdef _WIN32
    native_handle_type m_handle;
#endif

    template <typename F>
    void StartThread(F* param)
    {
#ifdef USE_BEGINTHREADEX
        m_handle = (HANDLE)_beginthreadex(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread);
#elif defined(_WIN32)
        m_handle = CreateThread(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread);
#else
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setstacksize(&attr, 1024 * 1024);
        if (pthread_create(&m_id.m_thread, &attr, &RunAndDelete<F>, param))
            m_id = id();
#endif
    }

    template <typename C>
    class Func
    {
    public:
        Func(C _func) : func(_func) {}

        void Run() { func(); }

    private:
        C const func;
    };

    template <typename C, typename A>
    class FuncArg
    {
    public:
        FuncArg(C _func, A _arg) : func(_func), arg(_arg) {}

        void Run() { func(arg); }

    private:
        C const func;
        A arg;
    };

    template <typename F>
    static THREAD_RETURN RunAndDelete(void* param)
    {
#ifdef __APPLE__
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
#endif
        static_cast<F*>(param)->Run();
        delete static_cast<F*>(param);
#ifdef __APPLE__
        [pool release];
#endif
        return 0;
    }
};

namespace this_thread
{

    inline void yield()
    {
#ifdef _WIN32
        SwitchToThread();
#else
        sleep(0);
#endif
    }

    inline thread::id get_id()
    {
#ifdef _WIN32
        return GetCurrentThreadId();
#else
        return pthread_self();
#endif
    }

}	// namespace this_thread

}	// namespace std

#undef USE_RVALUE_REFERENCES
#undef USE_BEGINTHREADEX
#undef THREAD_ID
#undef THREAD_RETURN
#undef THREAD_HANDLE

#endif
#endif