tc_thread.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_thread.h"
  17. #include "util/tc_port.h"
  18. #include "util/tc_coroutine.h"
  19. #include "util/tc_common.h"
  20. #include <sstream>
  21. #include <cerrno>
  22. #include <cassert>
  23. namespace tars
  24. {
  25. TC_ThreadControl::TC_ThreadControl(std::thread *th) : _th(th)
  26. {
  27. assert(_th != NULL);
  28. }
  29. void TC_ThreadControl::join()
  30. {
  31. if (std::this_thread::get_id() == _th->get_id())
  32. {
  33. throw TC_ThreadThreadControl_Exception("[TC_ThreadControl::join] can't be called in the same thread");
  34. }
  35. if (_th->joinable())
  36. {
  37. _th->join();
  38. }
  39. }
  40. void TC_ThreadControl::detach()
  41. {
  42. _th->detach();
  43. }
  44. std::thread::id TC_ThreadControl::id() const
  45. {
  46. return _th->get_id();
  47. }
  48. void TC_ThreadControl::sleep(int64_t millsecond)
  49. {
  50. std::this_thread::sleep_for(std::chrono::milliseconds(millsecond));
  51. }
  52. void TC_ThreadControl::yield()
  53. {
  54. std::this_thread::yield();
  55. }
  56. TC_Thread::TC_Thread(const string &threadName) : _threadName(threadName), _running(false), _th(NULL)
  57. {
  58. }
  59. TC_Thread::~TC_Thread()
  60. {
  61. if (_th != NULL)
  62. {
  63. //如果资源没有被detach或者被join,则自己释放
  64. if (_th->joinable())
  65. {
  66. _th->detach();
  67. }
  68. delete _th;
  69. _th = NULL;
  70. }
  71. }
  72. void TC_Thread::setThreadName(const string &threadName)
  73. {
  74. _threadName = threadName;
  75. }
  76. class RunningClosure
  77. {
  78. public:
  79. RunningClosure(TC_Thread *pThread): _pThread(pThread) {
  80. pThread->_running = true;
  81. }
  82. ~RunningClosure() {
  83. if(!_pThread->getScheduler())
  84. {
  85. //非协程模式
  86. _pThread->_running = false;
  87. }
  88. }
  89. protected:
  90. TC_Thread *_pThread;
  91. };
  92. void TC_Thread::threadEntry(TC_Thread *pThread)
  93. {
  94. RunningClosure r(pThread);
  95. {
  96. TC_ThreadLock::Lock sync(pThread->_lock);
  97. pThread->_lock.notifyAll();
  98. }
  99. try
  100. {
  101. pThread->run();
  102. }
  103. catch (exception &ex)
  104. {
  105. cerr << std::this_thread::get_id() << "|" << ex.what() << endl;
  106. throw ex;
  107. }
  108. catch (...)
  109. {
  110. throw;
  111. }
  112. }
  113. TC_ThreadControl TC_Thread::start()
  114. {
  115. TC_ThreadLock::Lock sync(_lock);
  116. if (_running)
  117. {
  118. throw TC_ThreadThreadControl_Exception("[TC_Thread::start] thread has start");
  119. }
  120. try
  121. {
  122. _th = new std::thread(&TC_Thread::threadEntry, this);
  123. }
  124. catch(...)
  125. {
  126. throw TC_ThreadThreadControl_Exception("[TC_Thread::start] thread start error");
  127. }
  128. _lock.wait();
  129. return TC_ThreadControl(_th);
  130. }
  131. void TC_Thread::coroutineEntry(TC_Thread *pThread, uint32_t iPoolSize, size_t iStackSize, bool autoQuit)
  132. {
  133. pThread->_scheduler = TC_CoroutineScheduler::create();
  134. pThread->_scheduler->setPoolStackSize(iPoolSize, iStackSize);
  135. if(autoQuit)
  136. {
  137. pThread->_scheduler->setNoCoroutineCallback([](TC_CoroutineScheduler *scheduler){
  138. scheduler->terminate();
  139. });
  140. }
  141. pThread->_scheduler->go(std::bind(TC_Thread::threadEntry, pThread));
  142. {
  143. TC_ThreadLock::Lock sync(pThread->_lock);
  144. pThread->_lock.notifyAll();
  145. }
  146. pThread->_scheduler->run();
  147. pThread->_running = false;
  148. pThread->_scheduler.reset();
  149. TC_CoroutineScheduler::reset();
  150. }
  151. TC_ThreadControl TC_Thread::startCoroutine(uint32_t iPoolSize, size_t iStackSize, bool autoQuit)
  152. {
  153. TC_ThreadLock::Lock sync(_lock);
  154. if (_running)
  155. {
  156. throw TC_ThreadThreadControl_Exception("[TC_Thread::startCoroutine] thread has start");
  157. }
  158. try
  159. {
  160. _th = new std::thread(&TC_Thread::coroutineEntry, this, iPoolSize, iStackSize, autoQuit);
  161. }
  162. catch(...)
  163. {
  164. throw TC_ThreadThreadControl_Exception("[TC_Thread::startCoroutine] thread start error");
  165. }
  166. _lock.wait();
  167. return TC_ThreadControl(_th);
  168. }
  169. void TC_Thread::join()
  170. {
  171. if(!_th)
  172. {
  173. return;
  174. }
  175. if (std::this_thread::get_id() == _th->get_id())
  176. {
  177. throw TC_ThreadThreadControl_Exception("[TC_Thread::join] can't be called in the same thread");
  178. }
  179. if (_th->joinable())
  180. {
  181. _th->join();
  182. }
  183. }
  184. bool TC_Thread::joinable()
  185. {
  186. if(!_th)
  187. {
  188. return false;
  189. }
  190. return _th->joinable();
  191. }
  192. void TC_Thread::detach()
  193. {
  194. _th->detach();
  195. }
  196. TC_ThreadControl TC_Thread::getThreadControl()
  197. {
  198. return TC_ThreadControl(_th);
  199. }
  200. bool TC_Thread::isAlive() const
  201. {
  202. return _running;
  203. }
  204. size_t TC_Thread::CURRENT_THREADID()
  205. {
  206. static thread_local size_t threadId = 0;
  207. if(threadId == 0 )
  208. {
  209. std::stringstream ss;
  210. ss << std::this_thread::get_id();
  211. threadId = strtol(ss.str().c_str(), NULL, 0);
  212. }
  213. return threadId;
  214. }
  215. }