~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Sun Microsystems
 *  Copyright 2005-2008 Intel Corporation.  All Rights Reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef DRIZZLED_ATOMICS_H
#define DRIZZLED_ATOMICS_H

#if defined(HAVE_LIBTBB)
# include <tbb/atomic.h>
/* We're actually using the TBB interface directly, but we don't want to tie
 * the code to a specific implementation. So suck the tbb:: stuff into the
 * drizzled namespace
 */
namespace drizzled {
  using namespace tbb;
}
#else

# if defined(__SUNPRO_CC)
#  include <drizzled/atomic/sun_studio.h>
# endif
# if defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC)
#  include <drizzled/atomic/gcc_traits.h>
#  define ATOMIC_TRAITS internal::gcc_traits
# else  /* use pthread impl */
#  define ATOMIC_TRAITS internal::pthread_traits
# endif

# if (SIZEOF_SIZE_T >= SIZEOF_LONG_LONG) || (!defined(HAVE_GCC_ATOMIC_BUILTINS) || !defined(__SUNPRO_CC))
#  include <pthread.h>
#  include <drizzled/atomic/pthread_traits.h>
# endif


namespace drizzled {

namespace internal {


template<typename I>            // Primary template
struct atomic_base {
  volatile I my_value;
};

template<typename I, typename D, typename T >
class atomic_impl: private atomic_base<I>
{
  T traits;
public:
  typedef I value_type;


  value_type fetch_and_add( D addend )
  {
    return traits.fetch_and_add(&this->my_value, addend);
  }

  value_type fetch_and_increment()
  {
    return traits.fetch_and_increment(&this->my_value);
  }

  value_type fetch_and_decrement()
  {
    return traits.fetch_and_decrement(&this->my_value);
  }

  value_type fetch_and_store( value_type value )
  {
    return traits.fetch_and_store(&this->my_value, value);
  }

  value_type compare_and_swap( value_type value, value_type comparand )
  {
    return traits.compare_and_swap(&this->my_value, value, comparand);
  }

  operator value_type() const volatile
  {
    return traits.fetch(&this->my_value);
  }

  value_type& _internal_reference() const
  {
    return this->my_value;
  }

protected:
  value_type store_with_release( value_type rhs )
  {
    return traits.store_with_release(&this->my_value, rhs);
  }

public:
  value_type operator+=( D addend )
  {
      return fetch_and_add(addend)+addend;
  }

  value_type operator-=( D addend )
  {
    // Additive inverse of addend computed using binary minus,
    // instead of unary minus, for sake of avoiding compiler warnings.
    return operator+=(D(0)-addend);
  }

  value_type operator++() {
    return fetch_and_add(1)+1;
  }

  value_type operator--() {
    return fetch_and_add(D(-1))-1;
  }

  value_type operator++(int) {
    return fetch_and_add(1);
  }

  value_type operator--(int) {
    return fetch_and_add(D(-1));
  }

};

} /* namespace internal */

//! Primary template for atomic.
/** See the Reference for details.
    @ingroup synchronization */
template<typename T>
struct atomic {
};

#define __DRIZZLE_DECL_ATOMIC(T)                                        \
  template<> struct atomic<T>                                           \
  : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> > {                    \
    atomic<T>() : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> >() {}   \
    T operator=( T rhs ) { return store_with_release(rhs); }            \
  };


__DRIZZLE_DECL_ATOMIC(long)
__DRIZZLE_DECL_ATOMIC(unsigned long)
__DRIZZLE_DECL_ATOMIC(unsigned int)
__DRIZZLE_DECL_ATOMIC(int)
__DRIZZLE_DECL_ATOMIC(unsigned short)
__DRIZZLE_DECL_ATOMIC(short)
__DRIZZLE_DECL_ATOMIC(char)
__DRIZZLE_DECL_ATOMIC(signed char)
__DRIZZLE_DECL_ATOMIC(unsigned char)
__DRIZZLE_DECL_ATOMIC(bool)

/* 32-bit platforms don't have a GCC atomic operation for 64-bit types,
 * so we'll use pthread locks to handler 64-bit types on that platforms
 */
#  if SIZEOF_SIZE_T >= SIZEOF_LONG_LONG
__DRIZZLE_DECL_ATOMIC(long long)
__DRIZZLE_DECL_ATOMIC(unsigned long long)
#  else
#   define __DRIZZLE_DECL_ATOMIC64(T)                                   \
  template<> struct atomic<T>                                           \
  : internal::atomic_impl<T,T,internal::pthread_traits<T,T> > {         \
    atomic<T>()                                                         \
      : internal::atomic_impl<T,T,internal::pthread_traits<T,T> >() {}  \
    T operator=( T rhs ) { return store_with_release(rhs); }            \
  };
__DRIZZLE_DECL_ATOMIC64(long long)
__DRIZZLE_DECL_ATOMIC64(unsigned long long)
#  endif

}
# endif /* defined(HAVE_LIBTBB) */

#endif /* DRIZZLED_ATOMIC_H */