2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* safe_refcount.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifndef SAFE_REFCOUNT_H
|
|
|
|
#define SAFE_REFCOUNT_H
|
|
|
|
|
|
|
|
#include "os/mutex.h"
|
|
|
|
/* x86/x86_64 GCC */
|
|
|
|
|
|
|
|
#include "platform_config.h"
|
|
|
|
|
|
|
|
#ifdef NO_THREADS
|
|
|
|
|
|
|
|
struct SafeRefCount {
|
|
|
|
|
|
|
|
int count;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// destroy() is called when weak_count_ drops to zero.
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool ref() { //true on success
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (count == 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
count++;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int refval() { //true on success
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (count == 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
return 0;
|
|
|
|
count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unref() { // true if must be disposed of
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (count > 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
count--;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return count == 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
long get() const { // nothrow
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return static_cast<int const volatile &>(count);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void init(int p_value = 1) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
count = p_value;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#if defined(PLATFORM_REFCOUNT)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#include "platform_refcount.h"
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#define REFCOUNT_T volatile int
|
2017-03-18 23:36:26 +00:00
|
|
|
#define REFCOUNT_GET_T int const volatile &
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static inline int atomic_conditional_increment(volatile int *pw) {
|
2014-02-10 01:10:30 +00:00
|
|
|
// int rv = *pw;
|
|
|
|
// if( rv != 0 ) ++*pw;
|
|
|
|
// return rv;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int rv, tmp;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
__asm__(
|
|
|
|
"movl %0, %%eax\n\t"
|
|
|
|
"0:\n\t"
|
|
|
|
"test %%eax, %%eax\n\t"
|
|
|
|
"je 1f\n\t"
|
|
|
|
"movl %%eax, %2\n\t"
|
|
|
|
"incl %2\n\t"
|
|
|
|
"lock\n\t"
|
|
|
|
"cmpxchgl %2, %0\n\t"
|
|
|
|
"jne 0b\n\t"
|
|
|
|
"1:"
|
|
|
|
: "=m"(*pw), "=&a"(rv), "=&r"(tmp)
|
|
|
|
: // outputs (%0, %1, %2)
|
|
|
|
"m"(*pw)
|
|
|
|
: // input (%3)
|
|
|
|
"cc" // clobbers
|
2017-12-11 14:31:49 +00:00
|
|
|
);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static inline int atomic_decrement(volatile int *pw) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
// return --(*pw);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
unsigned char rv;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
__asm__(
|
|
|
|
"lock\n\t"
|
|
|
|
"decl %0\n\t"
|
|
|
|
"setne %1"
|
|
|
|
: "=m"(*pw), "=qm"(rv)
|
|
|
|
: "m"(*pw)
|
|
|
|
: "memory");
|
2014-02-10 01:10:30 +00:00
|
|
|
return static_cast<int>(rv);
|
|
|
|
}
|
|
|
|
|
2017-12-11 14:31:49 +00:00
|
|
|
/* PowerPC32/64 GCC */
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#elif (defined(__GNUC__)) && (defined(__powerpc__) || defined(__ppc__))
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#define REFCOUNT_T int
|
2017-03-18 23:36:26 +00:00
|
|
|
#define REFCOUNT_GET_T int const volatile &
|
|
|
|
|
|
|
|
inline int atomic_conditional_increment(int *pw) {
|
|
|
|
// if( *pw != 0 ) ++*pw;
|
|
|
|
// return *pw;
|
|
|
|
|
|
|
|
int rv;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
__asm__(
|
|
|
|
"0:\n\t"
|
|
|
|
"lwarx %1, 0, %2\n\t"
|
|
|
|
"cmpwi %1, 0\n\t"
|
|
|
|
"beq 1f\n\t"
|
|
|
|
"addi %1, %1, 1\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"stwcx. %1, 0, %2\n\t"
|
|
|
|
"bne- 0b"
|
|
|
|
:
|
|
|
|
|
|
|
|
"=m"(*pw), "=&b"(rv)
|
|
|
|
: "r"(pw), "m"(*pw)
|
|
|
|
: "cc");
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
inline int atomic_decrement(int *pw) {
|
|
|
|
// return --*pw;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int rv;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
__asm__ __volatile__(
|
|
|
|
"sync\n\t"
|
|
|
|
"0:\n\t"
|
|
|
|
"lwarx %1, 0, %2\n\t"
|
|
|
|
"addi %1, %1, -1\n\t"
|
|
|
|
"stwcx. %1, 0, %2\n\t"
|
|
|
|
"bne- 0b\n\t"
|
|
|
|
"isync"
|
|
|
|
:
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
"=m"(*pw), "=&b"(rv)
|
|
|
|
: "r"(pw), "m"(*pw)
|
|
|
|
: "memory", "cc");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return rv;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:31:49 +00:00
|
|
|
/* CW ARM */
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#elif defined(__GNUC__) && (defined(__arm__))
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#define REFCOUNT_T int
|
2017-03-18 23:36:26 +00:00
|
|
|
#define REFCOUNT_GET_T int const volatile &
|
|
|
|
|
|
|
|
inline int atomic_conditional_increment(volatile int *v) {
|
|
|
|
int t;
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
__asm__ __volatile__(
|
|
|
|
"1: ldrex %0, [%2] \n"
|
|
|
|
" cmp %0, #0 \n"
|
|
|
|
" beq 2f \n"
|
|
|
|
" add %0, %0, #1 \n"
|
|
|
|
"2: \n"
|
|
|
|
" strex %1, %0, [%2] \n"
|
|
|
|
" cmp %1, #0 \n"
|
|
|
|
" bne 1b \n"
|
|
|
|
|
|
|
|
: "=&r"(t), "=&r"(tmp)
|
|
|
|
: "r"(v)
|
|
|
|
: "cc", "memory");
|
|
|
|
|
|
|
|
return t;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
inline int atomic_decrement(volatile int *v) {
|
|
|
|
int t;
|
|
|
|
int tmp;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
__asm__ __volatile__(
|
|
|
|
"1: ldrex %0, [%2] \n"
|
|
|
|
" add %0, %0, #-1 \n"
|
|
|
|
" strex %1, %0, [%2] \n"
|
|
|
|
" cmp %1, #0 \n"
|
|
|
|
" bne 1b \n"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
: "=&r"(t), "=&r"(tmp)
|
|
|
|
: "r"(v)
|
|
|
|
: "cc", "memory");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return t;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:31:49 +00:00
|
|
|
/* CW PPC */
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#elif (defined(__MWERKS__)) && defined(__POWERPC__)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
inline long atomic_conditional_increment(register long *pw) {
|
|
|
|
register int a;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
asm {
|
2014-02-10 01:10:30 +00:00
|
|
|
loop:
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
lwarx a, 0, pw
|
|
|
|
cmpwi a, 0
|
|
|
|
beq store
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
addi a, a, 1
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
store:
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
stwcx. a, 0, pw
|
|
|
|
bne- loop
|
2017-03-18 23:36:26 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return a;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
inline long atomic_decrement(register long *pw) {
|
|
|
|
register int a;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
asm {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
sync
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
loop:
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
lwarx a, 0, pw
|
|
|
|
addi a, a, -1
|
|
|
|
stwcx. a, 0, pw
|
|
|
|
bne- loop
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
isync
|
2017-03-18 23:36:26 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return a;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:31:49 +00:00
|
|
|
/* Any Windows (MSVC) */
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#elif defined(_MSC_VER)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
// made functions to not pollute namespace..
|
|
|
|
|
|
|
|
#define REFCOUNT_T long
|
2017-03-18 23:36:26 +00:00
|
|
|
#define REFCOUNT_GET_T long const volatile &
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
long atomic_conditional_increment(register long *pw);
|
|
|
|
long atomic_decrement(register long *pw);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#if 0
|
2017-03-18 23:36:26 +00:00
|
|
|
#elif defined(__GNUC__) && defined(ARMV6_ENABLED)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#error This platform cannot use safe refcount, compile with NO_THREADS or implement it.
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct SafeRefCount {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
REFCOUNT_T count;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// destroy() is called when weak_count_ drops to zero.
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool ref() { //true on success
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return atomic_conditional_increment(&count) != 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int refval() { //true on success
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return atomic_conditional_increment(&count);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool unref() { // true if must be disposed of
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (atomic_decrement(&count) == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
long get() const { // nothrow
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return static_cast<REFCOUNT_GET_T>(count);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void init(int p_value = 1) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
count = p_value;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // no thread safe
|
|
|
|
|
2016-03-08 23:00:52 +00:00
|
|
|
#endif
|