~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/atomic/pthread_traits.h

  • Committer: Mark Atwood
  • Date: 2009-03-04 01:02:00 UTC
  • mto: (968.2.20 mordred)
  • mto: This revision was merged to the branch mainline in revision 971.
  • Revision ID: me@mark.atwood.name-20090304010200-t1n4xxdoil2yae9a
add gearman logging plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#ifndef DRIZZLED_ATOMIC_PTHREAD_TRAITS_H
22
 
#define DRIZZLED_ATOMIC_PTHREAD_TRAITS_H
23
 
 
24
 
namespace drizzled {
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)
35
 
   : the_mutex(),
36
 
     locked(false)
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
 
 
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
 
 
79
 
  inline value_type fetch_and_add(volatile value_type *value, D addend )
80
 
  {
81
 
    my_lock.lock();
82
 
    value_type ret= *value;
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();
91
 
    value_type ret= *value;
92
 
    (*value)++;
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();
100
 
    value_type ret= *value;
101
 
    (*value)--;
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();
110
 
    value_type ret= *value;
111
 
    *value= new_value;
112
 
    my_lock.unlock();
113
 
    return ret;
114
 
  }
115
 
 
116
 
  inline bool compare_and_swap(volatile value_type *value,
117
 
                                     value_type new_value,
118
 
                                     value_type comparand )
119
 
  {
120
 
    my_lock.lock();
121
 
    bool ret= (*value == comparand);
122
 
    if (ret)
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
 
  {
130
 
    const_cast<pthread_traits *>(this)->my_lock.lock();
131
 
    value_type ret= *value;
132
 
    const_cast<pthread_traits *>(this)->my_lock.unlock();
133
 
    return ret;
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 */
150
 
} /* namespace drizzled */
151
 
 
152
 
 
153
 
#endif /* DRIZZLED_ATOMIC_PTHREAD_TRAITS_H */