37 Commits

Author SHA1 Message Date
Andrey Semashev
80cfbfd0de Added implementation of inter-process atomics.
The inter-process atomics have ipc_ prefixes: ipc_atomic, ipc_atomic_ref
and ipc_atomic_flag. These types are similar to their unprefixed counterparts
with the following distinctions:

- The operations are provided with an added precondition that is_lock_free()
  returns true.
- All operations, including waiting/notifying operations, are address-free,
  so the types are suitable for inter-process communication.
- The new has_native_wait_notify() operation and always_has_native_wait_notify
  static constant allow to test if the target platform has native support for
  address-free waiting/notifying operations. If it does not, a generic
  implementation is used based on a busy wait.
- The new set of capability macros added. The macros are named
  BOOST_ATOMIC_HAS_NATIVE_<T>_IPC_WAIT_NOTIFY and indicate whether address-free
  waiting/notifying operations are supported natively for a given type.

Additionally, to unify interface and implementation of different components,
the has_native_wait_notify() operation and always_has_native_wait_notify
static constant were added to non-IPC atomic types as well. Added
BOOST_ATOMIC_HAS_NATIVE_<T>_WAIT_NOTIFY capability macros to indicate
native support for inter-thread waiting/notifying operations.

Also, added is_lock_free() and is_always_lock_free to atomic_flag.

This commit adds implementation, docs and tests.
2020-06-11 13:07:16 +03:00
Andrey Semashev
76e25f36a3 Added generic implementation of C++20 waiting/notifying operations.
The generic implementation is based on the lock pool. A list of condition
variables (or waiting futexes) is added per lock. Basically, the lock
pool serves as a global hash table, where each lock represents
a bucket and each wait state is an element. Every wait operation
allocates a wait state keyed on the pointer to the atomic object. Notify
operations look up the wait state by the atomic pointer and notify
the condition variable/futex. The corresponding lock needs to be acquired
to protect the wait state list during all wait/notify operations.

Backends not involving the lock pool are going to be added later.

The implementation of wait operation extends the C++20 definition in that
it returns the newly loaded value instead of void. This allows the caller
to avoid loading the value himself.

The waiting/notifying operations are not address-free. Address-free variants
will be added later.

Added tests for the new operations and refactored existing tests for atomic
operations. Added docs for the new operations.
2020-06-03 01:39:20 +03:00
Andrey Semashev
e3dce0e226 Added missing const qualifiers in atomic_ref ops and updated tests to verify this. 2020-05-23 23:19:14 +03:00
Andrey Semashev
c22fc7d416 Added a workaround for gcc 4.7 not supporting constexpr ctors with unions.
gcc 4.7 does not support constexpr constructors that initialize one member
of an anonymous union data member of the class. atomic and atomic_flag
no longer have constexpr constructors on this compiler.
2020-03-01 18:05:01 +03:00
Andrey Semashev
a1a31e14e2 Reduce alignment requirements of lock-based backend of atomic_ref.
Lock-based operations have no reason to require object alignment higher
than alignof(T). This commit implements a special storage type for
lock-based operations, which has the same alignment as value_type.

Also, added tests to verify required_alignment correctness both
in lock-free and lock-based cases.
2020-03-01 00:22:35 +03:00
Andrey Semashev
4336ac66bd Added support for C++20 atomic_flag::test operation.
The operation allows to test whether the flag is in the set state.
Also added tests and docs.
2020-02-26 01:24:54 +03:00
Andrey Semashev
2ed311af18 Fixed wrong x86 code generated for bit_test_and_op for 8 and 16-bit atomics.
The implementation used to generate 32-bit bts/btr/btc for 8 and 16-bit
atomics, which could result in alignment and access violation and possibly
data corruption. 32 and 64-bit atomics are unaffected.

