mirror of
https://github.com/FirebirdSQL/firebird.git
synced 2025-01-23 18:03:04 +01:00
Fixed posix build
This commit is contained in:
parent
6959853feb
commit
09ecefd320
@ -51,13 +51,13 @@ typedef SLONG AtomicType;
|
||||
#if defined(WIN_NT)
|
||||
|
||||
#include <windows.h>
|
||||
#include <intrin.h>
|
||||
#include <intrin.h>
|
||||
|
||||
namespace Firebird {
|
||||
|
||||
// Win95 is not supported unless compiled conditionally and
|
||||
// redirected to generic version below
|
||||
class AtomicCounter
|
||||
class PlatformAtomicCounter
|
||||
{
|
||||
public:
|
||||
#ifdef _WIN64
|
||||
@ -66,8 +66,8 @@ public:
|
||||
typedef LONG counter_type;
|
||||
#endif
|
||||
|
||||
explicit AtomicCounter(counter_type val = 0) : counter(val) {}
|
||||
~AtomicCounter() {}
|
||||
explicit PlatformAtomicCounter(counter_type val = 0) : counter(val) {}
|
||||
~PlatformAtomicCounter() {}
|
||||
|
||||
// returns old value
|
||||
counter_type exchangeAdd(counter_type val)
|
||||
@ -97,102 +97,14 @@ public:
|
||||
#endif
|
||||
}
|
||||
|
||||
// platform-independent code
|
||||
|
||||
counter_type value() const { return counter; }
|
||||
|
||||
// returns old value
|
||||
counter_type exchangeBitAnd(counter_type val)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
volatile counter_type oldVal = counter;
|
||||
|
||||
if (compareExchange(oldVal, oldVal & val))
|
||||
return oldVal;
|
||||
}
|
||||
}
|
||||
|
||||
// returns old value
|
||||
counter_type exchangeBitOr(counter_type val)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
volatile counter_type oldVal = counter;
|
||||
|
||||
if (compareExchange(oldVal, oldVal | val))
|
||||
return oldVal;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// returns old value
|
||||
counter_type exchangeGreater(counter_type val)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
volatile counter_type oldVal = counter;
|
||||
|
||||
if (oldVal >= val)
|
||||
return oldVal;
|
||||
|
||||
if (compareExchange(oldVal, val))
|
||||
return oldVal;
|
||||
}
|
||||
}
|
||||
|
||||
operator counter_type () const
|
||||
{
|
||||
return value();
|
||||
}
|
||||
|
||||
void operator =(counter_type val)
|
||||
{
|
||||
setValue(val);
|
||||
}
|
||||
|
||||
// returns new value !
|
||||
counter_type operator ++()
|
||||
{
|
||||
return exchangeAdd(1) + 1;
|
||||
}
|
||||
|
||||
// returns new value !
|
||||
counter_type operator --()
|
||||
{
|
||||
return exchangeAdd(-1) - 1;
|
||||
}
|
||||
|
||||
void operator +=(counter_type val)
|
||||
{
|
||||
exchangeAdd(val);
|
||||
}
|
||||
|
||||
void operator -=(counter_type val)
|
||||
{
|
||||
exchangeAdd(-val);
|
||||
}
|
||||
|
||||
void operator &=(counter_type val)
|
||||
{
|
||||
exchangeBitAnd(val);
|
||||
}
|
||||
|
||||
void operator |=(counter_type val)
|
||||
{
|
||||
exchangeBitOr(val);
|
||||
}
|
||||
|
||||
private:
|
||||
#if defined(MINGW)
|
||||
counter_type counter;
|
||||
#else
|
||||
volatile counter_type counter;
|
||||
protected:
|
||||
#ifndef MINGW
|
||||
volatile
|
||||
#endif
|
||||
counter_type counter;
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
class AtomicPointer
|
||||
{
|
||||
@ -226,7 +138,7 @@ public:
|
||||
T* operator ->() const { return value(); }
|
||||
void operator =(T* val) { setValue(val); }
|
||||
|
||||
private:
|
||||
protected:
|
||||
volatile T* pointer;
|
||||
};
|
||||
|
||||
@ -243,42 +155,26 @@ private:
|
||||
namespace Firebird {
|
||||
|
||||
// AIX version - uses AIX atomic API
|
||||
class AtomicCounter
|
||||
class PlatformAtomicCounter
|
||||
{
|
||||
private:
|
||||
public:
|
||||
typedef long counter_type;
|
||||
|
||||
public:
|
||||
explicit AtomicCounter(AtomicType value = 0) : counter(value) {}
|
||||
~AtomicCounter() {}
|
||||
explicit PlatformAtomicCounter(AtomicType value = 0) : counter(value) {}
|
||||
~PlatformAtomicCounter() {}
|
||||
|
||||
counter_type exchangeAdd(AtomicType value)
|
||||
{
|
||||
return fetch_and_addlp(&counter, static_cast<unsigned long>(value));
|
||||
}
|
||||
|
||||
counter_type operator +=(AtomicType value)
|
||||
{
|
||||
return exchangeAdd(value) + value;
|
||||
}
|
||||
|
||||
counter_type operator -=(AtomicType value)
|
||||
{
|
||||
return exchangeAdd(-value) - value;
|
||||
}
|
||||
|
||||
counter_type operator ++()
|
||||
{
|
||||
return exchangeAdd(1) + 1;
|
||||
}
|
||||
|
||||
counter_type operator --()
|
||||
{
|
||||
return exchangeAdd(-1) - 1;
|
||||
}
|
||||
|
||||
counter_type value() const { return counter; }
|
||||
|
||||
bool compareExchange(counter_type oldVal, counter_type newVal)
|
||||
{
|
||||
return compare_and_swaplp(&counter, &oldVal, newVal);
|
||||
}
|
||||
|
||||
counter_type setValue(AtomicType val)
|
||||
{
|
||||
counter_type old;
|
||||
@ -289,7 +185,7 @@ public:
|
||||
return old;
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
counter_type counter;
|
||||
};
|
||||
|
||||
@ -302,13 +198,13 @@ private:
|
||||
namespace Firebird {
|
||||
|
||||
// HPUX version - uses atomic API library
|
||||
class AtomicCounter
|
||||
class PlatformAtomicCounter
|
||||
{
|
||||
public:
|
||||
typedef uint64_t counter_type;
|
||||
|
||||
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
||||
~AtomicCounter() {}
|
||||
explicit PlatformAtomicCounter(counter_type value = 0) : counter(value) {}
|
||||
~PlatformAtomicCounter() {}
|
||||
|
||||
counter_type exchangeAdd(counter_type value)
|
||||
{
|
||||
@ -321,34 +217,17 @@ public:
|
||||
return old;
|
||||
}
|
||||
|
||||
counter_type operator +=(counter_type value)
|
||||
bool compareExchange(counter_type oldVal, counter_type newVal)
|
||||
{
|
||||
return exchangeAdd(value) + value;
|
||||
return atomic_cas_64(&counter, oldVal, newVal) == oldVal;
|
||||
}
|
||||
|
||||
counter_type operator -=(counter_type value)
|
||||
{
|
||||
return exchangeAdd(-value) - value;
|
||||
}
|
||||
|
||||
counter_type operator ++()
|
||||
{
|
||||
return atomic_inc_64(&counter) + 1;
|
||||
}
|
||||
|
||||
counter_type operator --()
|
||||
{
|
||||
return atomic_dec_64(&counter) - 1;
|
||||
}
|
||||
|
||||
counter_type value() const { return counter; }
|
||||
|
||||
counter_type setValue(counter_type val)
|
||||
{
|
||||
return atomic_swap_64(&counter, val);
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
volatile counter_type counter;
|
||||
};
|
||||
|
||||
@ -361,49 +240,32 @@ private:
|
||||
namespace Firebird {
|
||||
|
||||
// Solaris version - uses Solaris atomic_ops
|
||||
class AtomicCounter
|
||||
class PlatformAtomicCounter
|
||||
{
|
||||
public:
|
||||
typedef volatile ulong_t counter_type;
|
||||
typedef ulong_t counter_type;
|
||||
typedef long delta_type;
|
||||
|
||||
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
||||
~AtomicCounter() {}
|
||||
explicit PlatformAtomicCounter(counter_type value = 0) : counter(value) {}
|
||||
~PlatformAtomicCounter() {}
|
||||
|
||||
counter_type exchangeAdd(delta_type value)
|
||||
{
|
||||
return atomic_add_long_nv(&counter, value);
|
||||
}
|
||||
|
||||
counter_type operator +=(delta_type value)
|
||||
{
|
||||
return exchangeAdd(value) + value;
|
||||
}
|
||||
|
||||
counter_type operator -=(delta_type value)
|
||||
{
|
||||
return exchangeAdd(-value) - value;
|
||||
}
|
||||
|
||||
counter_type operator ++()
|
||||
{
|
||||
return atomic_inc_ulong_nv(&counter);
|
||||
}
|
||||
|
||||
counter_type operator --()
|
||||
{
|
||||
return atomic_dec_ulong_nv(&counter);
|
||||
}
|
||||
|
||||
counter_type value() const { return counter; }
|
||||
|
||||
counter_type setValue(delta_type value)
|
||||
{
|
||||
return atomic_swap_ulong(&counter, value);
|
||||
}
|
||||
|
||||
private:
|
||||
counter_type counter;
|
||||
bool compareExchange(counter_type oldVal, counter_type newVal)
|
||||
{
|
||||
TODO: implement method for Solaris
|
||||
}
|
||||
|
||||
protected:
|
||||
volatile counter_type counter;
|
||||
};
|
||||
|
||||
} // namespace Firebird
|
||||
@ -426,42 +288,20 @@ extern "C"
|
||||
|
||||
namespace Firebird {
|
||||
|
||||
class AtomicCounter
|
||||
class PlatformAtomicCounter
|
||||
{
|
||||
public:
|
||||
typedef uint_t counter_type;
|
||||
typedef int delta_type;
|
||||
|
||||
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
||||
~AtomicCounter() {}
|
||||
explicit PlatformAtomicCounter(counter_type value = 0) : counter(value) {}
|
||||
~PlatformAtomicCounter() {}
|
||||
|
||||
counter_type exchangeAdd(delta_type value)
|
||||
{
|
||||
return fetch_and_add_il(&counter, value);
|
||||
}
|
||||
|
||||
counter_type operator +=(delta_type value)
|
||||
{
|
||||
return exchangeAdd(value) + value;
|
||||
}
|
||||
|
||||
counter_type operator -=(delta_type value)
|
||||
{
|
||||
return exchangeAdd(-value) - value;
|
||||
}
|
||||
|
||||
counter_type operator ++()
|
||||
{
|
||||
return exchangeAdd(1) + 1;
|
||||
}
|
||||
|
||||
counter_type operator --()
|
||||
{
|
||||
return exchangeAdd(-1) - 1;
|
||||
}
|
||||
|
||||
counter_type value() const { return counter; }
|
||||
|
||||
counter_type setValue(delta_type value)
|
||||
{
|
||||
counter_type old;
|
||||
@ -472,7 +312,12 @@ public:
|
||||
return old;
|
||||
}
|
||||
|
||||
private:
|
||||
bool compareExchange(counter_type oldVal, counter_type newVal)
|
||||
{
|
||||
return compare_and_swap_il(&counter, &oldVal, newVal);
|
||||
}
|
||||
|
||||
protected:
|
||||
counter_type counter;
|
||||
};
|
||||
|
||||
@ -483,65 +328,51 @@ private:
|
||||
namespace Firebird {
|
||||
|
||||
// Assembler version for x86 and AMD64. Note it uses xaddl thus it requires i486
|
||||
class AtomicCounter
|
||||
class PlatformAtomicCounter
|
||||
{
|
||||
public:
|
||||
typedef AtomicType counter_type;
|
||||
|
||||
explicit AtomicCounter(AtomicType value = 0) : counter(value) {}
|
||||
~AtomicCounter() {}
|
||||
explicit PlatformAtomicCounter(AtomicType value = 0) : counter(value) {}
|
||||
~PlatformAtomicCounter() {}
|
||||
|
||||
AtomicType exchangeAdd(AtomicType value)
|
||||
{
|
||||
register counter_type result;
|
||||
__asm __volatile (
|
||||
#if SIZEOF_VOID_P == 8
|
||||
"lock; xaddq %0, %1"
|
||||
#else
|
||||
"lock; xaddl %0, %1"
|
||||
#endif
|
||||
: "=r" (result), "=m" (counter)
|
||||
: "0" (value), "m" (counter));
|
||||
|
||||
__asm __volatile ("lock; xadd %0, %1"
|
||||
: "=r" (result), "=m" (counter)
|
||||
: "0" (value), "m" (counter)
|
||||
: "cc", "memory");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
AtomicType operator +=(AtomicType value)
|
||||
{
|
||||
return exchangeAdd(value) + value;
|
||||
}
|
||||
|
||||
AtomicType operator -=(AtomicType value)
|
||||
{
|
||||
return exchangeAdd(-value) - value;
|
||||
}
|
||||
|
||||
AtomicType operator ++()
|
||||
{
|
||||
return exchangeAdd(1) + 1;
|
||||
}
|
||||
|
||||
AtomicType operator --()
|
||||
{
|
||||
return exchangeAdd(-1) - 1;
|
||||
}
|
||||
|
||||
AtomicType value() const { return counter; }
|
||||
|
||||
AtomicType setValue(AtomicType val)
|
||||
{
|
||||
register counter_type result;
|
||||
__asm __volatile (
|
||||
#if SIZEOF_VOID_P == 8
|
||||
"lock; xchgq %0, %1"
|
||||
#else
|
||||
"lock; xchgl %0, %1"
|
||||
#endif
|
||||
: "=r" (result), "=m" (counter)
|
||||
: "0" (val), "m" (counter));
|
||||
|
||||
__asm __volatile ("lock; xchg %0, %1"
|
||||
: "=r" (result), "=m" (counter)
|
||||
: "0" (val), "m" (counter)
|
||||
: "cc", "memory");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
bool compareExchange(counter_type oldVal, counter_type newVal)
|
||||
{
|
||||
register char ret;
|
||||
|
||||
__asm __volatile ("lock; cmpxchg %2, %1 ; sete %0"
|
||||
: "=r" (ret), "+m" (counter)
|
||||
: "r" (newVal), "a" (oldVal)
|
||||
: "cc", "memory");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected:
|
||||
volatile counter_type counter;
|
||||
};
|
||||
|
||||
@ -563,20 +394,14 @@ extern "C" {
|
||||
|
||||
namespace Firebird {
|
||||
|
||||
class AtomicCounter
|
||||
class PlatformAtomicCounter
|
||||
{
|
||||
public:
|
||||
typedef AO_t counter_type; // AO_t should match maximum native in type
|
||||
|
||||
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
||||
~AtomicCounter() {}
|
||||
explicit PlatformAtomicCounter(counter_type value = 0) : counter(value) {}
|
||||
~PlatformAtomicCounter() {}
|
||||
|
||||
/*
|
||||
counter_type exchangeAdd(counter_type value)
|
||||
{
|
||||
return AO_fetch_and_add_full(&counter, value);
|
||||
}
|
||||
*/
|
||||
AtomicType exchangeAdd(AtomicType value)
|
||||
{
|
||||
#ifdef DEV_BUILD
|
||||
@ -592,28 +417,6 @@ public:
|
||||
return AtomicType(old);
|
||||
}
|
||||
|
||||
AtomicType operator +=(AtomicType value)
|
||||
{
|
||||
return exchangeAdd(value) + value;
|
||||
}
|
||||
|
||||
AtomicType operator -=(AtomicType value)
|
||||
{
|
||||
return exchangeAdd(-value) - value;
|
||||
}
|
||||
|
||||
AtomicType operator ++()
|
||||
{
|
||||
return exchangeAdd(1) + 1;
|
||||
}
|
||||
|
||||
AtomicType operator --()
|
||||
{
|
||||
return exchangeAdd(-1) - 1;
|
||||
}
|
||||
|
||||
AtomicType value() const { return counter; }
|
||||
|
||||
AtomicType setValue(AtomicType val)
|
||||
{
|
||||
#ifdef DEV_BUILD
|
||||
@ -629,7 +432,12 @@ public:
|
||||
return AtomicType(old);
|
||||
}
|
||||
|
||||
private:
|
||||
bool compareExchange(counter_type oldVal, counter_type newVal)
|
||||
{
|
||||
return AO_compare_and_swap_full(&counter, oldVal, newVal);
|
||||
}
|
||||
|
||||
protected:
|
||||
counter_type counter;
|
||||
};
|
||||
|
||||
@ -641,4 +449,101 @@ private:
|
||||
|
||||
#endif
|
||||
|
||||
// platform-independent code
|
||||
|
||||
namespace Firebird {
|
||||
|
||||
class AtomicCounter : public PlatformAtomicCounter
|
||||
{
|
||||
public:
|
||||
explicit AtomicCounter(counter_type value = 0) : PlatformAtomicCounter(value) {}
|
||||
~AtomicCounter() {}
|
||||
|
||||
counter_type value() const { return counter; }
|
||||
|
||||
// returns old value
|
||||
counter_type exchangeBitAnd(counter_type val)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
volatile counter_type oldVal = counter;
|
||||
|
||||
if (compareExchange(oldVal, oldVal & val))
|
||||
return oldVal;
|
||||
}
|
||||
}
|
||||
|
||||
// returns old value
|
||||
counter_type exchangeBitOr(counter_type val)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
volatile counter_type oldVal = counter;
|
||||
|
||||
if (compareExchange(oldVal, oldVal | val))
|
||||
return oldVal;
|
||||
}
|
||||
}
|
||||
|
||||
// returns old value
|
||||
counter_type exchangeGreater(counter_type val)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
volatile counter_type oldVal = counter;
|
||||
|
||||
if (oldVal >= val)
|
||||
return oldVal;
|
||||
|
||||
if (compareExchange(oldVal, val))
|
||||
return oldVal;
|
||||
}
|
||||
}
|
||||
|
||||
operator counter_type () const
|
||||
{
|
||||
return value();
|
||||
}
|
||||
|
||||
void operator =(counter_type val)
|
||||
{
|
||||
setValue(val);
|
||||
}
|
||||
|
||||
// returns new value !
|
||||
counter_type operator ++()
|
||||
{
|
||||
return exchangeAdd(1) + 1;
|
||||
}
|
||||
|
||||
// returns new value !
|
||||
counter_type operator --()
|
||||
{
|
||||
return exchangeAdd(-1) - 1;
|
||||
}
|
||||
|
||||
void operator +=(counter_type val)
|
||||
{
|
||||
exchangeAdd(val);
|
||||
}
|
||||
|
||||
void operator -=(counter_type val)
|
||||
{
|
||||
exchangeAdd(-val);
|
||||
}
|
||||
|
||||
void operator &=(counter_type val)
|
||||
{
|
||||
exchangeBitAnd(val);
|
||||
}
|
||||
|
||||
void operator |=(counter_type val)
|
||||
{
|
||||
exchangeBitOr(val);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} // namespace Firebird
|
||||
|
||||
#endif // CLASSES_FB_ATOMIC_H
|
||||
|
Loading…
Reference in New Issue
Block a user