Also make constructor private to enforce usage of create(), and fix bogus destructor.
Like for Mutex, could not manage to make ~Semaphore() private.
class Semaphore;
/** Smart pointer to a simgrid::s4u::Semaphore */
using SemaphorePtr = boost::intrusive_ptr<Semaphore>;
-XBT_PUBLIC void intrusive_ptr_release(Semaphore* m);
-XBT_PUBLIC void intrusive_ptr_add_ref(Semaphore* m);
+XBT_PUBLIC void intrusive_ptr_release(const Semaphore* m);
+XBT_PUBLIC void intrusive_ptr_add_ref(const Semaphore* m);
class Disk;
} // namespace s4u
*
*/
class XBT_PUBLIC Semaphore {
- kernel::activity::SemaphoreImpl* const pimpl_;
- std::atomic_int_fast32_t refcount_{0};
+ friend kernel::activity::SemaphoreImpl;
- friend void intrusive_ptr_add_ref(Semaphore* sem);
- friend void intrusive_ptr_release(Semaphore* sem);
+ kernel::activity::SemaphoreImpl* const pimpl_;
-public:
- explicit Semaphore(unsigned int initial_capacity);
- ~Semaphore();
+ friend void intrusive_ptr_add_ref(const Semaphore* sem);
+ friend void intrusive_ptr_release(const Semaphore* sem);
+ explicit Semaphore(kernel::activity::SemaphoreImpl* sem) : pimpl_(sem) {}
#ifndef DOXYGEN
Semaphore(Semaphore const&) = delete; // No copy constructor. Use SemaphorePtr instead
Semaphore& operator=(Semaphore const&) = delete; // No direct assignment either. Use SemaphorePtr instead
#endif
+public:
/** Constructs a new semaphore */
static SemaphorePtr create(unsigned int initial_capacity);
}
}
+/** Increase the refcount for this semaphore */
+SemaphoreImpl* SemaphoreImpl::ref()
+{
+ intrusive_ptr_add_ref(this);
+ return this;
+}
+
+/** Decrease the refcount for this mutex */
+void SemaphoreImpl::unref()
+{
+ intrusive_ptr_release(this);
+}
+
} // namespace activity
} // namespace kernel
} // namespace simgrid
class XBT_PUBLIC SemaphoreImpl {
std::atomic_int_fast32_t refcount_{1};
+ s4u::Semaphore piface_;
unsigned int value_;
actor::SynchroList sleeping_; /* list of sleeping actors*/
public:
- explicit SemaphoreImpl(unsigned int value) : value_(value){};
+ explicit SemaphoreImpl(unsigned int value) : piface_(this), value_(value){};
SemaphoreImpl(SemaphoreImpl const&) = delete;
SemaphoreImpl& operator=(SemaphoreImpl const&) = delete;
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)
{
XBT_ATTRIB_UNUSED auto previous = sem->refcount_.fetch_add(1);
}
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 {
namespace s4u {
-Semaphore::Semaphore(unsigned int initial_capacity) : pimpl_(new kernel::activity::SemaphoreImpl(initial_capacity)) {}
-
-Semaphore::~Semaphore()
-{
- xbt_assert(not pimpl_->is_used(), "Cannot destroy semaphore since someone is still using it");
- delete pimpl_;
-}
-
SemaphorePtr Semaphore::create(unsigned int initial_capacity)
{
- return SemaphorePtr(new Semaphore(initial_capacity));
+ auto* sem = new kernel::activity::SemaphoreImpl(initial_capacity);
+ return SemaphorePtr(&sem->sem(), false);
}
void Semaphore::acquire()
return kernel::actor::simcall([this] { return pimpl_->would_block(); });
}
-void intrusive_ptr_add_ref(Semaphore* sem)
+/* refcounting of the intrusive_ptr is delegated to the implementation object */
+void intrusive_ptr_add_ref(const Semaphore* sem)
{
xbt_assert(sem);
- sem->refcount_.fetch_add(1, std::memory_order_relaxed);
+ sem->pimpl_->ref();
}
-void intrusive_ptr_release(Semaphore* sem)
+void intrusive_ptr_release(const Semaphore* sem)
{
xbt_assert(sem);
- if (sem->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
- std::atomic_thread_fence(std::memory_order_acquire);
- delete sem;
- }
+ sem->pimpl_->unref();
}
} // namespace s4u
/** @brief creates a semaphore object of the given initial capacity */
sg_sem_t sg_sem_init(int initial_value)
{
- return new simgrid::s4u::Semaphore(initial_value);
+ return simgrid::s4u::Semaphore::create(initial_value).detach();
}
/** @brief locks on a semaphore object */
void sg_sem_destroy(const_sg_sem_t sem)
{
- delete sem;
+ intrusive_ptr_release(sem);
}
/** @brief returns a boolean indicating if this semaphore would block at this very specific time