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.
 
 
 
 
 
 

157 lines
2.6 KiB

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