Worker.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Worker.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "Worker.h"
  19. #include <chrono>
  20. #include <thread>
  21. #include "Log.h"
  22. using namespace std;
  23. using namespace dev;
  24. void Worker::startWorking()
  25. {
  26. // cnote << "startWorking for thread" << m_name;
  27. Guard l(x_work);
  28. if (m_work)
  29. {
  30. WorkerState ex = WorkerState::Stopped;
  31. m_state.compare_exchange_strong(ex, WorkerState::Starting);
  32. }
  33. else
  34. {
  35. m_state = WorkerState::Starting;
  36. m_work.reset(new thread([&]()
  37. {
  38. setThreadName(m_name.c_str());
  39. // cnote << "Thread begins";
  40. while (m_state != WorkerState::Killing)
  41. {
  42. WorkerState ex = WorkerState::Starting;
  43. bool ok = m_state.compare_exchange_strong(ex, WorkerState::Started);
  44. // cnote << "Trying to set Started: Thread was" << (unsigned)ex << "; " << ok;
  45. (void)ok;
  46. try
  47. {
  48. startedWorking();
  49. workLoop();
  50. doneWorking();
  51. }
  52. catch (std::exception const& _e)
  53. {
  54. clog(WarnChannel) << "Exception thrown in Worker thread: " << _e.what();
  55. }
  56. // ex = WorkerState::Stopping;
  57. // m_state.compare_exchange_strong(ex, WorkerState::Stopped);
  58. ex = m_state.exchange(WorkerState::Stopped);
  59. // cnote << "State: Stopped: Thread was" << (unsigned)ex;
  60. if (ex == WorkerState::Killing || ex == WorkerState::Starting)
  61. m_state.exchange(ex);
  62. // cnote << "Waiting until not Stopped...";
  63. DEV_TIMED_ABOVE("Worker stopping", 100)
  64. while (m_state == WorkerState::Stopped)
  65. this_thread::sleep_for(chrono::milliseconds(20));
  66. }
  67. }));
  68. // cnote << "Spawning" << m_name;
  69. }
  70. DEV_TIMED_ABOVE("Start worker", 100)
  71. while (m_state == WorkerState::Starting)
  72. this_thread::sleep_for(chrono::microseconds(20));
  73. }
  74. void Worker::stopWorking()
  75. {
  76. DEV_GUARDED(x_work)
  77. if (m_work)
  78. {
  79. WorkerState ex = WorkerState::Started;
  80. m_state.compare_exchange_strong(ex, WorkerState::Stopping);
  81. DEV_TIMED_ABOVE("Stop worker", 100)
  82. while (m_state != WorkerState::Stopped)
  83. this_thread::sleep_for(chrono::microseconds(20));
  84. }
  85. }
  86. void Worker::terminate()
  87. {
  88. // cnote << "stopWorking for thread" << m_name;
  89. DEV_GUARDED(x_work)
  90. if (m_work)
  91. {
  92. m_state.exchange(WorkerState::Killing);
  93. DEV_TIMED_ABOVE("Terminate worker", 100)
  94. m_work->join();
  95. m_work.reset();
  96. }
  97. }
  98. void Worker::workLoop()
  99. {
  100. while (m_state == WorkerState::Started)
  101. {
  102. if (m_idleWaitMs)
  103. this_thread::sleep_for(chrono::milliseconds(m_idleWaitMs));
  104. doWork();
  105. }
  106. }