00001 // 00002 // posix_mutex.hpp 00003 // ~~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #ifndef ASIO_DETAIL_POSIX_MUTEX_HPP 00012 #define ASIO_DETAIL_POSIX_MUTEX_HPP 00013 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00015 # pragma once 00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00017 00018 #include "asio/detail/push_options.hpp" 00019 00020 #include "asio/detail/push_options.hpp" 00021 #include <boost/config.hpp> 00022 #include "asio/detail/pop_options.hpp" 00023 00024 #if defined(BOOST_HAS_PTHREADS) 00025 00026 #include "asio/detail/push_options.hpp" 00027 #include <boost/throw_exception.hpp> 00028 #include <pthread.h> 00029 #include "asio/detail/pop_options.hpp" 00030 00031 #include "asio/system_error.hpp" 00032 #include "asio/detail/noncopyable.hpp" 00033 #include "asio/detail/scoped_lock.hpp" 00034 00035 namespace asio { 00036 namespace detail { 00037 00038 class posix_mutex 00039 : private noncopyable 00040 { 00041 public: 00042 typedef asio::detail::scoped_lock<posix_mutex> scoped_lock; 00043 00044 // Constructor. 00045 posix_mutex() 00046 { 00047 int error = ::pthread_mutex_init(&mutex_, 0); 00048 if (error != 0) 00049 { 00050 asio::system_error e( 00051 asio::error_code(error, asio::native_ecat), 00052 "mutex"); 00053 boost::throw_exception(e); 00054 } 00055 } 00056 00057 // Destructor. 00058 ~posix_mutex() 00059 { 00060 ::pthread_mutex_destroy(&mutex_); 00061 } 00062 00063 // Lock the mutex. 00064 void lock() 00065 { 00066 int error = ::pthread_mutex_lock(&mutex_); 00067 if (error != 0) 00068 { 00069 asio::system_error e( 00070 asio::error_code(error, asio::native_ecat), 00071 "mutex"); 00072 boost::throw_exception(e); 00073 } 00074 } 00075 00076 // Unlock the mutex. 00077 void unlock() 00078 { 00079 int error = ::pthread_mutex_unlock(&mutex_); 00080 if (error != 0) 00081 { 00082 asio::system_error e( 00083 asio::error_code(error, asio::native_ecat), 00084 "mutex"); 00085 boost::throw_exception(e); 00086 } 00087 } 00088 00089 private: 00090 ::pthread_mutex_t mutex_; 00091 }; 00092 00093 } // namespace detail 00094 } // namespace asio 00095 00096 #endif // defined(BOOST_HAS_PTHREADS) 00097 00098 #include "asio/detail/pop_options.hpp" 00099 00100 #endif // ASIO_DETAIL_POSIX_MUTEX_HPP
1.5.6