~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/atomics.h

  • Committer: Monty Taylor
  • Date: 2009-03-04 02:48:12 UTC
  • mto: (917.1.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 918.
  • Revision ID: mordred@inaugust.com-20090304024812-5wb6wpye5c1iitbq
Applied atomic patch to current tree.

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(HAVE_LIBTBB)
 
25
# include <tbb/atomic.h>
 
26
#else
 
27
 
 
28
# if defined(__SUNPRO_CC)
 
29
#  include <drizzled/atomic/sun_studio.h>
 
30
# endif
 
31
# if defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC)
 
32
#  include <drizzled/atomic/gcc_traits.h>
 
33
#  define ATOMIC_TRAITS internal::gcc_traits
 
34
# else  /* use pthread impl */
 
35
#  include <pthread.h>
 
36
#  include <drizzled/atomic/pthread_traits.h>
 
37
#  define ATOMIC_TRAITS internal::pthread_traits
 
38
# endif
 
39
 
 
40
 
 
41
namespace tbb {
 
42
 
 
43
namespace internal {
 
44
 
 
45
 
 
46
template<typename I>            // Primary template
 
47
struct atomic_base {
 
48
  volatile I my_value;
 
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
 
 
59
  value_type fetch_and_add( D addend )
 
60
  {
 
61
    return traits.fetch_and_add(&this->my_value, addend);
 
62
  }
 
63
 
 
64
  value_type fetch_and_increment()
 
65
  {
 
66
    return traits.fetch_and_increment(&this->my_value);
 
67
  }
 
68
 
 
69
  value_type fetch_and_decrement()
 
70
  {
 
71
    return traits.fetch_and_decrement(&this->my_value);
 
72
  }
 
73
 
 
74
  value_type fetch_and_store( value_type value )
 
75
  {
 
76
    return traits.fetch_and_store(&this->my_value, value);
 
77
  }
 
78
 
 
79
  value_type compare_and_swap( value_type value, value_type comparand )
 
80
  {
 
81
    return traits.compare_and_swap(&this->my_value, value, comparand);
 
82
  }
 
83
 
 
84
  operator value_type() const volatile
 
85
  {
 
86
    return traits.fetch(&this->my_value);
 
87
  }
 
88
 
 
89
  value_type& _internal_reference() const
 
90
  {
 
91
    return this->my_value;
 
92
  }
 
93
 
 
94
protected:
 
95
  value_type store_with_release( value_type rhs )
 
96
  {
 
97
    return traits.store_with_release(&this->my_value, rhs);
 
98
  }
 
99
 
 
100
public:
 
101
  value_type operator+=( D addend )
 
102
  {
 
103
      return fetch_and_add(addend)+addend;
 
104
  }
 
105
 
 
106
  value_type operator-=( D addend )
 
107
  {
 
108
    // Additive inverse of addend computed using binary minus,
 
109
    // instead of unary minus, for sake of avoiding compiler warnings.
 
110
    return operator+=(D(0)-addend);
 
111
  }
 
112
 
 
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
 
 
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
#define __TBB_DECL_ATOMIC(T)                                            \
 
141
  template<> struct atomic<T>                                           \
 
142
  : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> > {                    \
 
143
    atomic<T>() : internal::atomic_impl<T,T,ATOMIC_TRAITS<T,T> >() {}   \
 
144
    T operator=( T rhs ) { return store_with_release(rhs); }            \
 
145
  };
 
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)
 
158
 
 
159
}
 
160
#endif /* defined(HAVE_LIBTBB) */
 
161
 
 
162
#endif /* DRIZZLED_ATOMIC_H */