• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED
2 #define BOOST_SMART_PTR_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 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
21 
22 #include <boost/config/pragma_message.hpp>
23 BOOST_PRAGMA_MESSAGE("Using pthread_mutex spinlock emulation")
24 
25 #endif
26 
27 namespace boost
28 {
29 
30 namespace detail
31 {
32 
33 class spinlock
34 {
35 public:
36 
37     pthread_mutex_t v_;
38 
39 public:
40 
try_lock()41     bool try_lock()
42     {
43         return pthread_mutex_trylock( &v_ ) == 0;
44     }
45 
lock()46     void lock()
47     {
48         pthread_mutex_lock( &v_ );
49     }
50 
unlock()51     void unlock()
52     {
53         pthread_mutex_unlock( &v_ );
54     }
55 
56 public:
57 
58     class scoped_lock
59     {
60     private:
61 
62         spinlock & sp_;
63 
64         scoped_lock( scoped_lock const & );
65         scoped_lock & operator=( scoped_lock const & );
66 
67     public:
68 
scoped_lock(spinlock & sp)69         explicit scoped_lock( spinlock & sp ): sp_( sp )
70         {
71             sp.lock();
72         }
73 
~scoped_lock()74         ~scoped_lock()
75         {
76             sp_.unlock();
77         }
78     };
79 };
80 
81 } // namespace detail
82 } // namespace boost
83 
84 #define BOOST_DETAIL_SPINLOCK_INIT { PTHREAD_MUTEX_INITIALIZER }
85 
86 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED
87