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 |
*
|
|
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_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: |
|
34 |
mutex_wrapper(void): locked(false) |
|
35 |
{
|
|
36 |
locked= false; |
|
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 |
||
69 |
inline value_type fetch_and_add(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 |
||
78 |
inline value_type fetch_and_increment(volatile value_type *value) |
|
79 |
{
|
|
80 |
my_lock.lock(); |
|
81 |
*value++; |
|
82 |
value_type ret= *value; |
|
83 |
my_lock.unlock(); |
|
84 |
return ret; |
|
85 |
}
|
|
86 |
||
87 |
inline value_type fetch_and_decrement(volatile value_type *value) |
|
88 |
{
|
|
89 |
my_lock.lock(); |
|
90 |
*value--; |
|
91 |
value_type ret= *value; |
|
92 |
my_lock.unlock(); |
|
93 |
return ret; |
|
94 |
}
|
|
95 |
||
96 |
inline value_type fetch_and_store(volatile value_type *value, |
|
97 |
value_type new_value ) |
|
98 |
{
|
|
99 |
my_lock.lock(); |
|
1039.5.34
by Jay Pipes
Corrected mistake in pthread_traits fetch_and_store() method where new_value was not being stored. |
100 |
*value= new_value; |
910.5.2
by Monty Taylor
Applied atomic patch to current tree. |
101 |
value_type ret= *value; |
102 |
my_lock.unlock(); |
|
103 |
return ret; |
|
104 |
}
|
|
105 |
||
106 |
inline value_type compare_and_swap(volatile value_type *value, |
|
107 |
value_type new_value, |
|
108 |
value_type comparand ) |
|
109 |
{
|
|
110 |
my_lock.lock(); |
|
111 |
if (*value == comparand) |
|
112 |
*value= new_value; |
|
113 |
value_type ret= *value; |
|
114 |
my_lock.unlock(); |
|
115 |
return ret; |
|
116 |
}
|
|
117 |
||
118 |
inline value_type fetch(const volatile value_type *value) const volatile |
|
119 |
{
|
|
120 |
return *value; |
|
121 |
}
|
|
122 |
||
123 |
inline value_type store_with_release(volatile value_type *value, |
|
124 |
value_type new_value) |
|
125 |
{
|
|
126 |
my_lock.lock(); |
|
127 |
*value= new_value; |
|
128 |
value_type ret= *value; |
|
129 |
my_lock.unlock(); |
|
130 |
return ret; |
|
131 |
}
|
|
132 |
||
133 |
}; /* pthread_traits */ |
|
134 |
||
135 |
||
136 |
} /* namespace internal */ |
|
968.2.3
by Monty Taylor
Replacd use of tbb:: namespace with drizzled:: |
137 |
} /* namespace drizzled */ |
910.5.2
by Monty Taylor
Applied atomic patch to current tree. |
138 |
|
139 |
||
140 |
#endif /* DRIZZLED_ATOMIC_PTHREAD_TRAITS_H */ |