Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Disable parallel ucontexts on 64bit SunOS.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 21 May 2019 20:36:09 +0000 (22:36 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 21 May 2019 20:44:14 +0000 (22:44 +0200)
Ucontexts and TLS don't play well together on this platform.

For the record, here is a sample test code. The output should be the same before and after
swapcontext (recall that option "-m64" may be mandatory to compile in 64bit mode).

--------------------
#include <iostream>
#include <stdexcept>
#include <system_error>
#include <thread>
#include <ucontext.h>

thread_local int x = 1;

static char stack[66536];
static ucontext_t ctx;
static ucontext_t octx;

static void wrap()
{
    std::cerr << "o. After swapcontext...: x = " << x << " (" << std::this_thread::get_id() << ")\n";
}

static void thread_fun()
{
    x = 2;
    std::this_thread::sleep_for(std::chrono::milliseconds(100));

    ctx.uc_stack.ss_sp = stack;
    ctx.uc_stack.ss_size = sizeof(stack);
    ctx.uc_link = &octx;
    makecontext(&ctx, &wrap, 2, (int)0xdeadbeef);
    std::cerr << "o. Before swapcontext..: x = " << x << " (" << std::this_thread::get_id() << ")\n";
    swapcontext(&octx, &ctx);
    std::cerr << "o. Finish of thread....: x = " << x << " (" << std::this_thread::get_id() << ")\n";
}

int main()
{
    std::cerr << "x. Main (before thread): x = " << x << " (" << std::this_thread::get_id() << ")\n";
    std::thread thr(thread_fun);
    getcontext(&ctx);
    thr.join();
    std::cerr << "x. Main (after join)...: x = " << x << " (" << std::this_thread::get_id() << ")\n";
}

examples/deprecated/msg/CMakeLists.txt

index 0e62567..ac04e1a 100644 (file)
@@ -71,7 +71,14 @@ foreach (x trace-categories trace-route-user-variables trace-link-user-variables
                    ${CMAKE_HOME_DIRECTORY}/examples/deprecated/msg/${x}/${x}.tesh)
 endforeach()
 
-ADD_TESH_FACTORIES(msg-dht-kademlia-parallel "thread;ucontext;raw;boost" --cfg contexts/nthreads:4 ${CONTEXTS_SYNCHRO} 
+if(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "i386" AND CMAKE_SIZEOF_VOID_P EQUAL 8)
+  # Thread-local storage (TLS) is needed for parallel execution, but it doesn't
+  # play well with Ucontexts on 64bit SunOS (at least on x86_64).
+  set(parallel-factories "thread;raw;boost")
+else()
+  set(parallel-factories "thread;ucontext;raw;boost")
+endif()
+ADD_TESH_FACTORIES(msg-dht-kademlia-parallel "${parallel-factories}" --cfg contexts/nthreads:4 ${CONTEXTS_SYNCHRO}
                                              --setenv bindir=${CMAKE_BINARY_DIR}/examples/deprecated/msg/dht-kademlia
                                              --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/deprecated/msg/dht-kademlia
                                              --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms