~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/atomics.h

  • Committer: Olaf van der Spek
  • Date: 2011-07-05 11:15:32 UTC
  • mto: This revision was merged to the branch mainline in revision 2367.
  • Revision ID: olafvdspek@gmail.com-20110705111532-zod5hduzwcqe01ea
Use boost::to_lower

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