Implement fenced_block using std::atomic_thread_fence, when available.

This commit is contained in:
Christopher Kohlhoff 2017-01-16 09:46:55 +11:00
parent 595ff85c33
commit ffc9dd443f
3 changed files with 67 additions and 0 deletions

View File

@ -211,6 +211,7 @@ nobase_include_HEADERS = \
asio/detail/solaris_fenced_block.hpp \
asio/detail/static_mutex.hpp \
asio/detail/std_event.hpp \
asio/detail/std_fenced_block.hpp \
asio/detail/std_global.hpp \
asio/detail/std_mutex.hpp \
asio/detail/std_static_mutex.hpp \

View File

@ -20,6 +20,8 @@
#if !defined(ASIO_HAS_THREADS) \
|| defined(ASIO_DISABLE_FENCED_BLOCK)
# include "asio/detail/null_fenced_block.hpp"
#elif defined(ASIO_HAS_STD_ATOMIC)
# include "asio/detail/std_fenced_block.hpp"
#elif defined(__MACH__) && defined(__APPLE__)
# include "asio/detail/macos_fenced_block.hpp"
#elif defined(__sun)
@ -48,6 +50,8 @@ namespace detail {
#if !defined(ASIO_HAS_THREADS) \
|| defined(ASIO_DISABLE_FENCED_BLOCK)
typedef null_fenced_block fenced_block;
#elif defined(ASIO_HAS_STD_ATOMIC)
typedef std_fenced_block fenced_block;
#elif defined(__MACH__) && defined(__APPLE__)
typedef macos_fenced_block fenced_block;
#elif defined(__sun)

View File

@ -0,0 +1,62 @@
//
// detail/std_fenced_block.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_DETAIL_STD_FENCED_BLOCK_HPP
#define ASIO_DETAIL_STD_FENCED_BLOCK_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#if defined(ASIO_HAS_STD_ATOMIC)
#include <atomic>
#include "asio/detail/noncopyable.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
namespace detail {
class std_fenced_block
: private noncopyable
{
public:
enum half_t { half };
enum full_t { full };
// Constructor for a half fenced block.
explicit std_fenced_block(half_t)
{
}
// Constructor for a full fenced block.
explicit std_fenced_block(full_t)
{
std::atomic_thread_fence(std::memory_order_acquire);
}
// Destructor.
~std_fenced_block()
{
std::atomic_thread_fence(std::memory_order_release);
}
};
} // namespace detail
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // defined(ASIO_HAS_STD_ATOMIC)
#endif // ASIO_DETAIL_STD_FENCED_BLOCK_HPP