annotate DEPENDENCIES/generic/include/boost/interprocess/sync/interprocess_semaphore.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 //////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
Chris@16 4 // Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6 //
Chris@16 7 // See http://www.boost.org/libs/interprocess for documentation.
Chris@16 8 //
Chris@16 9 //////////////////////////////////////////////////////////////////////////////
Chris@16 10
Chris@16 11 #ifndef BOOST_INTERPROCESS_SEMAPHORE_HPP
Chris@16 12 #define BOOST_INTERPROCESS_SEMAPHORE_HPP
Chris@16 13
Chris@101 14 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 15
Chris@101 16 #ifndef BOOST_CONFIG_HPP
Chris@101 17 # include <boost/config.hpp>
Chris@101 18 #endif
Chris@101 19 #
Chris@101 20 #if defined(BOOST_HAS_PRAGMA_ONCE)
Chris@16 21 # pragma once
Chris@16 22 #endif
Chris@16 23
Chris@16 24 #include <boost/interprocess/detail/config_begin.hpp>
Chris@16 25 #include <boost/interprocess/detail/workaround.hpp>
Chris@16 26
Chris@16 27 #include <boost/interprocess/creation_tags.hpp>
Chris@16 28 #include <boost/interprocess/exceptions.hpp>
Chris@16 29 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
Chris@16 30
Chris@16 31 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
Chris@101 32 (defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && defined(BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES))
Chris@16 33 #include <boost/interprocess/sync/posix/semaphore.hpp>
Chris@16 34 #define BOOST_INTERPROCESS_USE_POSIX
Chris@16 35 //Experimental...
Chris@16 36 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
Chris@16 37 #include <boost/interprocess/sync/windows/semaphore.hpp>
Chris@16 38 #define BOOST_INTERPROCESS_USE_WINDOWS
Chris@16 39 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 40 #include <boost/interprocess/sync/spin/semaphore.hpp>
Chris@16 41 #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
Chris@16 42 #endif
Chris@16 43
Chris@101 44 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 45
Chris@16 46 //!\file
Chris@16 47 //!Describes a interprocess_semaphore class for inter-process synchronization
Chris@16 48
Chris@16 49 namespace boost {
Chris@16 50 namespace interprocess {
Chris@16 51
Chris@16 52 //!Wraps a interprocess_semaphore that can be placed in shared memory and can be
Chris@16 53 //!shared between processes. Allows timed lock tries
Chris@16 54 class interprocess_semaphore
Chris@16 55 {
Chris@101 56 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 57 //Non-copyable
Chris@16 58 interprocess_semaphore(const interprocess_semaphore &);
Chris@16 59 interprocess_semaphore &operator=(const interprocess_semaphore &);
Chris@101 60 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 61 public:
Chris@16 62 //!Creates a interprocess_semaphore with the given initial count.
Chris@16 63 //!interprocess_exception if there is an error.*/
Chris@16 64 interprocess_semaphore(unsigned int initialCount);
Chris@16 65
Chris@16 66 //!Destroys the interprocess_semaphore.
Chris@16 67 //!Does not throw
Chris@16 68 ~interprocess_semaphore();
Chris@16 69
Chris@16 70 //!Increments the interprocess_semaphore count. If there are processes/threads blocked waiting
Chris@16 71 //!for the interprocess_semaphore, then one of these processes will return successfully from
Chris@16 72 //!its wait function. If there is an error an interprocess_exception exception is thrown.
Chris@16 73 void post();
Chris@16 74
Chris@16 75 //!Decrements the interprocess_semaphore. If the interprocess_semaphore value is not greater than zero,
Chris@16 76 //!then the calling process/thread blocks until it can decrement the counter.
Chris@16 77 //!If there is an error an interprocess_exception exception is thrown.
Chris@16 78 void wait();
Chris@16 79
Chris@16 80 //!Decrements the interprocess_semaphore if the interprocess_semaphore's value is greater than zero
Chris@16 81 //!and returns true. If the value is not greater than zero returns false.
Chris@16 82 //!If there is an error an interprocess_exception exception is thrown.
Chris@16 83 bool try_wait();
Chris@16 84
Chris@16 85 //!Decrements the interprocess_semaphore if the interprocess_semaphore's value is greater
Chris@16 86 //!than zero and returns true. Otherwise, waits for the interprocess_semaphore
Chris@16 87 //!to the posted or the timeout expires. If the timeout expires, the
Chris@16 88 //!function returns false. If the interprocess_semaphore is posted the function
Chris@16 89 //!returns true. If there is an error throws sem_exception
Chris@16 90 bool timed_wait(const boost::posix_time::ptime &abs_time);
Chris@16 91
Chris@16 92 //!Returns the interprocess_semaphore count
Chris@16 93 // int get_count() const;
Chris@101 94 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 95 private:
Chris@16 96 #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
Chris@16 97 #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
Chris@16 98 ipcdetail::spin_semaphore m_sem;
Chris@16 99 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
Chris@16 100 #undef BOOST_INTERPROCESS_USE_WINDOWS
Chris@16 101 ipcdetail::windows_semaphore m_sem;
Chris@16 102 #else
Chris@16 103 #undef BOOST_INTERPROCESS_USE_POSIX
Chris@16 104 ipcdetail::posix_semaphore m_sem;
Chris@16 105 #endif //#if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
Chris@101 106 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 107 };
Chris@16 108
Chris@16 109 } //namespace interprocess {
Chris@16 110 } //namespace boost {
Chris@16 111
Chris@16 112 namespace boost {
Chris@16 113 namespace interprocess {
Chris@16 114
Chris@16 115 inline interprocess_semaphore::interprocess_semaphore(unsigned int initialCount)
Chris@16 116 : m_sem(initialCount)
Chris@16 117 {}
Chris@16 118
Chris@16 119 inline interprocess_semaphore::~interprocess_semaphore(){}
Chris@16 120
Chris@16 121 inline void interprocess_semaphore::wait()
Chris@16 122 {
Chris@16 123 #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
Chris@16 124 boost::posix_time::ptime wait_time
Chris@16 125 = boost::posix_time::microsec_clock::universal_time()
Chris@16 126 + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
Chris@16 127 if (!m_sem.timed_wait(wait_time))
Chris@16 128 {
Chris@16 129 throw interprocess_exception(timeout_when_waiting_error, "Interprocess semaphore timeout when waiting. Possible deadlock: owner died without posting?");
Chris@16 130 }
Chris@16 131 #else
Chris@16 132 m_sem.wait();
Chris@16 133 #endif
Chris@16 134 }
Chris@16 135 inline bool interprocess_semaphore::try_wait()
Chris@16 136 { return m_sem.try_wait(); }
Chris@16 137
Chris@16 138 inline bool interprocess_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
Chris@16 139 { return m_sem.timed_wait(abs_time); }
Chris@16 140
Chris@16 141 inline void interprocess_semaphore::post()
Chris@16 142 { m_sem.post(); }
Chris@16 143
Chris@16 144 } //namespace interprocess {
Chris@16 145 } //namespace boost {
Chris@16 146
Chris@16 147 #include <boost/interprocess/detail/config_end.hpp>
Chris@16 148
Chris@16 149 #endif //BOOST_INTERPROCESS_SEMAPHORE_HPP