PageRenderTime 43ms CodeModel.GetById 37ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/extlibs/Boost/include/boost/smart_ptr/detail/spinlock_sync.hpp

https://bitbucket.org/hugoruscitti/pilascpp
C++ Header | 87 lines | 56 code | 23 blank | 8 comment | 4 complexity | 35e4fbaaf95b82dd3628f768fd487e88 MD5 | raw file
 1#ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED
 2#define BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_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 <boost/smart_ptr/detail/yield_k.hpp>
19
20#if defined( __ia64__ ) && defined( __INTEL_COMPILER )
21# include <ia64intrin.h>
22#endif
23
24namespace boost
25{
26
27namespace detail
28{
29
30class spinlock
31{
32public:
33
34    int v_;
35
36public:
37
38    bool try_lock()
39    {
40        int r = __sync_lock_test_and_set( &v_, 1 );
41        return r == 0;
42    }
43
44    void lock()
45    {
46        for( unsigned k = 0; !try_lock(); ++k )
47        {
48            boost::detail::yield( k );
49        }
50    }
51
52    void unlock()
53    {
54        __sync_lock_release( &v_ );
55    }
56
57public:
58
59    class scoped_lock
60    {
61    private:
62
63        spinlock & sp_;
64
65        scoped_lock( scoped_lock const & );
66        scoped_lock & operator=( scoped_lock const & );
67
68    public:
69
70        explicit scoped_lock( spinlock & sp ): sp_( sp )
71        {
72            sp.lock();
73        }
74
75        ~scoped_lock()
76        {
77            sp_.unlock();
78        }
79    };
80};
81
82} // namespace detail
83} // namespace boost
84
85#define BOOST_DETAIL_SPINLOCK_INIT {0}
86
87#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED