include/am-refcounting-threadsafe.h
author David Anderson <dvander@alliedmods.net>
Fri Sep 06 13:54:31 2013 -0700 (2013-09-06)
changeset 18 9b30f80f6e49
parent 0 ba3d4abaf3f1
permissions -rw-r--r--
Eliminate Newborn/NoAddRef (bug 5907, r=ds).
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_ts_refcounting_h_
danderson@0
    31
#define _include_amtl_ts_refcounting_h_
danderson@0
    32
danderson@0
    33
#include <am-refcounting.h>
danderson@0
    34
#include <am-atomics.h>
danderson@0
    35
danderson@0
    36
namespace ke {
danderson@0
    37
dvander@18
    38
// See the comment above Refcounted<T> for more information. This class is
dvander@18
    39
// identical, except changing the reference count is guaranteed to be atomic
dvander@18
    40
// with respect to other threads changing the reference count.
danderson@0
    41
template <typename T>
danderson@0
    42
class RefcountedThreadsafe
danderson@0
    43
{
danderson@0
    44
  public:
danderson@0
    45
    RefcountedThreadsafe()
dvander@18
    46
     : refcount_(0)
danderson@0
    47
    {
danderson@0
    48
    }
danderson@0
    49
danderson@0
    50
    void AddRef() {
danderson@0
    51
      refcount_.increment();
danderson@0
    52
    }
danderson@0
    53
    bool Release() {
danderson@0
    54
        if (!refcount_.decrement()) {
danderson@0
    55
            delete static_cast<T *>(this);
danderson@0
    56
            return false;
danderson@0
    57
        }
danderson@0
    58
        return true;
danderson@0
    59
    }
danderson@0
    60
danderson@0
    61
  protected:
danderson@0
    62
    ~RefcountedThreadsafe() {
danderson@0
    63
    }
danderson@0
    64
danderson@0
    65
  private:
danderson@0
    66
    AtomicRefCount refcount_;
danderson@0
    67
};
danderson@0
    68
danderson@0
    69
} // namespace ke
danderson@0
    70
danderson@0
    71
#endif // _include_amtl_ts_refcounting_h_