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
34namespace fawkes {
35
36class WaitCondition;
37class Mutex;
38class Barrier;
39class ThreadNotificationListener;
40class ThreadLoopListener;
41class ThreadList;
42template <typename Type>
43class LockList;
44
45class Thread
46{
47 friend ThreadList;
48
49public:
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 */
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 */
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() noexcept;
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
128
129 void add_loop_listener(ThreadLoopListener *loop_listener);
130 void remove_loop_listener(ThreadLoopListener *loop_listener);
131
132protected:
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
154
155private:
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
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
List with a lock.
Definition: lock_list.h:45
Mutex mutual exclusion lock.
Definition: mutex.h:33
List of threads.
Definition: thread_list.h:56
Thread loop listener interface.
Thread notification listener interface.
Thread class encapsulation of pthreads.
Definition: thread.h:46
bool finalize_prepared
True if prepare_finalize() has been called and was not stopped with a cancel_finalize(),...
Definition: thread.h:151
void kill(int sig)
Send signal to a thread.
Definition: thread.cpp:662
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Definition: thread.cpp:716
Mutex * loop_mutex
Mutex that is used to protect a call to loop().
Definition: thread.h:152
void add_loop_listener(ThreadLoopListener *loop_listener)
Add loop listener.
Definition: thread.cpp:1181
const char * name() const
Get name of thread.
Definition: thread.h:100
OpMode opmode() const
Get operation mode.
Definition: thread.cpp:671
static void destroy_main()
Destroy main thread wrapper instance.
Definition: thread.cpp:1289
void start(bool wait=true)
Call this method to start the thread.
Definition: thread.cpp:499
static void init_main()
Initialize Thread wrapper instance for main thread.
Definition: thread.cpp:1276
void unset_flag(uint32_t flag)
Unset flag.
Definition: thread.cpp:1133
static const unsigned int FLAG_BAD
Standard thread flag: "thread is bad".
Definition: thread.h:69
CancelState
Cancel state.
Definition: thread.h:64
@ CANCEL_ENABLED
cancellation is possible
Definition: thread.h:65
@ CANCEL_DISABLED
thread cannot be cancelled
Definition: thread.h:66
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
Definition: thread.cpp:1396
virtual ~Thread()
Virtual destructor.
Definition: thread.cpp:284
void join()
Join the thread.
Definition: thread.cpp:597
static pthread_t current_thread_id()
Get the ID of the currently running thread.
Definition: thread.cpp:1305
void yield()
Yield the processor to another thread or process.
Definition: thread.cpp:883
void set_prepfin_hold(bool hold)
Hold prepare_finalize().
Definition: thread.cpp:781
virtual bool prepare_finalize_user()
Prepare finalization user implementation.
Definition: thread.cpp:424
void wait_loop_done()
Wait for the current loop iteration to finish.
Definition: thread.cpp:1052
bool waiting() const
Check if thread is currently waiting for wakeup.
Definition: thread.cpp:857
bool running() const
Check if the thread is running.
Definition: thread.cpp:838
static Thread * current_thread_noexc() noexcept
Similar to current_thread, but does never throw an exception.
Definition: thread.cpp:1381
bool detached() const
Check if thread has been detached.
Definition: thread.cpp:828
bool started() const
Check if thread has been started.
Definition: thread.cpp:810
void set_opmode(OpMode op_mode)
Set operation mode.
Definition: thread.cpp:682
void exit()
Exit the thread.
Definition: thread.cpp:582
void detach()
Detach the thread.
Definition: thread.cpp:636
virtual void once()
Execute an action exactly once.
Definition: thread.cpp:1085
bool prepare_finalize()
Prepare finalization.
Definition: thread.cpp:375
bool flagged_bad() const
Check if FLAG_BAD was set.
Definition: thread.cpp:1152
virtual void init()
Initialize the thread.
Definition: thread.cpp:343
void set_name(const char *format,...)
Set name of thread.
Definition: thread.cpp:748
void add_notification_listener(ThreadNotificationListener *notification_listener)
Add notification listener.
Definition: thread.cpp:1162
void wakeup()
Wake up thread.
Definition: thread.cpp:995
virtual void loop()
Code to execute in the thread.
Definition: thread.cpp:1067
void remove_loop_listener(ThreadLoopListener *loop_listener)
Remove loop listener.
Definition: thread.cpp:1190
pthread_t thread_id() const
Get ID of thread.
Definition: thread.cpp:801
void test_cancel()
Set cancellation point.
Definition: thread.cpp:871
void set_flag(uint32_t flag)
Set flag for the thread.
Definition: thread.cpp:1122
bool wakeup_pending()
Check if wakeups are pending.
Definition: thread.cpp:1106
void cancel()
Cancel a thread.
Definition: thread.cpp:646
void cancel_finalize()
Cancel finalization.
Definition: thread.cpp:481
Mutex * loopinterrupt_antistarve_mutex
Mutex to avoid starvation when trying to lock loop_mutex.
Definition: thread.h:153
static std::string current_thread_name()
Get the name of the current thread.
Definition: thread.cpp:1318
bool cancelled() const
Check if thread has been cancelled.
Definition: thread.cpp:819
static Thread * current_thread()
Get the Thread instance of the currently running thread.
Definition: thread.cpp:1366
void notify_of_failed_init()
Notify of failed init.
Definition: thread.cpp:1218
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:918
bool operator==(const Thread &thread)
Check if two threads are the same.
Definition: thread.cpp:897
void set_delete_on_exit(bool del)
Set whether the thread should be deleted on exit.
Definition: thread.cpp:1096
void remove_notification_listener(ThreadNotificationListener *notification_listener)
Remove notification listener.
Definition: thread.cpp:1171
void set_flags(uint32_t flags)
Set all flags in one go.
Definition: thread.cpp:1142
virtual void finalize()
Finalize the thread.
Definition: thread.cpp:463
OpMode
Thread operation mode.
Definition: thread.h:56
@ OPMODE_CONTINUOUS
operate in continuous mode (default)
Definition: thread.h:57
@ OPMODE_WAITFORWAKEUP
operate in wait-for-wakeup mode
Definition: thread.h:58
void set_coalesce_wakeups(bool coalesce=true)
Set wakeup coalescing.
Definition: thread.cpp:729
Wait until a given condition holds.
Fawkes library namespace.