continuation.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "libhdfspp/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> class Pipeline {
  74. public:
  75. typedef std::function<void(const Status &, const State &)> UserHandler;
  76. static Pipeline *Create() { return new Pipeline(); }
  77. Pipeline &Push(Continuation *stage);
  78. void Run(UserHandler &&handler);
  79. State &state() { return state_; }
  80. private:
  81. State state_;
  82. std::vector<std::unique_ptr<Continuation>> routines_;
  83. size_t stage_;
  84. std::function<void(const Status &, const State &)> handler_;
  85. Pipeline() : stage_(0) {}
  86. ~Pipeline() = default;
  87. void Schedule(const Status &status);
  88. };
  89. template <class State>
  90. inline Pipeline<State> &Pipeline<State>::Push(Continuation *stage) {
  91. routines_.emplace_back(std::unique_ptr<Continuation>(stage));
  92. return *this;
  93. }
  94. template <class State>
  95. inline void Pipeline<State>::Schedule(const Status &status) {
  96. if (!status.ok() || stage_ >= routines_.size()) {
  97. handler_(status, state_);
  98. routines_.clear();
  99. delete this;
  100. } else {
  101. auto next = routines_[stage_].get();
  102. ++stage_;
  103. next->Run(std::bind(&Pipeline::Schedule, this, std::placeholders::_1));
  104. }
  105. }
  106. template <class State> inline void Pipeline<State>::Run(UserHandler &&handler) {
  107. handler_ = std::move(handler);
  108. Schedule(Status::OK());
  109. }
  110. template <class Handler> class BindContinuation : public Continuation {
  111. public:
  112. BindContinuation(const Handler &handler) : handler_(handler) {}
  113. virtual void Run(const Next &next) override { handler_(next); }
  114. private:
  115. Handler handler_;
  116. };
  117. template <class Handler> static inline Continuation *Bind(const Handler &handler) {
  118. return new BindContinuation<Handler>(handler);
  119. }
  120. }
  121. }
  122. #endif