Skip to contentSkip to conversation

pthread_rt and C++ on Xenomai 2.6

Xenomai Giulio Moro 2 participants 3 messages Feb 19, 2017 – Mar 13, 2017 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

What's the deal with using the posix skin to wrap C++ mutex, locks and threads?
I am on ARMv7 (Beaglebone Black), with gcc 4.9, xeno-config on this system returns the following:$ /usr/xenomai/bin/xeno-config --skin=posix --ldflags --cflags-Wl,@/usr/xenomai/lib/posix.wrappers -L/usr/xenomai/lib -lpthread_rt -lxenomai -lpthread -lrt-I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -I/usr/xenomai/include/posix The example code here http://www.cplusplus.com/reference/mutex/mutex/try_lock/
compiles and runs fine (i.e.: I get a reasonable number of increases of the counter) with (1) g++ -pthread test.cpp -std=c++14 -lpthread

but if I wrap the posix calls with(2) gcc test.cpp -std=c++14  -I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -I/usr/xenomai/include/posix -L/usr/xenomai/lib -lstdc++ -lpthread_rt -lxenomai -lpthread -lrt -Wl,@/usr/xenomai/lib/posix.wrappers -pthread
then the mutex can never be acquired by any thread. Actually, I cannot lock it even if I place mtx.try_lock() on the first line of main().Also, if I try to sleep at sleep just before the end of attempt_10k_increases() with "std::this_thread::sleep_for", it does not sleep. Even if I increase the number of attempts, in order for the threads to stay alive for longer, I cannot see the threads show up in /proc/xenomai/stat . What I observe is that when using -Wl,@/usr/xenomai/lib/posix.wrapper:- mutex::try_lock fails- std::this_thread::sleep_for() does not sleep- threads do not show up in /proc/xenomai/stat
So I have two questions:a) is there any way to get -Wl,@/usr/xenomai/lib/posix.wrappers to correctly wrap underlying calls to pthread_* functions which are hidden in libstdc++ ?b) is there any way to safely link into the same executable some code that calls pthread_* which you want to wrap in the Xenomai posix skin and C++ code which uses std::thread? Thanks,Giulio

#2 lore raw

I think I now have the answer to this question which was - admittedly - not formulated very clearly.
The answer I came up with is: it depends on how your c++ standard library is implemented. The one we use (libstdc++.so which comes with gcc 4.9) does not make all the calls to the pthread API that I would expect. For instance, it does not call "pthread_mutex_init" but it calls "gthread_mutex_init". Therefore, wrapping does not work properly for the mutex API. Perhaps other implementations of the C++ standard library would be more wrapping-friendly in this sense.Has there been any effort in writing a Xenomai-wrappable (or Xenomai-friendly) C++ standard library?
Thanks,Giulio

From: Giulio Moro <[email protected]>
To: Xenomai <[email protected]>
Sent: Sunday, 19 February 2017, 1:38
Subject: pthread_rt and C++ on Xenomai 2.6

What's the deal with using the posix skin to wrap C++ mutex, locks and threads?
I am on ARMv7 (Beaglebone Black), with gcc 4.9, xeno-config on this system returns the following:$ /usr/xenomai/bin/xeno-config --skin=posix --ldflags --cflags-Wl,@/usr/xenomai/lib/posix.wrappers -L/usr/xenomai/lib -lpthread_rt -lxenomai -lpthread -lrt-I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -I/usr/xenomai/include/posix The example code here http://www.cplusplus.com/reference/mutex/mutex/try_lock/
compiles and runs fine (i.e.: I get a reasonable number of increases of the counter) with (1) g++ -pthread test.cpp -std=c++14 -lpthread

