-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.h
134 lines (105 loc) · 2.69 KB
/
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
134
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* thread.h
*
* This file defines a platform-inspecific thread wrapper
*/
#ifndef THOR_THREAD_H
#define THOR_THREAD_H
#pragma once
#ifndef THOR_REF_COUNTED_H
#include "ref_counted.h"
#endif
#ifndef THOR_BASIC_STRING_H
#include "basic_string.h"
#endif
#if defined(_WIN32)
#include "win/thread_base_win.inl"
#else
#error Unsupported platform!
#endif
namespace thor
{
class thread_id : public internal::thread_id_base
{
friend class thread;
thread_id(const internal::thread_id_base& rhs);
public:
bool is_current_thread() const { return get_current_thread() == *this; }
static thread_id get_current_thread();
};
class thread : public ref_counted<policy::thread_safe_ref_count, policy::delete_this_on_zero_refcount, 0>,
public internal::thread_base
{
public:
enum thread_priority
{
very_low_priority,
low_priority,
normal_priority,
high_priority,
very_high_priority,
priority_count
};
enum thread_state
{
state_idle,
state_running,
state_stopping,
state_finished,
state_count
};
thread(const char* thread_name = 0, thread_priority priority = normal_priority, size_type stack_size = 0);
bool start(bool start_suspended = false);
bool stop(bool join = false);
bool join(size_type timeout_ms = size_type(-1));
bool wait(size_type timeout_ms = size_type(-1)) { return join(timeout_ms); }
thread_id get_thread_id() const;
bool is_stop_requested() const;
thread_state get_thread_state() const;
thread_priority get_thread_priority() const;
void set_thread_priority(thread_priority priority);
void suspend();
void resume();
bool is_suspended() const;
protected:
virtual ~thread();
virtual void begin_run();
virtual void execute() = 0;
private:
basic_string<char, 64> name_;
atomic_integer<thread_priority> priority_;
thread_state state_;
size_type stack_size_;
atomic_integer<size_type> suspend_count_;
atomic_integer<size_type> stop_requested_;
internal::thread_id_base thread_id_;
};
inline thread_id thread::get_thread_id() const
{
return thread_id(thread_id_);
}
inline bool thread::is_stop_requested() const
{
return stop_requested_ > 0;
}
inline thread::thread_state thread::get_thread_state() const
{
return state_;
}
inline thread::thread_priority thread::get_thread_priority() const
{
return priority_;
}
inline bool thread::is_suspended() const
{
return suspend_count_ != 0;
}
}
#if defined(_WIN32)
// #include "win/thread_impl.h"
#else
#error Unsupported platform!
#endif
#endif