XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
namespace sg4 = simgrid::s4u;
-static sg4::Host* lilibeth = NULL;
+static sg4::Host* lilibeth = nullptr;
int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
{
+ /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
+ for (int i = 0; envp[i] != nullptr; i++)
+ if (strncmp(envp[i], "SMPI_GLOBAL_SIZE", strlen("SMPI_GLOBAL_SIZE")) == 0)
+ return raw_main(argc, argv, envp);
+
+ /* If not in SMPI, the old main becomes an actor in a newly created simulation */
std::ostringstream id;
id << std::this_thread::get_id();
- XBT_INFO("sthread main() is starting in thread %s", id.str().c_str());
+ XBT_DEBUG("sthread main() is starting in thread %s", id.str().c_str());
sg4::Engine e(&argc, argv);
auto* zone = sg4::create_full_zone("world");
zone->seal();
/* Launch the user's main() on an actor */
- sthread_inside_simgrid = 0;
+ sthread_enable();
sg4::ActorPtr main_actor = sg4::Actor::create("tid 0", lilibeth, raw_main, argc, argv, envp);
- XBT_INFO("sthread main() is launching the simulation");
+ XBT_INFO("Starting the simulation.");
sg4::Engine::get_instance()->run();
+ sthread_disable();
+ XBT_INFO("All threads exited. Terminating the simulation.");
return 0;
}
+
struct sthread_mutex {
s4u_Mutex* mutex;
};
if (SMPI_is_inited())
SMPI_thread_create();
#endif
- sthread_inside_simgrid = 0;
+ sthread_enable();
user_function(param);
- sthread_inside_simgrid = 1;
+ sthread_disable();
}
-int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*),
+int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
void* arg)
{
- static int TID = 1;
-
+ static int TID = 0;
TID++;
+ XBT_VERB("Create thread %d", TID);
int rank = 0;
#if HAVE_SMPI
if (SMPI_is_inited())
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#endif
- char name[100];
- sprintf(name, "%d:%d", rank, TID);
- sg4::ActorPtr actor = sg4::Actor::init(name, lilibeth);
+ std::string name = simgrid::xbt::string_printf("%d:%d", rank, TID);
+ sg4::ActorPtr actor = sg4::Actor::init(name.c_str(), lilibeth);
actor->start(thread_create_wrapper, start_routine, arg);
intrusive_ptr_add_ref(actor.get());
*thread = reinterpret_cast<unsigned long>(actor.get());
return 0;
}
-int sthread_join(sthread_t thread, void** retval)
+int sthread_join(sthread_t thread, void** /*retval*/)
{
sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
actor->join();
return 0;
}
-int sthread_mutex_init(sthread_mutex_t* mutex, const /*pthread_mutexattr_t*/ void* attr)
+int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/)
{
auto m = sg4::Mutex::create();
intrusive_ptr_add_ref(m.get());