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