From a21b8f25b77b99346b6b8c069400d7a46b3416b7 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 4 Jun 2017 09:10:57 +0200 Subject: [PATCH] make it possible to return intrusive_ptr in simcalls --- include/simgrid/forward.h | 8 +++++--- src/kernel/activity/ActivityImpl.cpp | 28 +++++++++++++++++++++++----- src/kernel/activity/ActivityImpl.hpp | 16 ++++------------ src/simix/popping_private.h | 18 ++++++++++++++++++ 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/include/simgrid/forward.h b/include/simgrid/forward.h index ff78fe381a..a387d1cc31 100644 --- a/include/simgrid/forward.h +++ b/include/simgrid/forward.h @@ -17,9 +17,11 @@ namespace simgrid { namespace kernel { namespace activity { class ActivityImpl; - } - namespace routing { - class NetPoint; +XBT_PUBLIC(void) intrusive_ptr_add_ref(ActivityImpl* activity); +XBT_PUBLIC(void) intrusive_ptr_release(ActivityImpl* activity); +} +namespace routing { +class NetPoint; } } namespace simix { diff --git a/src/kernel/activity/ActivityImpl.cpp b/src/kernel/activity/ActivityImpl.cpp index 77e657063f..c732b28eea 100644 --- a/src/kernel/activity/ActivityImpl.cpp +++ b/src/kernel/activity/ActivityImpl.cpp @@ -1,21 +1,25 @@ -/* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ #include "src/kernel/activity/ActivityImpl.hpp" -simgrid::kernel::activity::ActivityImpl::ActivityImpl() = default; -simgrid::kernel::activity::ActivityImpl::~ActivityImpl() = default; +namespace simgrid { +namespace kernel { +namespace activity { -void simgrid::kernel::activity::ActivityImpl::ref() +ActivityImpl::ActivityImpl() = default; +ActivityImpl::~ActivityImpl() = default; + +void ActivityImpl::ref() { // Atomic operation! Do not split in two instructions! xbt_assert(refcount_ != 0); refcount_++; } -void simgrid::kernel::activity::ActivityImpl::unref() +void ActivityImpl::unref() { xbt_assert(refcount_ > 0, "This activity has a negative refcount! You can only call test() or wait() once per activity."); @@ -23,3 +27,17 @@ void simgrid::kernel::activity::ActivityImpl::unref() if (refcount_ == 0) delete this; } + +// boost::intrusive_ptr support: +void intrusive_ptr_add_ref(simgrid::kernel::activity::ActivityImpl* activity) +{ + activity->ref(); +} + +void intrusive_ptr_release(simgrid::kernel::activity::ActivityImpl* activity) +{ + activity->unref(); +} +} +} +} // namespace simgrid::kernel::activity:: diff --git a/src/kernel/activity/ActivityImpl.hpp b/src/kernel/activity/ActivityImpl.hpp index e14429663e..1a28a5ff07 100644 --- a/src/kernel/activity/ActivityImpl.hpp +++ b/src/kernel/activity/ActivityImpl.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -31,21 +31,13 @@ namespace activity { virtual void resume()=0; virtual void post() =0; // What to do when a simcall terminates - // boost::intrusive_ptr support: - friend void intrusive_ptr_add_ref(ActivityImpl * activity) - { - activity->ref(); - } - - friend void intrusive_ptr_release(ActivityImpl * activity) - { - activity->unref(); - } - /** @brief Increases the refcount */ void ref(); /** @brief Reduces the refcount */ void unref(); + // boost::intrusive_ptr support: + friend void intrusive_ptr_add_ref(ActivityImpl * activity); + friend void intrusive_ptr_release(ActivityImpl * activity); private: std::atomic_int_fast32_t refcount_{1}; diff --git a/src/simix/popping_private.h b/src/simix/popping_private.h index abbc7566cf..389f89b407 100644 --- a/src/simix/popping_private.h +++ b/src/simix/popping_private.h @@ -9,6 +9,11 @@ #include #include +#include +#include + +#include + SG_BEGIN_DECL() /********************************* Simcalls *********************************/ @@ -118,6 +123,19 @@ T* unmarshal(type, u_smx_scalar const& simcall) return static_cast(simcall.dp); } +template +inline void marshal(type>, u_smx_scalar& simcall, boost::intrusive_ptr value) +{ + intrusive_ptr_add_ref(&*value); + simcall.dp = static_cast(&*value); +} +template inline boost::intrusive_ptr unmarshal(type>, u_smx_scalar const& simcall) +{ + boost::intrusive_ptr res = boost::intrusive_ptr(static_cast(simcall.dp), false); + intrusive_ptr_release(&*res); + return res; +} + template inline void marshal(type, u_smx_scalar& simcall, R(*value)(T...)) { -- 2.20.1