-/* Copyright (c) 2019-2022. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2019-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. */
#ifndef SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP
#define SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP
-#include <atomic>
-#include <boost/intrusive/list.hpp>
-
#include "simgrid/s4u/Semaphore.hpp"
#include "src/kernel/actor/ActorImpl.hpp"
+#include "src/kernel/actor/SynchroObserver.hpp"
+
+#include <atomic>
+#include <boost/intrusive/list.hpp>
-namespace simgrid {
-namespace kernel {
-namespace activity {
+namespace simgrid::kernel::activity {
/** Semaphore Acquisition: the act / process of acquiring the semaphore.
*
bool granted_ = false;
friend SemaphoreImpl;
+ friend actor::SemaphoreAcquisitionObserver;
public:
SemAcquisitionImpl(actor::ActorImpl* issuer, SemaphoreImpl* sem) : issuer_(issuer), semaphore_(sem) {}
bool test(actor::ActorImpl* issuer = nullptr) override { return granted_; }
void wait_for(actor::ActorImpl* issuer, double timeout) override;
- void post() override;
void finish() override;
+ void cancel() override;
void set_exception(actor::ActorImpl* issuer) override
{ /* nothing to do */
}
std::atomic_int_fast32_t refcount_{1};
s4u::Semaphore piface_;
unsigned int value_;
- std::deque<SemAcquisitionImplPtr> sleeping_; /* ongoing acquisitions */
+ std::deque<SemAcquisitionImplPtr> ongoing_acquisitions_;
+
+ static unsigned next_id_;
+ const unsigned id_ = next_id_++;
friend SemAcquisitionImpl;
+ friend actor::SemaphoreObserver;
public:
explicit SemaphoreImpl(unsigned int value) : piface_(this), value_(value){};
bool would_block() const { return (value_ == 0); }
unsigned int get_capacity() const { return value_; }
- bool is_used() const { return not sleeping_.empty(); }
+ bool is_used() const { return not ongoing_acquisitions_.empty(); }
friend void intrusive_ptr_add_ref(SemaphoreImpl* sem)
{
delete sem;
}
}
-
+ unsigned get_id() const { return id_; }
s4u::Semaphore& sem() { return piface_; }
};
-} // namespace activity
-} // namespace kernel
-} // namespace simgrid
+} // namespace simgrid::kernel::activity
#endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP */