]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost | |
4 | // Software License, Version 1.0. (See accompanying file | |
5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | // See http://www.boost.org/libs/interprocess for documentation. | |
8 | // | |
9 | ////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP | |
12 | #define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP | |
13 | ||
14 | #ifndef BOOST_CONFIG_HPP | |
15 | # include <boost/config.hpp> | |
16 | #endif | |
17 | # | |
18 | #if defined(BOOST_HAS_PRAGMA_ONCE) | |
19 | # pragma once | |
20 | #endif | |
21 | ||
22 | #include <boost/interprocess/detail/config_begin.hpp> | |
23 | #include <boost/interprocess/detail/workaround.hpp> | |
24 | #include <boost/interprocess/detail/posix_time_types_wrk.hpp> | |
25 | #include <boost/assert.hpp> | |
26 | #include <boost/interprocess/detail/atomic.hpp> | |
27 | #include <boost/cstdint.hpp> | |
28 | #include <boost/interprocess/detail/os_thread_functions.hpp> | |
29 | #include <boost/interprocess/sync/detail/common_algorithms.hpp> | |
30 | ||
31 | namespace boost { | |
32 | namespace interprocess { | |
33 | namespace ipcdetail { | |
34 | ||
35 | class spin_mutex | |
36 | { | |
37 | spin_mutex(const spin_mutex &); | |
38 | spin_mutex &operator=(const spin_mutex &); | |
39 | public: | |
40 | ||
41 | spin_mutex(); | |
42 | ~spin_mutex(); | |
43 | ||
44 | void lock(); | |
45 | bool try_lock(); | |
46 | bool timed_lock(const boost::posix_time::ptime &abs_time); | |
47 | void unlock(); | |
48 | void take_ownership(){}; | |
49 | private: | |
50 | volatile boost::uint32_t m_s; | |
51 | }; | |
52 | ||
53 | inline spin_mutex::spin_mutex() | |
54 | : m_s(0) | |
55 | { | |
56 | //Note that this class is initialized to zero. | |
57 | //So zeroed memory can be interpreted as an | |
58 | //initialized mutex | |
59 | } | |
60 | ||
61 | inline spin_mutex::~spin_mutex() | |
62 | { | |
63 | //Trivial destructor | |
64 | } | |
65 | ||
66 | inline void spin_mutex::lock(void) | |
67 | { return ipcdetail::try_based_lock(*this); } | |
68 | ||
69 | inline bool spin_mutex::try_lock(void) | |
70 | { | |
71 | boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0); | |
72 | return m_s == 1 && prev_s == 0; | |
73 | } | |
74 | ||
75 | inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time) | |
76 | { return ipcdetail::try_based_timed_lock(*this, abs_time); } | |
77 | ||
78 | inline void spin_mutex::unlock(void) | |
79 | { ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); } | |
80 | ||
81 | } //namespace ipcdetail { | |
82 | } //namespace interprocess { | |
83 | } //namespace boost { | |
84 | ||
85 | #include <boost/interprocess/detail/config_end.hpp> | |
86 | ||
87 | #endif //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP |