2
0
mirror of https://github.com/boostorg/atomic.git synced 2026-01-19 16:12:09 +00:00

57 Commits

Author SHA1 Message Date
Andrey Semashev
f676a29106 Prefer C++11 constructs in the docs. 2025-06-13 00:24:20 +03:00
Andrey Semashev
ff2574cd03 Enable bitwise operations for enumerations.
This is a Boost.Atomic extension over std::atomic.

Bitwise operations can be useful if the enumeration is used to implement
a bit mask or a set of flags. Without these operations, users have to
manually perform conversions between the enum type and the underlying
type, which can be tedious.

There are caveats with enums with non-fixed underlying type, but the
convenience outweighs the potential pitfalls.
2025-06-13 00:24:13 +03:00
Andrey Semashev
1fa02d93f0 Removed BOOST_ATOMIC_NO_ATOMIC_FLAG_INIT macro definition.
Since the library now requires C++11, BOOST_ATOMIC_FLAG_INIT is always
supported.
2025-06-12 00:54:03 +03:00
Andrey Semashev
03eb376616 Renamed smt_pause to thread_pause.
Although smt_pause matches the equivalent global function on Solaris and
some BSD systems both in name and behavior, this naming clash may cause
confusion and ambiguities in users' code that imports boost::atomics
namespace into their scope and calls smt_pause unqualified. Better
avoid these issues and pick a different name.

Add a simple test for thread_pause, which only tests that the operation
compiles and executes.

Also include the recently added headers in the global atomic.hpp.
2025-06-08 23:19:36 +03:00
Andrey Semashev
d3940538d3 Expose pause() as smt_pause().
The smt_pause() operation may be useful in spin loops to release CPU
resources for use in simpling threads on SMT-capable CPUs.

Compared to the previous pause() implementation, added support for
PowerPC and Solaris and use isb instruction on AArch64, which seems
to be used in various open source projects instead of yield.
2025-06-08 20:08:57 +03:00
Andrey Semashev
280f16ea1f Added docs for timed waiting operations. 2025-06-08 15:58:18 +03:00
Andrey Semashev
140bb01b17 Added a note for MinGW-w64 users regarding Windows 10. 2025-05-30 13:59:15 +03:00
Andrey Semashev
ab650ab37f Added a note about difference in behavior with C++11 in compare_exchange.
Boost.Atomic unconditionally writes to `expected` upon returning from
compare_exchange in most backends, and this behavior is documented. This
is different from std::atomic, which only writes to `expected` if
compare_exchange returns false. This may be a significant difference,
if `expected` references data that is supposed to be protected by
the atomic via the memory order constraint.

Added a note in the docs that highlights this difference. Since this
is a documented behavior, and relying on the strict std::atomic-conforming
behavior is probably not a good idea in practice anyway (i.e. such code
would be dubious at best), the implementation is left as is. The generated
code may be slightly more efficient this way, too, since there is one less
branch. We may revisit this later, if there appears a stronger incentive
to be more C++ standard compliant.
2025-05-23 11:56:29 +03:00
Andrey Semashev
4a9078a7b8 Dropping support for C++03 compilers.
As a result of dependent libraries dropping support for C++03, Boost.Atomic
is no longer able to support C++03 and now requires C++11 as a minimum.

Drop C++03 compilers from the CI and update docs accordingly. No code changes
at this time.
2023-09-02 19:31:10 +03:00
Andrey Semashev
39a184f68a Mention that atomics are only supposed to work in WB memory. 2023-02-28 12:24:54 +03:00
Andrey Semashev
08a055ec27 Added BOOST_ATOMIC_NO_DARWIN_ULOCK macro.
The new macro disables ulock-based implementation of waiting and notifying
operations on Darwin systems. This may be useful to comply with Apple
App Store requirements.

