From 53e3b26debedd9ec4e16c8f72d9e476f7e6f82c7 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sat, 3 Sep 2016 15:19:12 +0200 Subject: [PATCH] use C++11 threads for portability --- examples/msg/CMakeLists.txt | 12 ++++++- .../{maestro-set.c => maestro-set.cpp} | 36 +++++++++---------- 2 files changed, 29 insertions(+), 19 deletions(-) rename examples/msg/maestro-set/{maestro-set.c => maestro-set.cpp} (75%) diff --git a/examples/msg/CMakeLists.txt b/examples/msg/CMakeLists.txt index ee73989b74..d1b2acc0ba 100644 --- a/examples/msg/CMakeLists.txt +++ b/examples/msg/CMakeLists.txt @@ -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 - 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) @@ -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() +# 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) diff --git a/examples/msg/maestro-set/maestro-set.c b/examples/msg/maestro-set/maestro-set.cpp similarity index 75% rename from examples/msg/maestro-set/maestro-set.c rename to examples/msg/maestro-set/maestro-set.cpp index f59ff452f0..c1ab4d27f2 100644 --- a/examples/msg/maestro-set/maestro-set.c +++ b/examples/msg/maestro-set/maestro-set.cpp @@ -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. */ +/** @addtogroup MSG_examples + * + * - maestro-set/maestro-set.cpp: Switch the system thread hosting our maestro. + * 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"); -#define _GNU_SOURCE /* See feature_test_macros(7) */ -#include -#include -#include /* pid_t */ +#include -pid_t root_pid; +std::thread::id root_id; 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() { - 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"); } -/** @addtogroup MSG_examples - * - * - maestro-set/maestro-set.c: Switch the system thread hosting our maestro. - * 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[]) @@ -66,7 +66,7 @@ static void maestro(void* data) /** 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); -- 2.20.1