Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1 | #ifndef NDNBOOST_SMART_PTR_DETAIL_LWM_PTHREADS_HPP_INCLUDED |
| 2 | #define NDNBOOST_SMART_PTR_DETAIL_LWM_PTHREADS_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 | // |
Jeff Thompson | 9939dcd | 2013-10-15 15:12:24 -0700 | [diff] [blame] | 11 | // ndnboost/detail/lwm_pthreads.hpp |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 12 | // |
| 13 | // Copyright (c) 2002 Peter Dimov and Multi Media Ltd. |
| 14 | // |
| 15 | // Distributed under the Boost Software License, Version 1.0. (See |
| 16 | // accompanying file LICENSE_1_0.txt or copy at |
| 17 | // http://www.boost.org/LICENSE_1_0.txt) |
| 18 | // |
| 19 | |
Jeff Thompson | 2277ce5 | 2013-08-01 17:34:11 -0700 | [diff] [blame] | 20 | #include <ndnboost/assert.hpp> |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 21 | #include <pthread.h> |
| 22 | |
| 23 | namespace ndnboost |
| 24 | { |
| 25 | |
| 26 | namespace detail |
| 27 | { |
| 28 | |
| 29 | class lightweight_mutex |
| 30 | { |
| 31 | private: |
| 32 | |
| 33 | pthread_mutex_t m_; |
| 34 | |
| 35 | lightweight_mutex(lightweight_mutex const &); |
| 36 | lightweight_mutex & operator=(lightweight_mutex const &); |
| 37 | |
| 38 | public: |
| 39 | |
| 40 | lightweight_mutex() |
| 41 | { |
| 42 | |
| 43 | // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init |
| 44 | |
| 45 | #if defined(__hpux) && defined(_DECTHREADS_) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 46 | NDNBOOST_VERIFY( pthread_mutex_init( &m_, pthread_mutexattr_default ) == 0 ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 47 | #else |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 48 | NDNBOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 49 | #endif |
| 50 | } |
| 51 | |
| 52 | ~lightweight_mutex() |
| 53 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 54 | NDNBOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | class scoped_lock; |
| 58 | friend class scoped_lock; |
| 59 | |
| 60 | class scoped_lock |
| 61 | { |
| 62 | private: |
| 63 | |
| 64 | pthread_mutex_t & m_; |
| 65 | |
| 66 | scoped_lock(scoped_lock const &); |
| 67 | scoped_lock & operator=(scoped_lock const &); |
| 68 | |
| 69 | public: |
| 70 | |
| 71 | scoped_lock(lightweight_mutex & m): m_(m.m_) |
| 72 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 73 | NDNBOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | ~scoped_lock() |
| 77 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 78 | NDNBOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | }; |
| 82 | |
| 83 | } // namespace detail |
| 84 | |
| 85 | } // namespace ndnboost |
| 86 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 87 | #endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_LWM_PTHREADS_HPP_INCLUDED |