2013-06-28 15:54:40 -07:00
|
|
|
/*
|
2017-05-03 11:05:53 -07:00
|
|
|
* Copyright (c) 2013, 2014, 2017 Nicira, Inc.
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef OVS_ATOMIC_H
|
|
|
|
|
#define OVS_ATOMIC_H 1
|
|
|
|
|
|
|
|
|
|
/* Atomic operations.
|
|
|
|
|
*
|
|
|
|
|
* This library implements atomic operations with an API based on the one
|
|
|
|
|
* defined in C11. It includes multiple implementations for compilers and
|
2013-08-21 09:58:38 -07:00
|
|
|
* libraries with varying degrees of built-in support for C11, including a
|
2013-06-28 15:54:40 -07:00
|
|
|
* fallback implementation for systems that have pthreads but no other support
|
|
|
|
|
* for atomics.
|
|
|
|
|
*
|
|
|
|
|
* This comment describes the common features of all the implementations.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Types
|
|
|
|
|
* =====
|
|
|
|
|
*
|
|
|
|
|
* The following atomic types are supported as typedefs for atomic versions of
|
|
|
|
|
* the listed ordinary types:
|
|
|
|
|
*
|
|
|
|
|
* ordinary type atomic version
|
|
|
|
|
* ------------------- ----------------------
|
|
|
|
|
* bool atomic_bool
|
|
|
|
|
*
|
|
|
|
|
* char atomic_char
|
|
|
|
|
* signed char atomic_schar
|
|
|
|
|
* unsigned char atomic_uchar
|
|
|
|
|
*
|
|
|
|
|
* short atomic_short
|
|
|
|
|
* unsigned short atomic_ushort
|
|
|
|
|
*
|
|
|
|
|
* int atomic_int
|
|
|
|
|
* unsigned int atomic_uint
|
|
|
|
|
*
|
|
|
|
|
* long atomic_long
|
|
|
|
|
* unsigned long atomic_ulong
|
|
|
|
|
*
|
|
|
|
|
* long long atomic_llong
|
|
|
|
|
* unsigned long long atomic_ullong
|
|
|
|
|
*
|
|
|
|
|
* size_t atomic_size_t
|
|
|
|
|
* ptrdiff_t atomic_ptrdiff_t
|
|
|
|
|
*
|
|
|
|
|
* intmax_t atomic_intmax_t
|
|
|
|
|
* uintmax_t atomic_uintmax_t
|
|
|
|
|
*
|
|
|
|
|
* intptr_t atomic_intptr_t
|
|
|
|
|
* uintptr_t atomic_uintptr_t
|
|
|
|
|
*
|
|
|
|
|
* uint8_t atomic_uint8_t (*)
|
|
|
|
|
* uint16_t atomic_uint16_t (*)
|
|
|
|
|
* uint32_t atomic_uint32_t (*)
|
|
|
|
|
* int8_t atomic_int8_t (*)
|
|
|
|
|
* int16_t atomic_int16_t (*)
|
|
|
|
|
* int32_t atomic_int32_t (*)
|
2017-09-29 10:10:27 -07:00
|
|
|
* uint64_t atomic_uint64_t (*)
|
|
|
|
|
* int64_t atomic_int64_t (*)
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
|
|
|
|
* (*) Not specified by C11.
|
|
|
|
|
*
|
ovs-atomic: Use raw types, not structs, when locks are required.
Until now, the GCC 4+ and pthreads implementations of atomics have used
struct wrappers for their atomic types. This had the advantage of allowing
a mutex to be wrapped in, in some cases, and of better type-checking by
preventing stray uses of atomic variables other than through one of the
atomic_*() functions or macros. However, the mutex meant that an
atomic_destroy() function-like macro needed to be used. The struct wrapper
also made it impossible to define new atomic types that were compatible
with each other without using a typedef. For example, one could not simply
define a macro like
#define ATOMIC(TYPE) struct { TYPE value; }
and then have two declarations like:
ATOMIC(void *) x;
ATOMIC(void *) y;
and do anything with these objects that require type-compatibility, even
"&x == &y", because the two structs are not compatible. One can do it
through a typedef:
typedef ATOMIC(void *) atomic_voidp;
atomic_voidp x, y;
but that is inconvenient, especially because of the need to invent a name
for the type.
This commit aims to ease the problem by getting rid of the wrapper structs
in the cases where the atomic library used them. It gets rid of the
mutexes, in the cases where they are still needed, by using a global
array of mutexes instead.
This commit also defines the ATOMIC macro described above and documents
its use in ovs-atomic.h.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Andy Zhou <azhou@nicira.com>
2014-03-11 12:46:29 -07:00
|
|
|
* Atomic types may also be obtained via ATOMIC(TYPE), e.g. ATOMIC(void *).
|
|
|
|
|
* Only basic integer types and pointer types can be made atomic this way,
|
|
|
|
|
* e.g. atomic structs are not supported.
|
|
|
|
|
*
|
2013-06-28 15:54:40 -07:00
|
|
|
* The atomic version of a type doesn't necessarily have the same size or
|
|
|
|
|
* representation as the ordinary version; for example, atomic_int might be a
|
ovs-atomic: Use raw types, not structs, when locks are required.
Until now, the GCC 4+ and pthreads implementations of atomics have used
struct wrappers for their atomic types. This had the advantage of allowing
a mutex to be wrapped in, in some cases, and of better type-checking by
preventing stray uses of atomic variables other than through one of the
atomic_*() functions or macros. However, the mutex meant that an
atomic_destroy() function-like macro needed to be used. The struct wrapper
also made it impossible to define new atomic types that were compatible
with each other without using a typedef. For example, one could not simply
define a macro like
#define ATOMIC(TYPE) struct { TYPE value; }
and then have two declarations like:
ATOMIC(void *) x;
ATOMIC(void *) y;
and do anything with these objects that require type-compatibility, even
"&x == &y", because the two structs are not compatible. One can do it
through a typedef:
typedef ATOMIC(void *) atomic_voidp;
atomic_voidp x, y;
but that is inconvenient, especially because of the need to invent a name
for the type.
This commit aims to ease the problem by getting rid of the wrapper structs
in the cases where the atomic library used them. It gets rid of the
mutexes, in the cases where they are still needed, by using a global
array of mutexes instead.
This commit also defines the ATOMIC macro described above and documents
its use in ovs-atomic.h.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Andy Zhou <azhou@nicira.com>
2014-03-11 12:46:29 -07:00
|
|
|
* typedef for a struct. The range of an atomic type does match the range of
|
|
|
|
|
* the corresponding ordinary type.
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
|
|
|
|
* C11 says that one may use the _Atomic keyword in place of the typedef name,
|
|
|
|
|
* e.g. "_Atomic int" instead of "atomic_int". This library doesn't support
|
|
|
|
|
* that.
|
|
|
|
|
*
|
|
|
|
|
*
|
2014-01-08 10:42:12 -08:00
|
|
|
* Life Cycle
|
|
|
|
|
* ==========
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
|
|
|
|
* To initialize an atomic variable at its point of definition, use
|
|
|
|
|
* ATOMIC_VAR_INIT:
|
|
|
|
|
*
|
|
|
|
|
* static atomic_int ai = ATOMIC_VAR_INIT(123);
|
|
|
|
|
*
|
|
|
|
|
* To initialize an atomic variable in code, use atomic_init():
|
|
|
|
|
*
|
|
|
|
|
* static atomic_int ai;
|
|
|
|
|
* ...
|
|
|
|
|
* atomic_init(&ai, 123);
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Barriers
|
|
|
|
|
* ========
|
|
|
|
|
*
|
|
|
|
|
* enum memory_order specifies the strictness of a memory barrier. It has the
|
|
|
|
|
* following values:
|
|
|
|
|
*
|
|
|
|
|
* memory_order_relaxed:
|
|
|
|
|
*
|
2014-08-05 13:51:19 -07:00
|
|
|
* Only atomicity is provided, does not imply any memory ordering with
|
|
|
|
|
* respect to any other variable (atomic or not). Relaxed accesses to
|
|
|
|
|
* the same atomic variable will be performed in the program order.
|
|
|
|
|
* The compiler and CPU are free to move memory accesses to other
|
|
|
|
|
* variables past the atomic operation.
|
|
|
|
|
*
|
|
|
|
|
* memory_order_consume:
|
|
|
|
|
*
|
|
|
|
|
* Memory accesses with data dependency on the result of the consume
|
|
|
|
|
* operation (atomic_read_explicit, or a load operation preceding a
|
|
|
|
|
* atomic_thread_fence) will not be moved prior to the consume
|
|
|
|
|
* barrier. Non-data-dependent loads and stores can be reordered to
|
2015-08-03 15:30:02 -07:00
|
|
|
* happen before the consume barrier.
|
2014-08-05 13:51:19 -07:00
|
|
|
*
|
|
|
|
|
* RCU is the prime example of the use of the consume barrier: The
|
|
|
|
|
* consume barrier guarantees that reads from a RCU protected object
|
|
|
|
|
* are performed after the RCU protected pointer is read. A
|
|
|
|
|
* corresponding release barrier is used to store the modified RCU
|
|
|
|
|
* protected pointer after the RCU protected object has been fully
|
|
|
|
|
* constructed. The synchronization between these barriers prevents
|
|
|
|
|
* the RCU "consumer" from seeing uninitialized data.
|
|
|
|
|
*
|
|
|
|
|
* May not be used with atomic_store_explicit(), as consume semantics
|
|
|
|
|
* applies only to atomic loads.
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
|
|
|
|
* memory_order_acquire:
|
|
|
|
|
*
|
|
|
|
|
* Memory accesses after an acquire barrier cannot be moved before the
|
|
|
|
|
* barrier. Memory accesses before an acquire barrier *can* be moved
|
|
|
|
|
* after it.
|
|
|
|
|
*
|
2014-08-05 13:51:19 -07:00
|
|
|
* An atomic_thread_fence with memory_order_acquire does not have a
|
|
|
|
|
* load operation by itself; it prevents all following memory accesses
|
|
|
|
|
* from moving prior to preceding loads.
|
|
|
|
|
*
|
|
|
|
|
* May not be used with atomic_store_explicit(), as acquire semantics
|
|
|
|
|
* applies only to atomic loads.
|
|
|
|
|
*
|
2013-06-28 15:54:40 -07:00
|
|
|
* memory_order_release:
|
|
|
|
|
*
|
|
|
|
|
* Memory accesses before a release barrier cannot be moved after the
|
|
|
|
|
* barrier. Memory accesses after a release barrier *can* be moved
|
|
|
|
|
* before it.
|
|
|
|
|
*
|
2014-08-05 13:51:19 -07:00
|
|
|
* An atomic_thread_fence with memory_order_release does not have a
|
|
|
|
|
* store operation by itself; it prevents all preceding memory accesses
|
|
|
|
|
* from moving past subsequent stores.
|
|
|
|
|
*
|
|
|
|
|
* May not be used with atomic_read_explicit(), as release semantics
|
|
|
|
|
* applies only to atomic stores.
|
|
|
|
|
*
|
2013-06-28 15:54:40 -07:00
|
|
|
* memory_order_acq_rel:
|
|
|
|
|
*
|
|
|
|
|
* Memory accesses cannot be moved across an acquire-release barrier in
|
|
|
|
|
* either direction.
|
|
|
|
|
*
|
2014-08-05 13:51:19 -07:00
|
|
|
* May only be used with atomic read-modify-write operations, as both
|
|
|
|
|
* load and store operation is required for acquire-release semantics.
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
2014-08-05 13:51:19 -07:00
|
|
|
* An atomic_thread_fence with memory_order_acq_rel does not have
|
|
|
|
|
* either load or store operation by itself; it prevents all following
|
|
|
|
|
* memory accesses from moving prior to preceding loads and all
|
|
|
|
|
* preceding memory accesses from moving past subsequent stores.
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
2014-08-05 13:51:19 -07:00
|
|
|
* memory_order_seq_cst:
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
2014-08-05 13:51:19 -07:00
|
|
|
* Prevents movement of memory accesses like an acquire-release barrier,
|
|
|
|
|
* but whereas acquire-release synchronizes cooperating threads (using
|
|
|
|
|
* the same atomic variable), sequential-consistency synchronizes the
|
|
|
|
|
* whole system, providing a total order for stores on all atomic
|
|
|
|
|
* variables.
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
2014-08-05 13:51:19 -07:00
|
|
|
* OVS atomics require the memory_order to be passed as a compile-time constant
|
|
|
|
|
* value, as some compiler implementations may perform poorly if the memory
|
|
|
|
|
* order parameter is passed in as a run-time value.
|
|
|
|
|
*
|
2013-06-28 15:54:40 -07:00
|
|
|
* The following functions insert explicit barriers. Most of the other atomic
|
|
|
|
|
* functions also include barriers.
|
|
|
|
|
*
|
|
|
|
|
* void atomic_thread_fence(memory_order order);
|
|
|
|
|
*
|
|
|
|
|
* Inserts a barrier of the specified type.
|
|
|
|
|
*
|
|
|
|
|
* For memory_order_relaxed, this is a no-op.
|
|
|
|
|
*
|
|
|
|
|
* void atomic_signal_fence(memory_order order);
|
|
|
|
|
*
|
|
|
|
|
* Inserts a barrier of the specified type, but only with respect to
|
|
|
|
|
* signal handlers in the same thread as the barrier. This is
|
|
|
|
|
* basically a compiler optimization barrier, except for
|
|
|
|
|
* memory_order_relaxed, which is a no-op.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Atomic Operations
|
|
|
|
|
* =================
|
|
|
|
|
*
|
|
|
|
|
* In this section, A is an atomic type and C is the corresponding non-atomic
|
|
|
|
|
* type.
|
|
|
|
|
*
|
2014-07-07 13:18:46 -07:00
|
|
|
* The "store" and "compare_exchange" primitives match C11:
|
2013-06-28 15:54:40 -07:00
|
|
|
*
|
|
|
|
|
* void atomic_store(A *object, C value);
|
|
|
|
|
* void atomic_store_explicit(A *object, C value, memory_order);
|
|
|
|
|
*
|
|
|
|
|
* Atomically stores 'value' into '*object', respecting the given
|
|
|
|
|
* memory order (or memory_order_seq_cst for atomic_store()).
|
|
|
|
|
*
|
2014-07-07 13:18:46 -07:00
|
|
|
* bool atomic_compare_exchange_strong(A *object, C *expected, C desired);
|
|
|
|
|
* bool atomic_compare_exchange_weak(A *object, C *expected, C desired);
|
|
|
|
|
* bool atomic_compare_exchange_strong_explicit(A *object, C *expected,
|
|
|
|
|
* C desired,
|
|
|
|
|
* memory_order success,
|
|
|
|
|
* memory_order failure);
|
|
|
|
|
* bool atomic_compare_exchange_weak_explicit(A *object, C *expected,
|
|
|
|
|
* C desired,
|
|
|
|
|
* memory_order success,
|
|
|
|
|
* memory_order failure);
|
|
|
|
|
*
|
|
|
|
|
* Atomically loads '*object' and compares it with '*expected' and if
|
|
|
|
|
* equal, stores 'desired' into '*object' (an atomic read-modify-write
|
|
|
|
|
* operation) and returns true, and if non-equal, stores the actual
|
|
|
|
|
* value of '*object' into '*expected' (an atomic load operation) and
|
|
|
|
|
* returns false. The memory order for the successful case (atomic
|
|
|
|
|
* read-modify-write operation) is 'success', and for the unsuccessful
|
|
|
|
|
* case (atomic load operation) 'failure'. 'failure' may not be
|
|
|
|
|
* stronger than 'success'.
|
|
|
|
|
*
|
|
|
|
|
* The weak forms may fail (returning false) also when '*object' equals
|
|
|
|
|
* '*expected'. The strong form can be implemented by the weak form in
|
|
|
|
|
* a loop. Some platforms can implement the weak form more
|
|
|
|
|
* efficiently, so it should be used if the application will need to
|
|
|
|
|
* loop anyway.
|
|
|
|
|
*
|
2013-06-28 15:54:40 -07:00
|
|
|
* The following primitives differ from the C11 ones (and have different names)
|
|
|
|
|
* because there does not appear to be a way to implement the standard
|
|
|
|
|
* primitives in standard C:
|
|
|
|
|
*
|
|
|
|
|
* void atomic_read(A *src, C *dst);
|
|
|
|
|
* void atomic_read_explicit(A *src, C *dst, memory_order);
|
|
|
|
|
*
|
|
|
|
|
* Atomically loads a value from 'src', writing the value read into
|
|
|
|
|
* '*dst', respecting the given memory order (or memory_order_seq_cst
|
|
|
|
|
* for atomic_read()).
|
|
|
|
|
*
|
|
|
|
|
* void atomic_add(A *rmw, C arg, C *orig);
|
|
|
|
|
* void atomic_sub(A *rmw, C arg, C *orig);
|
|
|
|
|
* void atomic_or(A *rmw, C arg, C *orig);
|
|
|
|
|
* void atomic_xor(A *rmw, C arg, C *orig);
|
|
|
|
|
* void atomic_and(A *rmw, C arg, C *orig);
|
|
|
|
|
* void atomic_add_explicit(A *rmw, C arg, C *orig, memory_order);
|
|
|
|
|
* void atomic_sub_explicit(A *rmw, C arg, C *orig, memory_order);
|
|
|
|
|
* void atomic_or_explicit(A *rmw, C arg, C *orig, memory_order);
|
|
|
|
|
* void atomic_xor_explicit(A *rmw, C arg, C *orig, memory_order);
|
|
|
|
|
* void atomic_and_explicit(A *rmw, C arg, C *orig, memory_order);
|
|
|
|
|
*
|
|
|
|
|
* Atomically applies the given operation, with 'arg' as the second
|
|
|
|
|
* operand, to '*rmw', and stores the original value of '*rmw' into
|
|
|
|
|
* '*orig', respecting the given memory order (or memory_order_seq_cst
|
|
|
|
|
* if none is specified).
|
|
|
|
|
*
|
|
|
|
|
* The results are similar to those that would be obtained with +=, -=,
|
|
|
|
|
* |=, ^=, or |= on non-atomic types.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* atomic_flag
|
|
|
|
|
* ===========
|
|
|
|
|
*
|
|
|
|
|
* atomic_flag is a typedef for a type with two states, set and clear, that
|
|
|
|
|
* provides atomic test-and-set functionality.
|
|
|
|
|
*
|
2014-01-08 17:13:28 -08:00
|
|
|
*
|
|
|
|
|
* Life Cycle
|
|
|
|
|
* ----------
|
|
|
|
|
*
|
2013-06-28 15:54:40 -07:00
|
|
|
* ATOMIC_FLAG_INIT is an initializer for atomic_flag. The initial state is
|
|
|
|
|
* "clear".
|
|
|
|
|
*
|
2014-03-11 13:16:54 -07:00
|
|
|
* An atomic_flag may also be initialized at runtime with atomic_flag_clear().
|
2014-01-08 17:13:28 -08:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Operations
|
|
|
|
|
* ----------
|
|
|
|
|
*
|
2013-06-28 15:54:40 -07:00
|
|
|
* The following functions are available.
|
|
|
|
|
*
|
|
|
|
|
* bool atomic_flag_test_and_set(atomic_flag *object)
|
|
|
|
|
* bool atomic_flag_test_and_set_explicit(atomic_flag *object,
|
|
|
|
|
* memory_order);
|
|
|
|
|
*
|
|
|
|
|
* Atomically sets '*object', respsecting the given memory order (or
|
|
|
|
|
* memory_order_seq_cst for atomic_flag_test_and_set()). Returns the
|
|
|
|
|
* previous value of the flag (false for clear, true for set).
|
|
|
|
|
*
|
|
|
|
|
* void atomic_flag_clear(atomic_flag *object);
|
|
|
|
|
* void atomic_flag_clear_explicit(atomic_flag *object, memory_order);
|
|
|
|
|
*
|
|
|
|
|
* Atomically clears '*object', respecting the given memory order (or
|
|
|
|
|
* memory_order_seq_cst for atomic_flag_clear()).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include "compiler.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
#define IN_OVS_ATOMIC_H
|
|
|
|
|
#if __CHECKER__
|
|
|
|
|
/* sparse doesn't understand some GCC extensions we use. */
|
|
|
|
|
#include "ovs-atomic-pthreads.h"
|
2013-08-26 13:03:02 -07:00
|
|
|
#elif __has_extension(c_atomic)
|
|
|
|
|
#include "ovs-atomic-clang.h"
|
2017-10-17 16:51:42 -07:00
|
|
|
#elif HAVE_ATOMIC && __cplusplus >= 201103L
|
|
|
|
|
#include "ovs-atomic-c++.h"
|
|
|
|
|
#elif HAVE_STDATOMIC_H && !defined(__cplusplus)
|
2014-11-11 08:19:27 -08:00
|
|
|
#include "ovs-atomic-c11.h"
|
2017-10-17 16:51:42 -07:00
|
|
|
#elif __GNUC__ >= 5 && !defined(__cplusplus)
|
2017-05-03 11:05:53 -07:00
|
|
|
#error "GCC 5+ should have <stdatomic.h>"
|
2017-10-17 16:51:42 -07:00
|
|
|
#elif __GNUC__ >= 5 || (__GNUC__ >= 4 && __GNUC_MINOR__ >= 7)
|
2013-06-28 15:54:40 -07:00
|
|
|
#include "ovs-atomic-gcc4.7+.h"
|
lib/ovs-atomic: Native support for x86_64 with GCC.
Some supported XenServer build environments lack compiler support for
atomic operations. This patch provides native support for x86_64 on
GCC, which covers possible future 64-bit builds on XenServer.
Since this implementation is faster than the existing support prior to
GCC 4.7, especially for cmap inserts, we use this with GCC < 4.7 on
x86_64.
Example numbers with "tests/test-cmap benchmark 2000000 8 0.1" on
quad-core hyperthreaded laptop, built with GCC 4.6 -O2:
Using ovs-atomic-pthreads on x86_64:
Benchmarking with n=2000000, 8 threads, 0.10% mutations:
cmap insert: 4725 ms
cmap iterate: 329 ms
cmap search: 5945 ms
cmap destroy: 911 ms
Using ovs-atomic-gcc4+ on x86_64:
Benchmarking with n=2000000, 8 threads, 0.10% mutations:
cmap insert: 845 ms
cmap iterate: 58 ms
cmap search: 308 ms
cmap destroy: 295 ms
With the native support provided by this patch:
Benchmarking with n=2000000, 8 threads, 0.10% mutations:
cmap insert: 530 ms
cmap iterate: 59 ms
cmap search: 305 ms
cmap destroy: 232 ms
Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-08-05 13:51:19 -07:00
|
|
|
#elif __GNUC__ && defined(__x86_64__)
|
|
|
|
|
#include "ovs-atomic-x86_64.h"
|
2014-08-05 13:51:19 -07:00
|
|
|
#elif __GNUC__ && defined(__i386__)
|
|
|
|
|
#include "ovs-atomic-i586.h"
|
2013-07-15 14:13:53 -07:00
|
|
|
#elif HAVE_GCC4_ATOMICS
|
2013-06-28 15:54:40 -07:00
|
|
|
#include "ovs-atomic-gcc4+.h"
|
2017-09-06 01:39:45 +03:00
|
|
|
#elif _MSC_VER
|
ovs-atomics: Add atomic support Windows.
Before this change (i.e., with pthread locks for atomics on Windows),
the benchmark for cmap and hmap was as follows:
$ ./tests/ovstest.exe test-cmap benchmark 10000000 3 1
Benchmarking with n=10000000, 3 threads, 1.00% mutations:
cmap insert: 61070 ms
cmap iterate: 2750 ms
cmap search: 14238 ms
cmap destroy: 8354 ms
hmap insert: 1701 ms
hmap iterate: 985 ms
hmap search: 3755 ms
hmap destroy: 1052 ms
After this change, the benchmark is as follows:
$ ./tests/ovstest.exe test-cmap benchmark 10000000 3 1
Benchmarking with n=10000000, 3 threads, 1.00% mutations:
cmap insert: 3666 ms
cmap iterate: 365 ms
cmap search: 2016 ms
cmap destroy: 1331 ms
hmap insert: 1495 ms
hmap iterate: 1026 ms
hmap search: 4167 ms
hmap destroy: 1046 ms
So there is clearly a big improvement for cmap.
But the correspondig test on Linux (with gcc 4.6) yeilds the following:
./tests/ovstest test-cmap benchmark 10000000 3 1
Benchmarking with n=10000000, 3 threads, 1.00% mutations:
cmap insert: 3917 ms
cmap iterate: 355 ms
cmap search: 871 ms
cmap destroy: 1158 ms
hmap insert: 1988 ms
hmap iterate: 1005 ms
hmap search: 5428 ms
hmap destroy: 980 ms
So for this particular test, except for "cmap search", Windows and
Linux have similar performance. Windows is around 2.5x slower in "cmap search"
compared to Linux. This has to be investigated.
Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
[With a lot of inputs and help from Jarno]
Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
2014-08-21 13:57:37 -07:00
|
|
|
#include "ovs-atomic-msvc.h"
|
2013-06-28 15:54:40 -07:00
|
|
|
#else
|
2014-06-04 09:15:48 -07:00
|
|
|
/* ovs-atomic-pthreads implementation is provided for portability.
|
|
|
|
|
* It might be too slow for real use because Open vSwitch is
|
|
|
|
|
* optimized for platforms where real atomic ops are available. */
|
2013-06-28 15:54:40 -07:00
|
|
|
#include "ovs-atomic-pthreads.h"
|
|
|
|
|
#endif
|
|
|
|
|
#undef IN_OVS_ATOMIC_H
|
|
|
|
|
|
2014-03-11 13:11:22 -07:00
|
|
|
#ifndef OMIT_STANDARD_ATOMIC_TYPES
|
|
|
|
|
typedef ATOMIC(bool) atomic_bool;
|
|
|
|
|
|
|
|
|
|
typedef ATOMIC(char) atomic_char;
|
|
|
|
|
typedef ATOMIC(signed char) atomic_schar;
|
|
|
|
|
typedef ATOMIC(unsigned char) atomic_uchar;
|
|
|
|
|
|
|
|
|
|
typedef ATOMIC(short) atomic_short;
|
|
|
|
|
typedef ATOMIC(unsigned short) atomic_ushort;
|
|
|
|
|
|
|
|
|
|
typedef ATOMIC(int) atomic_int;
|
|
|
|
|
typedef ATOMIC(unsigned int) atomic_uint;
|
|
|
|
|
|
|
|
|
|
typedef ATOMIC(long) atomic_long;
|
|
|
|
|
typedef ATOMIC(unsigned long) atomic_ulong;
|
|
|
|
|
|
|
|
|
|
typedef ATOMIC(long long) atomic_llong;
|
|
|
|
|
typedef ATOMIC(unsigned long long) atomic_ullong;
|
|
|
|
|
|
|
|
|
|
typedef ATOMIC(size_t) atomic_size_t;
|
|
|
|
|
typedef ATOMIC(ptrdiff_t) atomic_ptrdiff_t;
|
|
|
|
|
|
|
|
|
|
typedef ATOMIC(intmax_t) atomic_intmax_t;
|
|
|
|
|
typedef ATOMIC(uintmax_t) atomic_uintmax_t;
|
|
|
|
|
|
|
|
|
|
typedef ATOMIC(intptr_t) atomic_intptr_t;
|
|
|
|
|
typedef ATOMIC(uintptr_t) atomic_uintptr_t;
|
|
|
|
|
#endif /* !OMIT_STANDARD_ATOMIC_TYPES */
|
|
|
|
|
|
|
|
|
|
/* Nonstandard atomic types. */
|
|
|
|
|
typedef ATOMIC(uint8_t) atomic_uint8_t;
|
|
|
|
|
typedef ATOMIC(uint16_t) atomic_uint16_t;
|
|
|
|
|
typedef ATOMIC(uint32_t) atomic_uint32_t;
|
2017-09-29 10:10:27 -07:00
|
|
|
typedef ATOMIC(uint64_t) atomic_uint64_t;
|
2014-03-11 13:11:22 -07:00
|
|
|
|
|
|
|
|
typedef ATOMIC(int8_t) atomic_int8_t;
|
|
|
|
|
typedef ATOMIC(int16_t) atomic_int16_t;
|
|
|
|
|
typedef ATOMIC(int32_t) atomic_int32_t;
|
2017-09-29 10:10:27 -07:00
|
|
|
typedef ATOMIC(int64_t) atomic_int64_t;
|
2014-03-11 13:11:22 -07:00
|
|
|
|
2014-08-29 10:34:52 -07:00
|
|
|
/* Relaxed atomic operations.
|
|
|
|
|
*
|
|
|
|
|
* When an operation on an atomic variable is not expected to synchronize
|
|
|
|
|
* with operations on other (atomic or non-atomic) variables, no memory
|
|
|
|
|
* barriers are needed and the relaxed memory ordering can be used. These
|
|
|
|
|
* macros make such uses less daunting, but not invisible. */
|
|
|
|
|
#define atomic_store_relaxed(VAR, VALUE) \
|
|
|
|
|
atomic_store_explicit(VAR, VALUE, memory_order_relaxed)
|
|
|
|
|
#define atomic_read_relaxed(VAR, DST) \
|
|
|
|
|
atomic_read_explicit(VAR, DST, memory_order_relaxed)
|
|
|
|
|
#define atomic_compare_exchange_strong_relaxed(DST, EXP, SRC) \
|
|
|
|
|
atomic_compare_exchange_strong_explicit(DST, EXP, SRC, \
|
|
|
|
|
memory_order_relaxed, \
|
|
|
|
|
memory_order_relaxed)
|
|
|
|
|
#define atomic_compare_exchange_weak_relaxed(DST, EXP, SRC) \
|
|
|
|
|
atomic_compare_exchange_weak_explicit(DST, EXP, SRC, \
|
|
|
|
|
memory_order_relaxed, \
|
|
|
|
|
memory_order_relaxed)
|
|
|
|
|
#define atomic_add_relaxed(RMW, ARG, ORIG) \
|
|
|
|
|
atomic_add_explicit(RMW, ARG, ORIG, memory_order_relaxed)
|
|
|
|
|
#define atomic_sub_relaxed(RMW, ARG, ORIG) \
|
|
|
|
|
atomic_sub_explicit(RMW, ARG, ORIG, memory_order_relaxed)
|
|
|
|
|
#define atomic_or_relaxed(RMW, ARG, ORIG) \
|
|
|
|
|
atomic_or_explicit(RMW, ARG, ORIG, memory_order_relaxed)
|
|
|
|
|
#define atomic_xor_relaxed(RMW, ARG, ORIG) \
|
|
|
|
|
atomic_xor_explicit(RMW, ARG, ORIG, memory_order_relaxed)
|
|
|
|
|
#define atomic_and_relaxed(RMW, ARG, ORIG) \
|
|
|
|
|
atomic_and_explicit(RMW, ARG, ORIG, memory_order_relaxed)
|
|
|
|
|
#define atomic_flag_test_and_set_relaxed(FLAG) \
|
|
|
|
|
atomic_flag_test_and_set_explicit(FLAG, memory_order_relaxed)
|
|
|
|
|
#define atomic_flag_clear_relaxed(FLAG) \
|
|
|
|
|
atomic_flag_clear_explicit(FLAG, memory_order_relaxed)
|
|
|
|
|
|
2014-08-29 10:34:52 -07:00
|
|
|
/* A simplified atomic count. Does not provide any synchronization with any
|
|
|
|
|
* other variables.
|
|
|
|
|
*
|
|
|
|
|
* Typically a counter is not used to synchronize the state of any other
|
|
|
|
|
* variables (with the notable exception of reference count, below).
|
|
|
|
|
* This abstraction releaves the user from the memory order considerations,
|
|
|
|
|
* and may make the code easier to read.
|
|
|
|
|
*
|
|
|
|
|
* We only support the unsigned int counters, as those are the most common. */
|
|
|
|
|
typedef struct atomic_count {
|
|
|
|
|
atomic_uint count;
|
|
|
|
|
} atomic_count;
|
|
|
|
|
|
|
|
|
|
#define ATOMIC_COUNT_INIT(VALUE) { VALUE }
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
|
atomic_count_init(atomic_count *count, unsigned int value)
|
|
|
|
|
{
|
|
|
|
|
atomic_init(&count->count, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline unsigned int
|
|
|
|
|
atomic_count_inc(atomic_count *count)
|
|
|
|
|
{
|
|
|
|
|
unsigned int old;
|
|
|
|
|
|
2017-10-17 16:51:42 -07:00
|
|
|
atomic_add_relaxed(&count->count, 1u, &old);
|
2014-08-29 10:34:52 -07:00
|
|
|
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline unsigned int
|
|
|
|
|
atomic_count_dec(atomic_count *count)
|
|
|
|
|
{
|
|
|
|
|
unsigned int old;
|
|
|
|
|
|
2017-10-17 16:51:42 -07:00
|
|
|
atomic_sub_relaxed(&count->count, 1u, &old);
|
2014-08-29 10:34:52 -07:00
|
|
|
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline unsigned int
|
|
|
|
|
atomic_count_get(atomic_count *count)
|
|
|
|
|
{
|
|
|
|
|
unsigned int value;
|
|
|
|
|
|
|
|
|
|
atomic_read_relaxed(&count->count, &value);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
|
atomic_count_set(atomic_count *count, unsigned int value)
|
|
|
|
|
{
|
|
|
|
|
atomic_store_relaxed(&count->count, value);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 19:39:24 -08:00
|
|
|
/* Reference count. */
|
|
|
|
|
struct ovs_refcount {
|
|
|
|
|
atomic_uint count;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Initializes 'refcount'. The reference count is initially 1. */
|
|
|
|
|
static inline void
|
|
|
|
|
ovs_refcount_init(struct ovs_refcount *refcount)
|
|
|
|
|
{
|
2017-10-17 16:51:42 -07:00
|
|
|
atomic_init(&refcount->count, 1u);
|
2013-12-27 19:39:24 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-07 13:18:46 -07:00
|
|
|
/* Increments 'refcount'.
|
|
|
|
|
*
|
|
|
|
|
* Does not provide a memory barrier, as the calling thread must have
|
|
|
|
|
* protected access to the object already. */
|
2013-12-27 19:39:24 -08:00
|
|
|
static inline void
|
|
|
|
|
ovs_refcount_ref(struct ovs_refcount *refcount)
|
|
|
|
|
{
|
|
|
|
|
unsigned int old_refcount;
|
|
|
|
|
|
2017-10-17 16:51:42 -07:00
|
|
|
atomic_add_explicit(&refcount->count, 1u, &old_refcount,
|
2014-07-07 13:18:46 -07:00
|
|
|
memory_order_relaxed);
|
2013-12-27 19:39:24 -08:00
|
|
|
ovs_assert(old_refcount > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Decrements 'refcount' and returns the previous reference count. Often used
|
|
|
|
|
* in this form:
|
|
|
|
|
*
|
|
|
|
|
* if (ovs_refcount_unref(&object->ref_cnt) == 1) {
|
|
|
|
|
* // ...uninitialize object...
|
|
|
|
|
* free(object);
|
|
|
|
|
* }
|
2014-07-07 13:18:46 -07:00
|
|
|
*
|
|
|
|
|
* Provides a release barrier making the preceding loads and stores to not be
|
2014-08-29 10:34:52 -07:00
|
|
|
* reordered after the unref, and in case of the last reference provides also
|
|
|
|
|
* an acquire barrier to keep all the following uninitialization from being
|
|
|
|
|
* reordered before the atomic decrement operation. Together these synchronize
|
|
|
|
|
* any concurrent unref operations between each other. */
|
2013-12-27 19:39:24 -08:00
|
|
|
static inline unsigned int
|
|
|
|
|
ovs_refcount_unref(struct ovs_refcount *refcount)
|
|
|
|
|
{
|
|
|
|
|
unsigned int old_refcount;
|
|
|
|
|
|
2017-10-17 16:51:42 -07:00
|
|
|
atomic_sub_explicit(&refcount->count, 1u, &old_refcount,
|
2014-07-07 13:18:46 -07:00
|
|
|
memory_order_release);
|
2013-12-27 19:39:24 -08:00
|
|
|
ovs_assert(old_refcount > 0);
|
2014-07-07 13:18:46 -07:00
|
|
|
if (old_refcount == 1) {
|
|
|
|
|
/* 'memory_order_release' above means that there are no (reordered)
|
2014-08-29 10:34:52 -07:00
|
|
|
* accesses to the protected object from any thread at this point.
|
2014-07-07 13:18:46 -07:00
|
|
|
* An acquire barrier is needed to keep all subsequent access to the
|
|
|
|
|
* object's memory from being reordered before the atomic operation
|
|
|
|
|
* above. */
|
|
|
|
|
atomic_thread_fence(memory_order_acquire);
|
|
|
|
|
}
|
2013-12-27 19:39:24 -08:00
|
|
|
return old_refcount;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-07 13:18:46 -07:00
|
|
|
/* Reads and returns 'refcount_''s current reference count.
|
|
|
|
|
*
|
|
|
|
|
* Does not provide a memory barrier.
|
2013-12-27 19:39:24 -08:00
|
|
|
*
|
|
|
|
|
* Rarely useful. */
|
|
|
|
|
static inline unsigned int
|
|
|
|
|
ovs_refcount_read(const struct ovs_refcount *refcount_)
|
|
|
|
|
{
|
|
|
|
|
struct ovs_refcount *refcount
|
|
|
|
|
= CONST_CAST(struct ovs_refcount *, refcount_);
|
|
|
|
|
unsigned int count;
|
|
|
|
|
|
2014-07-07 13:18:46 -07:00
|
|
|
atomic_read_explicit(&refcount->count, &count, memory_order_relaxed);
|
2013-12-27 19:39:24 -08:00
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
lib/ovs-atomic: Add ovs_refcount_unref_relaxed(), ovs_refcount_try_ref_rcu().
When a reference counted object is also RCU protected the deletion of
the object's memory is always postponed. This allows
memory_order_relaxed to be used also for unreferencing, as RCU
quiescing provides a full memory barrier (it has to, or otherwise
there could be lingering accesses to objects after they are recycled).
Also, when access to the reference counted object is protected via a
mutex or a lock, the locking primitives provide the required memory
barrier functionality.
Also, add ovs_refcount_try_ref_rcu(), which takes a reference only if
the refcount is non-zero and returns true if a reference was taken,
false otherwise. This can be used in combined RCU/refcount scenarios
where we have an RCU protected reference to an refcounted object, but
which may be unref'ed at any time. If ovs_refcount_try_ref_rcu()
fails, the object may still be safely used until the current thread
quiesces.
Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-07-07 13:18:46 -07:00
|
|
|
/* Increments 'refcount', but only if it is non-zero.
|
|
|
|
|
*
|
|
|
|
|
* This may only be called for an object which is RCU protected during
|
|
|
|
|
* this call. This implies that its possible destruction is postponed
|
|
|
|
|
* until all current RCU threads quiesce.
|
|
|
|
|
*
|
|
|
|
|
* Returns false if the refcount was zero. In this case the object may
|
|
|
|
|
* be safely accessed until the current thread quiesces, but no additional
|
|
|
|
|
* references to the object may be taken.
|
|
|
|
|
*
|
|
|
|
|
* Does not provide a memory barrier, as the calling thread must have
|
|
|
|
|
* RCU protected access to the object already.
|
|
|
|
|
*
|
|
|
|
|
* It is critical that we never increment a zero refcount to a
|
|
|
|
|
* non-zero value, as whenever a refcount reaches the zero value, the
|
|
|
|
|
* protected object may be irrevocably scheduled for deletion. */
|
|
|
|
|
static inline bool
|
|
|
|
|
ovs_refcount_try_ref_rcu(struct ovs_refcount *refcount)
|
|
|
|
|
{
|
|
|
|
|
unsigned int count;
|
|
|
|
|
|
|
|
|
|
atomic_read_explicit(&refcount->count, &count, memory_order_relaxed);
|
|
|
|
|
do {
|
|
|
|
|
if (count == 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} while (!atomic_compare_exchange_weak_explicit(&refcount->count, &count,
|
|
|
|
|
count + 1,
|
|
|
|
|
memory_order_relaxed,
|
|
|
|
|
memory_order_relaxed));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Decrements 'refcount' and returns the previous reference count. To
|
|
|
|
|
* be used only when a memory barrier is already provided for the
|
|
|
|
|
* protected object independently.
|
|
|
|
|
*
|
|
|
|
|
* For example:
|
|
|
|
|
*
|
|
|
|
|
* if (ovs_refcount_unref_relaxed(&object->ref_cnt) == 1) {
|
|
|
|
|
* // Schedule uninitialization and freeing of the object:
|
|
|
|
|
* ovsrcu_postpone(destructor_function, object);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* Here RCU quiescing already provides a full memory barrier. No additional
|
|
|
|
|
* barriers are needed here.
|
|
|
|
|
*
|
|
|
|
|
* Or:
|
|
|
|
|
*
|
|
|
|
|
* if (stp && ovs_refcount_unref_relaxed(&stp->ref_cnt) == 1) {
|
|
|
|
|
* ovs_mutex_lock(&mutex);
|
2016-03-25 14:10:22 -07:00
|
|
|
* ovs_list_remove(&stp->node);
|
lib/ovs-atomic: Add ovs_refcount_unref_relaxed(), ovs_refcount_try_ref_rcu().
When a reference counted object is also RCU protected the deletion of
the object's memory is always postponed. This allows
memory_order_relaxed to be used also for unreferencing, as RCU
quiescing provides a full memory barrier (it has to, or otherwise
there could be lingering accesses to objects after they are recycled).
Also, when access to the reference counted object is protected via a
mutex or a lock, the locking primitives provide the required memory
barrier functionality.
Also, add ovs_refcount_try_ref_rcu(), which takes a reference only if
the refcount is non-zero and returns true if a reference was taken,
false otherwise. This can be used in combined RCU/refcount scenarios
where we have an RCU protected reference to an refcounted object, but
which may be unref'ed at any time. If ovs_refcount_try_ref_rcu()
fails, the object may still be safely used until the current thread
quiesces.
Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-07-07 13:18:46 -07:00
|
|
|
* ovs_mutex_unlock(&mutex);
|
|
|
|
|
* free(stp->name);
|
|
|
|
|
* free(stp);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* Here a mutex is used to guard access to all of 'stp' apart from
|
|
|
|
|
* 'ref_cnt'. Hence all changes to 'stp' by other threads must be
|
|
|
|
|
* visible when we get the mutex, and no access after the unlock can
|
|
|
|
|
* be reordered to happen prior the lock operation. No additional
|
|
|
|
|
* barriers are needed here.
|
|
|
|
|
*/
|
|
|
|
|
static inline unsigned int
|
|
|
|
|
ovs_refcount_unref_relaxed(struct ovs_refcount *refcount)
|
|
|
|
|
{
|
|
|
|
|
unsigned int old_refcount;
|
|
|
|
|
|
2017-10-17 16:51:42 -07:00
|
|
|
atomic_sub_explicit(&refcount->count, 1u, &old_refcount,
|
lib/ovs-atomic: Add ovs_refcount_unref_relaxed(), ovs_refcount_try_ref_rcu().
When a reference counted object is also RCU protected the deletion of
the object's memory is always postponed. This allows
memory_order_relaxed to be used also for unreferencing, as RCU
quiescing provides a full memory barrier (it has to, or otherwise
there could be lingering accesses to objects after they are recycled).
Also, when access to the reference counted object is protected via a
mutex or a lock, the locking primitives provide the required memory
barrier functionality.
Also, add ovs_refcount_try_ref_rcu(), which takes a reference only if
the refcount is non-zero and returns true if a reference was taken,
false otherwise. This can be used in combined RCU/refcount scenarios
where we have an RCU protected reference to an refcounted object, but
which may be unref'ed at any time. If ovs_refcount_try_ref_rcu()
fails, the object may still be safely used until the current thread
quiesces.
Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-07-07 13:18:46 -07:00
|
|
|
memory_order_relaxed);
|
|
|
|
|
ovs_assert(old_refcount > 0);
|
|
|
|
|
return old_refcount;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-28 15:54:40 -07:00
|
|
|
#endif /* ovs-atomic.h */
|