1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
// $Id$
//
// Copyright (C) 2007 Tim Blechmann & Thomas Grill
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// $Revision$
// $LastChangedRevision$
// $LastChangedDate$
// $LastChangedBy$
#ifndef __LOCKFREE_ATOMIC_PTR_HPP
#define __LOCKFREE_ATOMIC_PTR_HPP
#include "cas.hpp"
#include "branch_hints.hpp"
#include <cstddef>
namespace lockfree
{
using std::size_t;
template <class T>
class atomic_ptr
{
public:
atomic_ptr() {}
atomic_ptr(const atomic_ptr &p): ptr(p.ptr),tag(p.tag) {}
atomic_ptr(T *p,size_t t = 0): ptr(p),tag(t) {}
/** atomic set operation */
inline atomic_ptr &operator =(const atomic_ptr &p)
{
for (;;)
{
atomic_ptr current(ptr, tag);
if(likely(CAS(current, p)))
return *this;
}
}
inline atomic_ptr &operator()(T *p,size_t t)
{
return operator=(atomic_ptr(p, t) );
}
inline bool operator ==(const atomic_ptr &p) const { return ptr == p.ptr && tag == p.tag; }
inline bool operator !=(const atomic_ptr &p) const { return !operator ==(p); }
inline T * getPtr() const { return ptr; }
inline void setPtr(T * p) { ptr = p; }
inline size_t getTag() const { return tag; }
inline void setTag(size_t t) { tag = t; }
inline size_t incTag() { return ++tag; }
inline bool CAS(const atomic_ptr &oldval,const atomic_ptr &newval)
{
return lockfree::CAS2(this,oldval.ptr,oldval.tag,newval.ptr,newval.tag);
}
inline bool CAS(const atomic_ptr &oldval,T *newptr)
{
return lockfree::CAS2(this,oldval.ptr,oldval.tag,newptr,oldval.tag+1);
}
inline bool CAS(const T *oldptr,size_t oldtag,T *newptr)
{
return lockfree::CAS2(this,oldptr,oldtag,newptr,oldtag+1);
}
protected:
T * volatile ptr;
size_t volatile tag;
};
} // namespace
#endif /* __LOCKFREE_ATOMIC_PTR_HPP */
|