-
Notifications
You must be signed in to change notification settings - Fork 70
Add support for pthread-based thread local storage #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@ros2/team review this please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a few questions but otherwise it looks solid to me.
I'll start some CI.
rmw/include/rmw/macros.h
Outdated
#else | ||
#define RMW_THREAD_LOCAL __thread | ||
#define RMW_THREAD_LOCAL _Thread_local |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change for the non-iOS<10 and non-windows case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C11 added support for TLS via _Thread_local
, __thread
is a GCC extension that Clang supports for compatibility reasons. I can revert it to __thread
, but I thought that given that IIRC ROS2 now requires a C11-capable compiler, it might be nice to use _Thread_local
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The MSVC compiler is far from C11 compliant, AFAIK. So I think that we do not require a C11 compiler. It has been turned on in a few of the packages, but I had explicitly tried to avoid it in rmw
. That being said, we already have to special case here for Windows, and the other compilers are likely C11 compliant. I'll either leave this as-is or try to detect when _Thread_local
is not available and fallback further to __thread
.
rmw/include/rmw/macros.h
Outdated
// iOS 10 added support for thread local storage | ||
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 100000 | ||
#define RMW_THREAD_LOCAL_PTHREAD 1 | ||
#define RMW_THREAD_LOCAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So in this case the PTHREAD alternative is used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly. If we happen to be targeting iOS < 10.0, the pthread
-based implementation of TLS will be used (RMW_THREAD_LOCAL_PTHREAD
is defined). This can be expanded to other platforms that have an implementation of pthread
, but for now it's only for iOS < 10.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about it, I think this might be somewhat dangerous because if someone is using RMW_THREAD_LOCAL
, but doesn't special case it for RMW_THREAD_LOCAL_PTHREAD
then they might end up with something that assumes it is thread local, but actually isn't because RMW_THREAD_LOCAL
is just empty. I think I'll do #undef RMW_THREAD_LOCAL
instead.
Oh, actually, it looks like it needs to be rebased. I'll do that too. |
rmw/CMakeLists.txt
Outdated
@@ -21,13 +21,17 @@ set(rmw_sources | |||
set_source_files_properties( | |||
${rmw_sources} | |||
PROPERTIES LANGUAGE "C") | |||
add_library(${PROJECT_NAME} SHARED ${rmw_sources}) | |||
add_library(${PROJECT_NAME} ${rmw_sources}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dirk-thomas this is ok now that we have the "default to SHARED" turned on with https://github.com/ros2/ament_cmake_ros/blob/master/ament_cmake_ros/cmake/build_shared_libs.cmake, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this package does currently not use ament_cmake_ros
since #81 was reverted. Therefore the SHARED
needs to stay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've readded the SHARED
keyword in 911bb77 until I can figure out why the ament_cmake_ros
changes don't work.
Looks like
-- http://ci.ros2.org/job/ci_linux/2410/consoleFull @dirk-thomas what is blocking us from removing @esteve since you're probably wanting to use this in the static linking mode at some point, I guess we should wait to merge this until the above issue is investigated? |
Ok @esteve, here's new CI with your latest commit: |
also undef RMW_THREAD_LOCAL to cause compiler error in the case that pthread must be used
Thanks @esteve! |
@wjwwood thanks for fleshing out this PR. Just a minor comment, I remember deciding not using Thanks again for testing this! |
Doh. That's what I get for trying to make it better. Ok we can address it if we need to in the future. Hopefully it works for some cases now.
No worries, I wanted to get it in before #101 because this whole code block is getting moved to another package for reuse in other places. |
@wjwwood oh sorry, I hope I didn't sound ungrateful, the code definitely looks much better now. It's actually my fault for not documenting why I didn't use |
Not at all. |
@esteve do you know if pthread is still required for iOS? I thought I read that it's no longer required. The reason I'm asking is that I'm refactoring the error handling code to no longer allocate memory when setting an error (trying to improve memory management) and the pthread specific code is making things really difficult. I'm simultaneously looking into whether or not other potential platforms need pthread support still as well. |
This pull request has been mentioned on ROS Discourse. There might be relevant details there: https://discourse.ros.org/t/announcing-objective-c-on-macos-and-ios-for-ros2/946/1 |
This PR adds support for pthread-based thread local storage on platforms that don't have compiler builtin support for it. I only enabled it for iOS < 10.0, but it can be extended to any other platform via CMake flags.