continuation.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIB_COMMON_CONTINUATION_CONTINUATION_H_
  19. #define LIB_COMMON_CONTINUATION_CONTINUATION_H_
  20. #include "libhdfs++/status.h"
  21. #include <functional>
  22. #include <memory>
  23. #include <vector>
  24. namespace hdfs {
  25. namespace continuation {
  26. class PipelineBase;
  27. /**
  28. * A continuation is a fragment of runnable code whose execution will
  29. * be scheduled by a \link Pipeline \endlink.
  30. *
  31. * The Continuation class is a build block to implement the
  32. * Continuation Passing Style (CPS) in libhdfs++. In CPS, the
  33. * upper-level user specifies the control flow by chaining a sequence
  34. * of continuations explicitly through the \link Run() \endlink method,
  35. * while in traditional imperative programming the sequences of
  36. * sentences implicitly specify the control flow.
  37. *
  38. * See http://en.wikipedia.org/wiki/Continuation for more details.
  39. **/
  40. class Continuation {
  41. public:
  42. typedef std::function<void(const Status &)> Next;
  43. virtual ~Continuation() = default;
  44. virtual void Run(const Next &next) = 0;
  45. Continuation(const Continuation &) = delete;
  46. Continuation &operator=(const Continuation &) = delete;
  47. protected:
  48. Continuation() = default;
  49. };
  50. /**
  51. * A pipeline schedules the execution of a chain of \link Continuation
  52. * \endlink. The pipeline schedules the execution of continuations
  53. * based on their order in the pipeline, where the next parameter for
  54. * each continuation points to the \link Schedule() \endlink
  55. * method. That way the pipeline executes all scheduled continuations
  56. * in sequence.
  57. *
  58. * The typical use case of a pipeline is executing continuations
  59. * asynchronously. Note that a continuation calls the next
  60. * continuation when it is finished. If the continuation is posted
  61. * into an asynchronous event loop, invoking the next continuation
  62. * can be done in the callback handler in the asynchronous event loop.
  63. *
  64. * The pipeline allocates the memory as follows. A pipeline is always
  65. * allocated on the heap. It owns all the continuations as well as the
  66. * the state specified by the user. Both the continuations and the
  67. * state have the same life cycle of the pipeline. The design
  68. * simplifies the problem of ensuring that the executions in the
  69. * asynchronous event loop always hold valid pointers w.r.t. the
  70. * pipeline. The pipeline will automatically deallocate itself right
  71. * after it invokes the callback specified the user.
  72. **/
  73. template<class State>
  74. class Pipeline {
  75. public:
  76. typedef std::function<void(const Status &, const State &)> UserHandler;
  77. static Pipeline *Create() { return new Pipeline(); }
  78. Pipeline &Push(Continuation *stage);
  79. void Run(UserHandler &&handler);
  80. State &state() { return state_; }
  81. private:
  82. State state_;
  83. std::vector<std::unique_ptr<Continuation> > routines_;
  84. size_t stage_;
  85. std::function<void(const Status &, const State &)> handler_;
  86. Pipeline() : stage_(0) {}
  87. ~Pipeline() = default;
  88. void Schedule(const Status &status);
  89. };
  90. template<class State>
  91. inline Pipeline<State> &Pipeline<State>::Push(Continuation *stage) {
  92. routines_.emplace_back(std::unique_ptr<Continuation>(stage));
  93. return *this;
  94. }
  95. template<class State>
  96. inline void Pipeline<State>::Schedule(const Status &status) {
  97. if (stage_ >= routines_.size()) {
  98. handler_(status, state_);
  99. routines_.clear();
  100. delete this;
  101. } else {
  102. auto next = routines_[stage_].get();
  103. ++stage_;
  104. next->Run(std::bind(&Pipeline::Schedule, this, std::placeholders::_1));
  105. }
  106. }
  107. template<class State>
  108. inline void Pipeline<State>::Run(UserHandler &&handler) {
  109. handler_ = std::move(handler);
  110. Schedule(Status::OK());
  111. }
  112. }
  113. }
  114. #endif