continuation.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "hdfspp/status.h"
  21. #include "common/cancel_tracker.h"
  22. #include <functional>
  23. #include <memory>
  24. #include <vector>
  25. namespace hdfs {
  26. namespace continuation {
  27. class PipelineBase;
  28. /**
  29. * A continuation is a fragment of runnable code whose execution will
  30. * be scheduled by a \link Pipeline \endlink.
  31. *
  32. * The Continuation class is a build block to implement the
  33. * Continuation Passing Style (CPS) in libhdfs++. In CPS, the
  34. * upper-level user specifies the control flow by chaining a sequence
  35. * of continuations explicitly through the \link Run() \endlink method,
  36. * while in traditional imperative programming the sequences of
  37. * sentences implicitly specify the control flow.
  38. *
  39. * See http://en.wikipedia.org/wiki/Continuation for more details.
  40. **/
  41. class Continuation {
  42. public:
  43. typedef std::function<void(const Status &)> Next;
  44. virtual ~Continuation() = default;
  45. virtual void Run(const Next &next) = 0;
  46. Continuation(const Continuation &) = delete;
  47. Continuation &operator=(const Continuation &) = delete;
  48. protected:
  49. Continuation() = default;
  50. };
  51. /**
  52. * A pipeline schedules the execution of a chain of \link Continuation
  53. * \endlink. The pipeline schedules the execution of continuations
  54. * based on their order in the pipeline, where the next parameter for
  55. * each continuation points to the \link Schedule() \endlink
  56. * method. That way the pipeline executes all scheduled continuations
  57. * in sequence.
  58. *
  59. * The typical use case of a pipeline is executing continuations
  60. * asynchronously. Note that a continuation calls the next
  61. * continuation when it is finished. If the continuation is posted
  62. * into an asynchronous event loop, invoking the next continuation
  63. * can be done in the callback handler in the asynchronous event loop.
  64. *
  65. * The pipeline allocates the memory as follows. A pipeline is always
  66. * allocated on the heap. It owns all the continuations as well as the
  67. * the state specified by the user. Both the continuations and the
  68. * state have the same life cycle of the pipeline. The design
  69. * simplifies the problem of ensuring that the executions in the
  70. * asynchronous event loop always hold valid pointers w.r.t. the
  71. * pipeline. The pipeline will automatically deallocate itself right
  72. * after it invokes the callback specified the user.
  73. **/
  74. template <class State> class Pipeline {
  75. public:
  76. typedef std::function<void(const Status &, const State &)> UserHandler;
  77. static Pipeline *Create() { return new Pipeline(); }
  78. static Pipeline *Create(CancelHandle cancel_handle) {
  79. return new Pipeline(cancel_handle);
  80. }
  81. Pipeline &Push(Continuation *stage);
  82. void Run(UserHandler &&handler);
  83. State &state() { return state_; }
  84. private:
  85. State state_;
  86. std::vector<std::unique_ptr<Continuation>> routines_;
  87. size_t stage_;
  88. std::function<void(const Status &, const State &)> handler_;
  89. Pipeline() : stage_(0), cancel_handle_(CancelTracker::New()) {}
  90. Pipeline(CancelHandle cancel_handle) : stage_(0), cancel_handle_(cancel_handle) {}
  91. ~Pipeline() = default;
  92. void Schedule(const Status &status);
  93. CancelHandle cancel_handle_;
  94. };
  95. template <class State>
  96. inline Pipeline<State> &Pipeline<State>::Push(Continuation *stage) {
  97. routines_.emplace_back(std::unique_ptr<Continuation>(stage));
  98. return *this;
  99. }
  100. template <class State>
  101. inline void Pipeline<State>::Schedule(const Status &status) {
  102. // catch cancelation signalled from outside of pipeline
  103. if(cancel_handle_->is_canceled()) {
  104. handler_(Status::Canceled(), state_);
  105. routines_.clear();
  106. delete this;
  107. } else if (!status.ok() || stage_ >= routines_.size()) {
  108. handler_(status, state_);
  109. routines_.clear();
  110. delete this;
  111. } else {
  112. auto next = routines_[stage_].get();
  113. ++stage_;
  114. next->Run(std::bind(&Pipeline::Schedule, this, std::placeholders::_1));
  115. }
  116. }
  117. template <class State> inline void Pipeline<State>::Run(UserHandler &&handler) {
  118. handler_ = std::move(handler);
  119. Schedule(Status::OK());
  120. }
  121. template <class Handler> class BindContinuation : public Continuation {
  122. public:
  123. BindContinuation(const Handler &handler) : handler_(handler) {}
  124. virtual void Run(const Next &next) override { handler_(next); }
  125. private:
  126. Handler handler_;
  127. };
  128. template <class Handler> static inline Continuation *Bind(const Handler &handler) {
  129. return new BindContinuation<Handler>(handler);
  130. }
  131. }
  132. }
  133. #endif