This commit fixes operaend width for 16-bit atomics. For 8-bit atomics the
generic implementation is used based on or/and/xor instructions since there
are no 8-bit bts/btr/btc.
2020-02-25 23:59:02 +03:00
Andrey Semashev
8b70628d91 Added compilation fixes for MSVC. 2020-02-25 17:09:22 +03:00
Andrey Semashev
904871c37d Implemented atomic_ref.
This commit adds C++20 atomic_ref implementation, documentation and tests.
2020-02-25 02:00:05 +03:00
Andrey Semashev
b2594ecbc0 Disable all pointer arithmetic tests on UBSAN. 2019-10-13 10:34:58 +03:00
Andrey Semashev
0d8ea24351 Added a workaround for clang UBSAN failures.
Clang UBSAN considers an UB subtracting from a null pointer, which is
required for one of the tests. Disable that test when UBSAN is enabled.
2019-10-13 01:33:43 +03:00
Andrey Semashev
4c2fc16ab1 Work around signed overflow on min value negation.
This should fix UBSAN failures.
2019-10-12 21:20:33 +03:00
Andrey Semashev
b990132e85 Added casts to silence MSVC warnings about lost FP precision of constants. 2018-02-18 13:57:32 +03:00
Andrey Semashev
e081674ce9 Use FP numbers in FP tests. 2018-02-14 03:19:09 +03:00
Andrey Semashev
edef50f042 Added support for atomic floating point operations.
The support includes:

- The standard fetch_add/fetch_sub operations.
- Extra operations: (fetch_/opaque_)negate, (opaque_)add/sub.
- Extra capability macros: BOOST_ATOMIC_FLOAT/DOUBLE/LONG_DOUBLE_LOCK_FREE.

The atomic operations are currently implemented on top of the integer-based
backends and thus are mostly CAS-based. The CAS operations perform binary
comparisons, and as such have different behavior wrt. special FP values like
NaN and signed zero than normal C++.

The support for floating point types is optional and can be disabled by
defining BOOST_ATOMIC_NO_FLOATING_POINT. This can be useful if on a certain
platform parameters of the floating point types cannot be deduced from the
compiler-defined or system macros (in which case the compilation fails).

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0020r6.html
2018-02-13 03:36:35 +03:00
Andrey Semashev
92c57ac1e4 Added atomic operations that return the result of the operation.
These operations are useful for two reasons. First, they are needed by
atomic<> interface as the pre-increment/decrement and add/subtract operators
need to perform the corresponding arithmetics and return the actual result while
not exhibiting UB in case of overflow. This means that the operation must be
performed on the unsigned storage type in the backend. Second, the (op)_and_test
operations on ARM and PowerPC can be implemented in a more generic way on top of
the operations that return the result. And since we have those operations
internally, why not expose them to users.

Added tests and docs for the new operations. Also, added docs for the recently
added scoped names of the memory_order enum values.

Also, added a specialized "emulated" backend for the extra operations. This
backend makes better use of the fact that the operations are lock-protected
by avoiding any CAS-based loops.
2018-02-11 00:56:23 +03:00
Andrey Semashev
9fd085c59f Added negate_and_test and complement_and_test ops.
As the names suggest, the methods perform the corresponding operation and test
if the result is not zero.

Also, for the emulated fetch_complement, take care of integral promotion, which
could mess up the storage bits that were not part of the value on backends
where the storage is larger than the value. This could in turn break CAS on
the atomic value as it compares the whole storage.
2018-02-04 00:13:27 +03:00
Andrey Semashev
b24cea0af1 Changed the result of (op)_and_test operations to the opposite.
This makes the result of (op)_and_test more consistent with other
methods such as test_and_set and bit_test_and_set, as well as the
methods used in the C++ standard library.

This is a breaking change. The users are able to define
BOOST_ATOMIC_HIGHLIGHT_OP_AND_TEST macro to generate warnings on each
use of the changed functions. This will help users to port from Boost
1.66 to newer Boost releases.

More info at:

