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