+const char* ActivityImpl::get_state_str() const
+{
+ return to_c_str(state_);
+}
+
+bool ActivityImpl::test(actor::ActorImpl* issuer)
+{
+ if (state_ != State::WAITING && state_ != State::RUNNING) {
+ finish();
+ issuer->exception_ = nullptr; // Do not propagate exception in that case
+ return true;
+ }
+
+ if (auto* observer = dynamic_cast<kernel::actor::ActivityTestSimcall*>(issuer->simcall_.observer_))
+ observer->set_result(false);
+
+ return false;
+}
+
+ssize_t ActivityImpl::test_any(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities)
+{
+ auto* observer = dynamic_cast<kernel::actor::ActivityTestanySimcall*>(issuer->simcall_.observer_);
+ xbt_assert(observer != nullptr);
+
+ if (MC_is_active() || MC_record_replay_is_active()) {
+ int idx = observer->get_value();
+ xbt_assert(idx == -1 || activities[idx]->test(issuer));
+ return idx;
+ }
+
+ for (std::size_t i = 0; i < activities.size(); ++i) {
+ if (activities[i]->test(issuer)) {
+ observer->set_result(i);
+ return i;
+ }
+ }
+ return -1;
+}
+
+void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
+{
+ XBT_DEBUG("Wait for execution of synchro %p, state %s", this, get_state_str());
+ xbt_assert(std::isfinite(timeout), "timeout is not finite!");
+
+ /* Associate this simcall to the synchro */
+ register_simcall(&issuer->simcall_);
+
+ xbt_assert(not MC_is_active() && not MC_record_replay_is_active(), "MC is currently not supported here.");
+
+ /* If the synchro is already finished then perform the error handling */
+ if (state_ != State::WAITING && state_ != State::RUNNING) {
+ finish();
+ } else {
+ /* we need a sleep action (even when the timeout is infinite) to be notified of host failures */
+ /* Comms handle that a bit differently of the other activities */
+ if (auto* comm = dynamic_cast<CommImpl*>(this)) {
+ resource::Action* sleep_action = issuer->get_host()->get_cpu()->sleep(timeout);
+ sleep_action->set_activity(comm);
+
+ if (issuer == comm->src_actor_)
+ comm->src_timeout_ = sleep_action;
+ else
+ comm->dst_timeout_ = sleep_action;
+ } else {
+ SynchroImplPtr synchro(new SynchroImpl([this, issuer]() {
+ this->unregister_simcall(&issuer->simcall_);
+ issuer->waiting_synchro_ = nullptr;
+ issuer->exception_ = nullptr;
+ auto* observer = dynamic_cast<kernel::actor::ActivityWaitSimcall*>(issuer->simcall_.observer_);
+ xbt_assert(observer != nullptr);
+ observer->set_result(true); // Returns that the wait_for timeouted
+ }));
+ synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
+ synchro->register_simcall(&issuer->simcall_);
+ }
+ }
+}
+
+void ActivityImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities, double timeout)
+{
+ XBT_DEBUG("Wait for execution of any synchro");
+ if (MC_is_active() || MC_record_replay_is_active()) {
+ auto* observer = dynamic_cast<kernel::actor::ActivityWaitanySimcall*>(issuer->simcall_.observer_);
+ xbt_assert(observer != nullptr);
+ xbt_assert(timeout <= 0.0, "Timeout not implemented for waitany in the model-checker");
+ if (int idx = observer->get_value(); idx != -1) {
+ auto* act = activities.at(idx);
+ act->simcalls_.push_back(&issuer->simcall_);
+ observer->set_result(idx);
+ act->set_state(State::DONE);
+ act->finish();
+ }
+ return;
+ }
+
+ if (timeout < 0.0) {
+ issuer->simcall_.timeout_cb_ = nullptr;
+ } else {
+ issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &activities]() {
+ issuer->simcall_.timeout_cb_ = nullptr;
+ for (auto* act : activities)
+ act->unregister_simcall(&issuer->simcall_);
+ // default result (-1) is set in actor::ActivityWaitanySimcall
+ issuer->simcall_answer();
+ });
+ }
+
+ for (auto* act : activities) {
+ /* associate this simcall to the the synchro */
+ act->simcalls_.push_back(&issuer->simcall_);
+ /* see if the synchro is already finished */
+ if (act->get_state() != State::WAITING && act->get_state() != State::RUNNING) {
+ act->finish();
+ break;
+ }
+ }
+ XBT_DEBUG("Exit from ActivityImlp::wait_any_for");
+}
+