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 #include <stdint.h>
29 
30 #define forever while (1)
31 
32 namespace fawkes {
33 
34 
35 class WaitCondition;
36 class Mutex;
37 class Barrier;
38 class ThreadNotificationListener;
39 class ThreadList;
40 template <typename Type> class LockList;
41 
42 class Thread {
43  public:
44  friend class ThreadList;
45 
46  /** Thread operation mode.
47  * A thread can operate in two different modes. In continuous mode the
48  * thread is on it's own running continuously. No timing is done. The loop() is
49  * immediately called again after it has finished once. In wait-for-wakeup mode
50  * the thread will pause after each loop and wait for an explicit wakeup.
51  */
52  typedef enum {
53  OPMODE_CONTINUOUS, /**< operate in continuous mode (default) */
54  OPMODE_WAITFORWAKEUP /**< operate in wait-for-wakeup mode */
55  } OpMode;
56 
57  /** Cancel state.
58  * The current cancel state of a thread.
59  */
60  typedef enum {
61  CANCEL_ENABLED, /**< cancellation is possible */
62  CANCEL_DISABLED /**< thread cannot be cancelled */
63  } CancelState;
64 
65  static const unsigned int FLAG_BAD;
66 
67  virtual ~Thread();
68 
69  virtual void init();
70  bool prepare_finalize();
71  virtual bool prepare_finalize_user();
72  virtual void finalize();
73  void cancel_finalize();
74 
75  void start(bool wait=true);
76  void cancel();
77  void join();
78  void detach();
79  void kill(int sig);
80 
81  bool operator==(const Thread &thread);
82 
83  void wakeup();
84  void wakeup(Barrier *barrier);
85 
86  void wait_loop_done();
87 
88  OpMode opmode() const;
89  pthread_t thread_id() const;
90  bool started() const;
91  bool cancelled() const;
92  bool detached() const;
93  bool running() const;
94  bool waiting() const;
95  const char * name() const { return __name; }
96 
97  void set_flags(uint32_t flags);
98  void set_flag(uint32_t flag);
99  void unset_flag(uint32_t flag);
100  bool flagged_bad() const;
101 
102  static Thread * current_thread();
103  static Thread * current_thread_noexc() throw();
104  static pthread_t current_thread_id();
105 
106  static void init_main();
107  static void destroy_main();
108 
109  static void set_cancel_state(CancelState new_state, CancelState *old_state = 0);
110 
111  void set_delete_on_exit(bool del);
112  void set_prepfin_hold(bool hold);
113 
114  void add_notification_listener(ThreadNotificationListener *notification_listener);
115  void remove_notification_listener(ThreadNotificationListener *notification_listener);
116 
117  void notify_of_failed_init();
118 
119  protected:
120  Thread(const char *name);
121  Thread(const char *name, OpMode op_mode);
122  void exit();
123  void test_cancel();
124  void yield();
125  virtual void run();
126 
127  void set_opmode(OpMode op_mode);
128  void set_prepfin_conc_loop(bool concurrent = true);
129  void set_coalesce_wakeups(bool coalesce = true);
130 
131  void set_name(const char *format, ...);
132 
133  virtual void once();
134  virtual void loop();
135 
136  bool wakeup_pending();
137 
139  mutable Mutex *loop_mutex;
141 
142  private:
143  Thread(const Thread &t);
144  Thread(const char *name, pthread_t id);
145  Thread & operator=(const Thread &t);
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();
150 
151  static void init_thread_key();
152  static void set_tsd_thread_instance(Thread *t);
153 
154  pthread_t __thread_id;
155 
156  Barrier *__startup_barrier;
157  mutable Mutex *__sleep_mutex;
158  WaitCondition *__sleep_condition;
159  unsigned int __pending_wakeups;
160  Barrier *__barrier;
161 
162  bool __loop_done;
163  Mutex *__loop_done_mutex;
164  WaitCondition *__loop_done_waitcond;
165 
166  bool __prepfin_hold;
167  Mutex *__prepfin_hold_mutex;
168  WaitCondition *__prepfin_hold_waitcond;
169 
170  bool __started;
171  bool __cancelled;
172  bool __detached;
173  bool __waiting_for_wakeup;
174  bool __delete_on_exit;
175  bool __wait;
176  char *__name;
177 
178  OpMode __op_mode;
179  bool __prepfin_conc_loop;
180  bool __coalesce_wakeups;
181 
182  uint32_t __flags;
183 
184  LockList<ThreadNotificationListener *> *__notification_listeners;
185 
186  static pthread_key_t THREAD_KEY;
187  static pthread_key_t MAIN_THREAD_KEY;
188  static pthread_mutex_t __thread_key_mutex;
189 };
190 
191 
192 } // end namespace fawkes
193 
194 #endif
bool operator==(const Thread &thread)
Check if two threads are the same.
Definition: thread.cpp:917
Thread(const char *name)
Constructor.
Definition: thread.cpp:205
void add_notification_listener(ThreadNotificationListener *notification_listener)
Add notification listener.
Definition: thread.cpp:1170
Wait until a given condition holds.
virtual void once()
Execute an action exactly once.
Definition: thread.cpp:1087
void unset_flag(uint32_t flag)
Unset flag.
Definition: thread.cpp:1138
bool finalize_prepared
True if prepare_finalize() has been called and was not stopped with a cancel_finalize(), false otherwise.
Definition: thread.h:138
Fawkes library namespace.
virtual ~Thread()
Virtual destructor.
Definition: thread.cpp:288
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:939
OpMode
Thread operation mode.
Definition: thread.h:52
bool running() const
Check if the thread is running.
Definition: thread.cpp:855
Thread notification listener interface.
void cancel_finalize()
Cancel finalization.
Definition: thread.cpp:492
bool flagged_bad() const
Check if FLAG_BAD was set.
Definition: thread.cpp:1159
bool wakeup_pending()
Check if wakeups are pending.
Definition: thread.cpp:1110
thread cannot be cancelled
Definition: thread.h:62
Thread class encapsulation of pthreads.
Definition: thread.h:42
bool waiting() const
Check if thread is currently waiting for wakeup.
Definition: thread.cpp:875
static void init_main()
Initialize Thread wrapper instance for main thread.
Definition: thread.cpp:1271
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Definition: thread.cpp:727
Mutex * loop_mutex
Mutex that is used to protect a call to loop().
Definition: thread.h:139
virtual bool prepare_finalize_user()
Prepare finalization user implementation.
Definition: thread.cpp:433
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
Definition: thread.cpp:1350
static Thread * current_thread_noexc()
Similar to current_thread, but does never throw an exception.
Definition: thread.cpp:1334
bool cancelled() const
Check if thread has been cancelled.
Definition: thread.cpp:834
void wait_loop_done()
Wait for the current loop iteration to finish.
Definition: thread.cpp:1053
OpMode opmode() const
Get operation mode.
Definition: thread.cpp:678
Mutex * loopinterrupt_antistarve_mutex
Mutex to avoid starvation when trying to lock loop_mutex.
Definition: thread.h:140
List of threads.
Definition: thread_list.h:57
void wakeup()
Wake up thread.
Definition: thread.cpp:1000
void set_name(const char *format,...)
Set name of thread.
Definition: thread.cpp:761
static pthread_t current_thread_id()
Get the ID of the currently running thread.
Definition: thread.cpp:1302
static void destroy_main()
Destroy main thread wrapper instance.
Definition: thread.cpp:1285
virtual void finalize()
Finalize the thread.
Definition: thread.cpp:473
bool started() const
Check if thread has been started.
Definition: thread.cpp:824
List with a lock.
Definition: thread.h:40
bool prepare_finalize()
Prepare finalization.
Definition: thread.cpp:383
operate in continuous mode (default)
Definition: thread.h:53
void set_delete_on_exit(bool del)
Set whether the thread should be deleted on exit.
Definition: thread.cpp:1099
void set_opmode(OpMode op_mode)
Set operation mode.
Definition: thread.cpp:690
static Thread * current_thread()
Get the Thread instance of the currently running thread.
Definition: thread.cpp:1318
void remove_notification_listener(ThreadNotificationListener *notification_listener)
Remove notification listener.
Definition: thread.cpp:1180
const char * name() const
Get name of thread.
Definition: thread.h:95
void notify_of_failed_init()
Notify of failed init.
Definition: thread.cpp:1210
static const unsigned int FLAG_BAD
Standard thread flag: "thread is bad".
Definition: thread.h:65
void set_coalesce_wakeups(bool coalesce=true)
Set wakeup coalescing.
Definition: thread.cpp:741
void cancel()
Cancel a thread.
Definition: thread.cpp:651
void kill(int sig)
Send signal to a thread.
Definition: thread.cpp:668
void test_cancel()
Set cancellation point.
Definition: thread.cpp:889
bool detached() const
Check if thread has been detached.
Definition: thread.cpp:844
virtual void loop()
Code to execute in the thread.
Definition: thread.cpp:1068
void yield()
Yield the processor to another thread or process.
Definition: thread.cpp:902
pthread_t thread_id() const
Get ID of thread.
Definition: thread.cpp:814
void detach()
Detach the thread.
Definition: thread.cpp:640
void join()
Join the thread.
Definition: thread.cpp:610
void set_prepfin_hold(bool hold)
Hold prepare_finalize().
Definition: thread.cpp:794
cancellation is possible
Definition: thread.h:61
virtual void init()
Initialize the thread.
Definition: thread.cpp:350
void set_flag(uint32_t flag)
Set flag for the thread.
Definition: thread.cpp:1126
operate in wait-for-wakeup mode
Definition: thread.h:54
Mutex mutual exclusion lock.
Definition: mutex.h:32
void exit()
Exit the thread.
Definition: thread.cpp:594
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
void set_flags(uint32_t flags)
Set all flags in one go.
Definition: thread.cpp:1148
void start(bool wait=true)
Call this method to start the thread.
Definition: thread.cpp:511
CancelState
Cancel state.
Definition: thread.h:60