SSP21-CPP
StrandExecutor.h
1 /*
2  * Copyright (c) 2018, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef EXE4CPP_ASIO_STRANDEXECUTOR_H
26 #define EXE4CPP_ASIO_STRANDEXECUTOR_H
27 
28 #include "exe4cpp/IExecutor.h"
29 #include "exe4cpp/asio/AsioTimer.h"
30 
31 #include "asio.hpp"
32 
33 namespace exe4cpp
34 {
35 
36 /**
37 *
38 * Implementation of openpal::IExecutor backed by asio::strand
39 *
40 * Shutdown life-cycle guarantees are provided by using std::shared_ptr
41 *
42 */
43 class StrandExecutor final :
44  public exe4cpp::IExecutor,
45  public std::enable_shared_from_this<StrandExecutor>
46 {
47 
48 public:
49 
50  StrandExecutor(const std::shared_ptr<asio::io_service>& io_service) :
51  io_service{io_service},
52  strand{*io_service}
53  {}
54 
55  static std::shared_ptr<StrandExecutor> create(const std::shared_ptr<asio::io_service>& io_service)
56  {
57  return std::make_shared<StrandExecutor>(io_service);
58  }
59 
60  std::shared_ptr<StrandExecutor> fork()
61  {
62  return create(this->io_service);
63  }
64 
65  // ---- Implement IExecutor -----
66 
67  virtual Timer start(const duration_t& duration, const action_t& action) override
68  {
69  return this->start(get_time() + duration, action);
70  }
71 
72  virtual Timer start(const steady_time_t& expiration, const action_t& action) override
73  {
74  const auto timer = AsioTimer::create(this->io_service);
75 
76  timer->impl.expires_at(expiration);
77 
78  // neither this executor nor the timer can be deleted while the timer is still active
79  auto callback = [timer, action, self = shared_from_this()](const std::error_code & ec)
80  {
81  if (!ec) // an error indicate timer was canceled
82  {
83  action();
84  }
85  };
86 
87  timer->impl.async_wait(strand.wrap(callback));
88 
89  return Timer(timer);
90  }
91 
92  virtual void post(const action_t& action) override
93  {
94  auto callback = [action, self = shared_from_this()]()
95  {
96  action();
97  };
98 
99  strand.post(callback);
100  }
101 
102  virtual steady_time_t get_time() override
103  {
104  return std::chrono::steady_clock::now();
105  }
106 
107  inline std::shared_ptr<asio::io_service> get_service()
108  {
109  return io_service;
110  }
111 
112  template <typename handler_t>
113  asio::detail::wrapped_handler<asio::strand, handler_t, asio::detail::is_continuation_if_running> wrap(const handler_t& handler)
114  {
115  return strand.wrap(handler);
116  }
117 
118 private:
119  // we hold a shared_ptr to the io_service so that it cannot dissapear while the strand is still executing
120  const std::shared_ptr<asio::io_service> io_service;
121  asio::strand strand;
122 };
123 
124 }
125 
126 #endif
virtual void post(const action_t &action) override
exe4cpp header-only library namespace
Definition: AsioTimer.h:29
virtual Timer start(const steady_time_t &expiration, const action_t &action) override
virtual steady_time_t get_time() override
virtual Timer start(const duration_t &duration, const action_t &action) override