35 template <
typename DERIVED>
47 template<
typename Callback>
57 explicit threadData(Callback c);
67 static void*
run(
void* arg);
75 [[nodiscard]]
virtual bool valid()
const = 0;
107 explicit operator
bool() const;
113 bool operator!() const;
160 [[nodiscard]]
bool valid()
const override;
177 template<
typename Callback,
typename... ARGS>
178 explicit pThread(Callback c, ARGS&&... args);
206 [[nodiscard]]
bool joinable()
const override;
219 void join()
override;
279 enum class joinPolicy {
284 [[nodiscard]]
bool valid()
const override;
302 static constexpr auto AUTO_JOIN = joinPolicy::AUTO_JOIN;
305 static constexpr auto AUTO_DETACH = joinPolicy::AUTO_DETACH;
321 template<
typename Callback,
typename... ARGS>
322 explicit thread(Callback c, ARGS&&... args);
335 template<
typename Callback,
typename... ARGS>
336 explicit thread(Callback c, joinPolicy policy, ARGS&&... args);
344 explicit thread(
pThread p_thread, joinPolicy policy = AUTO_JOIN);
383 [[nodiscard]]
bool joinable()
const override;
387 u_integer toHash() const noexcept override;
389 std::
string className() const override;
396 void join() override;
403 void detach() override;
415template <typename DERIVED>
416template <typename Callback>
420template <
typename DERIVED>
421template <
typename Callback>
427 }
catch (
const error&) {
428 throw sysError(
"Thread callback execution failed");
433template <
typename DERIVED>
436 return this->valid();
439template <
typename DERIVED>
442 return !this->valid();
445template<
typename DERIVED>
450template<
typename DERIVED>
452 std::stringstream ss;
453 ss <<
"(" << this->className() <<
" ";
454 ss <<
"#" << this->id();
463template<
typename Callback,
typename... ARGS>
467 [func = std::forward<Callback>(c), ...lambda_args = std::forward<ARGS>(args)]()
mutable {
468 std::invoke(std::move(func), std::move(lambda_args)...);
471 using bound_callback =
decltype(bound_lambda);
472 using bound_thread_data = threadData<bound_callback>;
474 auto task =
new bound_thread_data(std::move(bound_lambda));
476 if (
const int code = pthread_create(&this->handle,
nullptr, &bound_thread_data::run, task); code != 0)
483inline bool original::pThread::valid()
const
485 return this->handle != pthread_t{};
490 this->operator=(std::move(other));
495 if (
this == &other) {
499 if (this->is_joinable && this->valid()) {
500 pthread_detach(this->handle);
503 this->handle = other.handle;
505 this->is_joinable = other.is_joinable;
506 other.is_joinable =
false;
512 std::memcpy(&
id, &this->handle,
sizeof(pthread_t));
518 return this->is_joinable;
522original::pThread::compareTo(
const pThread& other)
const {
523 if (this->
id() != other.
id())
524 return this->id() > other.
id() ? 1 : -1;
538 if (this->is_joinable){
539 if (
const int code = pthread_join(this->handle,
nullptr);
541 throw sysError(
"Failed to join thread (pthread_join returned " + formatString(code) +
")");
543 this->is_joinable =
false;
549 if (this->is_joinable){
550 if (
const int code = pthread_detach(this->handle);
552 throw sysError(
"Failed to detach thread (pthread_detach returned " + formatString(code) +
")");
554 this->is_joinable =
false;
561 if (this->is_joinable) {
565 std::cerr <<
"Fatal error in pThread destructor" << std::endl;
571inline bool original::thread::valid()
const
573 return this->thread_.operator bool();
577original::thread::thisId() {
579#ifdef ORIGINAL_COMPILER_GCC
580 auto handle = pthread_self();
581 std::memcpy(&
id, &handle,
sizeof(pthread_t));
591#ifdef ORIGINAL_COMPILER_GCC
593 const auto ts = deadline.toTimespec();
597 if (ret = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts,
nullptr)
599 if (errno == EINTR)
continue;
600 if (errno == EINVAL) {
603 throw sysError(
"Failed to sleep thread (clock_nano-sleep returned " + formatString(ret) +
604 ", errno: " + std::to_string(errno) +
")");
614template <
typename Callback,
typename ... ARGS>
616 : thread_(std::forward<Callback>(c), std::forward<ARGS>(args)...), will_join(true) {}
618template <
typename Callback,
typename ... ARGS>
620 : thread_(std::forward<Callback>(c), std::forward<ARGS>(args)...), will_join(policy == AUTO_JOIN) {}
623 : thread_(std::move(p_thread)), will_join(policy == AUTO_JOIN) {}
626 : thread_(std::move(other.thread_)), will_join(
true) {}
629 : thread_(std::move(other.thread_)), will_join(policy == AUTO_JOIN) {}
632 if (
this == &other) {
636 this->thread_ = std::move(other.thread_);
637 this->will_join = other.will_join;
638 other.will_join =
false;
643original::thread::compareTo(
const thread& other)
const {
644 return this->thread_.compareTo(other.thread_);
649 return this->thread_.toHash();
658 this->thread_.join();
663 this->thread_.detach();
669 this->will_join ? this->thread_.join() : this->thread_.detach();
671 std::cerr <<
"Fatal error in thread destructor: " << e.
what() << std::endl;
677 return this->thread_.id();
682 return this->thread_.joinable();
Base class for comparable objects.
Definition comparable.h:32
virtual integer compareTo(const DERIVED &other) const =0
Compares the current object with another of the same type.
Base class for all exceptions in the Original project.
Definition error.h:53
const char * what() const noexcept override
Returns the full error message.
Definition error.h:92
static u_integer hashFunc(const T &t) noexcept
Default hash function fallback.
Forward declaration of hashable interface template.
Definition hash.h:206
virtual u_integer toHash() const noexcept
Computes the hash of the object.
Definition hash.h:333
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
POSIX thread implementation.
Definition thread.h:152
std::string className() const override
Gets the class name for type identification.
Definition thread.h:533
void join() override
Wait for thread to complete.
Definition thread.h:537
pThread & operator=(pThread &&other) noexcept
Move assignment.
Definition thread.h:493
void detach() override
Detach thread (allow it to run independently)
Definition thread.h:548
u_integer toHash() const noexcept override
Computes the hash of the object.
Definition thread.h:529
ul_integer id() const override
Get thread identifier.
Definition thread.h:510
~pThread() override
Destructor.
Definition thread.h:559
pThread()
Construct empty (invalid) thread.
Definition thread.h:461
bool joinable() const override
Check if thread is joinable.
Definition thread.h:516
Base class providing polymorphic string conversion capabilities.
Definition printable.h:39
static std::string formatString(const TYPE &t)
Universal value-to-string conversion.
Definition printable.h:263
Exception for generic system failure.
Definition error.h:306
Wrapper for thread execution data.
Definition thread.h:49
static void * run(void *arg)
Thread entry point wrapper.
Definition thread.h:422
Base class for thread implementations.
Definition thread.h:38
std::string toString(bool enter) const override
Generates formatted string representation.
Definition thread.h:451
virtual ul_integer id() const =0
Get thread identifier.
virtual void join()=0
Wait for thread to complete execution.
virtual bool valid() const =0
Check if thread is valid.
bool operator!() const
Check if thread is not valid.
Definition thread.h:440
std::string className() const override
Gets the class name for type identification.
Definition thread.h:446
threadBase & operator=(const threadBase &)=delete
Deleted copy assignment.
threadBase() noexcept=default
Default constructor.
virtual void detach()=0
Detach thread from handle.
virtual bool joinable() const =0
Check if thread is joinable.
High-level thread wrapper.
Definition thread.h:263
static void sleep(const time::duration &d)
Puts the current thread to sleep for a specified duration.
Definition thread.h:586
void detach() override
Detach thread (allow it to run independently)
Definition thread.h:661
void join() override
Wait for thread to complete.
Definition thread.h:656
thread & operator=(const thread &)=delete
Deleted copy assignment.
thread(const thread &)=delete
Deleted copy constructor.
bool joinable() const override
Check if thread is joinable.
Definition thread.h:680
thread()
Construct empty thread.
Definition thread.h:611
ul_integer id() const override
Get thread identifier.
Definition thread.h:676
u_integer toHash() const noexcept override
Computes the hash of the object.
Definition thread.h:648
~thread() override
Destructor.
Definition thread.h:666
std::string className() const override
Gets the class name for type identification.
Definition thread.h:652
Represents a time duration with nanosecond precision.
Definition zeit.h:134
time_val_type value(unit unit=MILLISECOND) const
Gets the duration value in specified units.
Definition zeit.h:1127
static point now()
Gets current time point.
Definition zeit.h:1352
static constexpr time_val_type FACTOR_MILLISECOND
Conversion factor for milliseconds.
Definition zeit.h:95
Custom exception classes and callback validation utilities.
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
std::uint64_t ul_integer
64-bit unsigned integer type
Definition config.h:274
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:254
Main namespace for the project Original.
Definition algorithms.h:21
Exclusive-ownership smart pointer implementation.
Time and date handling utilities.