2013-07-16 17:11:14 +00:00
|
|
|
/*
|
2013-07-19 17:29:33 +00:00
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
2013-07-16 17:11:14 +00:00
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
2013-07-19 17:29:33 +00:00
|
|
|
Author: Leonardo de Moura
|
2013-07-16 17:11:14 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// Goodies for reference counting
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#ifdef LEAN_THREAD_UNSAFE_REF_COUNT
|
|
|
|
#define MK_LEAN_RC() \
|
2013-07-22 11:03:46 +00:00
|
|
|
private: \
|
2013-07-16 17:11:14 +00:00
|
|
|
unsigned m_rc; \
|
2013-07-22 11:03:46 +00:00
|
|
|
public: \
|
2013-07-16 17:11:14 +00:00
|
|
|
unsigned get_rc() const { return m_rc; } \
|
|
|
|
void inc_ref() { m_rc++; } \
|
|
|
|
bool dec_ref_core() { lean_assert(get_rc() > 0); m_rc--; return m_rc == 0; } \
|
|
|
|
void dec_ref() { if (dec_ref_core()) dealloc(); }
|
|
|
|
#else
|
|
|
|
#include <atomic>
|
|
|
|
#define MK_LEAN_RC() \
|
2013-07-22 11:03:46 +00:00
|
|
|
private: \
|
2013-07-16 17:11:14 +00:00
|
|
|
std::atomic<unsigned> m_rc; \
|
2013-07-22 11:03:46 +00:00
|
|
|
public: \
|
2013-07-16 17:11:14 +00:00
|
|
|
unsigned get_rc() const { return std::atomic_load(&m_rc); } \
|
|
|
|
void inc_ref() { std::atomic_fetch_add_explicit(&m_rc, 1u, std::memory_order_relaxed); } \
|
|
|
|
bool dec_ref_core() { lean_assert(get_rc() > 0); return std::atomic_fetch_sub_explicit(&m_rc, 1u, std::memory_order_relaxed) == 1u; } \
|
|
|
|
void dec_ref() { if (dec_ref_core()) dealloc(); }
|
|
|
|
#endif
|