include/am-refcounting-threadsafe.h
author David Anderson <dvander@alliedmods.net>
Sat Aug 24 02:36:11 2013 -0700 (2013-08-24)
changeset 9 4cd655ff4c2c
parent 0 ba3d4abaf3f1
child 18 9b30f80f6e49
permissions -rw-r--r--
Fix HashTable/HashMap bugs, and add a clear() method.
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
danderson@0
    38
template <typename T>
danderson@0
    39
class RefcountedThreadsafe
danderson@0
    40
{
danderson@0
    41
  public:
danderson@0
    42
    RefcountedThreadsafe()
danderson@0
    43
     : refcount_(1)
danderson@0
    44
    {
danderson@0
    45
    }
danderson@0
    46
danderson@0
    47
    void AddRef() {
danderson@0
    48
      refcount_.increment();
danderson@0
    49
    }
danderson@0
    50
    bool Release() {
danderson@0
    51
        if (!refcount_.decrement()) {
danderson@0
    52
            delete static_cast<T *>(this);
danderson@0
    53
            return false;
danderson@0
    54
        }
danderson@0
    55
        return true;
danderson@0
    56
    }
danderson@0
    57
danderson@0
    58
  protected:
danderson@0
    59
    ~RefcountedThreadsafe() {
danderson@0
    60
    }
danderson@0
    61
danderson@0
    62
  private:
danderson@0
    63
    AtomicRefCount refcount_;
danderson@0
    64
};
danderson@0
    65
danderson@0
    66
} // namespace ke
danderson@0
    67
danderson@0
    68
#endif // _include_amtl_ts_refcounting_h_