Menu

[r1486]: / framework / trunk / src / thread_pool.cpp  Maximize  Restore  History

Download this file

189 lines (160 with data), 4.2 kB

  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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include <cppcms/thread_pool.h>
#include <booster/backtrace.h>
#include <booster/log.h>
#include <ostream>
#include <list>
#include <vector>
#include <cppcms/config.h>
#ifdef CPPCMS_USE_EXTERNAL_BOOST
# include <boost/bind.hpp>
#else // Internal Boost
# include <cppcms_boost/bind.hpp>
namespace boost = cppcms_boost;
#endif
#include <booster/shared_ptr.h>
#include <booster/thread.h>
#if defined(CPPCMS_POSIX)
#include <signal.h>
#endif
namespace cppcms {
namespace impl {
class thread_pool : public booster::noncopyable {
public:
bool cancel(int id) {
booster::unique_lock<booster::mutex> lock(mutex_);
queue_type::iterator p;
for(p=queue_.begin();p!=queue_.end();++p) {
if(p->first==id) {
queue_.erase(p);
return true;
}
}
return false;
}
int post(booster::function<void()> const &job)
{
booster::unique_lock<booster::mutex> lock(mutex_);
int id=job_id_++;
queue_.push_back(std::make_pair(id,job));
cond_.notify_one();
return id;
}
thread_pool(int threads) :
shut_down_(false),
job_id_(0)
{
workers_.resize(threads);
#if defined(CPPCMS_POSIX)
sigset_t set,old;
sigfillset(&set);
pthread_sigmask(SIG_BLOCK,&set,&old);
#endif
for(int i=0;i<threads;i++) {
workers_[i].reset(new booster::thread(boost::bind(&thread_pool::worker,this)));
}
#if defined(CPPCMS_POSIX)
pthread_sigmask(SIG_SETMASK,&old,0);
#endif
}
void stop()
{
{
booster::unique_lock<booster::mutex> lock(mutex_);
shut_down_=true;
cond_.notify_all();
}
for(unsigned i=0;i<workers_.size();i++) {
booster::shared_ptr<booster::thread> thread=workers_[i];
workers_[i].reset();
if(thread)
thread->join();
}
}
~thread_pool()
{
try {
stop();
}
catch(...)
{
}
}
private:
void worker()
{
for(;;) {
booster::function<void()> job;
{
booster::unique_lock<booster::mutex> lock(mutex_);
if(shut_down_)
return;
if(!queue_.empty()) {
queue_.front().second.swap(job);
queue_.pop_front();
}
else {
cond_.wait(lock);
}
}
if(job) {
try {
job();
}
catch(std::exception const &e) {
BOOSTER_ERROR("cppcms") << "Catched exception in thread pool" << e.what() <<'\n'
<< booster::trace(e);
}
catch(...) {
BOOSTER_ERROR("cppcms") << "Catched unknown exception in thread pool";
}
}
}
}
booster::mutex mutex_;
booster::condition_variable cond_;
bool shut_down_;
int job_id_;
typedef std::list<std::pair<int,booster::function<void()> > > queue_type;
queue_type queue_;
std::vector<booster::shared_ptr<booster::thread> > workers_;
};
}
thread_pool::thread_pool(int n) :
impl_(new impl::thread_pool(n))
{
}
int thread_pool::post(booster::function<void()> const &job)
{
return impl_->post(job);
}
void thread_pool::stop()
{
impl_->stop();
}
bool thread_pool::cancel(int id)
{
return impl_->cancel(id);
}
thread_pool::~thread_pool()
{
}
} // cppcms
MongoDB Logo MongoDB