/* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
#include "smpi/smpi.h"
+#include "xbt/string.hpp"
#include <simgrid/actor.h>
#include <simgrid/s4u/Actor.hpp>
#include <simgrid/s4u/Engine.hpp>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
+#include <string_view>
#include <thread>
XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
{
/* 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)
+ if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0)
return raw_main(argc, argv, envp);
/* If not in SMPI, the old main becomes an actor in a newly created simulation */
s4u_Mutex* mutex;
};
-static void thread_create_wrapper(void* (*user_function)(void*), void* param)
-{
-#if HAVE_SMPI
- if (SMPI_is_inited())
- SMPI_thread_create();
-#endif
- sthread_enable();
- user_function(param);
- sthread_disable();
-}
-
int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
void* arg)
{
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#endif
std::string name = simgrid::xbt::string_printf("%d:%d", rank, TID);
- sg4::ActorPtr actor = sg4::Actor::create(name, lilibeth, thread_create_wrapper, start_routine, arg);
+ sg4::ActorPtr actor = sg4::Actor::create(
+ name, lilibeth,
+ [](auto* user_function, auto* param) {
+#if HAVE_SMPI
+ if (SMPI_is_inited())
+ SMPI_thread_create();
+#endif
+ sthread_enable();
+ user_function(param);
+ sthread_disable();
+ },
+ start_routine, arg);
intrusive_ptr_add_ref(actor.get());
*thread = reinterpret_cast<unsigned long>(actor.get());
int sthread_mutex_lock(sthread_mutex_t* mutex)
{
+ /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
+ if (mutex->mutex == nullptr)
+ sthread_mutex_init(mutex, nullptr);
+
static_cast<sg4::Mutex*>(mutex->mutex)->lock();
return 0;
}
int sthread_mutex_trylock(sthread_mutex_t* mutex)
{
+ /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
+ if (mutex->mutex == nullptr)
+ sthread_mutex_init(mutex, nullptr);
+
return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
}
int sthread_mutex_unlock(sthread_mutex_t* mutex)
{
+ /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
+ if (mutex->mutex == nullptr)
+ sthread_mutex_init(mutex, nullptr);
+
static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
return 0;
}
int sthread_mutex_destroy(sthread_mutex_t* mutex)
{
+ /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
+ if (mutex->mutex == nullptr)
+ sthread_mutex_init(mutex, nullptr);
+
intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
return 0;
}
double secs = trunc(now);
double usecs = (now - secs) * 1e6;
tv->tv_sec = static_cast<time_t>(secs);
- tv->tv_usec = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t (or useconds_t on WIN32)
+ tv->tv_usec = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t
}
return 0;
}