/usr/include/caf/blocking_actor.hpp is in libcaf-dev 0.13.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | /******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2015 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_BLOCKING_ACTOR_HPP
#define CAF_BLOCKING_ACTOR_HPP
#include <mutex>
#include <condition_variable>
#include "caf/none.hpp"
#include "caf/on.hpp"
#include "caf/extend.hpp"
#include "caf/behavior.hpp"
#include "caf/local_actor.hpp"
#include "caf/typed_actor.hpp"
#include "caf/mailbox_element.hpp"
#include "caf/response_handle.hpp"
#include "caf/detail/type_traits.hpp"
#include "caf/mixin/sync_sender.hpp"
namespace caf {
/**
* A thread-mapped or context-switching actor using a blocking
* receive rather than a behavior-stack based message processing.
* @extends local_actor
*/
class blocking_actor
: public extend<local_actor, blocking_actor>::
with<mixin::sync_sender<blocking_response_handle_tag>::impl> {
public:
class functor_based;
blocking_actor();
~blocking_actor();
/**************************************************************************
* utility stuff and receive() member function family *
**************************************************************************/
using timeout_type = std::chrono::high_resolution_clock::time_point;
struct receive_while_helper {
std::function<void(behavior&)> m_dq;
std::function<bool()> m_stmt;
template <class... Ts>
void operator()(Ts&&... xs) {
static_assert(sizeof...(Ts) > 0,
"operator() requires at least one argument");
behavior bhvr{std::forward<Ts>(xs)...};
while (m_stmt()) m_dq(bhvr);
}
};
template <class T>
struct receive_for_helper {
std::function<void(behavior&)> m_dq;
T& begin;
T end;
template <class... Ts>
void operator()(Ts&&... xs) {
behavior bhvr{std::forward<Ts>(xs)...};
for (; begin != end; ++begin) m_dq(bhvr);
}
};
struct do_receive_helper {
std::function<void(behavior&)> m_dq;
behavior m_bhvr;
template <class Statement>
void until(Statement stmt) {
do {
m_dq(m_bhvr);
} while (stmt() == false);
}
void until(const bool& bvalue) {
until([&] { return bvalue; });
}
};
/**
* Dequeues the next message from the mailbox that is
* matched by given behavior.
*/
template <class... Ts>
void receive(Ts&&... xs) {
static_assert(sizeof...(Ts), "at least one argument required");
behavior bhvr{std::forward<Ts>(xs)...};
dequeue(bhvr);
}
/**
* Semantically equal to: `for (;;) { receive(...); }`, but does
* not cause a temporary behavior object per iteration.
*/
template <class... Ts>
void receive_loop(Ts&&... xs) {
behavior bhvr{std::forward<Ts>(xs)...};
for (;;) dequeue(bhvr);
}
/**
* Receives messages for range `[begin, first)`.
* Semantically equal to:
* `for ( ; begin != end; ++begin) { receive(...); }`.
*
* **Usage example:**
* ~~~
* int i = 0;
* receive_for(i, 10) (
* on(atom("get")) >> [&]() -> message {
* return {"result", i};
* }
* );
* ~~~
*/
template <class T>
receive_for_helper<T> receive_for(T& begin, const T& end) {
return {make_dequeue_callback(), begin, end};
}
/**
* Receives messages as long as `stmt` returns true.
* Semantically equal to: `while (stmt()) { receive(...); }`.
*
* **Usage example:**
* ~~~
* int i = 0;
* receive_while([&]() { return (++i <= 10); })
* (
* on<int>() >> int_fun,
* on<float>() >> float_fun
* );
* ~~~
*/
template <class Statement>
receive_while_helper receive_while(Statement stmt) {
static_assert(std::is_same<bool, decltype(stmt())>::value,
"functor or function does not return a boolean");
return {make_dequeue_callback(), stmt};
}
/**
* Receives messages until `stmt` returns true.
*
* Semantically equal to:
* `do { receive(...); } while (stmt() == false);`
*
* **Usage example:**
* ~~~
* int i = 0;
* do_receive
* (
* on<int>() >> int_fun,
* on<float>() >> float_fun
* )
* .until([&]() { return (++i >= 10); };
* ~~~
*/
template <class... Ts>
do_receive_helper do_receive(Ts&&... xs) {
return {make_dequeue_callback(), behavior{std::forward<Ts>(xs)...}};
}
/**
* Blocks this actor until all other actors are done.
*/
void await_all_other_actors_done();
/**
* Implements the actor's behavior.
*/
virtual void act() = 0;
/** @cond PRIVATE */
void initialize() override;
void dequeue(behavior& bhvr, message_id mid = invalid_message_id);
/** @endcond */
protected:
// helper function to implement receive_(for|while) and do_receive
std::function<void(behavior&)> make_dequeue_callback() {
return [=](behavior& bhvr) { dequeue(bhvr); };
}
};
class blocking_actor::functor_based : public blocking_actor {
public:
using act_fun = std::function<void(blocking_actor*)>;
template <class F, class... Ts>
functor_based(F f, Ts&&... xs) {
using trait = typename detail::get_callable_trait<F>::type;
using arg0 = typename detail::tl_head<typename trait::arg_types>::type;
blocking_actor* dummy = nullptr;
std::integral_constant<bool, std::is_same<arg0, blocking_actor*>::value> tk;
create(dummy, tk, f, std::forward<Ts>(xs)...);
}
void cleanup(uint32_t reason);
protected:
void act() override;
private:
void create(blocking_actor*, act_fun);
template <class Actor, typename F, class... Ts>
void create(Actor* dummy, std::true_type, F f, Ts&&... xs) {
create(dummy, std::bind(f, std::placeholders::_1, std::forward<Ts>(xs)...));
}
template <class Actor, typename F, class... Ts>
void create(Actor* dummy, std::false_type, F f, Ts&&... xs) {
std::function<void()> fun = std::bind(f, std::forward<Ts>(xs)...);
create(dummy, [fun](Actor*) { fun(); });
}
act_fun m_act;
};
} // namespace caf
#endif // CAF_BLOCKING_ACTOR_HPP
|