~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/detail/atomic_count_pthreads.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef BOOST_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
 
2
#define BOOST_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
 
3
 
 
4
//
 
5
//  boost/detail/atomic_count_pthreads.hpp
 
6
//
 
7
//  Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
 
8
//
 
9
//  Permission to copy, use, modify, sell and distribute this software
 
10
//  is granted provided this copyright notice appears in all copies.
 
11
//  This software is provided "as is" without express or implied
 
12
//  warranty, and with no claim as to its suitability for any purpose.
 
13
//
 
14
 
 
15
#include <pthread.h>
 
16
 
 
17
//
 
18
//  The generic pthread_mutex-based implementation sometimes leads to
 
19
//    inefficiencies. Example: a class with two atomic_count members
 
20
//    can get away with a single mutex.
 
21
//
 
22
//  Users can detect this situation by checking BOOST_AC_USE_PTHREADS.
 
23
//
 
24
 
 
25
namespace boost
 
26
{
 
27
 
 
28
namespace detail
 
29
{
 
30
 
 
31
class atomic_count
 
32
{
 
33
private:
 
34
 
 
35
    class scoped_lock
 
36
    {
 
37
    public:
 
38
 
 
39
        scoped_lock(pthread_mutex_t & m): m_(m)
 
40
        {
 
41
            pthread_mutex_lock(&m_);
 
42
        }
 
43
 
 
44
        ~scoped_lock()
 
45
        {
 
46
            pthread_mutex_unlock(&m_);
 
47
        }
 
48
 
 
49
    private:
 
50
 
 
51
        pthread_mutex_t & m_;
 
52
    };
 
53
 
 
54
public:
 
55
 
 
56
    explicit atomic_count(long v): value_(v)
 
57
    {
 
58
        pthread_mutex_init(&mutex_, 0);
 
59
    }
 
60
 
 
61
    ~atomic_count()
 
62
    {
 
63
        pthread_mutex_destroy(&mutex_);
 
64
    }
 
65
 
 
66
    void operator++()
 
67
    {
 
68
        scoped_lock lock(mutex_);
 
69
        ++value_;
 
70
    }
 
71
 
 
72
    long operator--()
 
73
    {
 
74
        scoped_lock lock(mutex_);
 
75
        return --value_;
 
76
    }
 
77
 
 
78
    operator long() const
 
79
    {
 
80
        scoped_lock lock(mutex_);
 
81
        return value_;
 
82
    }
 
83
 
 
84
private:
 
85
 
 
86
    atomic_count(atomic_count const &);
 
87
    atomic_count & operator=(atomic_count const &);
 
88
 
 
89
    mutable pthread_mutex_t mutex_;
 
90
    long value_;
 
91
};
 
92
 
 
93
} // namespace detail
 
94
 
 
95
} // namespace boost
 
96
 
 
97
#endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED