Commit d713ec10 authored by Dominik Charousset's avatar Dominik Charousset

fixed build with CPPA_DISABLE_CONTEXT_SWITCHING set

parent e9ce5447
......@@ -31,12 +31,9 @@
#ifndef CPPA_CONTEXT_SWITCHING_ACTOR_HPP
#define CPPA_CONTEXT_SWITCHING_ACTOR_HPP
#include "cppa/config.hpp"
#ifndef CPPA_DISABLE_CONTEXT_SWITCHING
#include <stack>
#include "cppa/config.hpp"
#include "cppa/either.hpp"
#include "cppa/pattern.hpp"
......@@ -122,6 +119,4 @@ class context_switching_actor : public detail::stacked_actor_mixin<
} // namespace cppa
#endif // CPPA_DISABLE_CONTEXT_SWITCHING
#endif // CPPA_CONTEXT_SWITCHING_ACTOR_HPP
......@@ -81,20 +81,6 @@ class thread_pool_scheduler : public scheduler {
actor_ptr spawn_as_thread(void_function fun, init_callback cb, bool hidden);
# ifndef CPPA_DISABLE_CONTEXT_SWITCHING
template<typename F>
actor_ptr spawn_impl(void_function fun, scheduling_hint sh, F cb) {
if (sh == scheduled) {
scheduled_actor_ptr ptr{new context_switching_actor(std::move(fun))};
ptr->attach_to_scheduler(this);
cb(ptr.get());
return spawn_impl(std::move(ptr));
}
else {
return spawn_as_thread(std::move(fun), std::move(cb));
}
}
# endif
};
} } // namespace cppa::detail
......
......@@ -31,20 +31,6 @@
#ifndef CPPA_FIBER_HPP
#define CPPA_FIBER_HPP
#include <memory>
#include "cppa/config.hpp"
#ifdef CPPA_DISABLE_CONTEXT_SWITCHING
namespace cppa { namespace util {
class fiber {
public:
inline static void swap(fiber&, fiber&) { }
};
} } // namespace cppa::util
#else // CPPA_DISABLE_CONTEXT_SWITCHING
namespace cppa { namespace util {
struct fiber_impl;
......@@ -69,6 +55,4 @@ class fiber {
} } // namespace cppa::util
#endif // CPPA_DISABLE_CONTEXT_SWITCHING
#endif // CPPA_FIBER_HPP
......@@ -28,17 +28,31 @@
\******************************************************************************/
#include <cstdint>
#include <stdexcept>
#include "cppa/util/fiber.hpp"
#ifdef CPPA_DISABLE_CONTEXT_SWITCHING
namespace { int keep_compiler_happy() { return 42; } }
namespace cppa { namespace util {
fiber::fiber() throw() : m_impl(nullptr) { }
fiber::fiber(void (*)(void*), void*) : m_impl(nullptr) { }
fiber::~fiber() { }
void fiber::swap(fiber&, fiber&) {
throw std::logic_error("libcppa was compiled using "
"CPPA_DISABLE_CONTEXT_SWITCHING");
}
} } // namespace cppa::util
#else // ifdef CPPA_DISABLE_CONTEXT_SWITCHING
#include <cstdint>
#include <boost/context/all.hpp>
#include "cppa/util/fiber.hpp"
namespace cppa { namespace util {
void fiber_trampoline(intptr_t iptr);
......
......@@ -51,6 +51,24 @@ namespace {
typedef std::unique_lock<std::mutex> guard_type;
typedef intrusive::single_reader_queue<thread_pool_scheduler::worker> worker_queue;
/*
template<typename F>
actor_ptr spawn_void_fun_impl(thread_pool_scheduler* _this,
void_function fun,
scheduling_hint sh,
F cb ) {
if (sh == scheduled) {
scheduled_actor_ptr ptr{new context_switching_actor(std::move(fun))};
ptr->attach_to_scheduler(_this);
cb(ptr.get());
return _this->spawn_impl(std::move(ptr));
}
else {
return _this->spawn_as_thread(std::move(fun), std::move(cb));
}
}
*/
} // namespace <anonmyous>
struct thread_pool_scheduler::worker {
......@@ -245,6 +263,7 @@ actor_ptr thread_pool_scheduler::spawn(scheduled_actor* raw, init_callback cb) {
}
#ifndef CPPA_DISABLE_CONTEXT_SWITCHING
actor_ptr thread_pool_scheduler::spawn(void_function fun, scheduling_hint sh) {
if (sh == scheduled) {
scheduled_actor_ptr ptr{new context_switching_actor(std::move(fun))};
......@@ -267,17 +286,26 @@ actor_ptr thread_pool_scheduler::spawn(void_function fun,
return spawn_impl(std::move(ptr));
}
else {
return spawn_as_thread(std::move(fun), std::move(init_cb), sh == detached_and_hidden);
return spawn_as_thread(std::move(fun),
std::move(init_cb),
sh == detached_and_hidden);
}
}
#else
actor_ptr thread_pool_scheduler::spawn(void_function what, scheduling_hint) {
return spawn_as_thread(std::move(what), [](local_actor*) { });
#else // CPPA_DISABLE_CONTEXT_SWITCHING
actor_ptr thread_pool_scheduler::spawn(void_function what, scheduling_hint sh) {
return spawn_as_thread(std::move(what),
[](local_actor*) { },
sh == detached_and_hidden);
}
actor_ptr thread_pool_scheduler::spawn(void_function what,
scheduling_hint,
scheduling_hint sh,
init_callback init_cb) {
return spawn_as_thread(std::move(what), std::move(init_cb));
return spawn_as_thread(std::move(what),
std::move(init_cb),
sh == detached_and_hidden);
}
#endif
......
......@@ -604,6 +604,63 @@ size_t test__spawn() {
await_all_others_done();
CPPA_IF_VERBOSE(cout << "ok" << endl);
auto inflater = factory::event_based(
[](std::string*, actor_ptr* receiver) {
self->become(
on_arg_match >> [=](int n, const std::string& s) {
send(*receiver, n * 2, s);
},
on(atom("done")) >> []() {
self->quit();
}
);
}
);
auto joe = inflater.spawn("Joe", self);
auto bob = inflater.spawn("Bob", joe);
send(bob, 1, "hello actor");
receive (
on(4, "hello actor") >> []() {
// done
},
others() >> [] {
cerr << "unexpected: " << to_string(self->last_dequeued()) << endl;
}
);
// kill joe and bob
auto poison_pill = make_any_tuple(atom("done"));
joe << poison_pill;
bob << poison_pill;
await_all_others_done();
std::function<actor_ptr (const std::string&, const actor_ptr&)> spawn_next;
auto kr34t0r = factory::event_based(
// it's safe to pass spawn_next as reference here, because
// - it is guaranteeed to outlive kr34t0r by general scoping rules
// - the lambda is always executed in the current actor's thread
// but using spawn_next in a message handler could
// still cause undefined behavior!
[&spawn_next](std::string* name, actor_ptr* pal) {
if (*name == "Joe" && !*pal) {
*pal = spawn_next("Bob", self);
}
self->become (
others() >> [pal,name]() {
cout << "hello & bye from " << *name << endl;
// forward message and die
*pal << self->last_dequeued();
self->quit();
}
);
}
);
spawn_next = [&kr34t0r](const std::string& name, const actor_ptr& pal) {
return kr34t0r.spawn(name, pal);
};
auto joe_the_second = kr34t0r.spawn("Joe");
send(joe_the_second, atom("done"));
await_all_others_done();
int zombie_init_called = 0;
int zombie_on_exit_called = 0;
factory::event_based([&]() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment