posix_mutex.hpp
Go to the documentation of this file.
00001 //
00002 // posix_mutex.hpp
00003 // ~~~~~~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2008 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/error.hpp"
00032 #include "asio/system_error.hpp"
00033 #include "asio/detail/noncopyable.hpp"
00034 #include "asio/detail/scoped_lock.hpp"
00035 
00036 namespace asio {
00037 namespace detail {
00038 
00039 class posix_event;
00040 
00041 class posix_mutex
00042   : private noncopyable
00043 {
00044 public:
00045   typedef asio::detail::scoped_lock<posix_mutex> scoped_lock;
00046 
00047   // Constructor.
00048   posix_mutex()
00049   {
00050     int error = ::pthread_mutex_init(&mutex_, 0);
00051     if (error != 0)
00052     {
00053       asio::system_error e(
00054           asio::error_code(error,
00055             asio::error::get_system_category()),
00056           "mutex");
00057       boost::throw_exception(e);
00058     }
00059   }
00060 
00061   // Destructor.
00062   ~posix_mutex()
00063   {
00064     ::pthread_mutex_destroy(&mutex_);
00065   }
00066 
00067   // Lock the mutex.
00068   void lock()
00069   {
00070     int error = ::pthread_mutex_lock(&mutex_);
00071     if (error != 0)
00072     {
00073       asio::system_error e(
00074           asio::error_code(error,
00075             asio::error::get_system_category()),
00076           "mutex");
00077       boost::throw_exception(e);
00078     }
00079   }
00080 
00081   // Unlock the mutex.
00082   void unlock()
00083   {
00084     int error = ::pthread_mutex_unlock(&mutex_);
00085     if (error != 0)
00086     {
00087       asio::system_error e(
00088           asio::error_code(error,
00089             asio::error::get_system_category()),
00090           "mutex");
00091       boost::throw_exception(e);
00092     }
00093   }
00094 
00095 private:
00096   friend class posix_event;
00097   ::pthread_mutex_t mutex_;
00098 };
00099 
00100 } // namespace detail
00101 } // namespace asio
00102 
00103 #endif // defined(BOOST_HAS_PTHREADS)
00104 
00105 #include "asio/detail/pop_options.hpp"
00106 
00107 #endif // ASIO_DETAIL_POSIX_MUTEX_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


Castor
Author(s): Carpe Noctem
autogenerated on Fri Nov 8 2013 11:05:39