Closes https://github.com/boostorg/atomic/issues/55.
2022-03-04 22:36:40 +03:00
Andrey Semashev
1d4e279d41 Updated the note about clearing padding in unions. 2021-10-12 23:19:14 +03:00
Andrey Semashev
8a7aaa543d Make atomic default ctors value initialize the contained object.
This is in line with the C++20 change that requires the default constructor
of std::atomic to value initialize the atomic object.

Also, use is_nothrow_default_constructible to properly deduce noexceptness
of the default constructors of atomics.
2021-10-12 20:36:11 +03:00
Andrey Semashev
2211fee1d4 Added bitwise_cast implementation based on bit_cast intrinsics.
This allows to mark bitwise_cast constexpr when the conditions to use
bit_cast are met:

 - both source and target types have the same size, and
 - the source type has no padding bits.

The latter is checked using has_unique_object_representations trait, which
we also implement using intrinsics when not available in the standard
library.

This allows atomic constructors to become constexpr for structs and floating
point types with no padding and whose size matches the atomic storage size.
2021-10-12 18:55:13 +03:00
Andrey Semashev
746ea2649b Added support for types with padding. Made atomic ctors for enums constexpr.
Use __builtin_clear_padding and __builtin_zero_non_value_bits that were
introduced in gcc 11 and MSVC 19.27 to clear the padding bits in atomic
types. The intrinsics are used in bitwise_cast and atomic reference
constructors.

Also, separated atomic impl specializations for enums to allow using
static_cast to convert values to storage. This in turn allows to
relax compiler requirements to mark atomic constructors constexpr.

Updated docs and added tests for structs with padding and constexpr
atomic constructors.
2021-10-11 18:05:06 +03:00
Andrey Semashev
d45947a406 [skip ci] Added an advice to avoid resizing memory with IPC ops running. 2021-10-06 14:27:06 +03:00
Andrey Semashev
ffdccceeb1 Added changelog document. 2021-09-26 23:07:49 +03:00
Andrey Semashev
cbf5ae1ad2 Allocate IPC atomic objects in dynamic memory instead of the stack.
This works around spurious test failures on Mac OS as the notifying operation
sometimes fails with ENOENT. Presumably, the OS sometimes invalidates the
internal identification of the stack memory region, which makes __ulock_wake
fail to find the ulock object that other threads are blocked on.

By using dynamic memory we (hopefully) are using a location in a normal mapped
memory region that should not be mangled by the OS. Ideally, we would use
process-shared memory for this test, but that makes it more difficult to make
it portable and runnable in parallel. Dynamic local memory should do for now.
2021-09-26 19:54:01 +03:00
Andrey Semashev
a548b3c15c Added template deduction guides and factory functions for atomic refs. 2021-06-16 02:22:40 +03:00
Andrey Semashev
559eba81af Use dummy atomic instruction instead of mfence for seq_cst fences on x86.
mfence is more expensive on most recent CPUs than a lock-prefixed instruction
on a dummy location, while the latter is sufficient to implement sequential
consistency on x86. Some performance test results are available here:

https://shipilev.net/blog/2014/on-the-fence-with-dependencies/

Also, for seq_cst stores in gcc_atomic backend, use an xchg instead of
mov+mfence, which are generated by gcc versions older than 10.1.

The machinery to detect mfence presence is still left intact just in case
if we need to use this instruction in the future.

Closes https://github.com/boostorg/atomic/issues/36.
2020-06-11 22:32:01 +03:00
Andrey Semashev
e5e96fbc9a Added atomic_unsigned/signed_lock_free typedefs introduced in C++20.
The typedefs indicate the atomic object type for an unsigned/signed
integer that is lock-free and preferably has native support for waiting
and notifying operations.
2020-06-11 13:07:45 +03:00
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
4b6884d9c9 Added a note explaining the incompatibility between atomic and atomic_ref. 2020-06-08 00:06:21 +03:00
Andrey Semashev
1cd7ba9bc5 Documented value() operation, clarified the limitation of no padding bits.
The value() operation is useful with futexes, but should not be used for
anything else, basically.

