Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::atomic instead of __sync_fetch_and_add for portability
authorMartin Quinson <martin.quinson@loria.fr>
Sun, 13 Sep 2015 22:50:06 +0000 (00:50 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Sun, 13 Sep 2015 22:50:06 +0000 (00:50 +0200)
include/xbt/base.h
src/xbt/parmap.cpp [moved from src/xbt/parmap.c with 99% similarity]
tools/cmake/DefinePackages.cmake

index 54ab270..ae3f926 100644 (file)
 /* Microsoft wants to improve the code quality blah blah blah */
 /* See: https://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx */
        /* warning C4996: '_strdup': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _strdup. */
-       # define _CRT_NONSTDC_NO_WARNINGS
+       #define _CRT_NONSTDC_NO_WARNINGS
        /* warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. */
-       # define _CRT_SECURE_NO_WARNINGS
+       #define _CRT_SECURE_NO_WARNINGS
 #endif
 
 
similarity index 99%
rename from src/xbt/parmap.c
rename to src/xbt/parmap.cpp
index a19ed04..b2c952c 100644 (file)
@@ -4,6 +4,8 @@
 /* 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 <atomic>
+
 #include "internal_config.h"
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -73,7 +75,7 @@ typedef struct s_xbt_parmap {
   xbt_os_thread_t *workers;        /**< worker thread handlers */
   void_f_pvoid_t fun;              /**< function to run in parallel on each element of data */
   xbt_dynar_t data;                /**< parameters to pass to fun in parallel */
-  unsigned int index;              /**< index of the next element of data to pick */
+  std::atomic<unsigned int> index; /**< index of the next element of data to pick */
 
 #ifdef HAVE_MC
   int finish;
@@ -304,7 +306,7 @@ void xbt_parmap_apply(xbt_parmap_t parmap, void_f_pvoid_t fun, xbt_dynar_t data)
  */
 void* xbt_parmap_next(xbt_parmap_t parmap)
 {
-  unsigned int index = __sync_fetch_and_add(&parmap->index, 1);
+  unsigned int index = parmap->index++;
   if (index < xbt_dynar_length(parmap->data)) {
     return xbt_dynar_get_as(parmap->data, index, void*);
   }
@@ -314,7 +316,7 @@ void* xbt_parmap_next(xbt_parmap_t parmap)
 static void xbt_parmap_work(xbt_parmap_t parmap)
 {
   unsigned index;
-  while ((index = __sync_fetch_and_add(&parmap->index, 1))
+  while ((index = parmap->index++)
          < xbt_dynar_length(parmap->data))
     parmap->fun(xbt_dynar_get_as(parmap->data, index, void*));
 }
index 0ffc042..6084499 100644 (file)
@@ -265,7 +265,7 @@ set(XBT_SRC
   src/xbt/lib.c
   src/xbt/log.c
   src/xbt/mallocator.c
-  src/xbt/parmap.c
+  src/xbt/parmap.cpp
   src/xbt/set.c
   src/xbt/setset.c
   src/xbt/snprintf.c