23 #ifndef ETCPAL_CPP_THREAD_H_
24 #define ETCPAL_CPP_THREAD_H_
34 #include <type_traits>
36 #include "etcpal/common.h"
37 #include "etcpal/thread.h"
147 template <
class Function,
149 typename std::enable_if<!std::is_arithmetic<Function>::value,
bool>::type =
true>
150 Thread(Function&& func, Args&&... args);
166 unsigned int priority() const noexcept;
168 const
char*
name() const noexcept;
184 template <class Function, class... Args>
189 static
void Sleep(
unsigned int ms) noexcept;
190 template <typename Rep, typename Period>
191 static
void Sleep(const std::chrono::duration<Rep, Period>& sleep_duration) noexcept;
194 using FunctionType = std::function<
void()>;
204 extern "C" inline void CppThreadFn(
void* arg)
206 std::unique_ptr<Thread::FunctionType> p_func(
static_cast<Thread::FunctionType*
>(arg));
221 template <
class Function,
class... Args,
typename std::enable_if<!std::is_arithmetic<Function>::value,
bool>::type>
225 auto result =
Start(std::forward<Function>(func), std::forward<Args>(args)...);
227 ETCPAL_THROW(std::runtime_error(
"Error while starting EtcPal thread: " + result.ToString()));
239 inline Thread::Thread(
unsigned int priority,
unsigned int stack_size,
const char* name,
void* platform_data)
240 : params_{priority, stack_size, name, platform_data}
261 *
this = std::move(other);
271 thread_ = std::move(other.thread_);
272 params_ = other.params_;
280 return (
bool)thread_;
357 params_.thread_name = name;
371 params_.thread_name = name.c_str();
424 template <
class Function,
class... Args>
435 auto new_f = std::unique_ptr<FunctionType>(
new FunctionType(
436 std::bind(std::forward<Function>(func), std::forward<Args>(args)...)));
487 return terminate_res;
499 template <
typename Rep,
typename Period>
500 void Thread::Sleep(
const std::chrono::duration<Rep, Period>& sleep_duration) noexcept
503 unsigned int sleep_ms_clamped =
static_cast<unsigned int>(
504 std::min(std::chrono::milliseconds(sleep_duration).count(),
505 static_cast<std::chrono::milliseconds::rep
>(std::numeric_limits<unsigned int>::max())));
506 Sleep(sleep_ms_clamped);
A wrapper class for the EtcPal error type.
Definition: error.h:94
A thread class, modeled after std::thread.
Definition: thread.h:143
Error Terminate() noexcept
Forcefully kill the thread.
Definition: thread.h:479
unsigned int stack_size() const noexcept
Get the stack size of this thread in bytes (not valid on all platforms).
Definition: thread.h:290
etcpal_thread_os_handle_t os_handle() const noexcept
Get the native OS handle of this thread.
Definition: thread.h:316
unsigned int priority() const noexcept
Get the priority of this thread (not valid on all platforms).
Definition: thread.h:284
Thread & SetPlatformData(void *platform_data) noexcept
Set the platform-specific parameter data.
Definition: thread.h:383
bool joinable() const noexcept
Whether the thread object identifies an active thread of execution.
Definition: thread.h:278
const char * name() const noexcept
Get the name of this thread.
Definition: thread.h:296
Thread & SetStackSize(unsigned int stack_size) noexcept
Set the stack size of this thread in bytes.
Definition: thread.h:341
Error Start(Function &&func, Args &&... args)
Associate this thread object with a new thread of execution.
Definition: thread.h:425
Thread & operator=(Thread &&other) noexcept
Move another thread into this thread.
Definition: thread.h:269
Thread(const Thread &other)=delete
Deleted copy constructor - threads are not copyable.
virtual ~Thread()
Destroy the thread object.
Definition: thread.h:248
Thread & SetParams(const EtcPalThreadParams ¶ms) noexcept
Set this thread's parameters from an existing EtcPalThreadParams struct.
Definition: thread.h:396
const EtcPalThreadParams & params() const noexcept
Get a reference the parameters of this thread.
Definition: thread.h:308
static void Sleep(unsigned int ms) noexcept
Blocks the current thread for the specified number of milliseconds.
Definition: thread.h:491
Thread & operator=(const Thread &other)=delete
Deleted copy assignment operator - threads are not copyable.
void * platform_data() const noexcept
Get the platform-specific data of this thread.
Definition: thread.h:302
Thread & SetPriority(unsigned int priority) noexcept
Set the priority of this thread.
Definition: thread.h:328
Thread()=default
Create a new thread object which does not yet represent a thread.
Thread & SetName(const char *name) noexcept
Set the name of this thread.
Definition: thread.h:355
Error Join(int timeout_ms=ETCPAL_WAIT_FOREVER) noexcept
Wait for the thread to finish execution.
Definition: thread.h:459
Common definitions used by EtcPal C++ wrappers.
C++ wrapper and utilities for etcpal/error.h.
@ kEtcPalErrNoMem
A dynamic memory allocation failed, or there is no space left in a statically allocated array.
Definition: error.h:56
@ kEtcPalErrInvalid
An invalid argument was provided to an API function.
Definition: error.h:62
etcpal_error_t etcpal_thread_create(etcpal_thread_t *id, const EtcPalThreadParams *params, void(*thread_fn)(void *), void *thread_arg)
Create a new thread.
etcpal_error_t etcpal_thread_timed_join(etcpal_thread_t *id, int timeout_ms)
Wait for a thread to finish execution, giving up after a timeout.
PLATFORM_DEFINED etcpal_thread_t
The thread handle.
Definition: thread.dox:35
etcpal_error_t etcpal_thread_join(etcpal_thread_t *id)
Wait for a thread to finish execution.
#define ETCPAL_THREAD_OS_HANDLE_INVALID
An invalid value for the native OS thread handle type.
Definition: thread.dox:49
etcpal_error_t etcpal_thread_terminate(etcpal_thread_t *id)
Forcefully kill a thread.
#define ETCPAL_THREAD_SET_DEFAULT_PARAMS(threadparamsptr)
Set the platform-default values for the EtcPalThreadParams struct.
Definition: thread.h:160
#define ETCPAL_THREAD_PARAMS_INIT_VALUES
The set of default values for an EtcPalThreadParamsStructure.
Definition: thread.h:177
PLATFORM_DEFINED etcpal_thread_os_handle_t
The native OS handle type for threads on this platform.
Definition: thread.dox:42
void etcpal_thread_sleep(unsigned int sleep_ms)
Provides a platform-neutral sleep.
etcpal_thread_os_handle_t etcpal_thread_get_os_handle(etcpal_thread_t *id)
Get the native OS handle of an EtcPal thread.
#define ETCPAL_WAIT_FOREVER
For etcpal_ functions that take a millisecond timeout, this means to wait indefinitely.
Definition: common.h:111
A set of parameters for an etcpal_thread.
Definition: thread.h:127
unsigned int priority
The priority of the thread.
Definition: thread.h:129
void * platform_data
Pointer to a platform-specific parameter structure.
Definition: thread.h:156
unsigned int stack_size
The stack size of the thread in bytes.
Definition: thread.h:138
const char * thread_name
A name for the thread, maximum length ETCPAL_THREAD_NAME_MAX_LENGTH.
Definition: thread.h:140