~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/atomics.h

  • Committer: Monty Taylor
  • Date: 2009-03-20 06:30:59 UTC
  • mfrom: (942.1.17 plugin-registration)
  • mto: This revision was merged to the branch mainline in revision 958.
  • Revision ID: mordred@inaugust.com-20090320063059-lr9hqvw15stxxgn3
Merged plugin registration branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#ifndef DRIZZLED_ATOMICS_H
22
22
#define DRIZZLED_ATOMICS_H
23
23
 
 
24
#if defined(HAVE_LIBTBB)
 
25
# include <tbb/atomic.h>
 
26
#else
 
27
 
24
28
# if defined(__SUNPRO_CC)
25
29
#  include <drizzled/atomic/sun_studio.h>
26
30
# endif
27
 
# if !defined(__ICC) && !defined(__clang__) && (defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC))
 
31
# if defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC)
28
32
#  include <drizzled/atomic/gcc_traits.h>
29
33
#  define ATOMIC_TRAITS internal::gcc_traits
30
34
# else  /* use pthread impl */
31
 
#  define ATOMIC_TRAITS internal::pthread_traits
32
 
# endif
33
 
 
34
 
# if (SIZEOF_SIZE_T >= SIZEOF_LONG_LONG) || (!defined(HAVE_GCC_ATOMIC_BUILTINS) || !defined(__SUNPRO_CC)) || defined(__ppc__)
35
35
#  include <pthread.h>
36
36
#  include <drizzled/atomic/pthread_traits.h>
 
37
#  define ATOMIC_TRAITS internal::pthread_traits
37
38
# endif
38
39
 
39
40
 
40
 
namespace drizzled {
 
41
namespace tbb {
41
42
 
42
43
namespace internal {
43
44
 
45
46
template<typename I>            // Primary template
46
47
struct atomic_base {
47
48
  volatile I my_value;
48
 
  atomic_base() : my_value(0) {}
49
 
  virtual ~atomic_base() {}
50
49
};
51
50
 
52
51
template<typename I, typename D, typename T >
56
55
public:
57
56
  typedef I value_type;
58
57
 
59
 
  atomic_impl() : atomic_base<I>(), traits() {}
60
 
 
61
 
  value_type add_and_fetch( D addend )
62
 
  {
63
 
    return traits.add_and_fetch(&this->my_value, addend);
64
 
  }
65
58
 
66
59
  value_type fetch_and_add( D addend )
67
60
  {
83
76
    return traits.fetch_and_store(&this->my_value, value);
84
77
  }
85
78
 
86
 
  bool compare_and_swap( value_type value, value_type comparand )
 
79
  value_type compare_and_swap( value_type value, value_type comparand )
87
80
  {
88
81
    return traits.compare_and_swap(&this->my_value, value, comparand);
89
82
  }
105
98
  }
106
99
 
107
100
public:
108
 
  atomic_impl<I,D,T>& operator+=( D addend )
 
101
  value_type operator+=( D addend )
109
102
  {
110
 
    increment(addend);
111
 
    return *this;
 
103
      return fetch_and_add(addend)+addend;
112
104
  }
113
105
 
114
 
  atomic_impl<I,D,T>& operator-=( D addend )
 
106
  value_type operator-=( D addend )
115
107
  {
116
108
    // Additive inverse of addend computed using binary minus,
117
109
    // instead of unary minus, for sake of avoiding compiler warnings.
118
110
    return operator+=(D(0)-addend);
119
111
  }
120
112
 
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
 
  }
 
113
  value_type operator++() {
 
114
    return fetch_and_add(1)+1;
 
115
  }
 
116
 
 
117
  value_type operator--() {
 
118
    return fetch_and_add(D(-1))-1;
 
119
  }
 
120
 
 
121
  value_type operator++(int) {
 
122
    return fetch_and_add(1);
 
123
  }
 
124
 
 
125
  value_type operator--(int) {
 
126
    return fetch_and_add(D(-1));
 
127
  }
 
128
 
130
129
};
131
130
 
132
131
} /* namespace internal */
138
137
struct atomic {
139
138
};
140
139
 
141
 
/* *INDENT-OFF* */
142
 
#define __DRIZZLE_DECL_ATOMIC(T)                                        \
 
140
#define __TBB_DECL_ATOMIC(T)                                            \
143
141
  template<> struct atomic<T>                                           \
144
142
  : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> > {                    \
145
143
    atomic<T>() : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> >() {}   \
146
 
    atomic<T>& operator=( T rhs ) { store_with_release(rhs); return *this; } \
147
 
  };
148
 
/* *INDENT-ON* */
149
 
 
150
 
 
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)
161
 
 
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
 
 */
165
 
/* *INDENT-OFF* */
166
 
# if !defined(__ppc__) && (defined(_INT64_TYPE) || defined(_LP64))
167
 
__DRIZZLE_DECL_ATOMIC(long long)
168
 
__DRIZZLE_DECL_ATOMIC(unsigned long long)
169
 
#  else
170
 
#   define __DRIZZLE_DECL_ATOMIC64(T)                                   \
171
 
  template<> struct atomic<T>                                           \
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> >() {}  \
175
144
    T operator=( T rhs ) { return store_with_release(rhs); }            \
176
145
  };
177
 
__DRIZZLE_DECL_ATOMIC64(long long)
178
 
__DRIZZLE_DECL_ATOMIC64(unsigned long long)
179
 
#  endif
180
 
/* *INDENT-ON* */
 
146
 
 
147
__TBB_DECL_ATOMIC(long long)
 
148
__TBB_DECL_ATOMIC(unsigned long long)
 
149
__TBB_DECL_ATOMIC(long)
 
150
__TBB_DECL_ATOMIC(unsigned long)
 
151
__TBB_DECL_ATOMIC(unsigned int)
 
152
__TBB_DECL_ATOMIC(int)
 
153
__TBB_DECL_ATOMIC(unsigned short)
 
154
__TBB_DECL_ATOMIC(short)
 
155
__TBB_DECL_ATOMIC(char)
 
156
__TBB_DECL_ATOMIC(signed char)
 
157
__TBB_DECL_ATOMIC(unsigned char)
181
158
 
182
159
}
 
160
#endif /* defined(HAVE_LIBTBB) */
183
161
 
184
 
#endif /* DRIZZLED_ATOMICS_H */
 
162
#endif /* DRIZZLED_ATOMIC_H */