Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use C++11 threads for portability
authorMartin Quinson <martin.quinson@loria.fr>
Sat, 3 Sep 2016 13:19:12 +0000 (15:19 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Sat, 3 Sep 2016 13:19:12 +0000 (15:19 +0200)
examples/msg/CMakeLists.txt
examples/msg/maestro-set/maestro-set.cpp [moved from examples/msg/maestro-set/maestro-set.c with 75% similarity]

index ee73989..d1b2acc 100644 (file)
@@ -1,8 +1,9 @@
+# C examples
 foreach(x actions-comm actions-storage app-masterworker app-pingpong app-pmm app-token-ring async-wait async-waitall 
           async-waitany cloud-capping cloud-masterworker cloud-migration cloud-simple cloud-two-tasks
           dht-chord dht-pastry energy-consumption energy-onoff energy-pstate energy-ptask energy-vm platform-failures 
           io-file io-remote io-storage task-priority process-create process-kill process-migration process-suspend 
 foreach(x actions-comm actions-storage app-masterworker app-pingpong app-pmm app-token-ring async-wait async-waitall 
           async-waitany cloud-capping cloud-masterworker cloud-migration cloud-simple cloud-two-tasks
           dht-chord dht-pastry energy-consumption energy-onoff energy-pstate energy-ptask energy-vm platform-failures 
           io-file io-remote io-storage task-priority process-create process-kill process-migration process-suspend 
-          platform-properties maestro-set process-startkilltime synchro-semaphore trace-categories 
+          platform-properties process-startkilltime synchro-semaphore trace-categories 
           trace-route-user-variables trace-link-user-variables trace-masterworker trace-platform 
           trace-process-migration trace-host-user-variables)
   add_executable       (${x}     ${x}/${x}.c)
           trace-route-user-variables trace-link-user-variables trace-masterworker trace-platform 
           trace-process-migration trace-host-user-variables)
   add_executable       (${x}     ${x}/${x}.c)
@@ -12,6 +13,15 @@ foreach(x actions-comm actions-storage app-masterworker app-pingpong app-pmm app
   set(tesh_files   ${tesh_files}   ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh)
 endforeach()
 
   set(tesh_files   ${tesh_files}   ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh)
 endforeach()
 
+# CPP examples
+foreach(x maestro-set)
+  add_executable       (${x}     ${x}/${x}.cpp)
+  target_link_libraries(${x}     simgrid)
+  set_target_properties(${x}  PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x})
+  set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.c)
+  set(tesh_files   ${tesh_files}   ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh)
+endforeach()
+
 if(HAVE_NS3)
   add_executable       (network-ns3 network-ns3/network-ns3.c)
   target_link_libraries(network-ns3 simgrid)
 if(HAVE_NS3)
   add_executable       (network-ns3 network-ns3/network-ns3.c)
   target_link_libraries(network-ns3 simgrid)
similarity index 75%
rename from examples/msg/maestro-set/maestro-set.c
rename to examples/msg/maestro-set/maestro-set.cpp
index f59ff45..c1ab4d2 100644 (file)
@@ -1,37 +1,37 @@
-/* Copyright (c) 2007-2015. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved.          */
 
 /* 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. */
 
 
 /* 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. */
 
+/** @addtogroup MSG_examples
+ *
+ *  - <b>maestro-set/maestro-set.cpp: Switch the system thread hosting our maestro</b>. 
+ *    That's a very advanced example in which we move the maestro thread to another process.
+ *    Not many users need it (maybe only one, actually), but this example is also a regression test.
+ * 
+ *    This example is in C++ because we use C++11 threads to ensure that the feature is working as
+ *    expected. You can still use that feature from a C code.
+ */
+
 #include "simgrid/msg.h"
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
 
 #include "simgrid/msg.h"
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
 
-#define _GNU_SOURCE         /* See feature_test_macros(7) */
-#include <unistd.h>
-#include <sys/syscall.h>
-#include <sys/types.h> /* pid_t */
+#include <thread>
 
 
-pid_t root_pid;
+std::thread::id root_id;
 
 static void ensure_root_tid() {
 
 static void ensure_root_tid() {
-  pid_t my_pid = syscall(SYS_gettid);
-  xbt_assert(my_pid == root_pid, "I was supposed to be the main thread but %d != %d", my_pid, root_pid);
+  std::thread::id this_id = std::this_thread::get_id();
+  xbt_assert(root_id == this_id, "I was supposed to be the main thread");
   XBT_INFO("I am the main thread, as expected");
 }
 static void ensure_other_tid() {
   XBT_INFO("I am the main thread, as expected");
 }
 static void ensure_other_tid() {
-  pid_t my_pid = syscall(SYS_gettid);
-  xbt_assert(my_pid != root_pid, "I was NOT supposed to be the main thread");
+  std::thread::id this_id = std::this_thread::get_id();
+  xbt_assert(this_id != root_id, "I was NOT supposed to be the main thread");
   XBT_INFO("I am not the main thread, as expected");
 }
 
   XBT_INFO("I am not the main thread, as expected");
 }
 
-/** @addtogroup MSG_examples
- *
- *  - <b>maestro-set/maestro-set.c: Switch the system thread hosting our maestro</b>. 
- *    That's a very advanced example in which we move the maestro thread to another process.
- *    Not many users need it (maybe only one, actually), but this example is also a regression test.
- */
 
 
 static int sender(int argc, char *argv[])
 
 
 static int sender(int argc, char *argv[])
@@ -66,7 +66,7 @@ static void maestro(void* data)
 /** Main function */
 int main(int argc, char *argv[])
 {
 /** Main function */
 int main(int argc, char *argv[])
 {
-  root_pid = syscall(SYS_gettid);
+  root_id = std::this_thread::get_id();
 
   SIMIX_set_maestro(maestro, NULL);
   MSG_init(&argc, argv);
 
   SIMIX_set_maestro(maestro, NULL);
   MSG_init(&argc, argv);