ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

148 lines
2.4 KiB

  1. #define CPPCMS_SOURCE
  2. #include "thread_pool.h"
  3. #include <list>
  4. #include <boost/thread.hpp>
  5. #include <boost/shared_ptr.hpp>
  6. #include <boost/bind.hpp>
  7. #if defined(CPPCMS_POSIX)
  8. #include <signal.h>
  9. #endif
  10. namespace cppcms {
  11. namespace impl {
  12. class thread_pool : public util::noncopyable {
  13. public:
  14. bool cancel(int id) {
  15. boost::unique_lock<boost::mutex> lock(mutex_);
  16. queue_type::iterator p;
  17. for(p=queue_.begin();p!=queue_.end();++p) {
  18. if(p->first==id) {
  19. queue_.erase(p);
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25. int post(util::callback0 const &job)
  26. {
  27. boost::unique_lock<boost::mutex> lock(mutex_);
  28. int id=job_id_++;
  29. queue_.push_back(std::make_pair(id,job));
  30. cond_.notify_one();
  31. return id;
  32. }
  33. thread_pool(int threads) :
  34. shut_down_(false),
  35. job_id_(0)
  36. {
  37. workers_.resize(threads);
  38. #if defined(CPPCMS_POSIX)
  39. sigset_t set,old;
  40. sigfillset(&set);
  41. pthread_sigmask(SIG_BLOCK,&set,&old);
  42. #endif
  43. for(int i=0;i<threads;i++) {
  44. workers_[i].reset(new boost::thread(boost::bind(&thread_pool::worker,this)));
  45. }
  46. #if defined(CPPCMS_POSIX)
  47. pthread_sigmask(SIG_SETMASK,&old,0);
  48. #endif
  49. }
  50. void stop()
  51. {
  52. {
  53. boost::unique_lock<boost::mutex> lock(mutex_);
  54. shut_down_=true;
  55. cond_.notify_all();
  56. }
  57. for(unsigned i=0;i<workers_.size();i++) {
  58. boost::shared_ptr<boost::thread> thread=workers_[i];
  59. workers_[i].reset();
  60. if(thread)
  61. thread->join();
  62. }
  63. }
  64. ~thread_pool()
  65. {
  66. try {
  67. stop();
  68. }
  69. catch(...)
  70. {
  71. }
  72. }
  73. private:
  74. void worker()
  75. {
  76. for(;;) {
  77. util::callback0 job;
  78. {
  79. boost::unique_lock<boost::mutex> lock(mutex_);
  80. if(shut_down_)
  81. return;
  82. if(!queue_.empty()) {
  83. queue_.front().second.swap(job);
  84. queue_.pop_front();
  85. }
  86. else {
  87. cond_.wait(lock);
  88. }
  89. }
  90. job();
  91. }
  92. }
  93. boost::mutex mutex_;
  94. boost::condition_variable cond_;
  95. bool shut_down_;
  96. int job_id_;
  97. typedef std::list<std::pair<int,util::callback0> > queue_type;
  98. queue_type queue_;
  99. std::vector<boost::shared_ptr<boost::thread> > workers_;
  100. };
  101. }
  102. thread_pool::thread_pool(int n) :
  103. impl_(new impl::thread_pool(n))
  104. {
  105. }
  106. int thread_pool::post(util::callback0 const &job)
  107. {
  108. return impl_->post(job);
  109. }
  110. void thread_pool::stop()
  111. {
  112. impl_->stop();
  113. }
  114. bool thread_pool::cancel(int id)
  115. {
  116. return impl_->cancel(id);
  117. }
  118. thread_pool::~thread_pool()
  119. {
  120. }
  121. } // cppcms