AsyncProcThread.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #ifndef __TARS_ASYNC_PROC_THREAD_H_
  17. #define __TARS_ASYNC_PROC_THREAD_H_
  18. #include "servant/Message.h"
  19. #include "servant/Global.h"
  20. #include "util/tc_cas_queue.h"
  21. #include "util/tc_thread_queue.h"
  22. namespace tars
  23. {
  24. //////////////////////////////////////////////////////////
  25. /**
  26. * 异步回调后的处理线程
  27. */
  28. class AsyncProcThread : public TC_Thread, public TC_ThreadLock
  29. {
  30. public:
  31. /**
  32. * 构造函数
  33. */
  34. AsyncProcThread(size_t iQueueCap, bool merge);
  35. /**
  36. * 析构函数
  37. */
  38. virtual ~AsyncProcThread();
  39. /**
  40. * 结束处理线程
  41. */
  42. void terminate();
  43. /**
  44. * 插入
  45. */
  46. void push_back(ReqMessage * msg);
  47. /**
  48. * 从队列中取消息后执行回调逻辑
  49. */
  50. void run();
  51. /**
  52. * 获取异步队列的大小
  53. */
  54. size_t getSize()
  55. {
  56. return _msgQueue->size();
  57. }
  58. protected:
  59. void callback(ReqMessage * msg);
  60. private:
  61. /**
  62. * 是否需要退出
  63. */
  64. bool _terminate;
  65. /**
  66. * 异步队列
  67. */
  68. TC_CasQueue<ReqMessage*> * _msgQueue;
  69. /**
  70. * 队列流量控制
  71. */
  72. size_t _iQueueCap;
  73. /**
  74. * 合并网络线程和回调线程
  75. */
  76. bool _merge;
  77. };
  78. ///////////////////////////////////////////////////////
  79. }
  80. #endif