forked from AlexanderBabansky/rtaudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSuspendable.cpp
More file actions
159 lines (146 loc) · 3.91 KB
/
ThreadSuspendable.cpp
File metadata and controls
159 lines (146 loc) · 3.91 KB
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
#include "ThreadSuspendable.h"
#include <cassert>
namespace {
#ifndef WIN32
inline int clampPriority(int priority, int policy)
{
int min = sched_get_priority_min(priority);
int max = sched_get_priority_max(priority);
return std::clamp(policy, min, max);
}
static void *threadMethodJump(void *user)
{
ThreadSuspendable *c = static_cast<ThreadSuspendable *>(user);
c->threadMethod();
pthread_exit(NULL);
return nullptr;
}
pthread_attr_t setupAttrsPriority(int priority)
{
pthread_attr_t attr{};
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
#ifdef SCHED_RR // Undefined with some OSes (e.g. NetBSD 1.6.x with GNU Pthread)
struct sched_param param
{};
priority = clampPriority(priority, SCHED_RR);
param.sched_priority = priority;
// Set the policy BEFORE the priority. Otherwise it fails.
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
// This is definitely required. Otherwise it fails.
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedparam(&attr, ¶m);
#else
pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
#endif
return attr;
}
#endif // !WIN32
} // namespace
ThreadSuspendable::ThreadSuspendable(std::function<bool()> process, bool realtime, int priority)
: mProcess(process)
{
#ifdef WIN32
mThread = std::thread(&ThreadSuspendable::threadMethod, this);
#else
pthread_attr_t attr = setupAttrsPriority(priority);
int result = pthread_create(&mThread, &attr, threadMethodJump, this);
pthread_attr_destroy(&attr);
if (result != 0) {
pthread_create(&mThread, nullptr, threadMethodJump, this);
}
#endif
}
ThreadSuspendable::~ThreadSuspendable()
{
stop();
}
void ThreadSuspendable::resume()
{
std::unique_lock g(mMutex);
if (mState == State::STOPPED)
return;
if (mState == State::RESUMING || mState == State::RUNNING)
return;
mState = State::RESUMING;
mCV.notify_all();
}
void ThreadSuspendable::suspend()
{
std::unique_lock g(mMutex);
if (mState == State::STOPPED)
return;
if (mState == State::SUSPENDED)
return;
mState = State::SUSPENDING;
mCV.notify_all();
while (mState != State::SUSPENDED) {
mCV.wait(g);
}
}
void ThreadSuspendable::stop()
{
std::unique_lock g(mMutex);
if (mState == State::STOPPED)
return;
mState = State::STOPPING;
mCV.notify_all();
while (mState != State::STOPPED) {
mCV.wait(g);
}
#ifdef WIN32
if (mThread.joinable()) {
mThread.join();
}
#else
if (mThread) {
pthread_join(mThread, 0);
}
#endif
}
bool ThreadSuspendable::isValid() const
{
#ifdef WIN32
return mThread.joinable();
#else
return mThread != 0;
#endif
}
void ThreadSuspendable::threadMethod()
{
bool lastProcessResult = true;
while (true) {
{
std::unique_lock g(mMutex);
if (lastProcessResult == false) {
mState = State::STOPPING;
}
while (mState != State::RUNNING) {
switch (mState) {
case State::SUSPENDED:
mCV.wait(g);
break;
case State::RESUMING:
mState = State::RUNNING;
mCV.notify_all();
break;
case State::SUSPENDING:
mState = State::SUSPENDED;
mCV.notify_all();
break;
case State::STOPPING:
mState = State::STOPPED;
mCV.notify_all();
return;
case State::RUNNING:
case State::STOPPED:
default:
assert(false);
return;
}
}
}
lastProcessResult = mProcess();
}
}