Drizzled Public API Documentation

pthread_traits.h
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, Inc.
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 #pragma once
22 
23 namespace drizzled {
24 
25 namespace internal {
26 
28 {
29 private:
30  pthread_mutex_t the_mutex;
31  bool locked;
32 public:
33  mutex_wrapper(void)
34  : the_mutex(),
35  locked(false)
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>
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 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 
78  inline value_type fetch_and_add(volatile value_type *value, D addend )
79  {
80  my_lock.lock();
81  value_type ret= *value;
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();
90  value_type ret= *value;
91  (*value)++;
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();
99  value_type ret= *value;
100  (*value)--;
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();
109  value_type ret= *value;
110  *value= new_value;
111  my_lock.unlock();
112  return ret;
113  }
114 
115  inline bool compare_and_swap(volatile value_type *value,
116  value_type new_value,
117  value_type comparand )
118  {
119  my_lock.lock();
120  bool ret= (*value == comparand);
121  if (ret)
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  {
129  const_cast<pthread_traits *>(this)->my_lock.lock();
130  value_type ret= *value;
131  const_cast<pthread_traits *>(this)->my_lock.unlock();
132  return ret;
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 */
149 } /* namespace drizzled */
150 
151