Home | History | Annotate | Download | only in detail
      1 //
      2 // detail/posix_mutex.hpp
      3 // ~~~~~~~~~~~~~~~~~~~~~~
      4 //
      5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
      6 //
      7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
      8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
      9 //
     10 
     11 #ifndef ASIO_DETAIL_POSIX_MUTEX_HPP
     12 #define ASIO_DETAIL_POSIX_MUTEX_HPP
     13 
     14 
     15 #include "asio/detail/config.hpp"
     16 
     17 #if defined(ASIO_HAS_PTHREADS)
     18 
     19 #include <pthread.h>
     20 #include "asio/detail/noncopyable.hpp"
     21 #include "asio/detail/scoped_lock.hpp"
     22 
     23 #include "asio/detail/push_options.hpp"
     24 
     25 namespace asio {
     26 namespace detail {
     27 
     28 class posix_event;
     29 
     30 class posix_mutex
     31   : private noncopyable
     32 {
     33 public:
     34   typedef asio::detail::scoped_lock<posix_mutex> scoped_lock;
     35 
     36   // Constructor.
     37   ASIO_DECL posix_mutex();
     38 
     39   // Destructor.
     40   ~posix_mutex()
     41   {
     42     ::pthread_mutex_destroy(&mutex_); // Ignore EBUSY.
     43   }
     44 
     45   // Lock the mutex.
     46   void lock()
     47   {
     48     (void)::pthread_mutex_lock(&mutex_); // Ignore EINVAL.
     49   }
     50 
     51   // Unlock the mutex.
     52   void unlock()
     53   {
     54     (void)::pthread_mutex_unlock(&mutex_); // Ignore EINVAL.
     55   }
     56 
     57 private:
     58   friend class posix_event;
     59   ::pthread_mutex_t mutex_;
     60 };
     61 
     62 } // namespace detail
     63 } // namespace asio
     64 
     65 #include "asio/detail/pop_options.hpp"
     66 
     67 # include "asio/detail/impl/posix_mutex.ipp"
     68 
     69 #endif // defined(ASIO_HAS_PTHREADS)
     70 
     71 #endif // ASIO_DETAIL_POSIX_MUTEX_HPP
     72