35 template <
typename DERIVED>
47 template<
typename Callback>
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;
383 [[
nodiscard]]
bool joinable()
const override;
420template <
typename DERIVED>
421template <
typename Callback>
428 throw sysError(
"Thread callback execution failed with message: " +
e.message());
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();
467 [
func = std::forward<Callback>(c), ...lambda_args = std::forward<ARGS>(
args)]()
mutable {
483inline bool original::pThread::valid()
const
490 this->operator=(std::move(
other));
495 if (
this == &
other) {
499 if (this->is_joinable && this->valid()) {
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){
541 throw sysError(
"Failed to join thread (pthread_join returned " + formatString(
code) +
")");
543 this->is_joinable =
false;
549 if (this->is_joinable){
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#if ORIGINAL_COMPILER_GCC || ORIGINAL_COMPILER_CLANG
603 throw sysError(
"Failed to sleep thread (clock_nano-sleep returned " + formatString(
ret) +
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 {
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();
integer compareTo(const autoPtr &other) const override
Compare reference counters.
Definition autoPtr.h:714
u_integer toHash() const noexcept override
Compute hash value for the pointer.
Definition autoPtr.h:735
Base class for comparable objects.
Definition comparable.h:35
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:92
static u_integer hashFunc(const T &t) noexcept
Default hash function fallback.
Forward declaration of hashable interface template.
Definition hash.h:220
virtual u_integer toHash() const noexcept
Computes the hash of the object.
Definition hash.h:359
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 with type-specific formatting.
Definition printable.h:339
Exception for generic system failure.
Definition error.h:413
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
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
Standard namespace extensions for original::alternative.
Definition allocator.h:351
std::string to_string(const T &t)
std::to_string overload for printable-derived types
Definition printable.h:415
Exclusive-ownership smart pointer implementation.
Time and date handling utilities.