1 #ifndef BOOST_DETAIL_LIGHTWEIGHT_MUTEX_HPP_INCLUDED
2 #define BOOST_DETAIL_LIGHTWEIGHT_MUTEX_HPP_INCLUDED
9 // boost/detail/lightweight_mutex.hpp - lightweight mutex
11 // Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
13 // Permission to copy, use, modify, sell and distribute this software
14 // is granted provided this copyright notice appears in all copies.
15 // This software is provided "as is" without express or implied
16 // warranty, and with no claim as to its suitability for any purpose.
18 // typedef <unspecified> boost::detail::lightweight_mutex;
20 // boost::detail::lightweight_mutex meets a subset of the Mutex concept
21 // requirements: http://www.boost.org/libs/thread/doc/mutex_concept.html#Mutex
23 // * Used by the smart pointer library
24 // * Performance oriented
25 // * Header-only implementation
26 // * Small memory footprint
27 // * Not a general purpose mutex, use boost::mutex, CRITICAL_SECTION or
28 // pthread_mutex instead.
29 // * Never spin in a tight lock/do-something/unlock loop, since
30 // lightweight_mutex does not guarantee fairness.
31 // * Never keep a lightweight_mutex locked for long periods.
33 // The current implementation can use a pthread_mutex, a CRITICAL_SECTION,
34 // or a platform-specific spinlock.
36 // You can force a particular implementation by defining BOOST_LWM_USE_PTHREADS,
37 // BOOST_LWM_USE_CRITICAL_SECTION, or BOOST_LWM_USE_SPINLOCK.
39 // If neither macro has been defined, the default is to use a spinlock on Win32,
40 // and a pthread_mutex otherwise.
42 // Note that a spinlock is not a general synchronization primitive. In particular,
43 // it is not guaranteed to be a memory barrier, and it is possible to "livelock"
44 // if a lower-priority thread has acquired the spinlock but a higher-priority
45 // thread is spinning trying to acquire the same lock.
47 // For these reasons, spinlocks have been disabled by default except on Windows,
48 // where a spinlock can be several orders of magnitude faster than a CRITICAL_SECTION.
51 // Note: lwm_linux.hpp has been disabled by default; see the comments
52 // inside for more info.
55 #include <boost/config.hpp>
57 // Note to implementors: if you write a platform-specific spinlock
58 // for a platform that supports pthreads, be sure to test its performance
59 // against the pthreads-based version using shared_ptr_timing_test.cpp and
60 // shared_ptr_mt_test.cpp. Custom versions are usually not worth the trouble
61 // _unless_ the performance gains are substantial.
63 // Be sure to compare against a "real" pthreads library;
64 // shared_ptr_timing_test.cpp will compile succesfully with a stub do-nothing
65 // pthreads library, since it doesn't create any threads.
67 #ifndef BOOST_HAS_THREADS
68 # include <boost/detail/lwm_nop.hpp>
69 #elif defined(BOOST_LWM_USE_SPINLOCK) && defined(BOOST_USE_ASM_ATOMIC_H)
70 # include <boost/detail/lwm_linux.hpp>
71 #elif defined(BOOST_LWM_USE_CRITICAL_SECTION)
72 # include <boost/detail/lwm_win32_cs.hpp>
73 #elif defined(BOOST_LWM_USE_PTHREADS)
74 # include <boost/detail/lwm_pthreads.hpp>
75 #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
76 # include <boost/detail/lwm_win32.hpp>
77 #elif defined(BOOST_LWM_USE_SPINLOCK) && defined(__sgi)
78 # include <boost/detail/lwm_irix.hpp>
79 #elif defined(BOOST_LWM_USE_SPINLOCK) && defined(__GLIBCPP__)
80 # include <boost/detail/lwm_gcc.hpp>
81 #elif defined(BOOST_HAS_PTHREADS)
82 # define BOOST_LWM_USE_PTHREADS
83 # include <boost/detail/lwm_pthreads.hpp>
85 # include <boost/detail/lwm_nop.hpp>
88 #endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_MUTEX_HPP_INCLUDED