Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 1 | #ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED |
| 2 | #define NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 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 ndnboost |
| 21 | { |
| 22 | |
| 23 | namespace detail |
| 24 | { |
| 25 | |
| 26 | class spinlock |
| 27 | { |
| 28 | public: |
| 29 | |
| 30 | pthread_mutex_t v_; |
| 31 | |
| 32 | public: |
| 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 | |
| 49 | public: |
| 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 77 | #define NDNBOOST_DETAIL_SPINLOCK_INIT { PTHREAD_MUTEX_INITIALIZER } |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 78 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 79 | #endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED |