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