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 
12 #include <boost/config/abi_prefix.hpp>
13 
14 namespace boost
15 {
16     namespace pthread
17     {
18         class pthread_mutex_scoped_lock
19         {
20             pthread_mutex_t* m;
21             bool locked;
22         public:
pthread_mutex_scoped_lock(pthread_mutex_t * m_)23             explicit pthread_mutex_scoped_lock(pthread_mutex_t* m_):
24                 m(m_),locked(true)
25             {
26                 BOOST_VERIFY(!pthread_mutex_lock(m));
27             }
unlock()28             void unlock()
29             {
30                 BOOST_VERIFY(!pthread_mutex_unlock(m));
31                 locked=false;
32             }
33 
~pthread_mutex_scoped_lock()34             ~pthread_mutex_scoped_lock()
35             {
36                 if(locked)
37                 {
38                     unlock();
39                 }
40             }
41 
42         };
43 
44         class pthread_mutex_scoped_unlock
45         {
46             pthread_mutex_t* m;
47         public:
pthread_mutex_scoped_unlock(pthread_mutex_t * m_)48             explicit pthread_mutex_scoped_unlock(pthread_mutex_t* m_):
49                 m(m_)
50             {
51                 BOOST_VERIFY(!pthread_mutex_unlock(m));
52             }
~pthread_mutex_scoped_unlock()53             ~pthread_mutex_scoped_unlock()
54             {
55                 BOOST_VERIFY(!pthread_mutex_lock(m));
56             }
57 
58         };
59     }
60 }
61 
62 #include <boost/config/abi_suffix.hpp>
63 
64 #endif
65