mutex.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (C) 2000 Stephen Cleary
  2. // Copyright (C) 2018 Peter Dimov
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org for updates, documentation, and revision history.
  9. #ifndef BOOST_POOL_MUTEX_HPP
  10. #define BOOST_POOL_MUTEX_HPP
  11. #include <boost/config.hpp>
  12. namespace boost{ namespace details{ namespace pool{
  13. class null_mutex
  14. {
  15. private:
  16. null_mutex(const null_mutex &);
  17. void operator=(const null_mutex &);
  18. public:
  19. null_mutex() {}
  20. static void lock() {}
  21. static void unlock() {}
  22. };
  23. }}} // namespace boost::details::pool
  24. #if !defined(BOOST_HAS_THREADS) || defined(BOOST_NO_MT) || defined(BOOST_POOL_NO_MT)
  25. namespace boost{ namespace details{ namespace pool{
  26. typedef null_mutex default_mutex;
  27. }}} // namespace boost::details::pool
  28. #elif !defined(BOOST_NO_CXX11_HDR_MUTEX)
  29. #include <mutex>
  30. namespace boost{ namespace details{ namespace pool{
  31. typedef std::mutex default_mutex;
  32. }}} // namespace boost::details::pool
  33. #elif defined(BOOST_HAS_PTHREADS)
  34. #include <boost/assert.hpp>
  35. #include <pthread.h>
  36. namespace boost{ namespace details{ namespace pool{
  37. class pt_mutex
  38. {
  39. private:
  40. pthread_mutex_t m_;
  41. pt_mutex(pt_mutex const &);
  42. pt_mutex & operator=(pt_mutex const &);
  43. public:
  44. pt_mutex()
  45. {
  46. BOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 );
  47. }
  48. ~pt_mutex()
  49. {
  50. BOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 );
  51. }
  52. void lock()
  53. {
  54. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  55. }
  56. void unlock()
  57. {
  58. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  59. }
  60. };
  61. typedef pt_mutex default_mutex;
  62. }}} // namespace boost::details::pool
  63. #elif defined(BOOST_HAS_WINTHREADS) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
  64. #include <boost/winapi/critical_section.hpp>
  65. namespace boost{ namespace details{ namespace pool{
  66. class cs_mutex
  67. {
  68. private:
  69. boost::winapi::CRITICAL_SECTION_ cs_;
  70. cs_mutex(cs_mutex const &);
  71. cs_mutex & operator=(cs_mutex const &);
  72. public:
  73. cs_mutex()
  74. {
  75. boost::winapi::InitializeCriticalSection( &cs_ );
  76. }
  77. ~cs_mutex()
  78. {
  79. boost::winapi::DeleteCriticalSection( &cs_ );
  80. }
  81. void lock()
  82. {
  83. boost::winapi::EnterCriticalSection( &cs_ );
  84. }
  85. void unlock()
  86. {
  87. boost::winapi::LeaveCriticalSection( &cs_ );
  88. }
  89. };
  90. typedef cs_mutex default_mutex;
  91. }}} // namespace boost::details::pool
  92. #else
  93. // Use #define BOOST_DISABLE_THREADS to avoid this error
  94. # error Unrecognized threading platform
  95. #endif
  96. #endif // #ifndef BOOST_POOL_MUTEX_HPP