~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
21
#ifndef DRIZZLED_ATOMIC_PTHREAD_TRAITS_H
22
#define DRIZZLED_ATOMIC_PTHREAD_TRAITS_H
23
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
24
namespace drizzled {
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
25
26
namespace internal {
27
28
class mutex_wrapper
29
{
30
private:
31
  pthread_mutex_t the_mutex;
32
  bool locked;
33
public:
1241.16.1 by Monty Taylor
Fixed some effc++ warnings.
34
  mutex_wrapper(void)
35
   : the_mutex(),
36
     locked(false)
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
37
  {
38
    (void) pthread_mutex_init(&the_mutex,  NULL);
39
  }
40
  ~mutex_wrapper(void)
41
  {
42
    if (locked)
43
      unlock();
44
    pthread_mutex_destroy(&the_mutex);
45
  }
46
  void lock(void)
47
  {
48
    pthread_mutex_lock(&the_mutex);
49
    locked=true;
50
  }
51
  void unlock(void)
52
  {
53
    pthread_mutex_unlock(&the_mutex);
54
    locked=false;
55
  }
56
};
57
58
template<typename T, typename D>
59
class pthread_traits
60
{
61
private:
62
  mutex_wrapper my_lock;
63
64
public:
65
66
  typedef T value_type;
67
68
  pthread_traits() {}
69
1405.4.7 by Jay Pipes
* Fixes drizzled's atomics:
70
  inline value_type add_and_fetch(volatile value_type *value, D addend )
71
  {
72
    my_lock.lock();
73
    *value += addend;
74
    value_type ret= *value;
75
    my_lock.unlock();
76
    return ret;
77
  }
78
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
79
  inline value_type fetch_and_add(volatile value_type *value, D addend )
80
  {
81
    my_lock.lock();
1405.4.7 by Jay Pipes
* Fixes drizzled's atomics:
82
    value_type ret= *value;
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
83
    *value += addend;
84
    my_lock.unlock();
85
    return ret;
86
  }
87
88
  inline value_type fetch_and_increment(volatile value_type *value)
89
  {
90
    my_lock.lock();
1405.4.7 by Jay Pipes
* Fixes drizzled's atomics:
91
    value_type ret= *value;
1491.6.9 by Stewart Smith
fix pthread atomics. operator precedence is important. The unit test now passes.
92
    (*value)++;
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
93
    my_lock.unlock();
94
    return ret;
95
  }
96
97
  inline value_type fetch_and_decrement(volatile value_type *value)
98
  {
99
    my_lock.lock();
1405.4.7 by Jay Pipes
* Fixes drizzled's atomics:
100
    value_type ret= *value;
1491.6.9 by Stewart Smith
fix pthread atomics. operator precedence is important. The unit test now passes.
101
    (*value)--;
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
102
    my_lock.unlock();
103
    return ret;
104
  }
105
106
  inline value_type fetch_and_store(volatile value_type *value,
107
                                    value_type new_value )
108
  {
109
    my_lock.lock();
1405.4.7 by Jay Pipes
* Fixes drizzled's atomics:
110
    value_type ret= *value;
1039.5.34 by Jay Pipes
Corrected mistake in pthread_traits fetch_and_store() method where new_value was not being stored.
111
    *value= new_value;
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
112
    my_lock.unlock();
113
    return ret;
114
  }
115
1405.4.7 by Jay Pipes
* Fixes drizzled's atomics:
116
  inline bool compare_and_swap(volatile value_type *value,
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
117
                                     value_type new_value,
118
                                     value_type comparand )
119
  {
120
    my_lock.lock();
1405.4.7 by Jay Pipes
* Fixes drizzled's atomics:
121
    bool ret= (*value == comparand);
122
    if (ret)
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
123
      *value= new_value;
124
    my_lock.unlock();
125
    return ret;
126
  }
127
128
  inline value_type fetch(const volatile value_type *value) const volatile
129
  {
1405.4.16 by Jay Pipes
Try another const_cast<> technique on pthread_traits.
130
    const_cast<pthread_traits *>(this)->my_lock.lock();
1405.4.7 by Jay Pipes
* Fixes drizzled's atomics:
131
    value_type ret= *value;
1405.4.16 by Jay Pipes
Try another const_cast<> technique on pthread_traits.
132
    const_cast<pthread_traits *>(this)->my_lock.unlock();
1405.4.7 by Jay Pipes
* Fixes drizzled's atomics:
133
    return ret;
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
134
  }
135
136
  inline value_type store_with_release(volatile value_type *value,
137
                                       value_type new_value)
138
  {
139
    my_lock.lock();
140
    *value= new_value;
141
    value_type ret= *value;
142
    my_lock.unlock();
143
    return ret;
144
  }
145
146
}; /* pthread_traits */
147
148
149
} /* namespace internal */
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
150
} /* namespace drizzled */
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
151
152
153
#endif /* DRIZZLED_ATOMIC_PTHREAD_TRAITS_H */