Home | History | Annotate | Download | only in base
      1 // Copyright 2010 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // The routines exported by this module are subtle.  If you use them, even if
      6 // you get the code right, it will depend on careful reasoning about atomicity
      7 // and memory ordering; it will be less readable, and harder to maintain.  If
      8 // you plan to use these routines, you should have a good reason, such as solid
      9 // evidence that performance would otherwise suffer, or there being no
     10 // alternative.  You should assume only properties explicitly guaranteed by the
     11 // specifications in this file.  You are almost certainly _not_ writing code
     12 // just for the x86; if you assume x86 semantics, x86 hardware bugs and
     13 // implementations on other archtectures will cause your code to break.  If you
     14 // do not know what you are doing, avoid these routines, and use a Mutex.
     15 //
     16 // It is incorrect to make direct assignments to/from an atomic variable.
     17 // You should use one of the Load or Store routines.  The NoBarrier
     18 // versions are provided when no barriers are needed:
     19 //   NoBarrier_Store()
     20 //   NoBarrier_Load()
     21 // Although there are currently no compiler enforcement, you are encouraged
     22 // to use these.
     23 //
     24 
     25 #ifndef V8_BASE_ATOMICOPS_H_
     26 #define V8_BASE_ATOMICOPS_H_
     27 
     28 #include <stdint.h>
     29 
     30 // Small C++ header which defines implementation specific macros used to
     31 // identify the STL implementation.
     32 // - libc++: captures __config for _LIBCPP_VERSION
     33 // - libstdc++: captures bits/c++config.h for __GLIBCXX__
     34 #include <cstddef>
     35 
     36 #include "src/base/base-export.h"
     37 #include "src/base/build_config.h"
     38 
     39 #if defined(V8_OS_WIN) && defined(V8_HOST_ARCH_64_BIT)
     40 // windows.h #defines this (only on x64). This causes problems because the
     41 // public API also uses MemoryBarrier at the public name for this fence. So, on
     42 // X64, undef it, and call its documented
     43 // (http://msdn.microsoft.com/en-us/library/windows/desktop/ms684208.aspx)
     44 // implementation directly.
     45 #undef MemoryBarrier
     46 #endif
     47 
     48 namespace v8 {
     49 namespace base {
     50 
     51 typedef char Atomic8;
     52 typedef int32_t Atomic32;
     53 #if defined(V8_HOST_ARCH_64_BIT)
     54 // We need to be able to go between Atomic64 and AtomicWord implicitly.  This
     55 // means Atomic64 and AtomicWord should be the same type on 64-bit.
     56 #if defined(__ILP32__)
     57 typedef int64_t Atomic64;
     58 #else
     59 typedef intptr_t Atomic64;
     60 #endif  // defined(__ILP32__)
     61 #endif  // defined(V8_HOST_ARCH_64_BIT)
     62 
     63 // Use AtomicWord for a machine-sized pointer.  It will use the Atomic32 or
     64 // Atomic64 routines below, depending on your architecture.
     65 typedef intptr_t AtomicWord;
     66 
     67 // Atomically execute:
     68 //      result = *ptr;
     69 //      if (*ptr == old_value)
     70 //        *ptr = new_value;
     71 //      return result;
     72 //
     73 // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
     74 // Always return the old value of "*ptr"
     75 //
     76 // This routine implies no memory barriers.
     77 Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
     78                                   Atomic32 old_value,
     79                                   Atomic32 new_value);
     80 
     81 // Atomically store new_value into *ptr, returning the previous value held in
     82 // *ptr.  This routine implies no memory barriers.
     83 Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
     84 
     85 // Atomically increment *ptr by "increment".  Returns the new value of
     86 // *ptr with the increment applied.  This routine implies no memory barriers.
     87 Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
     88 
     89 Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
     90                                  Atomic32 increment);
     91 
     92 // These following lower-level operations are typically useful only to people
     93 // implementing higher-level synchronization operations like spinlocks,
     94 // mutexes, and condition-variables.  They combine CompareAndSwap(), a load, or
     95 // a store with appropriate memory-ordering instructions.  "Acquire" operations
     96 // ensure that no later memory access can be reordered ahead of the operation.
     97 // "Release" operations ensure that no previous memory access can be reordered
     98 // after the operation.  "Barrier" operations have both "Acquire" and "Release"
     99 // semantics.   A MemoryBarrier() has "Barrier" semantics, but does no memory
    100 // access.
    101 Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
    102                                 Atomic32 old_value,
    103                                 Atomic32 new_value);
    104 Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
    105                                 Atomic32 old_value,
    106                                 Atomic32 new_value);
    107 
    108 void MemoryBarrier();
    109 void NoBarrier_Store(volatile Atomic8* ptr, Atomic8 value);
    110 void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
    111 void Release_Store(volatile Atomic32* ptr, Atomic32 value);
    112 
    113 Atomic8 NoBarrier_Load(volatile const Atomic8* ptr);
    114 Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
    115 Atomic32 Acquire_Load(volatile const Atomic32* ptr);
    116 
    117 // 64-bit atomic operations (only available on 64-bit processors).
    118 #ifdef V8_HOST_ARCH_64_BIT
    119 Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
    120                                   Atomic64 old_value,
    121                                   Atomic64 new_value);
    122 Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
    123 Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
    124 Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
    125 
    126 Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
    127                                 Atomic64 old_value,
    128                                 Atomic64 new_value);
    129 Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
    130                                 Atomic64 old_value,
    131                                 Atomic64 new_value);
    132 void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
    133 void Release_Store(volatile Atomic64* ptr, Atomic64 value);
    134 Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
    135 Atomic64 Acquire_Load(volatile const Atomic64* ptr);
    136 #endif  // V8_HOST_ARCH_64_BIT
    137 
    138 }  // namespace base
    139 }  // namespace v8
    140 
    141 #if defined(V8_OS_WIN)
    142 // TODO(hpayer): The MSVC header includes windows.h, which other files end up
    143 //               relying on. Fix this as part of crbug.com/559247.
    144 #include "src/base/atomicops_internals_x86_msvc.h"
    145 #else
    146 #include "src/base/atomicops_internals_portable.h"
    147 #endif
    148 
    149 // On some platforms we need additional declarations to make
    150 // AtomicWord compatible with our other Atomic* types.
    151 #if defined(V8_OS_MACOSX) || defined(V8_OS_OPENBSD) || defined(V8_OS_AIX)
    152 #include "src/base/atomicops_internals_atomicword_compat.h"
    153 #endif
    154 
    155 #endif  // V8_BASE_ATOMICOPS_H_
    156