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);
119 Thread(
const char *name, OpMode op_mode);
129 void set_name(
const char *format, ...);
142 Thread(
const char *name, pthread_t
id);
144 static void * entry(
void * pthis);
145 void __constructor(
const char *name, OpMode op_mode);
146 void notify_of_failed_init();
147 void notify_of_startup();
148 void lock_sleep_mutex();
150 static void init_thread_key();
151 static void set_tsd_thread_instance(
Thread *t);
153 pthread_t __thread_id;
156 mutable Mutex *__sleep_mutex;
158 unsigned int __pending_wakeups;
162 Mutex *__loop_done_mutex;
166 Mutex *__prepfin_hold_mutex;
172 bool __waiting_for_wakeup;
173 bool __delete_on_exit;
178 bool __prepfin_conc_loop;
179 bool __coalesce_wakeups;
185 static pthread_key_t THREAD_KEY;
186 static pthread_key_t MAIN_THREAD_KEY;
187 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.
OpMode opmode() const
Get operation mode.
Wait until a given condition holds.
bool detached() const
Check if thread has been detached.
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.
pthread_t thread_id() const
Get ID of thread.
Fawkes library namespace.
bool started() const
Check if thread has been started.
virtual ~Thread()
Virtual destructor.
virtual void run()
Code to execute in the thread.
OpMode
Thread operation mode.
Thread notification listener interface.
void cancel_finalize()
Cancel finalization.
bool wakeup_pending()
Check if wakeups are pending.
thread cannot be cancelled
Thread class encapsulation of pthreads.
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.
void wait_loop_done()
Wait for the current loop iteration to finish.
Mutex * loopinterrupt_antistarve_mutex
Mutex to avoid starvation when trying to lock loop_mutex.
bool flagged_bad() const
Check if FLAG_BAD was set.
bool waiting() const
Check if thread is currently waiting for wakeup.
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 cancelled() const
Check if thread has been cancelled.
bool running() const
Check if the thread is running.
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.
static const unsigned int FLAG_BAD
Standard thread flag: "thread is bad".
void set_coalesce_wakeups(bool coalesce=true)
Set wakeup coalescing.
const char * name() const
Get name of thread.
void cancel()
Cancel a thread.
void kill(int sig)
Send signal to a thread.
void test_cancel()
Set cancellation point.
virtual void loop()
Code to execute in the thread.
void yield()
Yield the processor to another thread or process.
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.