Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Tue, 28 Jun 2022 20:13:36 +0000 (22:13 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Tue, 28 Jun 2022 20:13:49 +0000 (22:13 +0200)
1  2 
examples/sthread/pthread-mutex-simple.c
examples/sthread/pthread-mutex-simple.tesh
src/sthread/sthread_impl.cpp

@@@ -5,34 -5,29 +5,28 @@@
  
  pthread_mutex_t mutex;
  
- static void* thread1_fun(void* ignore)
 -static void* thread_fun(void* ignore)
++static void* thread_fun(void* val)
  {
    pthread_mutex_lock(&mutex);
    pthread_mutex_unlock(&mutex);
  
-   fprintf(stderr, "The first thread is terminating.\n");
-   return NULL;
- }
- static void* thread2_fun(void* ignore)
- {
-   pthread_mutex_lock(&mutex);
-   pthread_mutex_unlock(&mutex);
-   fprintf(stderr, "The second thread is terminating.\n");
++  fprintf(stderr, "The thread %d is terminating.\n", *(int*)val);
    return NULL;
  }
  
  int main(int argc, char* argv[])
  {
 -  fprintf(stderr, "User main is starting\n");
 -
    pthread_mutex_init(&mutex, NULL);
  
-   pthread_t thread1, thread2;
-   pthread_create(&thread1, NULL, thread1_fun, NULL);
-   pthread_create(&thread2, NULL, thread2_fun, NULL);
++  int id[2] = {0, 1};
+   pthread_t thread1;
+   pthread_t thread2;
 -  pthread_create(&thread1, NULL, thread_fun, NULL);
 -  fprintf(stderr, "here\n");
 -  pthread_create(&thread2, NULL, thread_fun, NULL);
 -  fprintf(stderr, "there\n");
++  pthread_create(&thread1, NULL, thread_fun, (void*)&id[0]);
++  pthread_create(&thread2, NULL, thread_fun, (void*)&id[1]);
 +  fprintf(stderr, "All threads are started.\n");
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
  
 -  fprintf(stderr, "User main is done\n");
 +  fprintf(stderr, "User's main is terminating.\n");
    return 0;
  }
index 00827cb,0000000..b3bba4d
mode 100644,000000..100644
--- /dev/null
@@@ -1,7 -1,0 +1,7 @@@
- > The first thread is terminating.
 +$ ./pthread-mutex-simple
 +> [0.000000] [sthread/INFO] Starting the simulation.
- > The second thread is terminating.
++> The thread 0 is terminating.
 +> All threads are started.
++> The thread 1 is terminating.
 +> User's main is terminating.
 +> [0.000000] [sthread/INFO] All threads exited. Terminating the simulation.
  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**))
  {
    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");
@@@ -41,9 -41,8 +41,9 @@@
    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();
 +  XBT_INFO("All threads exited. Terminating the simulation.");
  
    return 0;
  }
@@@ -63,27 -62,26 +63,26 @@@ static void thread_create_wrapper(void
    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 = 0;
    TID++;
 -  XBT_INFO("Create thread %d", 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();
@@@ -92,7 -90,7 +91,7 @@@
    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());