drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
atomics.h
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2009 Sun Microsystems, Inc.
5 * Copyright 2005-2008 Intel Corporation. All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#pragma once
22
23#include <drizzled/atomic/gcc_traits.h>
24#define ATOMIC_TRAITS internal::gcc_traits
25
26namespace drizzled {
27namespace internal {
28
29template<typename I> // Primary template
30struct atomic_base {
31 volatile I my_value;
32 atomic_base() : my_value(0) {}
33 virtual ~atomic_base() {}
34};
35
36template<typename I, typename D, typename T >
37class atomic_impl: private atomic_base<I>
38{
39 T traits;
40public:
41 typedef I value_type;
42
43 atomic_impl() : atomic_base<I>(), traits() {}
44
45 value_type add_and_fetch( D addend )
46 {
47 return traits.add_and_fetch(&this->my_value, addend);
48 }
49
50 value_type fetch_and_add( D addend )
51 {
52 return traits.fetch_and_add(&this->my_value, addend);
53 }
54
55 value_type fetch_and_increment()
56 {
57 return traits.fetch_and_increment(&this->my_value);
58 }
59
60 value_type fetch_and_decrement()
61 {
62 return traits.fetch_and_decrement(&this->my_value);
63 }
64
65 value_type fetch_and_store( value_type value )
66 {
67 return traits.fetch_and_store(&this->my_value, value);
68 }
69
70 bool compare_and_swap( value_type value, value_type comparand )
71 {
72 return traits.compare_and_swap(&this->my_value, value, comparand);
73 }
74
75 operator value_type() const volatile
76 {
77 return traits.fetch(&this->my_value);
78 }
79
80 value_type& _internal_reference() const
81 {
82 return this->my_value;
83 }
84
85protected:
86 value_type store_with_release( value_type rhs )
87 {
88 return traits.store_with_release(&this->my_value, rhs);
89 }
90
91public:
92 atomic_impl<I,D,T>& operator+=( D addend )
93 {
94 increment(addend);
95 return *this;
96 }
97
98 atomic_impl<I,D,T>& operator-=( D addend )
99 {
100 // Additive inverse of addend computed using binary minus,
101 // instead of unary minus, for sake of avoiding compiler warnings.
102 return operator+=(D(0)-addend);
103 }
104
105 value_type increment()
106 {
107 return add_and_fetch(1);
108 }
109
110 value_type decrement()
111 {
112 return add_and_fetch(D(-1));
113 }
114};
115
116} /* namespace internal */
117
119
121template<typename T>
122struct atomic {
123};
124
125/* *INDENT-OFF* */
126#define __DRIZZLE_DECL_ATOMIC(T) \
127 template<> struct atomic<T> \
128 : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> > { \
129 atomic() : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> >() {} \
130 atomic& operator=( T rhs ) { store_with_release(rhs); return *this; } \
131 };
132/* *INDENT-ON* */
133
134
135__DRIZZLE_DECL_ATOMIC(long)
136__DRIZZLE_DECL_ATOMIC(unsigned long)
137__DRIZZLE_DECL_ATOMIC(unsigned int)
138__DRIZZLE_DECL_ATOMIC(int)
139__DRIZZLE_DECL_ATOMIC(unsigned short)
140__DRIZZLE_DECL_ATOMIC(short)
141__DRIZZLE_DECL_ATOMIC(char)
142__DRIZZLE_DECL_ATOMIC(signed char)
143__DRIZZLE_DECL_ATOMIC(unsigned char)
144__DRIZZLE_DECL_ATOMIC(bool)
145
146/* *INDENT-OFF* */
147__DRIZZLE_DECL_ATOMIC(long long)
148__DRIZZLE_DECL_ATOMIC(unsigned long long)
149/* *INDENT-ON* */
150
151}
TODO: Rename this file - func.h is stupid.