The lack of support for types with padding bits is documented more prominently.
The docs do mention that `long double` on x86 is supported though.

Also, added description of the new tests added recently.

Related to https://github.com/boostorg/atomic/issues/34.
2020-06-07 20:28:09 +03:00
Andrey Semashev
d5dc8f185a Added support for build-time configuration of the lock pool size.
The user may define BOOST_ATOMIC_LOCK_POOL_SIZE_LOG2 macro to specify
binary logarithm of the size of the internal lock pool. The macro
only has effect when building Boost.Atomic.
2020-06-03 01:48:48 +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
dd4cb7e540 Nonessential wording fix. 2020-04-01 19:31:22 +03:00
Andrey Semashev
9e26129616 Added a note of caution about object alignment for atomic_ref. 2020-04-01 19:28:38 +03:00
Andrey Semashev
11f3c3eb40 Replaced the term "referred" with "referenced" in the docs.
This is closer to the C++ standard wording regarding atomic_ref.
2020-03-09 19:33:52 +03:00
Andrey Semashev
59ee7c9e9e Removed support for BOOST_ATOMIC_DETAIL_HIGHLIGHT_OP_AND_TEST.
The macro was used to highlight the (op)_and_test methods of atomic<>
that changed the returned value to the opposite in Boost 1.67. The old
behavior was only released in 1.66 and the macro was a means to help
1.66 users to transition to the new releases.

1.67 will have been released 2 years before the upcoming 1.73 release,
in which this macro will be removed.
2020-02-29 23:52:19 +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
502208b6c5 Removed checks for padding bits in generic atomic_ref specialization.
We currently don't support structs with padding bits, so the checks
are useless. Also, updated docs so that users are not given the idea
that structs with padding bits are supported.
2020-02-26 00:42:15 +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
e16092f473 Updated copyright year. 2018-03-18 01:39:40 +03:00
Andrey Semashev
8d5c592da2 Updated the Limitations section. 2018-02-27 02:20:09 +03:00
Andrey Semashev
b575159f8e Improved the wording of the note re. padding bits in FP types. 2018-02-13 04:09:24 +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
de22c6a203 Added a link to a gcc bug in the comment about consume/acquire MO. 2017-09-20 13:50:33 +03:00
Andrey Semashev
23d5770bde Minor docs formatting correction. 2017-08-06 22:09:41 +03:00
Andrey Semashev
b23afe4b0c Added docs for atomic_flag. 2017-08-06 22:04:26 +03:00
Andrey Semashev
4467cfbd3b Documented the extra operations added as an extension of Boost.Atomic.
Also made a few wording corrections and added is_always_lock_free and
a section about atomic<> typedefs. Clarified the status quo regarding
memory_order_consume. Removed the obsolete preudo-header for doxygen
that was not used for docs (if we want doxygen, it's better to
add comments to the real headers anyway).
2017-07-19 22:05:20 +03:00
Andrey Semashev
5b30e196a6 Added is_always_lock_free static constant from C++17. 2016-10-13 15:59:22 +03:00
Andrey Semashev
cc9cff37af Documented BOOST_ATOMIC_NO_CMPXCHG8B and BOOST_ATOMIC_NO_MFENCE config macros. 2016-09-15 01:19:52 +03:00
Andrey Semashev
4dee330229 Added support for types with non-trivial default constructors. 2014-07-07 22:40:41 +04:00
Andrey Semashev
560e3c0465 Clarified the requirements on the types compatible with boost::atomic. 2014-06-10 00:21:31 +04:00
Andrey Semashev
7fcb3b18b0 #9527. Fixed a typo in the docs. 2014-05-17 22:06:27 +04:00
Andrey Semashev
b80d0ebe36 Documented the BOOST_ATOMIC_NO_ATOMIC_FLAG_INIT macro. 2014-05-17 20:07:52 +04:00