24 #ifndef _CORE_THREADING_THREAD_H_
25 #define _CORE_THREADING_THREAD_H_
27 #include <sys/types.h>
32 #define forever while (1)
39 class ThreadNotificationListener;
40 class ThreadLoopListener;
42 template <
typename Type>
79 void start(
bool wait =
true);
144 void set_name(const
char *format, ...);
159 static
void *entry(
void *pthis);
160 void __constructor(const
char *
name,
OpMode op_mode);
161 void notify_of_startup();
162 void lock_sleep_mutex();
164 static
void init_thread_key();
165 static
void set_tsd_thread_instance(
Thread *t);
167 pthread_t thread_id_;
170 mutable
Mutex *sleep_mutex_;
172 unsigned int pending_wakeups_;
176 Mutex * loop_done_mutex_;
180 Mutex * prepfin_hold_mutex_;
186 bool waiting_for_wakeup_;
187 bool delete_on_exit_;
192 bool prepfin_conc_loop_;
193 bool coalesce_wakeups_;
201 static pthread_key_t THREAD_KEY;
202 static pthread_key_t MAIN_THREAD_KEY;
203 static pthread_mutex_t thread_key_mutex_;
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Mutex mutual exclusion lock.
Thread loop listener interface.
Thread notification listener interface.
Thread class encapsulation of pthreads.
bool finalize_prepared
True if prepare_finalize() has been called and was not stopped with a cancel_finalize(),...
void kill(int sig)
Send signal to a thread.
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Mutex * loop_mutex
Mutex that is used to protect a call to loop().
void add_loop_listener(ThreadLoopListener *loop_listener)
Add loop listener.
const char * name() const
Get name of thread.
OpMode opmode() const
Get operation mode.
static void destroy_main()
Destroy main thread wrapper instance.
void start(bool wait=true)
Call this method to start the thread.
static void init_main()
Initialize Thread wrapper instance for main thread.
void unset_flag(uint32_t flag)
Unset flag.
static const unsigned int FLAG_BAD
Standard thread flag: "thread is bad".
@ CANCEL_ENABLED
cancellation is possible
@ CANCEL_DISABLED
thread cannot be cancelled
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
virtual ~Thread()
Virtual destructor.
void join()
Join the thread.
static pthread_t current_thread_id()
Get the ID of the currently running thread.
void yield()
Yield the processor to another thread or process.
void set_prepfin_hold(bool hold)
Hold prepare_finalize().
virtual bool prepare_finalize_user()
Prepare finalization user implementation.
void wait_loop_done()
Wait for the current loop iteration to finish.
bool waiting() const
Check if thread is currently waiting for wakeup.
bool running() const
Check if the thread is running.
bool detached() const
Check if thread has been detached.
bool started() const
Check if thread has been started.
void set_opmode(OpMode op_mode)
Set operation mode.
static Thread * current_thread_noexc()
Similar to current_thread, but does never throw an exception.
void exit()
Exit the thread.
void detach()
Detach the thread.
virtual void once()
Execute an action exactly once.
bool prepare_finalize()
Prepare finalization.
bool flagged_bad() const
Check if FLAG_BAD was set.
virtual void init()
Initialize the thread.
void set_name(const char *format,...)
Set name of thread.
void add_notification_listener(ThreadNotificationListener *notification_listener)
Add notification listener.
void wakeup()
Wake up thread.
virtual void loop()
Code to execute in the thread.
void remove_loop_listener(ThreadLoopListener *loop_listener)
Remove loop listener.
pthread_t thread_id() const
Get ID of thread.
void test_cancel()
Set cancellation point.
void set_flag(uint32_t flag)
Set flag for the thread.
bool wakeup_pending()
Check if wakeups are pending.
void cancel()
Cancel a thread.
void cancel_finalize()
Cancel finalization.
Mutex * loopinterrupt_antistarve_mutex
Mutex to avoid starvation when trying to lock loop_mutex.
static std::string current_thread_name()
Get the name of the current thread.
bool cancelled() const
Check if thread has been cancelled.
static Thread * current_thread()
Get the Thread instance of the currently running thread.
void notify_of_failed_init()
Notify of failed init.
virtual void run()
Code to execute in the thread.
bool operator==(const Thread &thread)
Check if two threads are the same.
void set_delete_on_exit(bool del)
Set whether the thread should be deleted on exit.
void remove_notification_listener(ThreadNotificationListener *notification_listener)
Remove notification listener.
void set_flags(uint32_t flags)
Set all flags in one go.
virtual void finalize()
Finalize the thread.
OpMode
Thread operation mode.
@ OPMODE_CONTINUOUS
operate in continuous mode (default)
@ OPMODE_WAITFORWAKEUP
operate in wait-for-wakeup mode
void set_coalesce_wakeups(bool coalesce=true)
Set wakeup coalescing.
Wait until a given condition holds.
Fawkes library namespace.