Skip to content

Commit

Permalink
Add thread annotation macros
Browse files Browse the repository at this point in the history
Signed-off-by: Emerson Knapp <[email protected]>
  • Loading branch information
Emerson Knapp committed Feb 26, 2019
1 parent f07480a commit 720c5be
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
33 changes: 33 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.5)

project(rcpputils)

# Default to C11
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 11)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

find_package(ament_cmake REQUIRED)

if(NOT WIN32)
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

add_library(${PROJECT_NAME} INTERFACE)

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)
install(DIRECTORY include/ DESTINATION include)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()
96 changes: 96 additions & 0 deletions include/rcpputils/thread_safety_annotations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// NOTE: This is not the final resting place of this file - here to keep proof
// of concept localized to a single package

#ifndef RCPPUTILS__THREAD_SAFETY_ANNOTATIONS_H_
#define RCPPUTILS__THREAD_SAFETY_ANNOTATIONS_H_

#include <mutex>

// Enable thread safety attributes only with clang.
// The attributes can be safely erased when compiling with other compilers.
// Prefixing all macros to avoid potential conflict with other projects

#if defined(__clang__) && (!defined(SWIG))
#define RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
#else
#define RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
#endif

// libcxx does not define this operator, needed for negative capabilities
// Here until someone has a better idea
inline const std::mutex & operator!(const std::mutex & a)
{
return a;
}

#define RCPPUTILS_CAPABILITY(x) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))

#define RCPPUTILS_SCOPED_CAPABILITY \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)

#define RCPPUTILS_GUARDED_BY(x) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))

#define RCPPUTILS_PT_GUARDED_BY(x) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))

#define RCPPUTILS_ACQUIRED_BEFORE(...) \
RCPPUTILS_RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))

#define RCPPUTILS_ACQUIRED_AFTER(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))

#define RCPPUTILS_REQUIRES(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))

#define RCPPUTILS_REQUIRES_SHARED(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))

#define RCPPUTILS_ACQUIRE(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))

#define RCPPUTILS_ACQUIRE_SHARED(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))

#define RCPPUTILS_RELEASE(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))

#define RCPPUTILS_RELEASE_SHARED(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))

#define RCPPUTILS_TRY_ACQUIRE(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))

#define RCPPUTILS_TRY_ACQUIRE_SHARED(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))

#define RCPPUTILS_EXCLUDES(...) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))

#define RCPPUTILS_ASSERT_CAPABILITY(x) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))

#define RCPPUTILS_ASSERT_SHARED_CAPABILITY(x) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))

#define RCPPUTILS_RETURN_CAPABILITY(x) \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))

#define RCPPUTILS_NO_THREAD_SAFETY_ANALYSIS \
RCPPUTILS_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)

#endif // RCPPUTILS__THREAD_SAFETY_ANNOTATIONS_H_
9 changes: 9 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@
<description>Package containing various utility types and functions for C++</description>
<maintainer email="[email protected]">Emerson Knapp</maintainer>
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<test_depend>ament_lint_common</test_depend>
<test_depend>ament_lint_auto</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>

0 comments on commit 720c5be

Please sign in to comment.