8#ifndef META_OCEAN_BASE_THREAD_H
9#define META_OCEAN_BASE_THREAD_H
136 inline bool isValid()
const;
143 inline uint64_t hash()
const;
150 inline bool operator==(
const ThreadId&
id)
const;
157 inline bool operator!=(
const ThreadId&
id)
const;
164 inline bool operator<(
const ThreadId&
id)
const;
172 explicit inline ThreadId(
const uint64_t value);
178 static constexpr uint64_t invalidThreadId();
183 uint64_t value_ = invalidThreadId();
224 explicit Thread(
const std::string& name = std::string());
231 explicit Thread(
const unsigned int randomNumberSeedValue,
const std::string& name = std::string());
262 bool joinThread(
const unsigned int timeout = (
unsigned int)(-1));
325 template <
typename TObject,
typename TExpectedValue>
326 static bool waitForValue(TObject&
object,
const TExpectedValue& expectedValue,
const double timeout = -1.0);
340 template <
typename TObject,
typename TExpectedValue>
341 static bool waitForValue(TObject&
object,
const TExpectedValue& expectedValue,
TemporaryScopedLock& temporaryScopedLock,
const double timeout = -1.0);
437 HANDLE threadHandle_ =
nullptr;
442 pthread_t threadObject_ = 0;
447 std::atomic<bool> threadShouldStop_{
false};
450 bool threadIsActive_ =
false;
453 bool threadIsInvokedToStart_ =
false;
459 unsigned int threadRandomNumberSeedValue_ = 0u;
470 return value_ != invalidThreadId();
480 return value_ ==
id.value_;
485 return !(*
this == id);
490 return hash() <
id.hash();
498template <
typename TObject,
typename TExpectedValue>
501 const Timestamp startTimestamp(timeout < 0.0 ?
false :
true);
505 if (
object == expectedValue)
521template <
typename TObject,
typename TExpectedValue>
524 ocean_assert(!temporaryScopedLock.
isReleased());
526 Lock* lock = temporaryScopedLock.
lock();
527 ocean_assert(lock !=
nullptr);
529 if (
object == expectedValue)
536 const Timestamp startTimestamp(timeout < 0.0 ?
false :
true);
540 temporaryScopedLock.
relock(*lock);
542 if (
object == expectedValue)
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a recursive scoped lock object allowing to release the lock before the scoped o...
Definition Lock.h:254
void relock(Lock &lock)
Re-locks this scoped lock with a given lock.
Definition Lock.h:525
Lock * lock() const
Returns the lock object which (if existing) is locked during the existence of this scoped lock object...
Definition Lock.h:509
void release()
Explicitly releases the lock before the scoped lock object is released.
Definition Lock.h:514
bool isReleased() const
Returns whether this scoped lock is released already.
Definition Lock.h:536
This class implements a platform independent wrapper for thread ids.
Definition Thread.h:122
uint64_t hash() const
Returns the hash value of this thread id.
Definition Thread.h:473
bool isValid() const
Returns whether this object holds a valid id.
Definition Thread.h:468
ThreadId()=default
Creates a new thread id object with invalid id value.
static constexpr uint64_t invalidThreadId()
Returns an invalid thread id value.
Definition Thread.h:493
bool operator!=(const ThreadId &id) const
Returns whether two thread id objects are not identical.
Definition Thread.h:483
bool operator<(const ThreadId &id) const
Compares two thread id objects by their hash values.
Definition Thread.h:488
bool operator==(const ThreadId &id) const
Returns whether two thread id objects are identical.
Definition Thread.h:478
This class implements a thread.
Definition Thread.h:115
static ThreadId currentThreadId()
Returns the thread id of the current (calling) thread.
void createThread()
Creates the thread itself.
bool startThread()
Starts the thread.
Thread & operator=(const Thread &thread)=delete
The disabled assign operator.
static bool setThreadPriority(const ThreadPriority priority)
Sets the priority of the current thread.
bool isThreadInvokedToStart() const
Returns whether this thread has been invoked to start immediately.
void stopThread()
Informs the thread to stop.
std::string threadName_
Name of the thread.
Definition Thread.h:456
void destroyThread()
Destroys the thread itself.
bool terminateThread()
Terminates the thread.
Thread(const Thread &thread)=delete
Disabled copy constructor.
static int pthread_timedjoin_np(pthread_t thread, void **retval, const struct timespec *abstime)
Implements a thread join function with timeout value.
ThreadPriority
Definition of different thread priority values.
Definition Thread.h:190
@ PRIORITY_IDLE
The thread runs if the system is idle.
Definition Thread.h:192
@ PRIORTY_BELOW_NORMAL
The thread has a priority below normal.
Definition Thread.h:194
@ PRIORTY_NORMAL
The thread has a normal priority.
Definition Thread.h:196
@ PRIORTY_ABOVE_NORMAL
The thread has a priority above normal.
Definition Thread.h:198
@ PRIORTY_HIGH
The thread has a high priority.
Definition Thread.h:200
static void sleep(unsigned int ms)
Sleeps the calling thread for a given time.
virtual ~Thread()
Destructs a thread object.
void internalThreadRun()
Platform independent internal thread function calling the external thread function.
static void giveUp()
Gives up the remaining thread time.
Thread(const std::string &name=std::string())
Creates a new thread object.
bool joinThread(const unsigned int timeout=(unsigned int)(-1))
Waits until this thread has been stopped.
std::pair< pthread_t, bool > TimedJoinPair
Definition of a pair holding a thread id and a boolean state.
Definition Thread.h:213
void stopThreadExplicitly(const unsigned int timeout=5000u)
Tries to stop the thread gracefully.
virtual void threadRun()=0
This function has to be overloaded in derived class.
static DWORD __stdcall staticThreadRun(void *data)
Internal thread function calling the external thread function.
static void * pthread_timedjoin_np_helper(void *threadData)
The helper function for the pthread_timedjoin_np() implementation.
static bool waitForValue(TObject &object, const TExpectedValue &expectedValue, const double timeout=-1.0)
Waits until an object/variable has an expected value.
Definition Thread.h:499
bool isThreadActive() const
Returns whether this thread is active.
static void * staticThreadRun(void *data)
Internal thread function calling the external thread function.
Thread(const unsigned int randomNumberSeedValue, const std::string &name=std::string())
Creates a new thread object.
bool shouldThreadStop() const
Returns whether this thread should stop.
static ThreadPriority threadPriority()
Returns the priority of the current thread.
This class implements a timestamp.
Definition Timestamp.h:36
bool hasTimePassed(const double seconds, const Timestamp ¤tTimestamp=Timestamp(true)) const
Returns whether a specified amount of time has passed since this timestamp.
Definition Timestamp.h:290
The namespace covering the entire Ocean framework.
Definition Accessor.h:15