Fawkes API  Fawkes Development Version
thread.h
1 
2 /***************************************************************************
3  * thread.h - base class for threads, implementation based on pthreads
4  *
5  * Created: Thu Sep 14 13:06:18 2006
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _CORE_THREADING_THREAD_H_
25 #define _CORE_THREADING_THREAD_H_
26 
27 #include <sys/types.h>
28 
29 #include <stdint.h>
30 #include <string>
31 
32 #define forever while (1)
33 
34 namespace fawkes {
35 
36 class WaitCondition;
37 class Mutex;
38 class Barrier;
39 class ThreadNotificationListener;
40 class ThreadLoopListener;
41 class ThreadList;
42 template <typename Type>
43 class LockList;
44 
45 class Thread
46 {
47  friend ThreadList;
48 
49 public:
50  /** Thread operation mode.
51  * A thread can operate in two different modes. In continuous mode the
52  * thread is on it's own running continuously. No timing is done. The loop() is
53  * immediately called again after it has finished once. In wait-for-wakeup mode
54  * the thread will pause after each loop and wait for an explicit wakeup.
55  */
56  typedef enum {
57  OPMODE_CONTINUOUS, /**< operate in continuous mode (default) */
58  OPMODE_WAITFORWAKEUP /**< operate in wait-for-wakeup mode */
59  } OpMode;
60 
61  /** Cancel state.
62  * The current cancel state of a thread.
63  */
64  typedef enum {
65  CANCEL_ENABLED, /**< cancellation is possible */
66  CANCEL_DISABLED /**< thread cannot be cancelled */
67  } CancelState;
68 
69  static const unsigned int FLAG_BAD;
70 
71  virtual ~Thread();
72 
73  virtual void init();
74  bool prepare_finalize();
75  virtual bool prepare_finalize_user();
76  virtual void finalize();
77  void cancel_finalize();
78 
79  void start(bool wait = true);
80  void cancel();
81  void join();
82  void detach();
83  void kill(int sig);
84 
85  bool operator==(const Thread &thread);
86 
87  void wakeup();
88  void wakeup(Barrier *barrier);
89 
90  void wait_loop_done();
91 
92  OpMode opmode() const;
93  pthread_t thread_id() const;
94  bool started() const;
95  bool cancelled() const;
96  bool detached() const;
97  bool running() const;
98  bool waiting() const;
99  const char *
100  name() const
101  {
102  return name_;
103  }
104 
105  void set_flags(uint32_t flags);
106  void set_flag(uint32_t flag);
107  void unset_flag(uint32_t flag);
108  bool flagged_bad() const;
109 
110  static Thread * current_thread();
111  static Thread * current_thread_noexc() throw();
112  static pthread_t current_thread_id();
113  static std::string current_thread_name();
114  static void current_thread_name(const std::string &thread_name);
115 
116  static void init_main();
117  static void destroy_main();
118 
119  static void set_cancel_state(CancelState new_state, CancelState *old_state = 0);
120 
121  void set_delete_on_exit(bool del);
122  void set_prepfin_hold(bool hold);
123 
124  void add_notification_listener(ThreadNotificationListener *notification_listener);
125  void remove_notification_listener(ThreadNotificationListener *notification_listener);
126 
127  void notify_of_failed_init();
128 
129  void add_loop_listener(ThreadLoopListener *loop_listener);
130  void remove_loop_listener(ThreadLoopListener *loop_listener);
131 
132 protected:
133  Thread(const char *name);
134  Thread(const char *name, OpMode op_mode);
135  void exit();
136  void test_cancel();
137  void yield();
138  virtual void run();
139 
140  void set_opmode(OpMode op_mode);
141  void set_prepfin_conc_loop(bool concurrent = true);
142  void set_coalesce_wakeups(bool coalesce = true);
143 
144  void set_name(const char *format, ...);
145 
146  virtual void once();
147  virtual void loop();
148 
149  bool wakeup_pending();
150 
152  mutable Mutex *loop_mutex;
154 
155 private:
156  Thread(const Thread &t);
157  Thread(const char *name, pthread_t id);
158  Thread & operator=(const Thread &t);
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();
163 
164  static void init_thread_key();
165  static void set_tsd_thread_instance(Thread *t);
166 
167  pthread_t thread_id_;
168 
169  Barrier * startup_barrier_;
170  mutable Mutex *sleep_mutex_;
171  WaitCondition *sleep_condition_;
172  unsigned int pending_wakeups_;
173  Barrier * barrier_;
174 
175  bool loop_done_;
176  Mutex * loop_done_mutex_;
177  WaitCondition *loop_done_waitcond_;
178 
179  bool prepfin_hold_;
180  Mutex * prepfin_hold_mutex_;
181  WaitCondition *prepfin_hold_waitcond_;
182 
183  bool started_;
184  bool cancelled_;
185  bool detached_;
186  bool waiting_for_wakeup_;
187  bool delete_on_exit_;
188  bool wait_;
189  char *name_;
190 
191  OpMode op_mode_;
192  bool prepfin_conc_loop_;
193  bool coalesce_wakeups_;
194 
195  uint32_t flags_;
196 
197  LockList<ThreadNotificationListener *> *notification_listeners_;
198 
199  LockList<ThreadLoopListener *> *loop_listeners_;
200 
201  static pthread_key_t THREAD_KEY;
202  static pthread_key_t MAIN_THREAD_KEY;
203  static pthread_mutex_t thread_key_mutex_;
204 };
205 
206 } // end namespace fawkes
207 
208 #endif
fawkes::Thread::set_delete_on_exit
void set_delete_on_exit(bool del)
Set whether the thread should be deleted on exit.
Definition: thread.cpp:1100
fawkes::Thread::init_main
static void init_main()
Initialize Thread wrapper instance for main thread.
Definition: thread.cpp:1280
fawkes::Thread::loop
virtual void loop()
Code to execute in the thread.
Definition: thread.cpp:1071
fawkes::Thread::set_prepfin_conc_loop
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Definition: thread.cpp:720
fawkes::Thread::kill
void kill(int sig)
Send signal to a thread.
Definition: thread.cpp:666
fawkes::Thread::finalize
virtual void finalize()
Finalize the thread.
Definition: thread.cpp:467
fawkes::Thread::CANCEL_ENABLED
cancellation is possible
Definition: thread.h:64
fawkes::Mutex
Definition: mutex.h:36
fawkes::Thread::current_thread_id
static pthread_t current_thread_id()
Get the ID of the currently running thread.
Definition: thread.cpp:1309
fawkes::Thread::notify_of_failed_init
void notify_of_failed_init()
Notify of failed init.
Definition: thread.cpp:1222
fawkes::WaitCondition
Definition: wait_condition.h:40
fawkes::Thread::remove_notification_listener
void remove_notification_listener(ThreadNotificationListener *notification_listener)
Remove notification listener.
Definition: thread.cpp:1175
fawkes::Thread::set_flag
void set_flag(uint32_t flag)
Set flag for the thread.
Definition: thread.cpp:1126
fawkes::Thread::wakeup
void wakeup()
Wake up thread.
Definition: thread.cpp:999
fawkes::LockList
Definition: thread.h:42
fawkes::Thread::current_thread
static Thread * current_thread()
Get the Thread instance of the currently running thread.
Definition: thread.cpp:1370
fawkes::Thread::OPMODE_CONTINUOUS
operate in continuous mode (default)
Definition: thread.h:56
fawkes::Thread::once
virtual void once()
Execute an action exactly once.
Definition: thread.cpp:1089
fawkes::Thread::yield
void yield()
Yield the processor to another thread or process.
Definition: thread.cpp:887
fawkes::Thread::add_loop_listener
void add_loop_listener(ThreadLoopListener *loop_listener)
Add loop listener.
Definition: thread.cpp:1185
fawkes::Thread::name
const char * name() const
Definition: thread.h:99
fawkes::Thread::CancelState
CancelState
Cancel state.
Definition: thread.h:63
fawkes::Thread::current_thread_name
static std::string current_thread_name()
Get the name of the current thread.
Definition: thread.cpp:1322
fawkes::Thread::test_cancel
void test_cancel()
Set cancellation point.
Definition: thread.cpp:875
fawkes::Thread::prepare_finalize
bool prepare_finalize()
Prepare finalization.
Definition: thread.cpp:379
fawkes::Thread::destroy_main
static void destroy_main()
Destroy main thread wrapper instance.
Definition: thread.cpp:1293
fawkes::ThreadLoopListener
Definition: thread_loop_listener.h:35
fawkes::Thread::cancel_finalize
void cancel_finalize()
Cancel finalization.
Definition: thread.cpp:485
fawkes::Thread::thread_id
pthread_t thread_id() const
Get ID of thread.
Definition: thread.cpp:805
fawkes::Thread::exit
void exit()
Exit the thread.
Definition: thread.cpp:586
fawkes::Thread::~Thread
virtual ~Thread()
Virtual destructor.
Definition: thread.cpp:288
fawkes::Thread::detached
bool detached() const
Check if thread has been detached.
Definition: thread.cpp:832
fawkes::Thread::running
bool running() const
Check if the thread is running.
Definition: thread.cpp:842
fawkes::Thread::set_coalesce_wakeups
void set_coalesce_wakeups(bool coalesce=true)
Set wakeup coalescing.
Definition: thread.cpp:733
fawkes::Thread::current_thread_noexc
static Thread * current_thread_noexc()
Similar to current_thread, but does never throw an exception.
Definition: thread.cpp:1385
fawkes::ThreadNotificationListener
Definition: thread_notification_listener.h:35
fawkes::Thread::detach
void detach()
Detach the thread.
Definition: thread.cpp:640
fawkes
fawkes::Thread::cancelled
bool cancelled() const
Check if thread has been cancelled.
Definition: thread.cpp:823
fawkes::Thread::CANCEL_DISABLED
thread cannot be cancelled
Definition: thread.h:65
fawkes::Thread::OPMODE_WAITFORWAKEUP
operate in wait-for-wakeup mode
Definition: thread.h:57
fawkes::Thread::init
virtual void init()
Initialize the thread.
Definition: thread.cpp:347
fawkes::Thread::set_flags
void set_flags(uint32_t flags)
Set all flags in one go.
Definition: thread.cpp:1146
fawkes::Thread::operator==
bool operator==(const Thread &thread)
Check if two threads are the same.
Definition: thread.cpp:901
fawkes::Thread::add_notification_listener
void add_notification_listener(ThreadNotificationListener *notification_listener)
Add notification listener.
Definition: thread.cpp:1166
fawkes::Thread::loopinterrupt_antistarve_mutex
Mutex * loopinterrupt_antistarve_mutex
Definition: thread.h:152
fawkes::Thread::started
bool started() const
Check if thread has been started.
Definition: thread.cpp:814
fawkes::Thread::wait_loop_done
void wait_loop_done()
Wait for the current loop iteration to finish.
Definition: thread.cpp:1056
fawkes::Thread::run
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:922
fawkes::Thread::prepare_finalize_user
virtual bool prepare_finalize_user()
Prepare finalization user implementation.
Definition: thread.cpp:428
fawkes::Thread::finalize_prepared
bool finalize_prepared
Definition: thread.h:150
fawkes::Thread
Definition: thread.h:44
fawkes::Thread::opmode
OpMode opmode() const
Get operation mode.
Definition: thread.cpp:675
fawkes::Thread::unset_flag
void unset_flag(uint32_t flag)
Unset flag.
Definition: thread.cpp:1137
fawkes::Thread::start
void start(bool wait=true)
Call this method to start the thread.
Definition: thread.cpp:503
fawkes::Thread::set_cancel_state
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
Definition: thread.cpp:1400
fawkes::Thread::loop_mutex
Mutex * loop_mutex
Definition: thread.h:151
fawkes::Thread::OpMode
OpMode
Thread operation mode.
Definition: thread.h:55
fawkes::Thread::cancel
void cancel()
Cancel a thread.
Definition: thread.cpp:650
fawkes::Thread::remove_loop_listener
void remove_loop_listener(ThreadLoopListener *loop_listener)
Remove loop listener.
Definition: thread.cpp:1194
fawkes::Barrier
Definition: barrier.h:35
fawkes::Thread::set_opmode
void set_opmode(OpMode op_mode)
Set operation mode.
Definition: thread.cpp:686
fawkes::Thread::flagged_bad
bool flagged_bad() const
Check if FLAG_BAD was set.
Definition: thread.cpp:1156
fawkes::Thread::wakeup_pending
bool wakeup_pending()
Check if wakeups are pending.
Definition: thread.cpp:1110
fawkes::Thread::set_name
void set_name(const char *format,...)
Set name of thread.
Definition: thread.cpp:752
fawkes::Thread::waiting
bool waiting() const
Check if thread is currently waiting for wakeup.
Definition: thread.cpp:861
fawkes::Thread::join
void join()
Join the thread.
Definition: thread.cpp:601
fawkes::ThreadList
Definition: thread_list.h:59
fawkes::Thread::set_prepfin_hold
void set_prepfin_hold(bool hold)
Hold prepare_finalize().
Definition: thread.cpp:785
fawkes::Thread::FLAG_BAD
static const unsigned int FLAG_BAD
Standard thread flag: "thread is bad".
Definition: thread.h:68