pipetask.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __H_DTC_PIPETASK_TEMP_H__
  17. #define __H_DTC_PIPETASK_TEMP_H__
  18. #include "log/log.h"
  19. #include "pipequeue.h"
  20. #include "compiler.h"
  21. template <typename T> class JobAskInterface;
  22. template <typename T> class JobAnswerInterface;
  23. template <typename T> class ChainJoint;
  24. template <typename T>
  25. class TaskIncomingPipe : public PipeQueue<T, TaskIncomingPipe<T> > {
  26. public:
  27. TaskIncomingPipe(void)
  28. {
  29. }
  30. virtual ~TaskIncomingPipe()
  31. {
  32. }
  33. inline void job_ask_procedure(T p)
  34. {
  35. proc->job_ask_procedure(p);
  36. }
  37. public:
  38. JobAskBase<T> *proc;
  39. };
  40. template <typename T>
  41. class TaskReturnPipe : public PipeQueue<T *, TaskReturnPipe<T> > {
  42. public:
  43. TaskReturnPipe(){};
  44. virtual ~TaskReturnPipe(){};
  45. inline void job_ask_procedure(T *p)
  46. {
  47. p->turn_around_job_answer();
  48. }
  49. };
  50. template <typename T>
  51. class JobTunnel : public JobAskInterface<T>, public JobAnswerInterface<T> {
  52. private:
  53. static LinkQueue<JobTunnel<T> *> pipelist;
  54. public:
  55. inline JobTunnel()
  56. {
  57. pipelist.Push(this);
  58. }
  59. ~JobTunnel()
  60. {
  61. }
  62. virtual void job_ask_procedure(T *p)
  63. {
  64. p->push_reply_dispatcher(this);
  65. incQueue.Push(p);
  66. }
  67. virtual void job_answer_procedure(T *p)
  68. {
  69. retQueue.Push(p);
  70. }
  71. inline int dig_tunnel(ChainJoint<T> *from, JobAskInterface<T> *to)
  72. {
  73. JobAskInterface<T>::owner = from->get_owner_thread();
  74. incQueue.attach_poller(from->get_owner_thread(),
  75. to->get_owner_thread());
  76. retQueue.attach_poller(to->get_owner_thread(),
  77. from->get_owner_thread());
  78. from->register_next_chain(this);
  79. incQueue.proc = to;
  80. from->disable_use_queue();
  81. return 0;
  82. }
  83. static inline void destroy_all(void)
  84. {
  85. JobTunnel *p;
  86. while ((p = pipelist.Pop()) != NULL) {
  87. delete p;
  88. }
  89. }
  90. private:
  91. TaskIncomingPipe<T *> incQueue;
  92. TaskReturnPipe<T> retQueue;
  93. };
  94. template <typename T> LinkQueue<JobTunnel<T> *> JobTunnel<T>::pipelist;
  95. #endif