-
Notifications
You must be signed in to change notification settings - Fork 511
/
spark_wiring_thread.h
245 lines (199 loc) · 6.18 KB
/
spark_wiring_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
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
/**
******************************************************************************
* @file spark_wiring_thread.h
* @authors Matthew McGowan
* @date 03 March 2015
******************************************************************************
Copyright (c) 2015 Particle Industries, Inc. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
#ifndef SPARK_WIRING_THREAD_H
#define SPARK_WIRING_THREAD_H
#if PLATFORM_THREADING
#include "concurrent_hal.h"
#include <stddef.h>
#include <mutex>
#include <functional>
#include <type_traits>
typedef std::function<os_thread_return_t(void)> wiring_thread_fn_t;
class SingleThreadedSection {
public:
SingleThreadedSection() {
os_thread_scheduling(false, NULL);
}
~SingleThreadedSection() {
os_thread_scheduling(true, NULL);
}
};
#define SINGLE_THREADED_SECTION() SingleThreadedSection __cs;
#define SINGLE_THREADED_BLOCK() for (bool __todo = true; __todo; ) for (SingleThreadedSection __cs; __todo; __todo=0)
#define WITH_LOCK(lock) for (bool __todo = true; __todo;) for (std::lock_guard<decltype(lock)> __lock((lock)); __todo; __todo=0)
#define TRY_LOCK(lock) for (bool __todo = true; __todo; ) for (std::unique_lock<typename std::remove_reference<decltype(lock)>::type> __lock##lock((lock), std::try_to_lock); __todo &= bool(__lock##lock); __todo=0)
#else
#define SINGLE_THREADED_SECTION()
#define SINGLE_THREADED_BLOCK()
#define WITH_LOCK(x)
#define TRY_LOCK(x)
#endif
#if PLATFORM_THREADING
class Thread
{
private:
mutable os_thread_t handle = OS_THREAD_INVALID_HANDLE;
mutable wiring_thread_fn_t *wrapper = NULL;
os_thread_fn_t func_ = NULL;
void* func_param_ = NULL;
bool exited_ = false;
public:
Thread() : handle(OS_THREAD_INVALID_HANDLE) {}
Thread(const char* name, os_thread_fn_t function, void* function_param=NULL,
os_thread_prio_t priority=OS_THREAD_PRIORITY_DEFAULT, size_t stack_size=OS_THREAD_STACK_SIZE_DEFAULT)
: wrapper(NULL),
func_(function),
func_param_(function_param)
{
os_thread_create(&handle, name, priority, &Thread::run, this, stack_size);
}
Thread(const char *name, wiring_thread_fn_t function,
os_thread_prio_t priority=OS_THREAD_PRIORITY_DEFAULT, size_t stack_size=OS_THREAD_STACK_SIZE_DEFAULT)
: handle(OS_THREAD_INVALID_HANDLE), wrapper(NULL)
{
if(function) {
wrapper = new wiring_thread_fn_t(function);
os_thread_create(&handle, name, priority, &Thread::run, this, stack_size);
}
}
~Thread()
{
dispose();
}
void dispose()
{
if (!is_valid())
return;
// We shouldn't dispose of current thread
if (is_current())
return;
if (!exited_) {
join();
}
if (wrapper) {
delete wrapper;
wrapper = NULL;
}
os_thread_cleanup(handle);
handle = OS_THREAD_INVALID_HANDLE;
}
bool join()
{
return is_valid() && os_thread_join(handle)==0;
}
bool cancel()
{
return is_valid() && os_thread_exit(handle)==0;
}
bool is_valid()
{
// TODO should this also check xTaskIsTaskFinished as well?
return handle!=OS_THREAD_INVALID_HANDLE;
}
bool is_current()
{
return os_thread_is_current(handle);
}
Thread& operator = (const Thread& rhs)
{
if (this != &rhs)
{
this->handle = rhs.handle;
this->wrapper = rhs.wrapper;
this->func_ = rhs.func_;
this->func_param_ = rhs.func_param_;
this->exited_ = rhs.exited_;
rhs.handle = OS_THREAD_INVALID_HANDLE;
rhs.wrapper = NULL;
}
return *this;
}
private:
static os_thread_return_t run(void* param) {
Thread* th = (Thread*)param;
if (th->func_) {
(*(th->func_))(th->func_param_);
} else if (th->wrapper) {
(*(th->wrapper))();
}
th->exited_ = true;
os_thread_exit(nullptr);
}
};
class Mutex
{
os_mutex_t handle_;
public:
/**
* Creates a shared mutex from an existing handle.
* This is mainly used to share mutexes between dynamically linked modules.
*/
Mutex(os_mutex_t handle) : handle_(handle) {}
/**
* Creates a new mutex.
*/
Mutex() : handle_(nullptr)
{
os_mutex_create(&handle_);
}
void dispose()
{
if (handle_) {
os_mutex_destroy(handle_);
handle_ = nullptr;
}
}
void lock() { os_mutex_lock(handle_); }
bool trylock() { return os_mutex_trylock(handle_)==0; }
void unlock() { os_mutex_unlock(handle_); }
};
class RecursiveMutex
{
os_mutex_recursive_t handle_;
public:
/**
* Creates a shared mutex.
*/
RecursiveMutex(os_mutex_recursive_t handle) : handle_(handle) {}
RecursiveMutex() : handle_(nullptr)
{
os_mutex_recursive_create(&handle_);
}
void dispose()
{
if (handle_) {
os_mutex_recursive_destroy(handle_);
handle_ = nullptr;
}
}
void lock() { os_mutex_recursive_lock(handle_); }
bool trylock() { return os_mutex_recursive_trylock(handle_)==0; }
void unlock() { os_mutex_recursive_unlock(handle_); }
};
#endif // PLATFORM_THREADING
// Class implementing a dummy locking policy
class NoLockingPolicy {
public:
void lock() {
}
void unlock() {
}
};
#endif /* SPARK_WIRING_THREAD_H */