Skip to content

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

Merged
merged 5 commits into from
Apr 13, 2017
Merged

Add support for pthread-based thread local storage #80

merged 5 commits into from
Apr 13, 2017

Conversation

esteve
Copy link
Member

@esteve esteve commented Dec 8, 2016

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.

@esteve esteve added in progress Actively being worked on (Kanban column) in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Dec 8, 2016
@esteve esteve changed the title Added support for pthread-based thread local storage Add support for pthread-based thread local storage Dec 8, 2016
@mikaelarguedas
Copy link
Member

@ros2/team review this please

Copy link
Member

@wjwwood wjwwood left a 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.

#else
#define RMW_THREAD_LOCAL __thread
#define RMW_THREAD_LOCAL _Thread_local
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

// 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
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

@wjwwood
Copy link
Member

wjwwood commented Apr 7, 2017

Oh, actually, it looks like it needs to be rebased. I'll do that too.

@@ -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})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just wait for #81 or should I try this change without removing SHARED? I guess it's not relevant to the rest of the thread local related changes.

@esteve I suppose that's up to you. I can easily remove this part of the patch and rerun CI for you.

Copy link
Member Author

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.

@wjwwood
Copy link
Member

wjwwood commented Apr 7, 2017

Looks like rmw is already defaulting to non-shared and causing issues:

02:10:57 /usr/bin/ld: /home/rosbuild/ci_scripts/ws/install/rmw/lib/librmw.a(error_handling.c.o): relocation R_X86_64_TPOFF32 against `__rmw_error_state' can not be used when making a shared object; recompile with -fPIC

-- http://ci.ros2.org/job/ci_linux/2410/consoleFull

@dirk-thomas what is blocking us from removing SHARED from the add_library call and still getting dynamic libraries by default?

@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?

@wjwwood
Copy link
Member

wjwwood commented Apr 7, 2017

All of the CI I just ran, in case it's helpful for debugging:

  • Linux:
    • Build Status
  • Linux-aarch64:
    • Build Status
  • macOS:
    • Build Status
  • Windows:
    • Build Status

@esteve
Copy link
Member Author

esteve commented Apr 7, 2017

@wjwwood thanks for rebasing this PR. I've readded the explicit SHARED keyword, until #81 is resolved so this PR can be merged sooner.

@wjwwood
Copy link
Member

wjwwood commented Apr 8, 2017

Ok @esteve, here's new CI with your latest commit:

  • Linux:
    • Build Status
  • Linux-aarch64:
    • Build Status
  • macOS:
    • Build Status
  • Windows:
    • Build Status

@wjwwood
Copy link
Member

wjwwood commented Apr 13, 2017

CI:

  • Linux:
    • Build Status
  • Linux-aarch64:
    • Build Status
  • macOS:
    • Build Status (known compiler warning)
  • Windows:
    • Build Status

@wjwwood wjwwood merged commit 3121262 into ros2:master Apr 13, 2017
@wjwwood wjwwood removed the in review Waiting for review (Kanban column) label Apr 13, 2017
@wjwwood
Copy link
Member

wjwwood commented Apr 13, 2017

Thanks @esteve!

@esteve esteve deleted the thread-local-ios branch April 13, 2017 09:50
@esteve
Copy link
Member Author

esteve commented Apr 13, 2017

@wjwwood thanks for fleshing out this PR. Just a minor comment, I remember deciding not using TargetConditionals.h (df1500b), but unfortunately I forgot why. I think it was because TargetConditionals.h was not available in certain versions of the iOS SDK or because some of its definitions are deprecated, but nevertheless, if any issue shows up, it can be addressed in the future.

Thanks again for testing this!

@wjwwood
Copy link
Member

wjwwood commented Apr 13, 2017

@wjwwood thanks for fleshing out this PR. Just a minor comment, I remember deciding not using TargetConditionals.h (df1500b), but unfortunately I forgot why. I think it was because TargetConditionals.h was not available in certain versions of the iOS SDK or because some of its definitions are deprecated, but nevertheless, if any issue shows up, it can be addressed in the future.

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.

Thanks again for testing this!

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.

@esteve
Copy link
Member Author

esteve commented Apr 14, 2017

@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 TargetConditionals.h, now I forgot why I opted not to use it 😄

@wjwwood
Copy link
Member

wjwwood commented Apr 14, 2017

@wjwwood oh sorry, I hope I didn't sound ungrateful

Not at all.

@wjwwood
Copy link
Member

wjwwood commented Oct 25, 2018

@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.

@esteve
Copy link
Member Author

esteve commented Oct 29, 2018

I recall needing pthread for TLS with a Linaro toolchain a while ago, but I imagine that's no longer a problem. As for iOS, C11-based TLS is supported since 10. If this code needs to be revived in the future, I can take a look at it, but for now... KILL IT WITH FIRE

@ros-discourse
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants