~drizzle-trunk/drizzle/development

910.5.2 by Monty Taylor
Applied atomic patch to current tree.
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
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
#ifndef DRIZZLED_ATOMICS_H
22
#define DRIZZLED_ATOMICS_H
23
24
# if defined(__SUNPRO_CC)
25
#  include <drizzled/atomic/sun_studio.h>
26
# endif
1093.2.2 by mordred
pandora-build v0.18
27
# if !defined(__ICC) && (defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC))
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
28
#  include <drizzled/atomic/gcc_traits.h>
29
#  define ATOMIC_TRAITS internal::gcc_traits
30
# else  /* use pthread impl */
31
#  define ATOMIC_TRAITS internal::pthread_traits
32
# endif
33
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
34
# if (SIZEOF_SIZE_T >= SIZEOF_LONG_LONG) || (!defined(HAVE_GCC_ATOMIC_BUILTINS) || !defined(__SUNPRO_CC))
35
#  include <pthread.h>
36
#  include <drizzled/atomic/pthread_traits.h>
37
# endif
38
39
40
namespace drizzled {
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
41
42
namespace internal {
43
44
45
template<typename I>            // Primary template
46
struct atomic_base {
47
  volatile I my_value;
48
};
49
50
template<typename I, typename D, typename T >
51
class atomic_impl: private atomic_base<I>
52
{
53
  T traits;
54
public:
55
  typedef I value_type;
56
57
58
  value_type fetch_and_add( D addend )
59
  {
60
    return traits.fetch_and_add(&this->my_value, addend);
61
  }
62
63
  value_type fetch_and_increment()
64
  {
65
    return traits.fetch_and_increment(&this->my_value);
66
  }
67
68
  value_type fetch_and_decrement()
69
  {
70
    return traits.fetch_and_decrement(&this->my_value);
71
  }
72
73
  value_type fetch_and_store( value_type value )
74
  {
75
    return traits.fetch_and_store(&this->my_value, value);
76
  }
77
78
  value_type compare_and_swap( value_type value, value_type comparand )
79
  {
80
    return traits.compare_and_swap(&this->my_value, value, comparand);
81
  }
82
83
  operator value_type() const volatile
84
  {
85
    return traits.fetch(&this->my_value);
86
  }
87
88
  value_type& _internal_reference() const
89
  {
90
    return this->my_value;
91
  }
92
93
protected:
94
  value_type store_with_release( value_type rhs )
95
  {
96
    return traits.store_with_release(&this->my_value, rhs);
97
  }
98
99
public:
100
  value_type operator+=( D addend )
101
  {
102
      return fetch_and_add(addend)+addend;
103
  }
104
105
  value_type operator-=( D addend )
106
  {
107
    // Additive inverse of addend computed using binary minus,
108
    // instead of unary minus, for sake of avoiding compiler warnings.
109
    return operator+=(D(0)-addend);
110
  }
111
112
  value_type operator++() {
113
    return fetch_and_add(1)+1;
114
  }
115
116
  value_type operator--() {
117
    return fetch_and_add(D(-1))-1;
118
  }
119
120
  value_type operator++(int) {
121
    return fetch_and_add(1);
122
  }
123
124
  value_type operator--(int) {
125
    return fetch_and_add(D(-1));
126
  }
127
128
};
129
130
} /* namespace internal */
131
132
//! Primary template for atomic.
133
/** See the Reference for details.
134
    @ingroup synchronization */
135
template<typename T>
136
struct atomic {
137
};
138
1122.2.4 by Monty Taylor
Two more cleanups.
139
/* *INDENT-OFF* */
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
140
#define __DRIZZLE_DECL_ATOMIC(T)                                        \
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
141
  template<> struct atomic<T>                                           \
142
  : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> > {                    \
143
    atomic<T>() : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> >() {}   \
144
    T operator=( T rhs ) { return store_with_release(rhs); }            \
145
  };
1122.2.4 by Monty Taylor
Two more cleanups.
146
/* *INDENT-ON* */
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
147
968.2.1 by Monty Taylor
Build fix making 64-bit atomic<> work on 32-bit systems.
148
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
149
__DRIZZLE_DECL_ATOMIC(long)
150
__DRIZZLE_DECL_ATOMIC(unsigned long)
151
__DRIZZLE_DECL_ATOMIC(unsigned int)
152
__DRIZZLE_DECL_ATOMIC(int)
153
__DRIZZLE_DECL_ATOMIC(unsigned short)
154
__DRIZZLE_DECL_ATOMIC(short)
155
__DRIZZLE_DECL_ATOMIC(char)
156
__DRIZZLE_DECL_ATOMIC(signed char)
157
__DRIZZLE_DECL_ATOMIC(unsigned char)
158
__DRIZZLE_DECL_ATOMIC(bool)
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
159
968.2.1 by Monty Taylor
Build fix making 64-bit atomic<> work on 32-bit systems.
160
/* 32-bit platforms don't have a GCC atomic operation for 64-bit types,
161
 * so we'll use pthread locks to handler 64-bit types on that platforms
162
 */
1122.2.4 by Monty Taylor
Two more cleanups.
163
/* *INDENT-OFF* */
968.2.1 by Monty Taylor
Build fix making 64-bit atomic<> work on 32-bit systems.
164
#  if SIZEOF_SIZE_T >= SIZEOF_LONG_LONG
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
165
__DRIZZLE_DECL_ATOMIC(long long)
166
__DRIZZLE_DECL_ATOMIC(unsigned long long)
968.2.1 by Monty Taylor
Build fix making 64-bit atomic<> work on 32-bit systems.
167
#  else
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
168
#   define __DRIZZLE_DECL_ATOMIC64(T)                                   \
968.2.1 by Monty Taylor
Build fix making 64-bit atomic<> work on 32-bit systems.
169
  template<> struct atomic<T>                                           \
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
170
  : internal::atomic_impl<T,T,internal::pthread_traits<T,T> > {         \
171
    atomic<T>()                                                         \
172
      : internal::atomic_impl<T,T,internal::pthread_traits<T,T> >() {}  \
968.2.1 by Monty Taylor
Build fix making 64-bit atomic<> work on 32-bit systems.
173
    T operator=( T rhs ) { return store_with_release(rhs); }            \
174
  };
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
175
__DRIZZLE_DECL_ATOMIC64(long long)
176
__DRIZZLE_DECL_ATOMIC64(unsigned long long)
968.2.1 by Monty Taylor
Build fix making 64-bit atomic<> work on 32-bit systems.
177
#  endif
1122.2.4 by Monty Taylor
Two more cleanups.
178
/* *INDENT-ON* */
968.2.1 by Monty Taylor
Build fix making 64-bit atomic<> work on 32-bit systems.
179
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
180
}
181
1122.2.10 by Monty Taylor
Fixed all of the include guards.
182
#endif /* DRIZZLED_ATOMICS_H */