but if I wrap the posix calls with(2) gcc test.cpp -std=c++14  -I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -I/usr/xenomai/include/posix -L/usr/xenomai/lib -lstdc++ -lpthread_rt -lxenomai -lpthread -lrt -Wl,@/usr/xenomai/lib/posix.wrappers -pthread
then the mutex can never be acquired by any thread. Actually, I cannot lock it even if I place mtx.try_lock() on the first line of main().Also, if I try to sleep at sleep just before the end of attempt_10k_increases() with "std::this_thread::sleep_for", it does not sleep. Even if I increase the number of attempts, in order for the threads to stay alive for longer, I cannot see the threads show up in /proc/xenomai/stat . What I observe is that when using -Wl,@/usr/xenomai/lib/posix.wrapper:- mutex::try_lock fails- std::this_thread::sleep_for() does not sleep- threads do not show up in /proc/xenomai/stat
So I have two questions:a) is there any way to get -Wl,@/usr/xenomai/lib/posix.wrappers to correctly wrap underlying calls to pthread_* functions which are hidden in libstdc++ ?b) is there any way to safely link into the same executable some code that calls pthread_* which you want to wrap in the Xenomai posix skin and C++ code which uses std::thread? Thanks,Giulio

#3 lore raw
Quoted 16 lines · Giulio Moro

I think I now have the answer to this question which was - admittedly - not formulated very clearly.
The answer I came up with is: it depends on how your c++ standard library is implemented. The one we use (libstdc++.so which comes with gcc 4.9) does not make all the calls to the pthread API that I would expect. For instance, it does not call "pthread_mutex_init" but it calls "gthread_mutex_init". Therefore, wrapping does not work properly for the mutex API. Perhaps other implementations of the C++ standard library would be more wrapping-friendly in this sense.Has there been any effort in writing a Xenomai-wrappable (or Xenomai-friendly) C++ standard library?
Thanks,Giulio

From: Giulio Moro <[email protected]>
To: Xenomai <[email protected]>
Sent: Sunday, 19 February 2017, 1:38
Subject: pthread_rt and C++ on Xenomai 2.6

What's the deal with using the posix skin to wrap C++ mutex, locks and threads?
I am on ARMv7 (Beaglebone Black), with gcc 4.9, xeno-config on this system returns the following:$ /usr/xenomai/bin/xeno-config --skin=posix --ldflags --cflags-Wl,@/usr/xenomai/lib/posix.wrappers -L/usr/xenomai/lib -lpthread_rt -lxenomai -lpthread -lrt-I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -I/usr/xenomai/include/posix The example code here http://www.cplusplus.com/reference/mutex/mutex/try_lock/
compiles and runs fine (i.e.: I get a reasonable number of increases of the counter) with (1) g++ -pthread test.cpp -std=c++14 -lpthread

Quoted 4 lines

but if I wrap the posix calls with(2) gcc test.cpp -std=c++14 -I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -I/usr/xenomai/include/posix -L/usr/xenomai/lib -lstdc++ -lpthread_rt -lxenomai -lpthread -lrt -Wl,@/usr/xenomai/lib/posix.wrappers -pthread
then the mutex can never be acquired by any thread. Actually, I cannot lock it even if I place mtx.try_lock() on the first line of main().Also, if I try to sleep at sleep just before the end of attempt_10k_increases() with "std::this_thread::sleep_for", it does not sleep. Even if I increase the number of attempts, in order for the threads to stay alive for longer, I cannot see the threads show up in /proc/xenomai/stat . What I observe is that when using -Wl,@/usr/xenomai/lib/posix.wrapper:- mutex::try_lock fails- std::this_thread::sleep_for() does not sleep- threads do not show up in /proc/xenomai/stat
So I have two questions:a) is there any way to get -Wl,@/usr/xenomai/lib/posix.wrappers to correctly wrap underlying calls to pthread_* functions which are hidden in libstdc++ ?b)

Only if the static version of libstdc++ is used, otherwise it's too late for the linker to do any wrapping, since it obviously won't update a shared library on the fly while building an application which depends on it.

is there any way to safely link into the same executable some code that
calls pthread_* which you want to wrap in the Xenomai posix skin and C++ code which uses std::thread?

You bring the safety by not passing POSIX resources initialized by libcobalt (e.g. pthread_mutex_init(), pthread_create(), sem_init() and so on) to methods dealing with POSIX resources created by the regular libc, and conversely.

Signature · 1 line
Philippe.