Commit c249a61c authored by Dominik Charousset's avatar Dominik Charousset

Merge branch 'master' of https://github.com/toffaletti/libcppa into unstable

parents 756ca14c 1ea993b2
cmake_minimum_required(VERSION 2.8)
project(cppa CXX)
project(cppa CXX C)
set(LIBCPPA_VERSION_MAJOR 0)
set(LIBCPPA_VERSION_MINOR 5)
......@@ -30,6 +30,15 @@ else (CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
endif (CMAKE_CXX_FLAGS)
include(CheckIncludeFiles)
check_include_files("valgrind/valgrind.h" HAVE_VALGRIND_H)
set(VALGRIND "no")
if (NOT HAVE_VALGRIND_H)
add_definitions(-DNVALGRIND)
else (NOT HAVE_VALGRIND_H)
set(VALGRIND "yes")
endif (NOT HAVE_VALGRIND_H)
# check for g++ >= 4.7 or clang++ > = 3.2
try_run(ProgramResult
CompilationSucceeded
......@@ -162,7 +171,7 @@ set(LD_FLAGS ${CMAKE_LD_LIBS} ${PTHREAD_LIBRARIES})
if (DISABLE_CONTEXT_SWITCHING)
# explicitly disabled
else (DISABLE_CONTEXT_SWITCHING)
find_package(Boost 1.52.0 COMPONENTS context)
find_package(Boost 1.51.0 COMPONENTS context)
# without REQUIRED, find_package(Boost...) returns with Boost_FOUND=FALSE
if (NOT Boost_FOUND)
set(DISABLE_CONTEXT_SWITCHING true)
......@@ -347,6 +356,7 @@ message("\n====================| Build Summary |===================="
"\nDebug mode: ${DEBUG_MODE_STR}"
"\nLog level: ${LOG_LEVEL_STR}"
"\nContext switching: ${CONTEXT_SWITCHING}"
"\nValgrind: ${VALGRIND}"
"\nBuild examples: ${BUILD_EXAMPLES}"
"\nBuild unit tests: ${BUILD_UNIT_TESTS}"
"\nBuild static: ${CPPA_BUILD_STATIC}"
......
......@@ -51,31 +51,47 @@ void fiber::swap(fiber&, fiber&) {
#else // ifdef CPPA_DISABLE_CONTEXT_SWITCHING
#ifndef NVALGRIND
#include <valgrind/valgrind.h>
#endif
#include <boost/version.hpp>
#include <boost/context/all.hpp>
namespace cppa { namespace util {
void fiber_trampoline(intptr_t iptr);
#if BOOST_VERSION == 105100
namespace ctx = boost::ctx;
#else
namespace ctx = boost::context;
#endif
class fiber_impl {
public:
fiber_impl() : m_ctx(nullptr) { }
fiber_impl() : m_ctx{} { }
virtual ~fiber_impl() { }
virtual void run() { }
void swap(fiber_impl* to) {
#if BOOST_VERSION == 105100
ctx::jump_fcontext(&m_ctx, &to->m_ctx, (intptr_t) to);
#else
ctx::jump_fcontext(m_ctx, to->m_ctx, (intptr_t) to);
#endif
}
protected:
#if BOOST_VERSION == 105100
ctx::fcontext_t m_ctx;
#else
ctx::fcontext_t* m_ctx;
#endif
};
......@@ -84,7 +100,11 @@ class converted_fiber : public fiber_impl {
public:
#if BOOST_VERSION == 105100
converted_fiber() { m_ctx = m_ctx_obj; }
#else
converted_fiber() { m_ctx = &m_ctx_obj; }
#endif
private:
......@@ -95,17 +115,50 @@ class converted_fiber : public fiber_impl {
// a fiber executing a function
class fun_fiber : public fiber_impl {
#if BOOST_VERSION == 105100
typedef ctx::stack_allocator allocator;
#else
typedef ctx::guarded_stack_allocator allocator;
#endif
public:
fun_fiber(void (*fun)(void*), void* arg) : m_arg(arg), m_fun(fun) {
m_size = allocator::minimum_stacksize();
m_ctx = ctx::make_fcontext(m_alloc.allocate(m_size), m_size, fiber_trampoline);
#if BOOST_VERSION == 105100
size_t stacksize = ctx::minimum_stacksize();
m_ctx.fc_stack.base = m_alloc.allocate(stacksize);
m_ctx.fc_stack.limit = reinterpret_cast<void *>(
reinterpret_cast<intptr_t>(m_ctx.fc_stack.base)-stacksize);
ctx::make_fcontext(&m_ctx, fiber_trampoline);
#ifndef NVALGRIND
m_valgrind_stack_id =
VALGRIND_STACK_REGISTER(m_ctx.fc_stack.base,
reinterpret_cast<intptr_t>(m_ctx.fc_stack.base)-stacksize);
#endif
#else // BOOST_VERSION
size_t stacksize = allocator::minimum_stacksize();
m_ctx = ctx::make_fcontext(m_alloc.allocate(stacksize), stacksize, fiber_trampoline);
#ifndef NVALGRIND
m_valgrind_stack_id =
VALGRIND_STACK_REGISTER(m_ctx->fc_stack.sp,
reinterpret_cast<intptr_t>(m_ctx->fc_stack.sp)-stacksize);
#endif
#endif // BOOST_VERSION
}
~fun_fiber() {
m_alloc.deallocate(m_ctx->fc_stack.sp, m_size);
#ifndef NVALGRIND
VALGRIND_STACK_DEREGISTER(m_valgrind_stack_id);
#endif
#if BOOST_VERSION == 105100
size_t stacksize =
reinterpret_cast<intptr_t>(m_ctx.fc_stack.base)
- reinterpret_cast<intptr_t>(m_ctx.fc_stack.base);
m_alloc.deallocate(m_ctx.fc_stack.base, stacksize);
#else
m_alloc.deallocate(m_ctx->fc_stack.sp, m_ctx->fc_stack.size);
#endif
}
virtual void run() {
......@@ -117,8 +170,12 @@ class fun_fiber : public fiber_impl {
void* m_arg;
void (*m_fun)(void*);
size_t m_size;
allocator m_alloc;
#ifndef NVALGRIND
//! stack id so valgrind doesn't freak when stack swapping happens
int m_valgrind_stack_id;
#endif
};
......
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