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 namespace internal {
25 
27 {
28 private:
29  pthread_mutex_t the_mutex;
30  bool locked;
31 public:
32  mutex_wrapper(void)
33  : the_mutex(),
34  locked(false)
35  {
36  (void) pthread_mutex_init(&the_mutex, NULL);
37  }
38  ~mutex_wrapper(void)
39  {
40  if (locked)
41  unlock();
42  pthread_mutex_destroy(&the_mutex);
43  }
44  void lock(void)
45  {
46  pthread_mutex_lock(&the_mutex);
47  locked=true;
48  }
49  void unlock(void)
50  {
51  pthread_mutex_unlock(&the_mutex);
52  locked=false;
53  }
54 };
55 
56 template<typename T, typename D>
58 {
59 private:
60  mutex_wrapper my_lock;
61 
62 public:
63  typedef T value_type;
64 
65  pthread_traits() {}
66 
67  inline value_type add_and_fetch(volatile value_type *value, D addend )
68  {
69  my_lock.lock();
70  *value += addend;
71  value_type ret= *value;
72  my_lock.unlock();
73  return ret;
74  }
75 
76  inline value_type fetch_and_add(volatile value_type *value, D addend )
77  {
78  my_lock.lock();
79  value_type ret= *value;
80  *value += addend;
81  my_lock.unlock();
82  return ret;
83  }
84 
85  inline value_type fetch_and_increment(volatile value_type *value)
86  {
87  my_lock.lock();
88  value_type ret= *value;
89  (*value)++;
90  my_lock.unlock();
91  return ret;
92  }
93 
94  inline value_type fetch_and_decrement(volatile value_type *value)
95  {
96  my_lock.lock();
97  value_type ret= *value;
98  (*value)--;
99  my_lock.unlock();
100  return ret;
101  }
102 
103  inline value_type fetch_and_store(volatile value_type *value,
104  value_type new_value )
105  {
106  my_lock.lock();
107  value_type ret= *value;
108  *value= new_value;
109  my_lock.unlock();
110  return ret;
111  }
112 
113  inline bool compare_and_swap(volatile value_type *value,
114  value_type new_value,
115  value_type comparand )
116  {
117  my_lock.lock();
118  bool ret= (*value == comparand);
119  if (ret)
120  *value= new_value;
121  my_lock.unlock();
122  return ret;
123  }
124 
125  inline value_type fetch(const volatile value_type *value) const volatile
126  {
127  const_cast<pthread_traits *>(this)->my_lock.lock();
128  value_type ret= *value;
129  const_cast<pthread_traits *>(this)->my_lock.unlock();
130  return ret;
131  }
132 
133  inline value_type store_with_release(volatile value_type *value,
134  value_type new_value)
135  {
136  my_lock.lock();
137  *value= new_value;
138  value_type ret= *value;
139  my_lock.unlock();
140  return ret;
141  }
142 
143 }; /* pthread_traits */
144 
145 
146 } /* namespace internal */
147 } /* namespace drizzled */
TODO: Rename this file - func.h is stupid.