Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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  protected:
118  Thread(const char *name);
119  Thread(const char *name, OpMode op_mode);
120  void exit();
121  void test_cancel();
122  void yield();
123  virtual void run();
124 
125  void set_opmode(OpMode op_mode);
126  void set_prepfin_conc_loop(bool concurrent = true);
127  void set_coalesce_wakeups(bool coalesce = true);
128 
129  void set_name(const char *format, ...);
130 
131  virtual void once();
132  virtual void loop();
133 
134  bool wakeup_pending();
135 
137  mutable Mutex *loop_mutex;
139 
140  private:
141  Thread(const Thread &t);
142  Thread(const char *name, pthread_t id);
143  Thread & operator=(const Thread &t);
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();
149 
150  static void init_thread_key();
151  static void set_tsd_thread_instance(Thread *t);
152 
153  pthread_t __thread_id;
154 
155  Barrier *__startup_barrier;
156  mutable Mutex *__sleep_mutex;
157  WaitCondition *__sleep_condition;
158  unsigned int __pending_wakeups;
159  Barrier *__barrier;
160 
161  bool __loop_done;
162  Mutex *__loop_done_mutex;
163  WaitCondition *__loop_done_waitcond;
164 
165  bool __prepfin_hold;
166  Mutex *__prepfin_hold_mutex;
167  WaitCondition *__prepfin_hold_waitcond;
168 
169  bool __started;
170  bool __cancelled;
171  bool __detached;
172  bool __waiting_for_wakeup;
173  bool __delete_on_exit;
174  bool __wait;
175  char *__name;
176 
177  OpMode __op_mode;
178  bool __prepfin_conc_loop;
179  bool __coalesce_wakeups;
180 
181  uint32_t __flags;
182 
183  LockList<ThreadNotificationListener *> *__notification_listeners;
184 
185  static pthread_key_t THREAD_KEY;
186  static pthread_key_t MAIN_THREAD_KEY;
187  static pthread_mutex_t __thread_key_mutex;
188 };
189 
190 
191 } // end namespace fawkes
192 
193 #endif