Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use ssize_t as return type for Comm::wait_any.
[simgrid.git] / src / simix / libsmx.cpp
index 3c1804b..d32231a 100644 (file)
@@ -5,57 +5,77 @@
 /*                                                                            */
 /* This is somehow the "libc" of SimGrid                                      */
 
-/* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-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. */
 
 #include "mc/mc.h"
-#include "simgrid/simix/blocking_simcall.hpp"
 #include "src/kernel/activity/CommImpl.hpp"
 #include "src/kernel/activity/ConditionVariableImpl.hpp"
 #include "src/kernel/activity/ExecImpl.hpp"
 #include "src/kernel/activity/IoImpl.hpp"
 #include "src/kernel/activity/MailboxImpl.hpp"
 #include "src/kernel/activity/MutexImpl.hpp"
+#include "src/kernel/activity/SemaphoreImpl.hpp"
+#include "src/kernel/actor/SimcallObserver.hpp"
 #include "src/mc/mc_replay.hpp"
 #include "src/plugins/vm/VirtualMachineImpl.hpp"
+#include "xbt/random.hpp"
 
 #include "popping_bodies.cpp"
 
+#include <boost/core/demangle.hpp>
+#include <string>
+#include <typeinfo>
+
 /**
  * @ingroup simix_host_management
  * @brief Waits for the completion of an execution synchro and destroy it.
  *
  * @param execution The execution synchro
  */
-e_smx_state_t simcall_execution_wait(simgrid::kernel::activity::ActivityImpl* execution, double timeout)
+simgrid::kernel::activity::State simcall_execution_wait(simgrid::kernel::activity::ActivityImpl* execution,
+                                                        double timeout) // XBT_ATTRIB_DEPRECATED_v330
 {
-  return (e_smx_state_t)simcall_BODY_execution_wait(static_cast<simgrid::kernel::activity::ExecImpl*>(execution),
-                                                    timeout);
+  simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
+  simgrid::kernel::actor::simcall_blocking([execution, issuer, timeout] { execution->wait_for(issuer, timeout); });
+  return simgrid::kernel::activity::State::DONE;
 }
 
-bool simcall_execution_test(simgrid::kernel::activity::ActivityImpl* execution) // XBT_ATTRIB_DEPRECATED_v330
+simgrid::kernel::activity::State simcall_execution_wait(const simgrid::kernel::activity::ActivityImplPtr& execution,
+                                                        double timeout) // XBT_ATTRIB_DEPRECATED_v330
 {
-  return simgrid::kernel::actor::simcall([execution] { return execution->test(); });
+  simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
+  simgrid::kernel::actor::simcall_blocking([execution, issuer, timeout] { execution->wait_for(issuer, timeout); });
+  return simgrid::kernel::activity::State::DONE;
 }
 
-unsigned int simcall_execution_waitany_for(simgrid::kernel::activity::ExecImpl* execs[], size_t count, double timeout)
+bool simcall_execution_test(simgrid::kernel::activity::ActivityImpl* execution) // XBT_ATTRIB_DEPRECATED_v330
 {
-  return simcall_BODY_execution_waitany_for(execs, count, timeout);
+  return simgrid::kernel::actor::simcall([execution] { return execution->test(); });
 }
 
-void simcall_process_join(smx_actor_t process, double timeout) // XBT_DEPRECATED_v328
+bool simcall_execution_test(const simgrid::kernel::activity::ActivityImplPtr& execution) // XBT_ATTRIB_DEPRECATED_v330
 {
-  simgrid::kernel::actor::ActorImpl::self()->join(process, timeout);
+  return simgrid::kernel::actor::simcall([execution] { return execution->test(); });
 }
 
