~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/atomics.h

  • Committer: Brian Aker
  • Date: 2010-02-10 18:04:24 UTC
  • mfrom: (1286.1.5 build)
  • Revision ID: brian@gaz-20100210180424-03ypoyifmlc2lgcp
Merge of Brian/Padraig

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
27
# if !defined(__ICC) && (defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC))
 
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
 
 
34
# if (SIZEOF_SIZE_T >= SIZEOF_LONG_LONG) || (!defined(HAVE_GCC_ATOMIC_BUILTINS) || !defined(__SUNPRO_CC)) || defined(__ppc__)
 
35
#  include <pthread.h>
 
36
#  include <drizzled/atomic/pthread_traits.h>
 
37
# endif
 
38
 
 
39
 
 
40
namespace drizzled {
 
41
 
 
42
namespace internal {
 
43
 
 
44
 
 
45
template<typename I>            // Primary template
 
46
struct atomic_base {
 
47
  volatile I my_value;
 
48
  atomic_base() : my_value(0) {}
 
49
  virtual ~atomic_base() {}
 
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
 
 
59
  atomic_impl() : atomic_base<I>(), traits() {}
 
60
 
 
61
  value_type fetch_and_add( D addend )
 
62
  {
 
63
    return traits.fetch_and_add(&this->my_value, addend);
 
64
  }
 
65
 
 
66
  value_type fetch_and_increment()
 
67
  {
 
68
    return traits.fetch_and_increment(&this->my_value);
 
69
  }
 
70
 
 
71
  value_type fetch_and_decrement()
 
72
  {
 
73
    return traits.fetch_and_decrement(&this->my_value);
 
74
  }
 
75
 
 
76
  value_type fetch_and_store( value_type value )
 
77
  {
 
78
    return traits.fetch_and_store(&this->my_value, value);
 
79
  }
 
80
 
 
81
  value_type compare_and_swap( value_type value, value_type comparand )
 
82
  {
 
83
    return traits.compare_and_swap(&this->my_value, value, comparand);
 
84
  }
 
85
 
 
86
  operator value_type() const volatile
 
87
  {
 
88
    return traits.fetch(&this->my_value);
 
89
  }
 
90
 
 
91
  value_type& _internal_reference() const
 
92
  {
 
93
    return this->my_value;
 
94
  }
 
95
 
 
96
protected:
 
97
  value_type store_with_release( value_type rhs )
 
98
  {
 
99
    return traits.store_with_release(&this->my_value, rhs);
 
100
  }
 
101
 
 
102
public:
 
103
  atomic_impl<I,D,T>& operator+=( D addend )
 
104
  {
 
105
    fetch_and_add(addend)+addend;
 
106
    return *this;
 
107
  }
 
108
 
 
109
  atomic_impl<I,D,T>& operator-=( D addend )
 
110
  {
 
111
    // Additive inverse of addend computed using binary minus,
 
112
    // instead of unary minus, for sake of avoiding compiler warnings.
 
113
    return operator+=(D(0)-addend);
 
114
  }
 
115
 
 
116
  value_type increment() {
 
117
    return fetch_and_add(1)+1;
 
118
  }
 
119
 
 
120
  value_type decrement() {
 
121
    return fetch_and_add(D(-1))-1;
 
122
  }
 
123
 
 
124
 
 
125
};
 
126
 
 
127
} /* namespace internal */
 
128
 
 
129
//! Primary template for atomic.
 
130
/** See the Reference for details.
 
131
    @ingroup synchronization */
 
132
template<typename T>
 
133
struct atomic {
 
134
};
 
135
 
 
136
/* *INDENT-OFF* */
 
137
#define __DRIZZLE_DECL_ATOMIC(T)                                        \
 
138
  template<> struct atomic<T>                                           \
 
139
  : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> > {                    \
 
140
    atomic<T>() : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> >() {}   \
 
141
    atomic<T>& operator=( T rhs ) { store_with_release(rhs); return *this; } \
 
142
  };
 
143
/* *INDENT-ON* */
 
144
 
 
145
 
 
146
__DRIZZLE_DECL_ATOMIC(long)
 
147
__DRIZZLE_DECL_ATOMIC(unsigned long)
 
148
__DRIZZLE_DECL_ATOMIC(unsigned int)
 
149
__DRIZZLE_DECL_ATOMIC(int)
 
150
__DRIZZLE_DECL_ATOMIC(unsigned short)
 
151
__DRIZZLE_DECL_ATOMIC(short)
 
152
__DRIZZLE_DECL_ATOMIC(char)
 
153
__DRIZZLE_DECL_ATOMIC(signed char)
 
154
__DRIZZLE_DECL_ATOMIC(unsigned char)
 
155
__DRIZZLE_DECL_ATOMIC(bool)
 
156
 
 
157
/* 32-bit platforms don't have a GCC atomic operation for 64-bit types,
 
158
 * so we'll use pthread locks to handler 64-bit types on that platforms
 
159
 */
 
160
/* *INDENT-OFF* */
 
161
# if !defined(__ppc__) && (defined(_INT64_TYPE) || defined(_LP64))
 
162
__DRIZZLE_DECL_ATOMIC(long long)
 
163
__DRIZZLE_DECL_ATOMIC(unsigned long long)
 
164
#  else
 
165
#   define __DRIZZLE_DECL_ATOMIC64(T)                                   \
 
166
  template<> struct atomic<T>                                           \
 
167
  : internal::atomic_impl<T,T,internal::pthread_traits<T,T> > {         \
 
168
    atomic<T>()                                                         \
 
169
      : internal::atomic_impl<T,T,internal::pthread_traits<T,T> >() {}  \
 
170
    T operator=( T rhs ) { return store_with_release(rhs); }            \
 
171
  };
 
172
__DRIZZLE_DECL_ATOMIC64(long long)
 
173
__DRIZZLE_DECL_ATOMIC64(unsigned long long)
 
174
#  endif
 
175
/* *INDENT-ON* */
 
176
 
 
177
}
 
178
 
 
179
#endif /* DRIZZLED_ATOMICS_H */