1 #ifndef BOOST_PTHREAD_MUTEX_SCOPED_LOCK_HPP
2 #define BOOST_PTHREAD_MUTEX_SCOPED_LOCK_HPP
3 //  (C) Copyright 2007-8 Anthony Williams
4 //
5 //  Distributed under the Boost Software License, Version 1.0. (See
6 //  accompanying file LICENSE_1_0.txt or copy at
7 //  http://www.boost.org/LICENSE_1_0.txt)
8 
9 #include <pthread.h>
10 #include <boost/assert.hpp>
11 #include <boost/thread/pthread/pthread_helpers.hpp>
12 
13 #include <boost/config/abi_prefix.hpp>
14 
15 namespace boost
16 {
17     namespace pthread
18     {
19         class pthread_mutex_scoped_lock
20         {
21             pthread_mutex_t* m;
22             bool locked;
23         public:
pthread_mutex_scoped_lock(pthread_mutex_t * m_)24             explicit pthread_mutex_scoped_lock(pthread_mutex_t* m_) BOOST_NOEXCEPT:
25                 m(m_),locked(true)
26             {
27                 BOOST_VERIFY(!posix::pthread_mutex_lock(m));
28             }
unlock()29             void unlock() BOOST_NOEXCEPT
30             {
31                 BOOST_VERIFY(!posix::pthread_mutex_unlock(m));
32                 locked=false;
33             }
unlock_if_locked()34             void unlock_if_locked() BOOST_NOEXCEPT
35             {
36               if(locked)
37               {
38                   unlock();
39               }
40             }
~pthread_mutex_scoped_lock()41             ~pthread_mutex_scoped_lock() BOOST_NOEXCEPT
42             {
43                 if(locked)
44                 {
45                     unlock();
46                 }
47             }
48 
49         };
50 
51         class pthread_mutex_scoped_unlock
52         {
53             pthread_mutex_t* m;
54         public:
pthread_mutex_scoped_unlock(pthread_mutex_t * m_)55             explicit pthread_mutex_scoped_unlock(pthread_mutex_t* m_) BOOST_NOEXCEPT:
56                 m(m_)
57             {
58                 BOOST_VERIFY(!posix::pthread_mutex_unlock(m));
59             }
~pthread_mutex_scoped_unlock()60             ~pthread_mutex_scoped_unlock() BOOST_NOEXCEPT
61             {
62                 BOOST_VERIFY(!posix::pthread_mutex_lock(m));
63             }
64 
65         };
66     }
67 }
68 
69 #include <boost/config/abi_suffix.hpp>
70 
71 #endif
72