/Src/Dependencies/Boost/boost/pool/detail/mutex.hpp

http://hadesmem.googlecode.com/ · C++ Header · 147 lines · 100 code · 37 blank · 10 comment · 8 complexity · 6b05355a95e92c8e6045739bbd1227b8 MD5 · raw file

  1. // Copyright (C) 2000 Stephen Cleary
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org for updates, documentation, and revision history.
  8. #ifndef BOOST_POOL_MUTEX_HPP
  9. #define BOOST_POOL_MUTEX_HPP
  10. #include <boost/config.hpp> // for workarounds
  11. // Extremely Light-Weight wrapper classes for OS thread synchronization
  12. // Configuration: for now, we just choose between pthread or Win32 mutexes or none
  13. #define BOOST_MUTEX_HELPER_NONE 0
  14. #define BOOST_MUTEX_HELPER_WIN32 1
  15. #define BOOST_MUTEX_HELPER_PTHREAD 2
  16. #if !defined(BOOST_HAS_THREADS) && !defined(BOOST_NO_MT)
  17. # define BOOST_NO_MT
  18. #endif
  19. #ifdef BOOST_NO_MT
  20. // No multithreading -> make locks into no-ops
  21. #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_NONE
  22. #else
  23. #ifdef BOOST_WINDOWS
  24. #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_WIN32
  25. #else
  26. #if defined(BOOST_HAS_UNISTD_H)
  27. #include <unistd.h>
  28. #endif
  29. #if defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS)
  30. #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_PTHREAD
  31. #endif
  32. #endif
  33. #endif
  34. #ifndef BOOST_MUTEX_HELPER
  35. #error Unable to determine platform mutex type; define BOOST_NO_MT to assume single-threaded
  36. #endif
  37. #ifndef BOOST_NO_MT
  38. # ifdef BOOST_WINDOWS
  39. # include <windows.h>
  40. # endif
  41. # if defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS)
  42. # include <pthread.h>
  43. # endif
  44. #endif
  45. namespace boost {
  46. namespace details {
  47. namespace pool {
  48. #ifndef BOOST_NO_MT
  49. #ifdef BOOST_WINDOWS
  50. class win32_mutex
  51. {
  52. private:
  53. ::CRITICAL_SECTION mtx;
  54. win32_mutex(const win32_mutex &);
  55. void operator=(const win32_mutex &);
  56. public:
  57. win32_mutex()
  58. { ::InitializeCriticalSection(&mtx); }
  59. ~win32_mutex()
  60. { ::DeleteCriticalSection(&mtx); }
  61. void lock()
  62. { ::EnterCriticalSection(&mtx); }
  63. void unlock()
  64. { ::LeaveCriticalSection(&mtx); }
  65. };
  66. #endif // defined(BOOST_WINDOWS)
  67. #if defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS)
  68. class pthread_mutex
  69. {
  70. private:
  71. ::pthread_mutex_t mtx;
  72. pthread_mutex(const pthread_mutex &);
  73. void operator=(const pthread_mutex &);
  74. public:
  75. pthread_mutex()
  76. { ::pthread_mutex_init(&mtx, 0); }
  77. ~pthread_mutex()
  78. { ::pthread_mutex_destroy(&mtx); }
  79. void lock()
  80. { ::pthread_mutex_lock(&mtx); }
  81. void unlock()
  82. { ::pthread_mutex_unlock(&mtx); }
  83. };
  84. #endif // defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS)
  85. #endif // !defined(BOOST_NO_MT)
  86. class null_mutex
  87. {
  88. private:
  89. null_mutex(const null_mutex &);
  90. void operator=(const null_mutex &);
  91. public:
  92. null_mutex() { }
  93. static void lock() { }
  94. static void unlock() { }
  95. };
  96. #if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE
  97. typedef null_mutex default_mutex;
  98. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32
  99. typedef win32_mutex default_mutex;
  100. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD
  101. typedef pthread_mutex default_mutex;
  102. #endif
  103. } // namespace pool
  104. } // namespace details
  105. } // namespace boost
  106. #undef BOOST_MUTEX_HELPER_WIN32
  107. #undef BOOST_MUTEX_HELPER_PTHREAD
  108. #undef BOOST_MUTEX_HELPER_NONE
  109. #undef BOOST_MUTEX_HELPER
  110. #endif