2004-03-26 00:12:50 +01:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: fb_atomic.h
|
|
|
|
* DESCRIPTION: Atomic counters
|
|
|
|
*
|
2004-06-30 03:26:40 +02:00
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's Public License Version 1.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing rights
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Nickolay Samofatov
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
2004-03-26 00:12:50 +01:00
|
|
|
*
|
2004-06-30 03:26:40 +02:00
|
|
|
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
|
|
|
|
* and all contributors signed below.
|
2004-03-26 00:12:50 +01:00
|
|
|
*
|
2004-06-30 03:26:40 +02:00
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
2008-12-05 01:56:15 +01:00
|
|
|
*
|
2004-03-26 00:12:50 +01:00
|
|
|
*/
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2004-03-28 11:10:30 +02:00
|
|
|
#ifndef CLASSES_FB_ATOMIC_H
|
|
|
|
#define CLASSES_FB_ATOMIC_H
|
2004-03-26 00:12:50 +01:00
|
|
|
|
2009-09-22 11:08:57 +02:00
|
|
|
// Here we define an atomic type, which is to be used in interface for all OS
|
|
|
|
#if SIZEOF_VOID_P == 8
|
|
|
|
typedef SINT64 AtomicType;
|
|
|
|
#else
|
|
|
|
typedef SLONG AtomicType;
|
|
|
|
#endif
|
|
|
|
|
2011-04-28 16:35:58 +02:00
|
|
|
// IMPORTANT !
|
|
|
|
// Most of the interlocked functions returns "old" value of operand (except of
|
|
|
|
// InterlockedIncrement and InterlockedDecrement on Windows) and this is correct
|
|
|
|
// as "old" value impossible to restore from "new" value and operation parameters
|
|
|
|
// and "new" value could be changed at time when code looks at it.
|
|
|
|
// This (returning of original value) is not how operators such as "=", "+=",
|
|
|
|
// "&=" etc, usually works. Therefore overloaded operators in AtomicCounter class
|
|
|
|
// are void and all of them have corresponding equivalent functions returning
|
|
|
|
// "old" value of operand.
|
|
|
|
// The only exceptions from this rule is unary increment and decrement (for
|
|
|
|
// historical reasons). Use it with care ! If one need old value of just
|
|
|
|
// incremented atomic variable he should use exchangeAdd, not operator++.
|
|
|
|
|
2004-03-26 00:12:50 +01:00
|
|
|
#if defined(WIN_NT)
|
|
|
|
|
|
|
|
#include <windows.h>
|
2011-04-29 11:25:06 +02:00
|
|
|
#include <intrin.h>
|
2004-03-26 00:12:50 +01:00
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
// Win95 is not supported unless compiled conditionally and
|
|
|
|
// redirected to generic version below
|
2011-04-29 11:25:06 +02:00
|
|
|
class PlatformAtomicCounter
|
2004-03-26 00:12:50 +01:00
|
|
|
{
|
|
|
|
public:
|
2009-09-09 12:23:54 +02:00
|
|
|
#ifdef _WIN64
|
|
|
|
typedef LONGLONG counter_type;
|
|
|
|
#else
|
2004-03-26 00:12:50 +01:00
|
|
|
typedef LONG counter_type;
|
2009-09-09 12:23:54 +02:00
|
|
|
#endif
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
explicit PlatformAtomicCounter(counter_type val = 0) : counter(val) {}
|
|
|
|
~PlatformAtomicCounter() {}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2011-04-28 16:35:58 +02:00
|
|
|
// returns old value
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type exchangeAdd(counter_type val)
|
|
|
|
{
|
2009-09-09 12:23:54 +02:00
|
|
|
#ifdef _WIN64
|
2011-04-28 16:35:58 +02:00
|
|
|
return _InterlockedExchangeAdd64(&counter, val);
|
|
|
|
#else
|
|
|
|
return _InterlockedExchangeAdd(&counter, val);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool compareExchange(counter_type oldVal, counter_type newVal)
|
|
|
|
{
|
|
|
|
#ifdef _WIN64
|
|
|
|
return (_InterlockedCompareExchange64(&counter, newVal, oldVal) == oldVal);
|
|
|
|
#else
|
|
|
|
return (_InterlockedCompareExchange(&counter, newVal, oldVal) == oldVal);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-29 13:12:29 +02:00
|
|
|
// returns old value
|
|
|
|
counter_type setValue(counter_type val)
|
2011-04-28 16:35:58 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN64
|
2011-04-29 13:12:29 +02:00
|
|
|
return _InterlockedExchange64(&counter, val);
|
2009-09-09 12:23:54 +02:00
|
|
|
#else
|
2011-04-29 13:12:29 +02:00
|
|
|
return _InterlockedExchange(&counter, val);
|
2009-09-09 12:23:54 +02:00
|
|
|
#endif
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
protected:
|
|
|
|
#ifndef MINGW
|
|
|
|
volatile
|
2011-04-28 16:35:58 +02:00
|
|
|
#endif
|
2011-04-29 11:25:06 +02:00
|
|
|
counter_type counter;
|
2011-04-28 16:35:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-04-29 21:11:43 +02:00
|
|
|
class PlatformAtomicPointer
|
2011-04-28 16:35:58 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-04-29 21:11:43 +02:00
|
|
|
explicit PlatformAtomicPointer(void* val = NULL) : pointer(val) {}
|
|
|
|
~PlatformAtomicPointer() {}
|
2011-04-28 16:35:58 +02:00
|
|
|
|
2011-04-29 21:11:43 +02:00
|
|
|
void* platformValue() const { return (void*)pointer; }
|
2011-04-28 16:35:58 +02:00
|
|
|
|
2011-04-29 13:12:29 +02:00
|
|
|
// returns old value
|
2011-04-29 21:11:43 +02:00
|
|
|
void* platformSetValue(void* val)
|
2008-12-13 10:19:22 +01:00
|
|
|
{
|
2009-09-09 12:23:54 +02:00
|
|
|
#ifdef _WIN64
|
2011-04-29 21:11:43 +02:00
|
|
|
return _InterlockedExchangePointer((volatile PVOID*)&pointer, val);
|
2009-09-09 12:23:54 +02:00
|
|
|
#else
|
2011-04-28 16:35:58 +02:00
|
|
|
//InterlockedExchangePointer((volatile PVOID*)&pointer, val);
|
2011-04-29 21:11:43 +02:00
|
|
|
return (void*)_InterlockedExchange((LONG volatile*)&pointer, (LONG)val);
|
2009-09-09 12:23:54 +02:00
|
|
|
#endif
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2011-04-29 21:11:43 +02:00
|
|
|
bool platformCompareExchange(void* oldVal, void* newVal)
|
2008-12-13 10:19:22 +01:00
|
|
|
{
|
2009-09-09 12:23:54 +02:00
|
|
|
#ifdef _WIN64
|
2011-04-28 16:35:58 +02:00
|
|
|
return (_InterlockedCompareExchangePointer((PVOID volatile*)&pointer, newVal, oldVal) == oldVal);
|
2009-09-09 12:23:54 +02:00
|
|
|
#else
|
2011-04-28 16:35:58 +02:00
|
|
|
//return (InterlockedCompareExchangePointer((PVOID volatile*)&pointer, newVal, oldVal) == oldVal);
|
|
|
|
return ((PVOID)(LONG_PTR)_InterlockedCompareExchange((LONG volatile*)&pointer, (LONG)newVal, (LONG)oldVal)) == oldVal;
|
2009-09-09 12:23:54 +02:00
|
|
|
#endif
|
2008-12-13 10:19:22 +01:00
|
|
|
}
|
|
|
|
|
2011-04-29 17:50:04 +02:00
|
|
|
private:
|
2011-04-29 21:11:43 +02:00
|
|
|
volatile void* pointer;
|
2004-03-26 00:12:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Firebird
|
|
|
|
|
2009-01-09 15:44:53 +01:00
|
|
|
#elif defined(AIX)
|
2004-03-26 00:12:50 +01:00
|
|
|
|
2009-09-22 11:08:57 +02:00
|
|
|
#if SIZEOF_VOID_P != 8
|
|
|
|
#error: Only 64-bit mode supported on AIX
|
|
|
|
#endif
|
|
|
|
|
2009-01-09 15:44:53 +01:00
|
|
|
#include <sys/atomic_op.h>
|
2004-03-26 00:12:50 +01:00
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2009-01-09 15:44:53 +01:00
|
|
|
// AIX version - uses AIX atomic API
|
2011-04-29 11:25:06 +02:00
|
|
|
class PlatformAtomicCounter
|
2004-03-26 00:12:50 +01:00
|
|
|
{
|
2011-04-29 11:25:06 +02:00
|
|
|
public:
|
2009-09-22 11:08:57 +02:00
|
|
|
typedef long counter_type;
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
explicit PlatformAtomicCounter(AtomicType value = 0) : counter(value) {}
|
|
|
|
~PlatformAtomicCounter() {}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2009-09-22 11:08:57 +02:00
|
|
|
counter_type exchangeAdd(AtomicType value)
|
2008-12-13 10:19:22 +01:00
|
|
|
{
|
2009-09-22 11:08:57 +02:00
|
|
|
return fetch_and_addlp(&counter, static_cast<unsigned long>(value));
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
counter_type value() const { return counter; }
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
bool compareExchange(counter_type oldVal, counter_type newVal)
|
2008-12-13 10:19:22 +01:00
|
|
|
{
|
2011-04-29 11:25:06 +02:00
|
|
|
return compare_and_swaplp(&counter, &oldVal, newVal);
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2009-09-22 11:08:57 +02:00
|
|
|
counter_type setValue(AtomicType val)
|
2008-12-13 10:19:22 +01:00
|
|
|
{
|
2009-01-09 15:44:53 +01:00
|
|
|
counter_type old;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
2009-09-22 11:08:57 +02:00
|
|
|
} while (!compare_and_swaplp(&counter, &old, val));
|
2009-01-09 15:44:53 +01:00
|
|
|
return old;
|
2008-12-13 10:19:22 +01:00
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
protected:
|
2009-01-09 15:44:53 +01:00
|
|
|
counter_type counter;
|
2004-03-26 00:12:50 +01:00
|
|
|
};
|
|
|
|
|
2011-04-29 17:50:04 +02:00
|
|
|
#define FLUSH_CACHE 1
|
|
|
|
inline void FlushCache()
|
|
|
|
{
|
|
|
|
asm_sync();
|
|
|
|
}
|
|
|
|
inline void WaitForFlushCache()
|
|
|
|
{
|
|
|
|
asm_isync();
|
|
|
|
}
|
|
|
|
|
2004-03-26 00:12:50 +01:00
|
|
|
} // namespace Firebird
|
2009-06-05 12:04:45 +02:00
|
|
|
|
2009-11-02 22:08:58 +01:00
|
|
|
#elif defined(HPUX) && defined(HAVE_ATOMIC_H)
|
2009-06-05 12:04:45 +02:00
|
|
|
|
|
|
|
#include <atomic.h>
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
// HPUX version - uses atomic API library
|
2011-04-29 11:25:06 +02:00
|
|
|
class PlatformAtomicCounter
|
2009-06-05 12:04:45 +02:00
|
|
|
{
|
|
|
|
public:
|
2009-09-29 21:13:51 +02:00
|
|
|
typedef uint64_t counter_type;
|
2009-06-05 12:04:45 +02:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
explicit PlatformAtomicCounter(counter_type value = 0) : counter(value) {}
|
|
|
|
~PlatformAtomicCounter() {}
|
2009-06-05 12:04:45 +02:00
|
|
|
|
|
|
|
counter_type exchangeAdd(counter_type value)
|
|
|
|
{
|
|
|
|
counter_type old = counter;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
|
|
|
errno = 0;
|
2009-09-29 21:13:51 +02:00
|
|
|
} while (atomic_cas_64(&counter, old, old + value) != old);
|
2009-06-05 12:04:45 +02:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
bool compareExchange(counter_type oldVal, counter_type newVal)
|
2009-06-05 12:04:45 +02:00
|
|
|
{
|
2011-04-29 11:25:06 +02:00
|
|
|
return atomic_cas_64(&counter, oldVal, newVal) == oldVal;
|
2009-06-05 12:04:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
counter_type setValue(counter_type val)
|
|
|
|
{
|
2009-09-29 21:13:51 +02:00
|
|
|
return atomic_swap_64(&counter, val);
|
2009-06-05 12:04:45 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
protected:
|
2009-06-05 12:04:45 +02:00
|
|
|
volatile counter_type counter;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Firebird
|
2004-03-26 00:12:50 +01:00
|
|
|
|
2009-07-21 10:58:28 +02:00
|
|
|
#elif defined(SOLARIS) && defined(HAVE_ATOMIC_H)
|
2009-04-03 15:09:58 +02:00
|
|
|
|
|
|
|
#include <atomic.h>
|
|
|
|
|
2011-04-29 17:50:04 +02:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
extern void membar_flush(void);
|
|
|
|
extern void membar_wait(void);
|
|
|
|
}
|
|
|
|
|
2009-04-03 15:09:58 +02:00
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
// Solaris version - uses Solaris atomic_ops
|
2011-04-29 11:25:06 +02:00
|
|
|
class PlatformAtomicCounter
|
2009-04-03 15:09:58 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-04-29 11:25:06 +02:00
|
|
|
typedef ulong_t counter_type;
|
2009-09-28 19:14:24 +02:00
|
|
|
typedef long delta_type;
|
2009-04-03 15:09:58 +02:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
explicit PlatformAtomicCounter(counter_type value = 0) : counter(value) {}
|
|
|
|
~PlatformAtomicCounter() {}
|
2009-04-03 15:09:58 +02:00
|
|
|
|
2009-04-16 10:59:21 +02:00
|
|
|
counter_type exchangeAdd(delta_type value)
|
2009-04-03 15:09:58 +02:00
|
|
|
{
|
2009-09-28 19:14:24 +02:00
|
|
|
return atomic_add_long_nv(&counter, value);
|
2009-04-03 15:09:58 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
counter_type setValue(delta_type value)
|
2009-04-03 15:09:58 +02:00
|
|
|
{
|
2011-04-29 11:25:06 +02:00
|
|
|
return atomic_swap_ulong(&counter, value);
|
2009-04-03 15:09:58 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
bool compareExchange(counter_type oldVal, counter_type newVal)
|
2009-04-03 15:09:58 +02:00
|
|
|
{
|
2011-04-29 11:25:06 +02:00
|
|
|
TODO: implement method for Solaris
|
2009-04-03 15:09:58 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
protected:
|
|
|
|
volatile counter_type counter;
|
2009-04-03 15:09:58 +02:00
|
|
|
};
|
|
|
|
|
2011-04-29 17:50:04 +02:00
|
|
|
#define FLUSH_CACHE 1
|
|
|
|
inline void FlushCache()
|
|
|
|
{
|
|
|
|
membar_flush();
|
|
|
|
}
|
|
|
|
inline void WaitForFlushCache()
|
|
|
|
{
|
|
|
|
membar_wait();
|
|
|
|
}
|
|
|
|
|
2009-04-03 15:09:58 +02:00
|
|
|
} // namespace Firebird
|
|
|
|
|
|
|
|
|
2009-07-21 10:58:28 +02:00
|
|
|
#elif defined(__SUNPRO_CC) && !defined(HAVE_ATOMIC_H) && defined(__sparc)
|
|
|
|
// Solaris 9 does not have nice <atomic.h> header file, so we provide
|
|
|
|
// an assembly language, Sun Studio Pro only, implementation.
|
|
|
|
|
|
|
|
// assembly versions of fetch_and_add_il, compare_and_swap_il,
|
|
|
|
// are in fb_atomic_*.il
|
|
|
|
|
|
|
|
// No GNU CC implementation on Solaris 9 is planned!
|
|
|
|
extern "C"
|
|
|
|
{
|
2009-07-23 02:56:28 +02:00
|
|
|
extern int fetch_and_add_il(volatile unsigned int* word_addr, int value);
|
|
|
|
extern boolean_t compare_and_swap_il(volatile unsigned int* word_addr,
|
|
|
|
unsigned int* old_val_addr, unsigned int new_val);
|
2009-07-21 10:58:28 +02:00
|
|
|
}
|
2009-07-23 02:56:28 +02:00
|
|
|
|
2009-07-21 10:58:28 +02:00
|
|
|
namespace Firebird {
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
class PlatformAtomicCounter
|
2009-07-21 10:58:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef uint_t counter_type;
|
|
|
|
typedef int delta_type;
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
explicit PlatformAtomicCounter(counter_type value = 0) : counter(value) {}
|
|
|
|
~PlatformAtomicCounter() {}
|
2009-07-21 10:58:28 +02:00
|
|
|
|
|
|
|
counter_type exchangeAdd(delta_type value)
|
|
|
|
{
|
|
|
|
return fetch_and_add_il(&counter, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type setValue(delta_type value)
|
|
|
|
{
|
|
|
|
counter_type old;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
|
|
|
} while (!compare_and_swap_il(&counter, &old, value));
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
bool compareExchange(counter_type oldVal, counter_type newVal)
|
|
|
|
{
|
|
|
|
return compare_and_swap_il(&counter, &oldVal, newVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2009-07-21 10:58:28 +02:00
|
|
|
counter_type counter;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Firebird
|
|
|
|
|
2009-07-28 13:16:31 +02:00
|
|
|
#elif defined(__GNUC__) && (defined(i386) || defined(I386) || defined(_M_IX86) || defined(AMD64) || defined(__x86_64__))
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
// Assembler version for x86 and AMD64. Note it uses xaddl thus it requires i486
|
2011-04-29 11:25:06 +02:00
|
|
|
class PlatformAtomicCounter
|
2009-07-28 13:16:31 +02:00
|
|
|
{
|
|
|
|
public:
|
2009-09-22 11:08:57 +02:00
|
|
|
typedef AtomicType counter_type;
|
2009-07-28 13:16:31 +02:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
explicit PlatformAtomicCounter(AtomicType value = 0) : counter(value) {}
|
|
|
|
~PlatformAtomicCounter() {}
|
2009-07-28 13:16:31 +02:00
|
|
|
|
2009-09-22 11:08:57 +02:00
|
|
|
AtomicType exchangeAdd(AtomicType value)
|
2009-07-28 13:16:31 +02:00
|
|
|
{
|
|
|
|
register counter_type result;
|
2011-04-29 11:25:06 +02:00
|
|
|
|
|
|
|
__asm __volatile ("lock; xadd %0, %1"
|
|
|
|
: "=r" (result), "=m" (counter)
|
|
|
|
: "0" (value), "m" (counter)
|
|
|
|
: "cc", "memory");
|
|
|
|
|
2009-07-28 13:16:31 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
AtomicType setValue(AtomicType val)
|
2009-07-28 13:16:31 +02:00
|
|
|
{
|
2011-04-29 11:25:06 +02:00
|
|
|
register counter_type result;
|
2009-07-28 13:16:31 +02:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
__asm __volatile ("lock; xchg %0, %1"
|
|
|
|
: "=r" (result), "=m" (counter)
|
|
|
|
: "0" (val), "m" (counter)
|
|
|
|
: "cc", "memory");
|
2009-07-28 13:16:31 +02:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
return result;
|
2009-07-28 13:16:31 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
bool compareExchange(counter_type oldVal, counter_type newVal)
|
2009-07-28 13:16:31 +02:00
|
|
|
{
|
2011-04-29 11:25:06 +02:00
|
|
|
register char ret;
|
2009-07-28 13:16:31 +02:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
__asm __volatile ("lock; cmpxchg %2, %1 ; sete %0"
|
|
|
|
: "=r" (ret), "+m" (counter)
|
|
|
|
: "r" (newVal), "a" (oldVal)
|
|
|
|
: "cc", "memory");
|
2009-07-28 13:16:31 +02:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
return ret;
|
2009-07-28 13:16:31 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
protected:
|
2009-07-28 13:16:31 +02:00
|
|
|
volatile counter_type counter;
|
|
|
|
};
|
|
|
|
|
2011-04-29 17:50:04 +02:00
|
|
|
class PlatformAtomicPointer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit PlatformAtomicPointer(void* val = NULL) : pointer(val) {}
|
|
|
|
~PlatformAtomicPointer() {}
|
|
|
|
|
|
|
|
void* platformValue() const { return (void*)pointer; }
|
|
|
|
|
|
|
|
void* platformSetValue(void* val)
|
|
|
|
{
|
|
|
|
register void* result;
|
|
|
|
|
|
|
|
__asm __volatile ("lock; xchg %0, %1"
|
|
|
|
: "=r" (result), "=m" (pointer)
|
|
|
|
: "0" (val), "m" (pointer)
|
|
|
|
: "cc", "memory");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool platformCompareExchange(void* oldVal, void* newVal)
|
|
|
|
{
|
|
|
|
register char ret;
|
|
|
|
|
|
|
|
__asm __volatile ("lock; cmpxchg %2, %1 ; sete %0"
|
|
|
|
: "=r" (ret), "+m" (pointer)
|
|
|
|
: "r" (newVal), "a" (oldVal)
|
|
|
|
: "cc", "memory");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
volatile void* pointer;
|
|
|
|
};
|
|
|
|
|
2009-07-28 13:16:31 +02:00
|
|
|
} // namespace Firebird
|
|
|
|
|
2009-11-02 22:08:58 +01:00
|
|
|
#elif defined(HAVE_AO_COMPARE_AND_SWAP_FULL) && !defined(HPUX)
|
2009-07-28 13:16:31 +02:00
|
|
|
|
|
|
|
// Sometimes in the future it can become the best choice for all platforms.
|
|
|
|
// Currently far not CPUs/OSs/compilers are supported well.
|
2009-07-28 14:03:58 +02:00
|
|
|
// Therefore use it as last chance to build successfully.
|
2009-07-28 13:16:31 +02:00
|
|
|
|
2009-11-02 22:08:58 +01:00
|
|
|
// wbo 2009-10 - We tested libatomic_ops against Firebird 2.5/HPUX and the
|
|
|
|
// atomic implementation was incomplete on IA-64/HPPA
|
|
|
|
|
2010-10-25 17:48:35 +02:00
|
|
|
extern "C" {
|
|
|
|
#define AO_REQUIRE_CAS
|
2009-07-28 13:16:31 +02:00
|
|
|
#include <atomic_ops.h>
|
2010-10-27 02:32:58 +02:00
|
|
|
}
|
2009-07-28 13:16:31 +02:00
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
class PlatformAtomicCounter
|
2009-07-28 13:16:31 +02:00
|
|
|
{
|
|
|
|
public:
|
2009-09-22 11:08:57 +02:00
|
|
|
typedef AO_t counter_type; // AO_t should match maximum native in type
|
2009-07-28 13:16:31 +02:00
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
explicit PlatformAtomicCounter(counter_type value = 0) : counter(value) {}
|
|
|
|
~PlatformAtomicCounter() {}
|
2009-07-28 13:16:31 +02:00
|
|
|
|
2009-09-22 11:08:57 +02:00
|
|
|
AtomicType exchangeAdd(AtomicType value)
|
2009-07-28 13:16:31 +02:00
|
|
|
{
|
2009-09-22 11:08:57 +02:00
|
|
|
#ifdef DEV_BUILD
|
|
|
|
// check that AO_t size is as we expect (can't fb_assert here)
|
|
|
|
if (sizeof(AtomicType) != sizeof(AO_t)) abort();
|
|
|
|
#endif
|
|
|
|
|
2009-07-28 13:16:31 +02:00
|
|
|
counter_type old;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
2009-09-22 11:08:57 +02:00
|
|
|
} while (!AO_compare_and_swap_full(&counter, old, old + counter_type(value)));
|
|
|
|
return AtomicType(old);
|
2009-07-28 13:16:31 +02:00
|
|
|
}
|
|
|
|
|
2009-09-22 11:08:57 +02:00
|
|
|
AtomicType setValue(AtomicType val)
|
2009-07-28 13:16:31 +02:00
|
|
|
{
|
2009-09-22 11:08:57 +02:00
|
|
|
#ifdef DEV_BUILD
|
|
|
|
// check that AO_t size is as we expect (can't fb_assert here)
|
|
|
|
if (sizeof(AtomicType) != sizeof(AO_t)) abort();
|
|
|
|
#endif
|
|
|
|
|
2009-07-28 13:16:31 +02:00
|
|
|
counter_type old;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
2009-09-22 11:08:57 +02:00
|
|
|
} while (!AO_compare_and_swap_full(&counter, old, counter_type(val)));
|
|
|
|
return AtomicType(old);
|
2009-07-28 13:16:31 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
bool compareExchange(counter_type oldVal, counter_type newVal)
|
|
|
|
{
|
|
|
|
return AO_compare_and_swap_full(&counter, oldVal, newVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2009-07-28 13:16:31 +02:00
|
|
|
counter_type counter;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Firebird
|
|
|
|
|
2009-01-09 15:44:53 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
#error AtomicCounter: implement appropriate code for your platform!
|
|
|
|
|
2004-03-26 00:12:50 +01:00
|
|
|
#endif
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-04-29 17:50:04 +02:00
|
|
|
template <typename T>
|
|
|
|
class AtomicPointer : public PlatformAtomicPointer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AtomicPointer(T* val = NULL) : PlatformAtomicPointer(val) {}
|
|
|
|
~AtomicPointer() {}
|
|
|
|
|
|
|
|
T* value() const
|
|
|
|
{
|
|
|
|
return (T*)platformValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setValue(T* val)
|
|
|
|
{
|
|
|
|
platformSetValue(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool compareExchange(T* oldVal, T* newVal)
|
|
|
|
{
|
|
|
|
return platformCompareExchange(oldVal, newVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T* () const
|
|
|
|
{
|
|
|
|
return value();
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator ->() const
|
|
|
|
{
|
|
|
|
return value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator =(T* val)
|
|
|
|
{
|
|
|
|
setValue(val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef FLUSH_CACHE
|
|
|
|
inline void FlushCache() { }
|
|
|
|
inline void WaitForFlushCache() { }
|
|
|
|
#endif
|
|
|
|
|
2011-04-29 11:25:06 +02:00
|
|
|
} // namespace Firebird
|
|
|
|
|
2004-03-28 11:10:30 +02:00
|
|
|
#endif // CLASSES_FB_ATOMIC_H
|