-/* Copyright (c) 2018-2020. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2018-2023. 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/msg/msg_private.hpp"
-#include "xbt/log.h"
+#include <simgrid/modelchecker.h>
+#include <simgrid/s4u/Semaphore.hpp>
+#include <simgrid/semaphore.h>
-#include "simgrid/forward.h"
-#include "simgrid/s4u/Semaphore.hpp"
#include "src/kernel/activity/SemaphoreImpl.hpp"
+#include "src/kernel/actor/SynchroObserver.hpp"
+#include "src/mc/mc_replay.hpp"
-namespace simgrid {
-namespace s4u {
-
-Semaphore::Semaphore(unsigned int initial_capacity)
-{
- sem_ = kernel::actor::simcall([initial_capacity] { return new kernel::activity::SemaphoreImpl(initial_capacity); });
-}
-
-Semaphore::~Semaphore()
-{
- if (sem_ != nullptr) {
- xbt_assert(not sem_->is_used(), "Cannot destroy semaphore since someone is still using it");
- delete sem_;
- }
-}
+namespace simgrid::s4u {
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()
{
- simcall_sem_acquire(sem_);
+ acquire_timeout(-1);
}
-int Semaphore::acquire_timeout(double timeout)
+bool Semaphore::acquire_timeout(double timeout)
{
- return simcall_sem_acquire_timeout(sem_, timeout);
+ kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
+
+ if (MC_is_active() || MC_record_replay_is_active()) { // Split in 2 simcalls for transition persistency
+ kernel::actor::SemaphoreObserver lock_observer{issuer, mc::Transition::Type::SEM_ASYNC_LOCK, pimpl_};
+ auto acquisition =
+ kernel::actor::simcall_answered([issuer, this] { return pimpl_->acquire_async(issuer); }, &lock_observer);
+
+ kernel::actor::SemaphoreAcquisitionObserver wait_observer{issuer, mc::Transition::Type::SEM_WAIT, acquisition.get(),
+ timeout};
+ return kernel::actor::simcall_blocking([issuer, acquisition, timeout] { acquisition->wait_for(issuer, timeout); },
+ &wait_observer);
+
+ } else { // Do it in one simcall only and without observer
+ kernel::actor::SemaphoreAcquisitionObserver observer{issuer, mc::Transition::Type::SEM_WAIT, nullptr, timeout};
+ return kernel::actor::simcall_blocking(
+ [this, issuer, timeout] { pimpl_->acquire_async(issuer)->wait_for(issuer, timeout); }, &observer);
+ }
}
void Semaphore::release()
{
- kernel::actor::simcall([this] { sem_->release(); });
+ kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
+ kernel::actor::SemaphoreObserver observer{issuer, mc::Transition::Type::SEM_UNLOCK, pimpl_};
+
+ kernel::actor::simcall_answered([this] { pimpl_->release(); }, &observer);
}
int Semaphore::get_capacity() const
{
- return kernel::actor::simcall([this] { return sem_->get_capacity(); });
+ return pimpl_->get_capacity();
}
-int Semaphore::would_block() const
+bool Semaphore::would_block() const
{
- return kernel::actor::simcall([this] { return sem_->would_block(); });
+ 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);
+ intrusive_ptr_add_ref(sem->pimpl_);
}
-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;
- }
+ intrusive_ptr_release(sem->pimpl_);
}
-} // namespace s4u
-} // namespace simgrid
+} // namespace simgrid::s4u
/* **************************** Public C interface *************************** */
/** @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