8#ifndef META_OCEAN_BASE_THREAD_H
9#define META_OCEAN_BASE_THREAD_H
143 inline uint64_t
hash()
const;
172 explicit inline ThreadId(
const uint64_t value);
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>
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:266
void relock(Lock &lock)
Re-locks this scoped lock with a given lock.
Definition Lock.h:587
Lock * lock() const
Returns the lock object which (if existing) is locked during the existence of this scoped lock object...
Definition Lock.h:571
void release()
Explicitly releases the lock before the scoped lock object is released.
Definition Lock.h:576
bool isReleased() const
Returns whether this scoped lock is released already.
Definition Lock.h:598
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
uint64_t value_
The value of the thread id.
Definition Thread.h:183
friend class Thread
Definition Thread.h:123
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.
unsigned int threadRandomNumberSeedValue_
The seed value for random number generators.
Definition Thread.h:459
HANDLE threadHandle_
Internal windows thread handle.
Definition Thread.h:437
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
@ PRIORTY_REALTIME
The thread has a real time priority.
Definition Thread.h:202
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.
bool threadIsActive_
Determines whether this thread is actually running.
Definition Thread.h:450
pthread_t threadObject_
Internal pthread object.
Definition Thread.h:442
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.
std::pair< pthread_t, bool > TimedJoinPair
Definition of a pair holding a thread id and a boolean state.
Definition Thread.h:213
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 threadIsInvokedToStart_
Determines whether this thread has been invoked to start immediately.
Definition Thread.h:453
bool shouldThreadStop() const
Returns whether this thread should stop.
static ThreadPriority threadPriority()
Returns the priority of the current thread.
std::atomic< bool > threadShouldStop_
Determines whether this thread should stop.
Definition Thread.h:447
This class implements a timestamp.
Definition Timestamp.h:64
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:633
The namespace covering the entire Ocean framework.
Definition Accessor.h:15