[857] | 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 |
|
---|
| 9 | #ifndef BOOST_POOL_MUTEX_HPP
|
---|
| 10 | #define BOOST_POOL_MUTEX_HPP
|
---|
| 11 |
|
---|
| 12 | #include <boost/config.hpp> // for workarounds
|
---|
| 13 |
|
---|
| 14 | // Extremely Light-Weight wrapper classes for OS thread synchronization
|
---|
| 15 |
|
---|
| 16 | // Configuration: for now, we just choose between pthread or Win32 mutexes or none
|
---|
| 17 |
|
---|
| 18 | #define BOOST_MUTEX_HELPER_NONE 0
|
---|
| 19 | #define BOOST_MUTEX_HELPER_WIN32 1
|
---|
| 20 | #define BOOST_MUTEX_HELPER_PTHREAD 2
|
---|
| 21 |
|
---|
| 22 | #if !defined(BOOST_HAS_THREADS) && !defined(BOOST_NO_MT)
|
---|
| 23 | # define BOOST_NO_MT
|
---|
| 24 | #endif
|
---|
| 25 |
|
---|
| 26 | #ifdef BOOST_NO_MT
|
---|
| 27 | // No multithreading -> make locks into no-ops
|
---|
| 28 | #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_NONE
|
---|
| 29 | #else
|
---|
| 30 | #ifdef BOOST_WINDOWS
|
---|
| 31 | #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_WIN32
|
---|
| 32 | #else
|
---|
| 33 | #include <unistd.h>
|
---|
| 34 | #ifdef _POSIX_THREADS
|
---|
| 35 | #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_PTHREAD
|
---|
| 36 | #endif
|
---|
| 37 | #endif
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
| 40 | #ifndef BOOST_MUTEX_HELPER
|
---|
| 41 | #error Unable to determine platform mutex type; define BOOST_NO_MT to assume single-threaded
|
---|
| 42 | #endif
|
---|
| 43 |
|
---|
| 44 | #ifndef BOOST_NO_MT
|
---|
| 45 | # ifdef BOOST_WINDOWS
|
---|
| 46 | # include <windows.h>
|
---|
| 47 | # endif
|
---|
| 48 | # ifdef _POSIX_THREADS
|
---|
| 49 | # include <pthread.h>
|
---|
| 50 | # endif
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 | namespace boost {
|
---|
| 54 |
|
---|
| 55 | namespace details {
|
---|
| 56 | namespace pool {
|
---|
| 57 |
|
---|
| 58 | #ifndef BOOST_NO_MT
|
---|
| 59 |
|
---|
| 60 | #ifdef BOOST_WINDOWS
|
---|
| 61 |
|
---|
| 62 | class win32_mutex
|
---|
| 63 | {
|
---|
| 64 | private:
|
---|
| 65 | CRITICAL_SECTION mtx;
|
---|
| 66 |
|
---|
| 67 | win32_mutex(const win32_mutex &);
|
---|
| 68 | void operator=(const win32_mutex &);
|
---|
| 69 |
|
---|
| 70 | public:
|
---|
| 71 | win32_mutex()
|
---|
| 72 | { InitializeCriticalSection(&mtx); }
|
---|
| 73 |
|
---|
| 74 | ~win32_mutex()
|
---|
| 75 | { DeleteCriticalSection(&mtx); }
|
---|
| 76 |
|
---|
| 77 | void lock()
|
---|
| 78 | { EnterCriticalSection(&mtx); }
|
---|
| 79 |
|
---|
| 80 | void unlock()
|
---|
| 81 | { LeaveCriticalSection(&mtx); }
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 | #endif // defined(BOOST_WINDOWS)
|
---|
| 85 |
|
---|
| 86 | #ifdef _POSIX_THREADS
|
---|
| 87 |
|
---|
| 88 | class pthread_mutex
|
---|
| 89 | {
|
---|
| 90 | private:
|
---|
| 91 | pthread_mutex_t mtx;
|
---|
| 92 |
|
---|
| 93 | pthread_mutex(const pthread_mutex &);
|
---|
| 94 | void operator=(const pthread_mutex &);
|
---|
| 95 |
|
---|
| 96 | public:
|
---|
| 97 | pthread_mutex()
|
---|
| 98 | { pthread_mutex_init(&mtx, 0); }
|
---|
| 99 |
|
---|
| 100 | ~pthread_mutex()
|
---|
| 101 | { pthread_mutex_destroy(&mtx); }
|
---|
| 102 |
|
---|
| 103 | void lock()
|
---|
| 104 | { pthread_mutex_lock(&mtx); }
|
---|
| 105 |
|
---|
| 106 | void unlock()
|
---|
| 107 | { pthread_mutex_unlock(&mtx); }
|
---|
| 108 | };
|
---|
| 109 |
|
---|
| 110 | #endif // defined(_POSIX_THREADS)
|
---|
| 111 |
|
---|
| 112 | #endif // !defined(BOOST_NO_MT)
|
---|
| 113 |
|
---|
| 114 | class null_mutex
|
---|
| 115 | {
|
---|
| 116 | private:
|
---|
| 117 | null_mutex(const null_mutex &);
|
---|
| 118 | void operator=(const null_mutex &);
|
---|
| 119 |
|
---|
| 120 | public:
|
---|
| 121 | null_mutex() { }
|
---|
| 122 |
|
---|
| 123 | static void lock() { }
|
---|
| 124 | static void unlock() { }
|
---|
| 125 | };
|
---|
| 126 |
|
---|
| 127 | #if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE
|
---|
| 128 | typedef null_mutex default_mutex;
|
---|
| 129 | #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32
|
---|
| 130 | typedef win32_mutex default_mutex;
|
---|
| 131 | #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD
|
---|
| 132 | typedef pthread_mutex default_mutex;
|
---|
| 133 | #endif
|
---|
| 134 |
|
---|
| 135 | } // namespace pool
|
---|
| 136 | } // namespace details
|
---|
| 137 |
|
---|
| 138 | } // namespace boost
|
---|
| 139 |
|
---|
| 140 | #undef BOOST_MUTEX_HELPER_WIN32
|
---|
| 141 | #undef BOOST_MUTEX_HELPER_PTHREAD
|
---|
| 142 | #undef BOOST_MUTEX_HELPER_NONE
|
---|
| 143 | #undef BOOST_MUTEX_HELPER
|
---|
| 144 |
|
---|
| 145 | #endif
|
---|