1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/** \file job_queue.h */ // -*-c++-*-
// Copyright (C) 2009-2010 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#include <cwidget/generic/threads/threads.h>
#include <loggers.h>
#include <deque>
#include <memory>
namespace aptitude
{
namespace util
{
/** \brief Base class for threads that work by processing a single
* job at a time.
*
* \tparam Subclass The class that will be derived from
* job_queue. Must be default-constructable and must define a
* static method get_log_category() returning the category under
* which messages should be logged.
*
* \tparam Job The type that represents jobs in the queue. Must
* be copy-constructable, default-constructable, and support
* output to ostreams via operator<<.
*
* \todo Add support for running up to a fixed number of jobs at
* the same time?
*/
template<typename Subclass, typename Job>
class job_queue_thread
{
// The jobs waiting to be run.
static std::deque<Job> jobs;
// The single instance of this class.
static std::shared_ptr<job_queue_thread> active_instance;
// The active thread, or NULL if there isn't one.
static std::shared_ptr<cwidget::threads::thread> active_thread;
// Set to true if the thread is currently stopped. This causes
// the job-processing loop to exit and prevents the thread from
// being started if a new job is added to the queue.
static bool stopped;
// This mutex protects accesses to all the static state of the
// job.
static cwidget::threads::mutex state_mutex;
class bootstrap
{
std::shared_ptr<job_queue_thread> target;
public:
bootstrap(const std::shared_ptr<job_queue_thread> &_target)
: target(_target)
{
}
void operator()() const
{
target->run();
}
};
public:
// Instance members first:
job_queue_thread()
{
}
/** \brief Test whether there are more jobs in the thread's
* input queue.
*/
static bool empty()
{
cwidget::threads::mutex::lock l(state_mutex);
return jobs.empty();
}
/** \brief Test whether the queue has been stopped by a call to
* stop().
*/
bool get_stopped()
{
cwidget::threads::mutex::lock l(state_mutex);
return stopped;
}
/** \brief Add a new job to the queue of jobs for this thread to
* run.
*
* If the background thread isn't stopped, starts it.
*/
static void add_job(const Job &job)
{
cwidget::threads::mutex::lock l(state_mutex);
LOG_TRACE(Subclass::get_log_category(),
"Adding a job to the queue: " << job);
jobs.push_back(job);
if(!stopped)
start();
}
/** \brief Stop the active thread if there is one.
*
* The background thread will only be stopped between jobs.
*
* Blocks until the thread exits. Until start() is invoked, no
* jobs will be processed.
*/
static void stop()
{
cwidget::threads::mutex::lock l(state_mutex);
LOG_TRACE(Subclass::get_log_category(),
"Pausing the background thread.");
stopped = true;
// Copy this since it'll be zeroed out when the thread exits,
// which can happen as soon as the lock is released below.
std::shared_ptr<cwidget::threads::thread> active_thread_copy(active_thread);
l.release();
if(active_thread_copy.get() != NULL)
active_thread_copy->join();
}
/** \brief Start the background thread if it has jobs to
* process.
*
* Has no effect if there are no jobs or if a thread is already
* running.
*/
static void start()
{
cwidget::threads::mutex::lock l(state_mutex);
stopped = false;
if(active_thread != NULL)
LOG_TRACE(Subclass::get_log_category(),
"Not starting the background thread: it's already running.");
else if(empty())
LOG_TRACE(Subclass::get_log_category(),
"Not starting the background thread: it has no jobs.");
else
{
LOG_TRACE(Subclass::get_log_category(), "Starting the background thread.");
active_instance = std::make_shared<Subclass>();
active_thread = std::make_shared<cwidget::threads::thread>(bootstrap(active_instance));
}
}
/** \brief Process a single job serially. */
virtual void process_job(const Job &job) = 0;
private:
/** \brief Dequeue and process jobs until the queue is empty or
* the thread is stopped.
*/
void run()
{
try
{
cwidget::threads::mutex::lock l(state_mutex);
while(!jobs.empty() && !stopped)
{
Job next(jobs.front());
jobs.pop_front();
// Unlock the state mutex, so that jobs can be
// inserted without blocking while this job is being
// processed.
l.release();
try
{
process_job(next);
}
catch(const std::exception &ex)
{
LOG_WARN(Subclass::get_log_category(), "Background thread: got std::exception: " << ex.what());
}
catch(const cwidget::util::Exception &ex)
{
LOG_WARN(Subclass::get_log_category(), "Background thread: got cwidget::util::Exception: " << ex.errmsg());
}
catch(...)
{
LOG_WARN(Subclass::get_log_category(), "Background thread: got an unknown exception.");
}
l.acquire();
}
active_thread.reset();
active_instance.reset();
return; // Unless there's an unlikely error, we exit here;
// otherwise we try some last-chance error
// handling below.
}
catch(const std::exception &ex)
{
LOG_FATAL(Subclass::get_log_category(), "Background thread aborting with std::exception: " << ex.what());
}
catch(const cwidget::util::Exception &ex)
{
LOG_FATAL(Subclass::get_log_category(), "Background thread aborting with cwidget::util::Exception: " << ex.errmsg());
}
catch(...)
{
LOG_FATAL(Subclass::get_log_category(), "Background thread aborting with an unknown exception.");
}
// Do what we can to recover, although there might be jobs
// that can't be processed!
{
cwidget::threads::mutex::lock l(state_mutex);
active_thread.reset();
}
}
};
// Instantiate static members:
template<typename Subclass, typename Job>
std::deque<Job> job_queue_thread<Subclass, Job>::jobs;
template<typename Subclass, typename Job>
std::shared_ptr<job_queue_thread<Subclass, Job> > job_queue_thread<Subclass, Job>::active_instance;
template<typename Subclass, typename Job>
std::shared_ptr<cwidget::threads::thread> job_queue_thread<Subclass, Job>::active_thread;
template<typename Subclass, typename Job>
bool job_queue_thread<Subclass, Job>::stopped = false;
template<typename Subclass, typename Job>
cwidget::threads::mutex job_queue_thread<Subclass, Job>::state_mutex((cwidget::threads::mutex::attr(PTHREAD_MUTEX_RECURSIVE)));
}
}
|