include/am-atomics.h
author David Anderson <dvander@alliedmods.net>
Mon Aug 26 20:41:58 2013 -0700 (2013-08-26)
changeset 10 0ac292cc1467
parent 0 ba3d4abaf3f1
permissions -rw-r--r--
Add move semantics and fix some bugs.
danderson@0
     1
// vim: set sts=8 ts=2 sw=2 tw=99 et:
danderson@0
     2
//
danderson@0
     3
// Copyright (C) 2013, David Anderson and AlliedModders LLC
danderson@0
     4
// All rights reserved.
danderson@0
     5
// 
danderson@0
     6
// Redistribution and use in source and binary forms, with or without
danderson@0
     7
// modification, are permitted provided that the following conditions are met:
danderson@0
     8
// 
danderson@0
     9
//  * Redistributions of source code must retain the above copyright notice, this
danderson@0
    10
//    list of conditions and the following disclaimer.
danderson@0
    11
//  * Redistributions in binary form must reproduce the above copyright notice,
danderson@0
    12
//    this list of conditions and the following disclaimer in the documentation
danderson@0
    13
//    and/or other materials provided with the distribution.
danderson@0
    14
//  * Neither the name of AlliedModders LLC nor the names of its contributors
danderson@0
    15
//    may be used to endorse or promote products derived from this software
danderson@0
    16
//    without specific prior written permission.
danderson@0
    17
//
danderson@0
    18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
danderson@0
    19
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
danderson@0
    20
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
danderson@0
    21
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
danderson@0
    22
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
danderson@0
    23
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
danderson@0
    24
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
danderson@0
    25
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
danderson@0
    26
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
danderson@0
    27
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
danderson@0
    28
// POSSIBILITY OF SUCH DAMAGE.
danderson@0
    29
danderson@0
    30
#ifndef _include_amtl_atomics_h_
danderson@0
    31
#define _include_amtl_atomics_h_
danderson@0
    32
danderson@0
    33
#include <am-utility.h>
danderson@0
    34
danderson@0
    35
namespace ke {
danderson@0
    36
danderson@0
    37
#if defined(_MSC_VER)
danderson@0
    38
extern "C" {
danderson@0
    39
  long __cdecl _InterlockedIncrement(long volatile *dest);
danderson@0
    40
  long __cdecl _InterlockedDecrement(long volatile *dest);
danderson@0
    41
}
danderson@0
    42
# pragma intrinsic(_InterlockedIncrement)
danderson@0
    43
# pragma intrinsic(_InterlockedDecrement)
danderson@0
    44
#endif
danderson@0
    45
danderson@0
    46
template <size_t Width>
danderson@0
    47
struct AtomicOps;
danderson@0
    48
danderson@0
    49
template <>
danderson@0
    50
struct AtomicOps<4>
danderson@0
    51
{
danderson@0
    52
#if defined(_MSC_VER)
danderson@0
    53
  typedef long Type;
danderson@0
    54
danderson@0
    55
  static Type Increment(Type *ptr) {
danderson@0
    56
    return _InterlockedIncrement(ptr);
danderson@0
    57
  }
danderson@0
    58
  static Type Decrement(Type *ptr) {
danderson@0
    59
    return _InterlockedDecrement(ptr);
danderson@0
    60
  };
danderson@0
    61
#elif defined(__GNUC__)
danderson@0
    62
  typedef int Type;
danderson@0
    63
danderson@0
    64
  // x86/x64 notes: When using GCC < 4.8, this will compile to a spinlock.
danderson@0
    65
  // On 4.8+, or when using Clang, we'll get the more optimal "lock addl"
danderson@0
    66
  // variant.
danderson@0
    67
  static Type Increment(Type *ptr) {
danderson@0
    68
    return __sync_add_and_fetch(ptr, 1);
danderson@0
    69
  }
danderson@0
    70
  static Type Decrement(Type *ptr) {
danderson@0
    71
    return __sync_sub_and_fetch(ptr, 1);
danderson@0
    72
  }
danderson@0
    73
#endif
danderson@0
    74
};
danderson@0
    75
danderson@0
    76
class AtomicRefCount
danderson@0
    77
{
danderson@0
    78
  typedef AtomicOps<sizeof(uintptr_t)> Ops;
danderson@0
    79
danderson@0
    80
 public:
danderson@0
    81
  AtomicRefCount(uintptr_t value)
danderson@0
    82
   : value_(value)
danderson@0
    83
  {
danderson@0
    84
  }
danderson@0
    85
danderson@0
    86
  void increment() {
danderson@0
    87
    Ops::Increment(&value_);
danderson@0
    88
  }
danderson@0
    89
danderson@0
    90
  // Return false if all references are gone.
danderson@0
    91
  bool decrement() {
danderson@0
    92
    return Ops::Decrement(&value_) != 0;
danderson@0
    93
  }
danderson@0
    94
danderson@0
    95
 private:
danderson@0
    96
  Ops::Type value_;
danderson@0
    97
};
danderson@0
    98
danderson@0
    99
}
danderson@0
   100
danderson@0
   101
#endif // _include_amtl_atomics_h_
danderson@0
   102