blob: c08b1eb90efb4e1e576ad2bfd2280efbc788460c [file] [log] [blame]
Jeff Thompsonf7d49942013-08-01 16:47:40 -07001#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
20namespace ndnboost
21{
22
23namespace detail
24{
25
26class spinlock
27{
28public:
29
30 pthread_mutex_t v_;
31
32public:
33
34 bool try_lock()
35 {
36 return pthread_mutex_trylock( &v_ ) == 0;
37 }
38
39 void lock()
40 {
41 pthread_mutex_lock( &v_ );
42 }
43
44 void unlock()
45 {
46 pthread_mutex_unlock( &v_ );
47 }
48
49public:
50
51 class scoped_lock
52 {
53 private:
54
55 spinlock & sp_;
56
57 scoped_lock( scoped_lock const & );
58 scoped_lock & operator=( scoped_lock const & );
59
60 public:
61
62 explicit scoped_lock( spinlock & sp ): sp_( sp )
63 {
64 sp.lock();
65 }
66
67 ~scoped_lock()
68 {
69 sp_.unlock();
70 }
71 };
72};
73
74} // namespace detail
75} // namespace ndnboost
76
77#define BOOST_DETAIL_SPINLOCK_INIT { PTHREAD_MUTEX_INITIALIZER }
78
79#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED