libstdc++
std_mutex.h
Go to the documentation of this file.
00001 // std::mutex implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/std_mutex.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{mutex}
00028  */
00029 
00030 #ifndef _GLIBCXX_MUTEX_H
00031 #define _GLIBCXX_MUTEX_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #if __cplusplus < 201103L
00036 # include <bits/c++0x_warning.h>
00037 #else
00038 
00039 #include <system_error>
00040 #include <bits/functexcept.h>
00041 #include <bits/gthr.h>
00042 
00043 namespace std _GLIBCXX_VISIBILITY(default)
00044 {
00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00046 
00047   /**
00048    * @defgroup mutexes Mutexes
00049    * @ingroup concurrency
00050    *
00051    * Classes for mutex support.
00052    * @{
00053    */
00054 
00055 #ifdef _GLIBCXX_HAS_GTHREADS
00056   // Common base class for std::mutex and std::timed_mutex
00057   class __mutex_base
00058   {
00059   protected:
00060     typedef __gthread_mutex_t                   __native_type;
00061 
00062 #ifdef __GTHREAD_MUTEX_INIT
00063     __native_type  _M_mutex = __GTHREAD_MUTEX_INIT;
00064 
00065     constexpr __mutex_base() noexcept = default;
00066 #else
00067     __native_type  _M_mutex;
00068 
00069     __mutex_base() noexcept
00070     {
00071       // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
00072       __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
00073     }
00074 
00075     ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
00076 #endif
00077 
00078     __mutex_base(const __mutex_base&) = delete;
00079     __mutex_base& operator=(const __mutex_base&) = delete;
00080   };
00081 
00082   /// The standard mutex type.
00083   class mutex : private __mutex_base
00084   {
00085   public:
00086     typedef __native_type*                      native_handle_type;
00087 
00088 #ifdef __GTHREAD_MUTEX_INIT
00089     constexpr
00090 #endif
00091     mutex() noexcept = default;
00092     ~mutex() = default;
00093 
00094     mutex(const mutex&) = delete;
00095     mutex& operator=(const mutex&) = delete;
00096 
00097     void
00098     lock()
00099     {
00100       int __e = __gthread_mutex_lock(&_M_mutex);
00101 
00102       // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
00103       if (__e)
00104         __throw_system_error(__e);
00105     }
00106 
00107     bool
00108     try_lock() noexcept
00109     {
00110       // XXX EINVAL, EAGAIN, EBUSY
00111       return !__gthread_mutex_trylock(&_M_mutex);
00112     }
00113 
00114     void
00115     unlock()
00116     {
00117       // XXX EINVAL, EAGAIN, EPERM
00118       __gthread_mutex_unlock(&_M_mutex);
00119     }
00120 
00121     native_handle_type
00122     native_handle() noexcept
00123     { return &_M_mutex; }
00124   };
00125 
00126 #endif // _GLIBCXX_HAS_GTHREADS
00127 
00128   /// Do not acquire ownership of the mutex.
00129   struct defer_lock_t { explicit defer_lock_t() = default; };
00130 
00131   /// Try to acquire ownership of the mutex without blocking.
00132   struct try_to_lock_t { explicit try_to_lock_t() = default; };
00133 
00134   /// Assume the calling thread has already obtained mutex ownership
00135   /// and manage it.
00136   struct adopt_lock_t { explicit adopt_lock_t() = default; };
00137 
00138   /// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
00139   _GLIBCXX17_INLINE constexpr defer_lock_t      defer_lock { };
00140 
00141   /// Tag used to prevent a scoped lock from blocking if a mutex is locked.
00142   _GLIBCXX17_INLINE constexpr try_to_lock_t     try_to_lock { };
00143 
00144   /// Tag used to make a scoped lock take ownership of a locked mutex.
00145   _GLIBCXX17_INLINE constexpr adopt_lock_t      adopt_lock { };
00146 
00147   /** @brief A simple scoped lock type.
00148    *
00149    * A lock_guard controls mutex ownership within a scope, releasing
00150    * ownership in the destructor.
00151    */
00152   template<typename _Mutex>
00153     class lock_guard
00154     {
00155     public:
00156       typedef _Mutex mutex_type;
00157 
00158       explicit lock_guard(mutex_type& __m) : _M_device(__m)
00159       { _M_device.lock(); }
00160 
00161       lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
00162       { } // calling thread owns mutex
00163 
00164       ~lock_guard()
00165       { _M_device.unlock(); }
00166 
00167       lock_guard(const lock_guard&) = delete;
00168       lock_guard& operator=(const lock_guard&) = delete;
00169 
00170     private:
00171       mutex_type&  _M_device;
00172     };
00173 
00174   // @} group mutexes
00175 _GLIBCXX_END_NAMESPACE_VERSION
00176 } // namespace
00177 #endif // C++11
00178 #endif // _GLIBCXX_MUTEX_H