fixing stuff
[dyninst.git] / dynutil / h / dyn_detail / boost / detail / spinlock_pt.hpp
1 #ifndef DYN_DETAIL_BOOST_DETAIL_SPINLOCK_PT_HPP_INCLUDED
2 #define DYN_DETAIL_BOOST_DETAIL_SPINLOCK_PT_HPP_INCLUDED
3
4 // MS compatible compilers support #pragma once
5
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
9
10 //
11 //  Copyright (c) 2008 Peter Dimov
12 //
13 //  Distributed under the Boost Software License, Version 1.0.
14 //  See accompanying file LICENSE_1_0.txt or copy at
15 //  http://www.boost.org/LICENSE_1_0.txt)
16 //
17
18 #include <pthread.h>
19
20 namespace dyn_detail
21 {
22   
23 namespace boost
24 {
25
26 namespace detail
27 {
28
29 class spinlock
30 {
31 public:
32
33     pthread_mutex_t v_;
34
35 public:
36
37     bool try_lock()
38     {
39         return pthread_mutex_trylock( &v_ ) == 0;
40     }
41
42     void lock()
43     {
44         pthread_mutex_lock( &v_ );
45     }
46
47     void unlock()
48     {
49         pthread_mutex_unlock( &v_ );
50     }
51
52 public:
53
54     class scoped_lock
55     {
56     private:
57
58         spinlock & sp_;
59
60         scoped_lock( scoped_lock const & );
61         scoped_lock & operator=( scoped_lock const & );
62
63     public:
64
65         explicit scoped_lock( spinlock & sp ): sp_( sp )
66         {
67             sp.lock();
68         }
69
70         ~scoped_lock()
71         {
72             sp_.unlock();
73         }
74     };
75 };
76
77 } // namespace detail
78 } // namespace boost
79 } // namespace dyn_detail
80
81
82 #define DYN_DETAIL_BOOST_DETAIL_SPINLOCK_INIT { PTHREAD_MUTEX_INITIALIZER }
83
84 #endif // #ifndef DYN_DETAIL_BOOST_DETAIL_SPINLOCK_PT_HPP_INCLUDED