-void simcall_process_suspend(smx_actor_t process) // XBT_DEPRECATED_v328
+unsigned int simcall_execution_waitany_for(simgrid::kernel::activity::ExecImpl* execs[], size_t count,
+                                           double timeout) // XBT_ATTRIB_DEPRECATED_v331
 {
-  process->iface()->suspend();
-}
-
-e_smx_state_t simcall_process_sleep(double duration) // XBT_DEPRECATED_v329
+  std::vector<simgrid::kernel::activity::ExecImpl*> execs_vec(execs, execs + count);
+  simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
+  simgrid::kernel::actor::ExecutionWaitanySimcall observer{issuer, execs_vec, timeout};
+  return simgrid::kernel::actor::simcall_blocking(
+      [&observer] {
+        simgrid::kernel::activity::ExecImpl::wait_any_for(observer.get_issuer(), observer.get_execs(),
+                                                          observer.get_timeout());
+      },
+      &observer);
+}
+
+simgrid::kernel::activity::State simcall_process_sleep(double duration) // XBT_ATTRIB_DEPRECATED_v329
 {
   simgrid::kernel::actor::ActorImpl::self()->sleep(duration);
   return simgrid::kernel::activity::State::DONE;
@@ -80,8 +100,8 @@ void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size,
     /* the model-checker wants two separate simcalls */
     simgrid::kernel::activity::ActivityImplPtr comm =
         nullptr; /* MC needs the comm to be set to nullptr during the simcall */
-    comm = simcall_comm_isend(sender, mbox, task_size, rate,
-        src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
+    comm = simcall_comm_isend(sender, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun,
+                              data, false);
     simcall_comm_wait(comm.get(), timeout);
     comm = nullptr;
   }
@@ -167,14 +187,14 @@ simcall_comm_iprobe(smx_mailbox_t mbox, int type, bool (*match_fun)(void*, void*
 unsigned int simcall_comm_waitany(simgrid::kernel::activity::ActivityImplPtr comms[], size_t count,
                                   double timeout) // XBT_ATTRIB_DEPRECATED_v330
 {
-  std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
-  std::transform(comms, comms + count, rcomms.get(), [](const simgrid::kernel::activity::ActivityImplPtr& comm) {
+  std::vector<simgrid::kernel::activity::CommImpl*> rcomms(count);
+  std::transform(comms, comms + count, begin(rcomms), [](const simgrid::kernel::activity::ActivityImplPtr& comm) {
     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
   });
-  return simcall_BODY_comm_waitany(rcomms.get(), count, timeout);
+  return static_cast<unsigned int>(simcall_BODY_comm_waitany(rcomms.data(), rcomms.size(), timeout));
 }
 
-unsigned int simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
+ssize_t simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
 {
   return simcall_BODY_comm_waitany(comms, count, timeout);
 }
@@ -186,14 +206,14 @@ int simcall_comm_testany(simgrid::kernel::activity::ActivityImplPtr comms[], siz
 {
   if (count == 0)
     return -1;
-  std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
-  std::transform(comms, comms + count, rcomms.get(), [](const simgrid::kernel::activity::ActivityImplPtr& comm) {
+  std::vector<simgrid::kernel::activity::CommImpl*> rcomms(count);
+  std::transform(comms, comms + count, begin(rcomms), [](const simgrid::kernel::activity::ActivityImplPtr& comm) {
     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
   });
-  return simcall_BODY_comm_testany(rcomms.get(), count);
+  return static_cast<int>(simcall_BODY_comm_testany(rcomms.data(), rcomms.size()));
 }
 
-int simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
+ssize_t simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
 {
   if (count == 0)
     return -1;
@@ -236,27 +256,27 @@ smx_mutex_t simcall_mutex_init() // XBT_ATTRIB_DEPRECATED_v330
  * @ingroup simix_synchro_management
  *
  */
-void simcall_mutex_lock(smx_mutex_t mutex)
+void simcall_mutex_lock(smx_mutex_t mutex) // XBT_ATTRIB_DEPRECATD_v331
 {
-  simcall_BODY_mutex_lock(mutex);
+  mutex->mutex().lock();
 }
 
 /**
  * @ingroup simix_synchro_management
  *
  */
-int simcall_mutex_trylock(smx_mutex_t mutex)
+int simcall_mutex_trylock(smx_mutex_t mutex) // XBT_ATTRIB_DEPRECATD_v331
 {
-  return simcall_BODY_mutex_trylock(mutex);
+  return mutex->mutex().try_lock();
 }
 
 /**
  * @ingroup simix_synchro_management
  *
  */
-void simcall_mutex_unlock(smx_mutex_t mutex)
+void simcall_mutex_unlock(smx_mutex_t mutex) // XBT_ATTRIB_DEPRECATD_v331
 {
-  simcall_BODY_mutex_unlock(mutex);
+  mutex->mutex().unlock();
 }
 
 /**
@@ -272,71 +292,99 @@ smx_cond_t simcall_cond_init() // XBT_ATTRIB_DEPRECATED_v330
  * @ingroup simix_synchro_management
  *
  */
-void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
+void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex) // XBT_ATTRIB_DEPRECATED_v331
 {
-  simcall_BODY_cond_wait(cond, mutex);
+  cond->get_iface()->wait(std::unique_lock<simgrid::s4u::Mutex>(mutex->mutex()));
 }
 
 /**
  * @ingroup simix_synchro_management
  *
  */
-int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
+int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout) // XBT_ATTRIB_DEPRECATD_v331
 {
-  xbt_assert(std::isfinite(timeout), "timeout is not finite!");
-  return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
+  return cond->get_iface()->wait_for(std::unique_lock<simgrid::s4u::Mutex>(mutex->mutex()), timeout) ==
+         std::cv_status::timeout;
 }
 
 /**
  * @ingroup simix_synchro_management
  *
  */
-void simcall_sem_acquire(smx_sem_t sem)
+void simcall_sem_acquire(smx_sem_t sem) // XBT_ATTRIB_DEPRECATD_v331
 {
-  simcall_BODY_sem_acquire(sem);
+  return sem->sem().acquire();
 }
 
 /**
  * @ingroup simix_synchro_management
  *
  */
-int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
+int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout) // XBT_ATTRIB_DEPRECATD_v331
 {
-  xbt_assert(std::isfinite(timeout), "timeout is not finite!");
-  return simcall_BODY_sem_acquire_timeout(sem, timeout);
+  return sem->sem().acquire_timeout(timeout);
 }
 
-e_smx_state_t simcall_io_wait(simgrid::kernel::activity::ActivityImpl* io, double timeout)
+simgrid::kernel::activity::State simcall_io_wait(simgrid::kernel::activity::ActivityImpl* io,
+                                                 double timeout) // XBT_ATTRIB_DEPRECATED_v330
 {
-  return (e_smx_state_t)simcall_BODY_io_wait(static_cast<simgrid::kernel::activity::IoImpl*>(io), timeout);
+  simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
+  simgrid::kernel::actor::simcall_blocking([io, issuer, timeout] { io->wait_for(issuer, timeout); });
+  return simgrid::kernel::activity::State::DONE;
+}
+
+simgrid::kernel::activity::State simcall_io_wait(const simgrid::kernel::activity::ActivityImplPtr& io,
+                                                 double timeout) // XBT_ATTRIB_DEPRECATED_v330
+{
+  simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
+  simgrid::kernel::actor::simcall_blocking([io, issuer, timeout] { io->wait_for(issuer, timeout); });
+  return simgrid::kernel::activity::State::DONE;
 }
 
-bool simcall_io_test(simgrid::kernel::activity::ActivityImpl* io)
+bool simcall_io_test(simgrid::kernel::activity::ActivityImpl* io) // XBT_ATTRIB_DEPRECATED_v330
 {
-  return simcall_BODY_io_test(static_cast<simgrid::kernel::activity::IoImpl*>(io));
+  return simgrid::kernel::actor::simcall([io] { return io->test(); });
 }
 
-void simcall_run_kernel(std::function<void()> const& code, simgrid::mc::SimcallInspector* t)
+bool simcall_io_test(const simgrid::kernel::activity::ActivityImplPtr& io) // XBT_ATTRIB_DEPRECATD_v330
 {
-  simgrid::kernel::actor::ActorImpl::self()->simcall.inspector_ = t;
+  return simgrid::kernel::actor::simcall([io] { return io->test(); });
+}
+
+void simcall_run_kernel(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
+{
+  simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = observer;
   simcall_BODY_run_kernel(&code);
+  simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = nullptr;
 }
 
-void simcall_run_blocking(std::function<void()> const& code, simgrid::mc::SimcallInspector* t = nullptr)
+void simcall_run_blocking(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
 {
-  simgrid::kernel::actor::ActorImpl::self()->simcall.inspector_ = t;
+  simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = observer;
   simcall_BODY_run_blocking(&code);
+  simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = nullptr;
 }
 
-int simcall_mc_random(int min, int max) {
-  return simcall_BODY_mc_random(min, max);
+int simcall_mc_random(int min, int max) // XBT_ATTRIB_DEPRECATD_v331
+{
+  return MC_random(min, max);
 }
 
 /* ************************************************************************** */
 
 /** @brief returns a printable string representing a simcall */
-const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
-  return simcall_names[kind];
+const char* SIMIX_simcall_name(const s_smx_simcall& simcall)
+{
+  if (simcall.observer_ != nullptr) {
+    static std::string name;
+    name              = boost::core::demangle(typeid(*simcall.observer_).name());
+    const char* cname = name.c_str();
+    if (name.rfind("simgrid::kernel::", 0) == 0)
+      cname += 17; // strip prefix "simgrid::kernel::"
+    return cname;
+  } else {
+    return simcall_names[static_cast<int>(simcall.call_)];
+  }
 }
 
 namespace simgrid {