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.
 
 
 
 
 
 

172 lines
3.4 KiB

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