From 891abf1a3579d0e378a6e1b3426e5c34b23a58ad Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Tue, 14 Jun 2016 13:26:58 +0200 Subject: [PATCH] [simix] Add refcount to Process --- src/simix/smx_process.cpp | 14 +++++++++----- src/simix/smx_process_private.h | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/simix/smx_process.cpp b/src/simix/smx_process.cpp index 1eb504d23d..836d2d0ae8 100644 --- a/src/simix/smx_process.cpp +++ b/src/simix/smx_process.cpp @@ -126,17 +126,21 @@ void SIMIX_process_empty_trash(void) while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) { XBT_DEBUG("Getting rid of %p",process); - delete process->context; - xbt_dict_free(&process->properties); - xbt_fifo_free(process->comms); - xbt_dynar_free(&process->on_exit); - delete process; + intrusive_ptr_release(process); } } namespace simgrid { namespace simix { +Process::~Process() +{ + delete this->context; + xbt_dict_free(&this->properties); + xbt_fifo_free(this->comms); + xbt_dynar_free(&this->on_exit); +} + void create_maestro(std::function code) { smx_process_t maestro = nullptr; diff --git a/src/simix/smx_process_private.h b/src/simix/smx_process_private.h index 0a81e9a410..837cc22d8e 100644 --- a/src/simix/smx_process_private.h +++ b/src/simix/smx_process_private.h @@ -7,6 +7,7 @@ #ifndef _SIMIX_PROCESS_PRIVATE_H #define _SIMIX_PROCESS_PRIVATE_H +#include #include #include @@ -68,6 +69,24 @@ public: std::function code; smx_timer_t kill_timer = nullptr; int segment_index = 0; /*Reference to an SMPI process' data segment. Default value is -1 if not in SMPI context*/ + + friend void intrusive_ptr_add_ref(Process* process) + { + auto previous = (process->refcount_)++; + xbt_assert(previous != 0); + (void) previous; + } + friend void intrusive_ptr_release(Process* process) + { + auto count = --(process->refcount_); + if (count == 0) + delete process; + } + + ~Process(); + +private: + std::atomic_int_fast32_t refcount_ { 1 }; }; } -- 2.20.1