24 #ifndef __CORE_THREADING_THREAD_H_ 25 #define __CORE_THREADING_THREAD_H_ 27 #include <sys/types.h> 30 #define forever while (1) 38 class ThreadNotificationListener;
75 void start(
bool wait=
true);
95 const char *
name()
const {
return __name; }
109 static void set_cancel_state(CancelState new_state, CancelState *old_state = 0);
121 Thread(
const char *name, OpMode op_mode);
131 void set_name(
const char *format, ...);
144 Thread(
const char *name, pthread_t
id);
146 static void * entry(
void * pthis);
147 void __constructor(
const char *name, OpMode op_mode);
148 void notify_of_startup();
149 void lock_sleep_mutex();
151 static void init_thread_key();
152 static void set_tsd_thread_instance(
Thread *t);
154 pthread_t __thread_id;
157 mutable Mutex *__sleep_mutex;
159 unsigned int __pending_wakeups;
163 Mutex *__loop_done_mutex;
167 Mutex *__prepfin_hold_mutex;
173 bool __waiting_for_wakeup;
174 bool __delete_on_exit;
179 bool __prepfin_conc_loop;
180 bool __coalesce_wakeups;
186 static pthread_key_t THREAD_KEY;
187 static pthread_key_t MAIN_THREAD_KEY;
188 static pthread_mutex_t __thread_key_mutex;
bool operator==(const Thread &thread)
Check if two threads are the same.
Thread(const char *name)
Constructor.
void add_notification_listener(ThreadNotificationListener *notification_listener)
Add notification listener.
Wait until a given condition holds.
virtual void once()
Execute an action exactly once.
void unset_flag(uint32_t flag)
Unset flag.
bool finalize_prepared
True if prepare_finalize() has been called and was not stopped with a cancel_finalize(), false otherwise.
Fawkes library namespace.
virtual ~Thread()
Virtual destructor.
virtual void run()
Code to execute in the thread.
OpMode
Thread operation mode.
bool running() const
Check if the thread is running.
Thread notification listener interface.
void cancel_finalize()
Cancel finalization.
bool flagged_bad() const
Check if FLAG_BAD was set.
bool wakeup_pending()
Check if wakeups are pending.
thread cannot be cancelled
Thread class encapsulation of pthreads.
bool waiting() const
Check if thread is currently waiting for wakeup.
static void init_main()
Initialize Thread wrapper instance for main 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().
virtual bool prepare_finalize_user()
Prepare finalization user implementation.
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
static Thread * current_thread_noexc()
Similar to current_thread, but does never throw an exception.
bool cancelled() const
Check if thread has been cancelled.
void wait_loop_done()
Wait for the current loop iteration to finish.
OpMode opmode() const
Get operation mode.
Mutex * loopinterrupt_antistarve_mutex
Mutex to avoid starvation when trying to lock loop_mutex.
void wakeup()
Wake up thread.
void set_name(const char *format,...)
Set name of thread.
static pthread_t current_thread_id()
Get the ID of the currently running thread.
static void destroy_main()
Destroy main thread wrapper instance.
virtual void finalize()
Finalize the thread.
bool started() const
Check if thread has been started.
bool prepare_finalize()
Prepare finalization.
operate in continuous mode (default)
void set_delete_on_exit(bool del)
Set whether the thread should be deleted on exit.
void set_opmode(OpMode op_mode)
Set operation mode.
static Thread * current_thread()
Get the Thread instance of the currently running thread.
void remove_notification_listener(ThreadNotificationListener *notification_listener)
Remove notification listener.
const char * name() const
Get name of thread.
void notify_of_failed_init()
Notify of failed init.
static const unsigned int FLAG_BAD
Standard thread flag: "thread is bad".
void set_coalesce_wakeups(bool coalesce=true)
Set wakeup coalescing.
void cancel()
Cancel a thread.
void kill(int sig)
Send signal to a thread.
void test_cancel()
Set cancellation point.
bool detached() const
Check if thread has been detached.
virtual void loop()
Code to execute in the thread.
void yield()
Yield the processor to another thread or process.
pthread_t thread_id() const
Get ID of thread.
void detach()
Detach the thread.
void join()
Join the thread.
void set_prepfin_hold(bool hold)
Hold prepare_finalize().
virtual void init()
Initialize the thread.
void set_flag(uint32_t flag)
Set flag for the thread.
operate in wait-for-wakeup mode
Mutex mutual exclusion lock.
void exit()
Exit the thread.
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
void set_flags(uint32_t flags)
Set all flags in one go.
void start(bool wait=true)
Call this method to start the thread.