Skip to content
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

Fix AtomicSharedPtr for C++20 #114

Merged
merged 5 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ jobs:
- name: run tests
run: ./ci/do_ci.sh cmake.test

cmake_test_cxx20:
name: CMake C++20 test
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: setup
run: |
sudo ./ci/setup_ci_environment.sh
sudo ./ci/setup_cmake.sh
- name: run tests
run: ./ci/do_ci.sh cmake.c++20.test

plugin_test:
name: Plugin -> CMake
runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ cmake_policy(SET CMP0057 NEW)

project(opentelemetry-cpp)

set(CMAKE_CXX_STANDARD 11)
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
endif()

option(WITH_OTPROTOCOL
"Whether to include the OpenTelemetry Protocol in the SDK" OFF)
Expand Down
10 changes: 10 additions & 0 deletions ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ if [[ "$1" == "cmake.test" ]]; then
make
make test
exit 0
elif [[ "$1" == "cmake.c++20.test" ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

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

Please mention this in the README.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

cd "${BUILD_DIR}"
rm -rf *
cmake -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-Werror" \
-DCMAKE_CXX_STANDARD=20 \
"${SRC_DIR}"
make
make test
exit 0
elif [[ "$1" == "cmake.exporter.otprotocol.test" ]]; then
cd "${BUILD_DIR}"
rm -rf *
Expand Down
2 changes: 1 addition & 1 deletion ci/setup_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ apt-get install --no-install-recommends --no-install-suggests -y \
pushd /usr/src/gtest
cmake CMakeLists.txt
make
cp *.a /usr/lib
cp *.a /usr/lib || cp lib/*.a /usr/lib
popd
24 changes: 3 additions & 21 deletions sdk/include/opentelemetry/sdk/common/atomic_shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,10 @@ namespace sdk
/**
* A wrapper to provide atomic shared pointers.
*
* This wrapper relies on std::atomic for C++20, a mutex for gcc 4.8, and
* specializations of std::atomic_store and std::atomic_load for all other
* instances.
* This wrapper relies on a mutex for gcc 4.8, and specializations of
* std::atomic_store and std::atomic_load for all other instances.
*/
#if __cplusplus > 201703L
template <class T>
class AtomicSharedPtr
{
public:
explicit AtomicSharedPtr(std::shared_ptr<T> ptr) noexcept : ptr_{std::move(ptr)} {}

void store(const std::shared_ptr<T> &other) noexcept
{
ptr_.store(other, std::memory_order_release);
}

std::shared_ptr<T> load() const noexcept { return ptr_.load(std::memory_order_acquire); }

private:
std::atomic<std::shared_ptr<T>> ptr_;
};
#elif (__GNUC__ == 4 && (__GNUC_MINOR__ >= 8))
#if (__GNUC__ == 4 && (__GNUC_MINOR__ >= 8))
template <class T>
class AtomicSharedPtr
{
Expand Down