AsyncProcThread.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "servant/AsyncProcThread.h"
  17. #include "servant/Communicator.h"
  18. #include "servant/StatReport.h"
  19. #include "servant/RemoteLogger.h"
  20. #include "servant/AdapterProxy.h"
  21. namespace tars
  22. {
  23. AsyncProcThread::AsyncProcThread(size_t iQueueCap, bool merge)
  24. : _terminate(false), _iQueueCap(iQueueCap), _merge(merge)
  25. {
  26. _msgQueue = new TC_CasQueue<ReqMessage*>();
  27. if(!_merge)
  28. {
  29. start();
  30. }
  31. }
  32. AsyncProcThread::~AsyncProcThread()
  33. {
  34. terminate();
  35. if(_msgQueue)
  36. {
  37. delete _msgQueue;
  38. _msgQueue = NULL;
  39. }
  40. }
  41. void AsyncProcThread::terminate()
  42. {
  43. if(!_merge) {
  44. Lock lock(*this);
  45. _terminate = true;
  46. notifyAll();
  47. }
  48. }
  49. void AsyncProcThread::push_back(ReqMessage * msg)
  50. {
  51. if(_merge) {
  52. //合并了, 直接回调
  53. callback(msg);
  54. }
  55. else {
  56. if(_msgQueue->size() >= _iQueueCap)
  57. {
  58. TLOGERROR("[AsyncProcThread::push_back] async_queue full:" << _msgQueue->size() << ">=" << _iQueueCap << endl);
  59. delete msg;
  60. }
  61. else
  62. {
  63. _msgQueue->push_back(msg);
  64. TC_ThreadLock::Lock lock(*this);
  65. notify();
  66. }
  67. }
  68. }
  69. void AsyncProcThread::run()
  70. {
  71. while (!_terminate)
  72. {
  73. ReqMessage * msg;
  74. //异步请求回来的响应包处理
  75. if (_msgQueue->pop_front(msg))
  76. {
  77. callback(msg);
  78. }
  79. else
  80. {
  81. TC_ThreadLock::Lock lock(*this);
  82. timedWait(1000);
  83. }
  84. }
  85. ReqMessage * msg;
  86. while(_msgQueue->pop_front(msg))
  87. {
  88. delete msg;
  89. }
  90. ServantProxyThreadData::g_sp.reset();
  91. CallbackThreadData::g_sp.reset();
  92. }
  93. void AsyncProcThread::callback(ReqMessage * msg)
  94. {
  95. TLOGTARS("[AsyncProcThread::run] get one msg." << endl);
  96. //从回调对象把线程私有数据传递到回调线程中
  97. ServantProxyThreadData * pServantProxyThreadData = ServantProxyThreadData::getData();
  98. // assert(pServantProxyThreadData != NULL);
  99. //把染色的消息设置在线程私有数据里面
  100. pServantProxyThreadData->_data._dyeing = msg->data._dyeing;
  101. pServantProxyThreadData->_data._dyeingKey = msg->data._dyeingKey;
  102. pServantProxyThreadData->_data._cookie = msg->data._cookie;
  103. //=======
  104. // pServantProxyThreadData->_dyeing = msg->bDyeing;
  105. // pServantProxyThreadData->_dyeingKey = msg->sDyeingKey;
  106. pServantProxyThreadData->_traceCall = msg->bTraceCall;
  107. pServantProxyThreadData->initTrace(msg->sTraceKey);
  108. // pServantProxyThreadData->_cookie = msg->cookie;
  109. //>>>>>>> origin/delay
  110. if(msg->adapter)
  111. {
  112. pServantProxyThreadData->_data._szHost = msg->adapter->endpoint().desc();
  113. }
  114. try
  115. {
  116. ReqMessagePtr msgPtr = msg;
  117. msg->callback->dispatch(msgPtr);
  118. }
  119. catch (exception& e)
  120. {
  121. TLOGERROR("[AsyncProcThread exception]:" << e.what() << endl);
  122. }
  123. catch (...)
  124. {
  125. TLOGERROR("[AsyncProcThread exception.]" << endl);
  126. }
  127. }
  128. /////////////////////////////////////////////////////////////////////////
  129. }