Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make xbt_os_thread_self() useless, and kill it
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 5 Jan 2019 01:28:55 +0000 (02:28 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 5 Jan 2019 01:59:18 +0000 (02:59 +0100)
Also remove every parts of xbt_os_thread_t that I managed to make useless

ChangeLog
include/xbt/xbt_os_thread.h
src/include/xbt/parmap.hpp
src/kernel/context/ContextThread.cpp
src/xbt/xbt_os_thread.c
teshsuite/xbt/parallel_log_crashtest/parallel_log_crashtest.c
teshsuite/xbt/parmap_test/parmap_test.cpp

index ad00e70..99d68e9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -22,6 +22,7 @@ XBT:
  - Drop several unused xbt_os_thread_t functions:
    - xbt_os_thread_get_extra_data()
    - xbt_os_thread_set_extra_data()
+   - xbt_os_thread_self()
    - xbt_os_thread_self_name()
 
 Fixed bugs:
index 5c90354..7d1e18f 100644 (file)
@@ -26,10 +26,9 @@ SG_BEGIN_DECL()
 
 /** @brief Thread data type (opaque structure) */
 typedef struct xbt_os_thread_ *xbt_os_thread_t;
-XBT_PUBLIC xbt_os_thread_t xbt_os_thread_create(const char* name, pvoid_f_pvoid_t start_routine, void* param);
+XBT_PUBLIC xbt_os_thread_t xbt_os_thread_create(pvoid_f_pvoid_t start_routine, void* param);
 XBT_PUBLIC void xbt_os_thread_exit(int* retcode);
 
-XBT_PUBLIC xbt_os_thread_t xbt_os_thread_self(void);
 /* xbt_os_thread_join frees the joined thread (ie the XBT wrapper around it, the OS frees the rest) */
 XBT_PUBLIC void xbt_os_thread_join(xbt_os_thread_t thread, void** thread_return);
 XBT_PUBLIC void xbt_os_thread_setstacksize(int stack_size);
index 5f97049..c5c79a6 100644 (file)
@@ -168,7 +168,7 @@ template <typename T> Parmap<T>::Parmap(unsigned num_workers, e_xbt_parmap_mode_
   unsigned int core_bind = 0;
   for (unsigned i = 1; i < num_workers; i++) {
     ThreadData* data = new ThreadData(*this, i);
-    this->workers[i] = xbt_os_thread_create(nullptr, worker_main, data);
+    this->workers[i] = xbt_os_thread_create(worker_main, data);
     xbt_os_thread_bind(this->workers[i], core_bind);
     if (core_bind != std::thread::hardware_concurrency() - 1)
       core_bind++;
index 3f93201..01c084e 100644 (file)
@@ -69,9 +69,7 @@ ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t c
       xbt_os_thread_setguardsize(smx_context_guard_size);
 
     /* create and start the process */
-    /* NOTE: The first argument to xbt_os_thread_create used to be the process *
-    * name, but now the name is stored at SIMIX level, so we pass a null  */
-    this->thread_ = xbt_os_thread_create(nullptr, ThreadContext::wrapper, this);
+    this->thread_ = xbt_os_thread_create(ThreadContext::wrapper, this);
     /* wait the starting of the newly created process */
     this->end_.acquire();
   }
index a84ba93..a469b4d 100644 (file)
@@ -45,18 +45,15 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os, xbt, "Synchronization mechanism (OS
 
 typedef struct xbt_os_thread_ {
   pthread_t t;
-  char *name;
   void *param;
   pvoid_f_pvoid_t start_routine;
 } s_xbt_os_thread_t;
 static xbt_os_thread_t main_thread = NULL;
 
 /* thread-specific data containing the xbt_os_thread_t structure */
-static pthread_key_t xbt_self_thread_key;
 static int thread_mod_inited = 0;
 
 /* defaults attribute for pthreads */
-//FIXME: find where to put this
 static pthread_attr_t thread_attr;
 
 /* frees the xbt_os_thread_t corresponding to the current thread */
@@ -64,7 +61,6 @@ static void xbt_os_thread_free_thread_data(xbt_os_thread_t thread)
 {
   if (thread == main_thread)    /* just killed main thread */
     main_thread = NULL;
-  free(thread->name);
   free(thread);
 }
 
@@ -73,19 +69,10 @@ void xbt_os_thread_mod_preinit(void)
   if (thread_mod_inited)
     return;
 
-  int errcode = pthread_key_create(&xbt_self_thread_key, NULL);
-  xbt_assert(errcode == 0, "pthread_key_create failed for xbt_self_thread_key");
-
   main_thread = xbt_new(s_xbt_os_thread_t, 1);
-  main_thread->name = NULL;
-  main_thread->name = xbt_strdup("main");
   main_thread->param = NULL;
   main_thread->start_routine = NULL;
 
-  if ((errcode = pthread_setspecific(xbt_self_thread_key, main_thread)))
-    THROWF(system_error, errcode,
-           "Impossible to set the SimGrid identity descriptor to the main thread (pthread_setspecific failed)");
-
   pthread_attr_init(&thread_attr);
 
   thread_mod_inited = 1;
@@ -93,13 +80,6 @@ void xbt_os_thread_mod_preinit(void)
 
 void xbt_os_thread_mod_postexit(void)
 {
-  /* FIXME: don't try to free our key on shutdown.
-     Valgrind detects no leak if we don't, and whine if we try to */
-  //   int errcode;
-
-  //   if ((errcode=pthread_key_delete(xbt_self_thread_key)))
-  //     THROWF(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
-  free(main_thread->name);
   free(main_thread);
   main_thread = NULL;
   thread_mod_inited = 0;
@@ -122,16 +102,12 @@ static void *wrapper_start_routine(void *s)
 {
   xbt_os_thread_t t = s;
 
-  int errcode = pthread_setspecific(xbt_self_thread_key, t);
-  xbt_assert(errcode == 0, "pthread_setspecific failed for xbt_self_thread_key");
-
   return t->start_routine(t->param);
 }
 
-xbt_os_thread_t xbt_os_thread_create(const char* name, pvoid_f_pvoid_t start_routine, void* param)
+xbt_os_thread_t xbt_os_thread_create(pvoid_f_pvoid_t start_routine, void* param)
 {
   xbt_os_thread_t res_thread = xbt_new(s_xbt_os_thread_t, 1);
-  res_thread->name = xbt_strdup(name);
   res_thread->start_routine = start_routine;
   res_thread->param = param;
 
@@ -215,14 +191,6 @@ void xbt_os_thread_exit(int *retval)
   pthread_exit(retval);
 }
 
-xbt_os_thread_t xbt_os_thread_self(void )
-{
-  if (!thread_mod_inited)
-    return NULL;
-
-  return pthread_getspecific(xbt_self_thread_key);
-}
-
 /****** mutex related functions ******/
 typedef struct xbt_os_mutex_ {
   pthread_mutex_t m;
index 5d32c4e..b2962c1 100644 (file)
@@ -39,14 +39,10 @@ static int crasher()
   id = xbt_new0(int, crasher_amount);
   xbt_os_thread_t* crashers = xbt_new(xbt_os_thread_t, crasher_amount);
 
-  for (int i = 0; i < crasher_amount; i++)
-    id[i] = i;
-
   /* spawn threads */
   for (int i = 0; i < crasher_amount; i++) {
-    char name[16];
-    snprintf(name, sizeof name, "thread %d", i);
-    crashers[i] = xbt_os_thread_create(name, &crasher_thread, &id[i]);
+    id[i]       = i;
+    crashers[i] = xbt_os_thread_create(&crasher_thread, &id[i]);
   }
 
   /* wait for them */
index 0a11d62..687b079 100644 (file)
@@ -13,6 +13,8 @@
 #include <algorithm>
 #include <cstdlib>
 #include <numeric> // std::iota
+#include <sstream>
+#include <thread>
 #include <vector>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
@@ -50,9 +52,11 @@ static int test_parmap_basic(e_xbt_parmap_mode_t mode)
   return ret;
 }
 
-static void fun_get_id(uintptr_t* arg)
+static void fun_get_id(std::string* arg)
 {
-  *arg = (uintptr_t)xbt_os_thread_self();
+  std::stringstream ss;
+  ss << std::this_thread::get_id();
+  *arg = ss.str();
   xbt_os_sleep(0.05);
 }
 
@@ -63,9 +67,9 @@ static int test_parmap_extended(e_xbt_parmap_mode_t mode)
   for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
     const unsigned len = 2 * num_workers;
 
-    simgrid::xbt::Parmap<uintptr_t*> parmap(num_workers, mode);
-    std::vector<uintptr_t> a(len);
-    std::vector<uintptr_t*> data(len);
+    simgrid::xbt::Parmap<std::string*> parmap(num_workers, mode);
+    std::vector<std::string> a(len);
+    std::vector<std::string*> data(len);
     std::iota(begin(data), end(data), &a[0]);
 
     parmap.apply(fun_get_id, data);