X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/fbcf6ab31cae1988be858f9f894dafe529c575d7..90680837ebef1bd470bd846a684b28a7aff1ab90:/src/kernel/activity/SemaphoreImpl.hpp diff --git a/src/kernel/activity/SemaphoreImpl.hpp b/src/kernel/activity/SemaphoreImpl.hpp index e6dc57d6bb..33793ce403 100644 --- a/src/kernel/activity/SemaphoreImpl.hpp +++ b/src/kernel/activity/SemaphoreImpl.hpp @@ -1,10 +1,10 @@ -/* Copyright (c) 2019. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2019-2021. 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. */ -#ifndef SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP_ -#define SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP_ +#ifndef SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP +#define SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP #include #include @@ -18,22 +18,26 @@ namespace activity { class XBT_PUBLIC SemaphoreImpl { std::atomic_int_fast32_t refcount_{1}; + s4u::Semaphore piface_; unsigned int value_; - -public: actor::SynchroList sleeping_; /* list of sleeping actors*/ - explicit SemaphoreImpl(unsigned int value) : value_(value){}; - ~SemaphoreImpl() = default; +public: + explicit SemaphoreImpl(unsigned int value) : piface_(this), value_(value){}; SemaphoreImpl(SemaphoreImpl const&) = delete; SemaphoreImpl& operator=(SemaphoreImpl const&) = delete; void acquire(actor::ActorImpl* issuer, double timeout); void release(); - bool would_block() { return (value_ == 0); } + bool would_block() const { return (value_ == 0); } + void remove_sleeping_actor(actor::ActorImpl& actor) { xbt::intrusive_erase(sleeping_, actor); } - unsigned int get_capacity() { return value_; } + unsigned int get_capacity() const { return value_; } + bool is_used() const { return not sleeping_.empty(); } + + SemaphoreImpl* ref(); + void unref(); friend void intrusive_ptr_add_ref(SemaphoreImpl* sem) { @@ -42,12 +46,16 @@ public: } friend void intrusive_ptr_release(SemaphoreImpl* sem) { - if (sem->refcount_.fetch_sub(1) == 1) + if (sem->refcount_.fetch_sub(1) == 1) { + xbt_assert(not sem->is_used(), "Cannot destroy semaphore since someone is still using it"); delete sem; + } } + + s4u::Semaphore& sem() { return piface_; } }; } // namespace activity } // namespace kernel } // namespace simgrid -#endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP_ */ +#endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP */