~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/atomic/pthread_traits.h

  • Committer: Monty Taylor
  • Date: 2009-03-04 02:48:12 UTC
  • mto: (917.1.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 918.
  • Revision ID: mordred@inaugust.com-20090304024812-5wb6wpye5c1iitbq
Applied atomic patch to current tree.

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
 
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 tbb {
 
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();
 
100
    value_type ret= *value;
 
101
    my_lock.unlock();
 
102
    return ret;
 
103
  }
 
104
 
 
105
  inline value_type compare_and_swap(volatile value_type *value,
 
106
                                     value_type new_value,
 
107
                                     value_type comparand )
 
108
  {
 
109
    my_lock.lock();
 
110
    if (*value == comparand)
 
111
      *value= new_value;
 
112
    value_type ret= *value;
 
113
    my_lock.unlock();
 
114
    return ret;
 
115
  }
 
116
 
 
117
  inline value_type fetch(const volatile value_type *value) const volatile
 
118
  {
 
119
    return *value;
 
120
  }
 
121
 
 
122
  inline value_type store_with_release(volatile value_type *value,
 
123
                                       value_type new_value)
 
124
  {
 
125
    my_lock.lock();
 
126
    *value= new_value;
 
127
    value_type ret= *value;
 
128
    my_lock.unlock();
 
129
    return ret;
 
130
  }
 
131
 
 
132
}; /* pthread_traits */
 
133
 
 
134
 
 
135
} /* namespace internal */
 
136
} /* namespace tbb */
 
137
 
 
138
 
 
139
#endif /* DRIZZLED_ATOMIC_PTHREAD_TRAITS_H */