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
|
|
|
|
|
|
|
#if defined(WIN_NT)
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
// Win95 is not supported unless compiled conditionally and
|
|
|
|
// redirected to generic version below
|
|
|
|
class AtomicCounter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef LONG counter_type;
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
explicit AtomicCounter(counter_type val = 0) : counter(val) {}
|
2004-03-26 00:12:50 +01:00
|
|
|
~AtomicCounter() {}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type exchangeAdd(counter_type val)
|
|
|
|
{
|
2004-05-12 21:23:17 +02:00
|
|
|
return InterlockedExchangeAdd(&counter, val);
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type operator +=(counter_type val)
|
|
|
|
{
|
2004-05-12 21:23:17 +02:00
|
|
|
return exchangeAdd(val) + val;
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type operator -=(counter_type val)
|
|
|
|
{
|
2004-05-12 21:23:17 +02:00
|
|
|
return exchangeAdd(-val) - val;
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type operator ++()
|
|
|
|
{
|
2004-03-26 00:12:50 +01:00
|
|
|
return InterlockedIncrement(&counter);
|
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type operator --()
|
|
|
|
{
|
2004-03-26 00:12:50 +01:00
|
|
|
return InterlockedDecrement(&counter);
|
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2004-03-26 00:12:50 +01:00
|
|
|
counter_type value() const { return counter; }
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type setValue(counter_type val)
|
|
|
|
{
|
|
|
|
return InterlockedExchange(&counter, val);
|
|
|
|
}
|
|
|
|
|
2004-03-26 00:12:50 +01:00
|
|
|
private:
|
2008-05-14 07:17:53 +02:00
|
|
|
# if defined(MINGW)
|
2004-03-26 00:12:50 +01:00
|
|
|
counter_type counter;
|
|
|
|
# else
|
|
|
|
volatile counter_type counter;
|
|
|
|
# endif
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Firebird
|
|
|
|
|
2009-01-09 15:44:53 +01:00
|
|
|
#elif defined(AIX)
|
2004-03-26 00:12:50 +01:00
|
|
|
|
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
|
2004-03-26 00:12:50 +01:00
|
|
|
class AtomicCounter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef int counter_type;
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
2004-03-26 00:12:50 +01:00
|
|
|
~AtomicCounter() {}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type exchangeAdd(counter_type value)
|
|
|
|
{
|
2009-01-14 14:58:41 +01:00
|
|
|
return fetch_and_add(&counter, value);
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type operator +=(counter_type value)
|
|
|
|
{
|
2009-01-09 15:44:53 +01:00
|
|
|
return exchangeAdd(value) + value;
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type operator -=(counter_type value)
|
|
|
|
{
|
2009-01-09 15:44:53 +01:00
|
|
|
return exchangeAdd(-value) - value;
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type operator ++()
|
|
|
|
{
|
2009-01-09 15:44:53 +01:00
|
|
|
return exchangeAdd(1) + 1;
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type operator --()
|
|
|
|
{
|
2009-01-09 15:44:53 +01:00
|
|
|
return exchangeAdd(-1) - 1;
|
2004-03-26 00:12:50 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2004-03-26 00:12:50 +01:00
|
|
|
counter_type value() const { return counter; }
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-12-13 10:19:22 +01:00
|
|
|
counter_type setValue(counter_type val)
|
|
|
|
{
|
2009-01-09 15:44:53 +01:00
|
|
|
counter_type old;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
2009-01-11 01:40:46 +01:00
|
|
|
} while (!compare_and_swap(&counter, &old, val));
|
2009-01-09 15:44:53 +01:00
|
|
|
return old;
|
2008-12-13 10:19:22 +01:00
|
|
|
}
|
|
|
|
|
2004-03-26 00:12:50 +01:00
|
|
|
private:
|
2009-01-09 15:44:53 +01:00
|
|
|
counter_type counter;
|
2004-03-26 00:12:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Firebird
|
2009-06-05 12:04:45 +02:00
|
|
|
|
|
|
|
#elif defined(HPUX)
|
|
|
|
|
|
|
|
#include <atomic.h>
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
// HPUX version - uses atomic API library
|
|
|
|
class AtomicCounter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef uint32_t counter_type;
|
|
|
|
|
|
|
|
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
|
|
|
~AtomicCounter() {}
|
|
|
|
|
|
|
|
counter_type exchangeAdd(counter_type value)
|
|
|
|
{
|
|
|
|
counter_type old = counter;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
|
|
|
errno = 0;
|
|
|
|
} while (atomic_cas_32(&counter, old, old + value) != old);
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator +=(counter_type value)
|
|
|
|
{
|
|
|
|
return exchangeAdd(value) + value;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator -=(counter_type value)
|
|
|
|
{
|
|
|
|
return exchangeAdd(-value) - value;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator ++()
|
|
|
|
{
|
|
|
|
return atomic_inc_32(&counter) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator --()
|
|
|
|
{
|
|
|
|
return atomic_dec_32(&counter) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type value() const { return counter; }
|
|
|
|
|
|
|
|
counter_type setValue(counter_type val)
|
|
|
|
{
|
|
|
|
return atomic_swap_32(&counter, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
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>
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
// Solaris version - uses Solaris atomic_ops
|
|
|
|
class AtomicCounter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef uint_t counter_type;
|
2009-04-16 10:59:21 +02:00
|
|
|
typedef int delta_type;
|
2009-04-03 15:09:58 +02:00
|
|
|
|
|
|
|
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
|
|
|
~AtomicCounter() {}
|
|
|
|
|
2009-04-16 10:59:21 +02:00
|
|
|
counter_type exchangeAdd(delta_type value)
|
2009-04-03 15:09:58 +02:00
|
|
|
{
|
|
|
|
return atomic_add_int_nv(&counter, value);
|
|
|
|
}
|
|
|
|
|
2009-04-16 10:59:21 +02:00
|
|
|
counter_type operator +=(delta_type value)
|
2009-04-03 15:09:58 +02:00
|
|
|
{
|
|
|
|
return exchangeAdd(value) + value;
|
|
|
|
}
|
|
|
|
|
2009-04-16 10:59:21 +02:00
|
|
|
counter_type operator -=(delta_type value)
|
2009-04-03 15:09:58 +02:00
|
|
|
{
|
|
|
|
return exchangeAdd(-value) - value;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator ++()
|
|
|
|
{
|
2009-04-17 16:10:11 +02:00
|
|
|
return atomic_inc_uint_nv(&counter);
|
2009-04-03 15:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator --()
|
|
|
|
{
|
2009-04-17 16:10:11 +02:00
|
|
|
return atomic_dec_uint_nv(&counter);
|
2009-04-03 15:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
counter_type value() const { return counter; }
|
|
|
|
|
2009-07-21 10:58:28 +02:00
|
|
|
counter_type setValue(delta_type value)
|
2009-04-03 15:09:58 +02:00
|
|
|
{
|
2009-07-21 10:58:28 +02:00
|
|
|
return atomic_swap_uint(&counter, value);
|
2009-04-03 15:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
counter_type counter;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // 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 {
|
|
|
|
|
|
|
|
class AtomicCounter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef uint_t counter_type;
|
|
|
|
typedef int delta_type;
|
|
|
|
|
|
|
|
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
|
|
|
~AtomicCounter() {}
|
|
|
|
|
|
|
|
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;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
|
|
|
} while (!compare_and_swap_il(&counter, &old, value));
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
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
|
|
|
|
class AtomicCounter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef int counter_type;
|
|
|
|
|
|
|
|
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
|
|
|
~AtomicCounter() {}
|
|
|
|
|
|
|
|
counter_type exchangeAdd(counter_type value)
|
|
|
|
{
|
|
|
|
register counter_type result;
|
|
|
|
__asm __volatile (
|
|
|
|
"lock; xaddl %0, %1"
|
|
|
|
: "=r" (result), "=m" (counter)
|
|
|
|
: "0" (value), "m" (counter));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator +=(counter_type value)
|
|
|
|
{
|
|
|
|
return exchangeAdd(value) + value;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator -=(counter_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(counter_type val)
|
|
|
|
{
|
|
|
|
register counter_type result;
|
|
|
|
__asm __volatile (
|
|
|
|
"lock; xchg %0, %1"
|
|
|
|
: "=r" (result), "=m" (counter)
|
|
|
|
: "0" (val), "m" (counter));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
volatile counter_type counter;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Firebird
|
|
|
|
|
|
|
|
#elif defined(HAVE_AO_COMPARE_AND_SWAP_FULL)
|
|
|
|
|
|
|
|
// Sometimes in the future it can become the best choice for all platforms.
|
|
|
|
// Currently far not CPUs/OSs/compilers are supported well.
|
|
|
|
// Therefore use it as lsct chance to build successfully.
|
|
|
|
|
|
|
|
#include <atomic_ops.h>
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
class AtomicCounter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef AO_t counter_type;
|
|
|
|
|
|
|
|
explicit AtomicCounter(counter_type value = 0) : counter(value) {}
|
|
|
|
~AtomicCounter() {}
|
|
|
|
|
|
|
|
/* counter_type exchangeAdd(counter_type value)
|
|
|
|
{
|
|
|
|
return AO_fetch_and_add_full(&counter, value);
|
|
|
|
} */
|
|
|
|
counter_type exchangeAdd(counter_type value)
|
|
|
|
{
|
|
|
|
counter_type old;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
|
|
|
} while (!AO_compare_and_swap_full(&counter, old, old + value));
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator +=(counter_type value)
|
|
|
|
{
|
|
|
|
return exchangeAdd(value) + value;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter_type operator -=(counter_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(counter_type val)
|
|
|
|
{
|
|
|
|
counter_type old;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
old = counter;
|
|
|
|
} while (!AO_compare_and_swap_full(&counter, old, val));
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
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
|
|
|
|
|
2004-03-28 11:10:30 +02:00
|
|
|
#endif // CLASSES_FB_ATOMIC_H
|