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"
150 template <
class Function,
152 typename std::enable_if<!std::is_arithmetic<Function>::value,
bool>::type =
true>
153 Thread(Function&& func, Args&&... args);
169 unsigned int priority() const noexcept;
171 const
char*
name() const noexcept;
187 template <class Function, class... Args>
193 template <typename Rep, typename Period>
194 static
Error Sleep(const std::chrono::duration<Rep, Period>& sleep_duration) noexcept;
197 using FunctionType = std::function<
void()>;
207 extern "C" inline void CppThreadFn(
void* arg)
209 std::unique_ptr<Thread::FunctionType> p_func(
static_cast<Thread::FunctionType*
>(arg));
224 template <
class Function,
class... Args,
typename std::enable_if<!std::is_arithmetic<Function>::value,
bool>::type>
228 auto result =
Start(std::forward<Function>(func), std::forward<Args>(args)...);
230 ETCPAL_THROW(std::runtime_error(
"Error while starting EtcPal thread: " + result.ToString()));
242 inline Thread::Thread(
unsigned int priority,
unsigned int stack_size,
const char* name,
void* platform_data)
243 : params_{priority, stack_size, name, platform_data}
264 *
this = std::move(other);
274 thread_ = std::move(other.thread_);
275 params_ = other.params_;
283 return (
bool)thread_;
360 params_.thread_name = name;
374 params_.thread_name = name.c_str();
427 template <
class Function,
class... Args>
438 auto new_f = std::unique_ptr<FunctionType>(
new FunctionType(
439 std::bind(std::forward<Function>(func), std::forward<Args>(args)...)));
490 return terminate_res;
505 template <
typename Rep,
typename Period>
509 unsigned int sleep_ms_clamped =
static_cast<unsigned int>(
510 std::min(std::chrono::milliseconds(sleep_duration).count(),
511 static_cast<std::chrono::milliseconds::rep
>(std::numeric_limits<unsigned int>::max())));
512 return 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:146
Error Terminate() noexcept
Forcefully kill the thread.
Definition: thread.h:482
unsigned int stack_size() const noexcept
Get the stack size of this thread in bytes (not valid on all platforms).
Definition: thread.h:293
etcpal_thread_os_handle_t os_handle() const noexcept
Get the native OS handle of this thread.
Definition: thread.h:319
unsigned int priority() const noexcept
Get the priority of this thread (not valid on all platforms).
Definition: thread.h:287
Thread & SetPlatformData(void *platform_data) noexcept
Set the platform-specific parameter data.
Definition: thread.h:386
bool joinable() const noexcept
Whether the thread object identifies an active thread of execution.
Definition: thread.h:281
const char * name() const noexcept
Get the name of this thread.
Definition: thread.h:299
Thread & SetStackSize(unsigned int stack_size) noexcept
Set the stack size of this thread in bytes.
Definition: thread.h:344
static Error Sleep(unsigned int ms) noexcept
Blocks the current thread for the specified number of milliseconds.
Definition: thread.h:497
Error Start(Function &&func, Args &&... args)
Associate this thread object with a new thread of execution.
Definition: thread.h:428
Thread & operator=(Thread &&other) noexcept
Move another thread into this thread.
Definition: thread.h:272
Thread(const Thread &other)=delete
Deleted copy constructor - threads are not copyable.
virtual ~Thread()
Destroy the thread object.
Definition: thread.h:251
Thread & SetParams(const EtcPalThreadParams ¶ms) noexcept
Set this thread's parameters from an existing EtcPalThreadParams struct.
Definition: thread.h:399
const EtcPalThreadParams & params() const noexcept
Get a reference the parameters of this thread.
Definition: thread.h:311
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:305
Thread & SetPriority(unsigned int priority) noexcept
Set the priority of this thread.
Definition: thread.h:331
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:358
Error Join(int timeout_ms=ETCPAL_WAIT_FOREVER) noexcept
Wait for the thread to finish execution.
Definition: thread.h:462
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.
etcpal_error_t etcpal_thread_sleep(unsigned int sleep_ms)
Provides a platform-neutral sleep.
#define ETCPAL_THREAD_SET_DEFAULT_PARAMS(threadparamsptr)
Set the platform-default values for the EtcPalThreadParams struct.
Definition: thread.h:163
#define ETCPAL_THREAD_PARAMS_INIT_VALUES
The set of default values for an EtcPalThreadParamsStructure.
Definition: thread.h:180
PLATFORM_DEFINED etcpal_thread_os_handle_t
The native OS handle type for threads on this platform.
Definition: thread.dox:42
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:118
A set of parameters for an etcpal_thread.
Definition: thread.h:130
unsigned int priority
The priority of the thread.
Definition: thread.h:132
void * platform_data
Pointer to a platform-specific parameter structure.
Definition: thread.h:159
unsigned int stack_size
The stack size of the thread in bytes.
Definition: thread.h:141
const char * thread_name
A name for the thread, maximum length ETCPAL_THREAD_NAME_MAX_LENGTH.
Definition: thread.h:143