https://github.com/boostorg/atomic/issues/11
http://boost.2283326.n4.nabble.com/atomic-op-and-test-naming-
tc4701445.html
2018-01-28 20:50:12 +03:00
Andrey Semashev
573732ea4a Added a workaround for libstdc++ not specializing numeric_limits for __int128 in strict mode. 2017-07-29 18:13:27 +03:00
Andrey Semashev
a980d1c3ba Attempt to work around absent opereator<< for __int128 in some libstdc++ versions. 2017-07-20 23:46:54 +03:00
Andrey Semashev
c172831e5d Added a workaround for gcc on PPC not providing operator<< for __int128. 2017-07-17 01:15:04 +03:00
Andrey Semashev
5b28e12f1d Fixed tests to accommodate MSVC bugs and improved diagnostics.
Fixed incorrect calculation of the min distance limit for arithmetic tests.

Moved some of the arithmetic tests to a separate function because
otherwise MSVC-10 for x64 generated broken code (the code would use
garbage values in registers to pass arguments to
add_and_test/sub_and_test).

Added output operators and employed newer test macros that output compared
values in case of test failure.
2017-07-11 23:00:52 +03:00
Andrey Semashev
3622ce85c7 Updated test to avoid signed overflows when add_and_test/sub_and_test is
called on pointer types.
2017-07-11 23:00:52 +03:00
Andrey Semashev
65d0d557f1 Fixed a test constant, added a few more checks. 2017-07-11 23:00:52 +03:00
Andrey Semashev
158a114325 Corrected test constants. 2017-07-11 23:00:52 +03:00
Andrey Semashev
add6f5640c Added tests for the new operations. 2017-07-11 23:00:52 +03:00
Andrey Semashev
ca151bedb4 Fixed warnings about left-shifting negative signed integers. 2017-05-22 15:21:22 +03:00
Andrey Semashev
11c785768c Updated to reflect changes from P0558R1 accepted into C++17.
1. Expose value_type and difference_type (where present) to user's code.

2. Prohibit arithmetic operations on pointers to non-object types. In
   particular, arithmetic operations such as fetch_add/fetch_sub will no longer
   compile for pointers to cv void, pointers to functions and pointers to
   non-static class members.

Also, use C++11 <type_traits> when possible instead of Boost.TypeTraits to
reduce dependencies. Cleaned up value_arg_type internal type usage for more
efficient argument passing.
2017-04-01 18:29:26 +03:00
Andrey Semashev
ff91811c1a Fixes #10994. Fixed compilation with gcc 4.4 when cmpxchg16b is used. 2015-08-17 23:27:37 +03:00
Andrey Semashev
b1bb36c5b3 Only use intptr_t when available. Only test integer overflows/underflows for unsigned integers (the behavior is undefined for signed ints anyway). This should silence compiler warnings as well. 2015-08-17 22:25:19 +03:00
Andrey Semashev
7b1d722d94 Ported tests to core/lightweight_test.hpp.
Boost.Test was dropped due to instability and long-standing differences between develop and master branches. We don't use most of its features anyway.
2015-01-25 18:50:58 +03:00
Andrey Semashev
4dee330229 Added support for types with non-trivial default constructors. 2014-07-07 22:40:41 +04:00
Andrey Semashev
625c3c3a2e Compilation fixes. Added public headers for atomic_flag and atomic. 2014-04-20 17:26:42 +04:00
Andrey Semashev
55e8b16a12 Fixed compilation with gcc 4.4. Optimized code for gcc older than 4.7 and also added support for 128-bit atomic ops. Completed transition to defaulted and deleted functions.
[SVN r84801]
2013-06-16 13:40:11 +00:00
Tim Blechmann
96b44933f0 atomic: provide additive functionality for atomic<void*>
fixes #8501

[SVN r84190]
2013-05-08 04:45:38 +00:00
Tim Blechmann
764db51b6d atomic: adapt for constexpr and noexcept
Signed-off-by: Tim Blechmann <tim@klingt.org>

[SVN r82870]
2013-02-14 12:32:41 +00:00
Helge Bahmann
131b70c1fa atomic: initial import
[SVN r79348]
2012-07-08 11:21:45 +00:00