File indexing completed on 2025-01-18 09:38:33
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_SEMAPHORE_HPP
0012 #define BOOST_INTERPROCESS_SEMAPHORE_HPP
0013
0014 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0015
0016 #ifndef BOOST_CONFIG_HPP
0017 # include <boost/config.hpp>
0018 #endif
0019 #
0020 #if defined(BOOST_HAS_PRAGMA_ONCE)
0021 # pragma once
0022 #endif
0023
0024 #include <boost/interprocess/detail/config_begin.hpp>
0025 #include <boost/interprocess/detail/workaround.hpp>
0026
0027 #include <boost/interprocess/creation_tags.hpp>
0028 #include <boost/interprocess/exceptions.hpp>
0029 #include <boost/interprocess/sync/detail/locks.hpp>
0030 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
0031
0032 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
0033 defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && \
0034 defined(BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES)
0035 #include <boost/interprocess/sync/posix/semaphore.hpp>
0036 #define BOOST_INTERPROCESS_SEMAPHORE_USE_POSIX
0037 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
0038
0039 #include <boost/interprocess/sync/windows/semaphore.hpp>
0040 #define BOOST_INTERPROCESS_SEMAPHORE_USE_WINAPI
0041 #else
0042
0043 #include <boost/interprocess/sync/spin/semaphore.hpp>
0044 #endif
0045
0046 #endif
0047
0048
0049
0050
0051 namespace boost {
0052 namespace interprocess {
0053
0054
0055
0056 class interprocess_semaphore
0057 {
0058 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0059
0060 interprocess_semaphore(const interprocess_semaphore &);
0061 interprocess_semaphore &operator=(const interprocess_semaphore &);
0062 #endif
0063 public:
0064
0065
0066 interprocess_semaphore(unsigned int initialCount);
0067
0068
0069
0070 ~interprocess_semaphore();
0071
0072
0073
0074
0075 void post();
0076
0077
0078
0079
0080 void wait();
0081
0082
0083
0084
0085 bool try_wait();
0086
0087
0088
0089
0090
0091
0092 template<class TimePoint>
0093 bool timed_wait(const TimePoint &abs_time);
0094
0095
0096
0097 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0098 private:
0099 #if defined(BOOST_INTERPROCESS_SEMAPHORE_USE_POSIX)
0100 typedef ipcdetail::posix_semaphore internal_sem_t;
0101 #elif defined(BOOST_INTERPROCESS_SEMAPHORE_USE_WINAPI)
0102 typedef ipcdetail::winapi_semaphore internal_sem_t;
0103 #else
0104 typedef ipcdetail::spin_semaphore internal_sem_t;
0105 #endif
0106 internal_sem_t m_sem;
0107 #endif
0108 };
0109
0110 }
0111 }
0112
0113 namespace boost {
0114 namespace interprocess {
0115
0116 inline interprocess_semaphore::interprocess_semaphore(unsigned int initialCount)
0117 : m_sem(initialCount)
0118 {}
0119
0120 inline interprocess_semaphore::~interprocess_semaphore(){}
0121
0122 inline void interprocess_semaphore::wait()
0123 {
0124 ipcdetail::lock_to_wait<internal_sem_t> ltw(m_sem);
0125 timeout_when_locking_aware_lock(ltw);
0126 }
0127
0128 inline bool interprocess_semaphore::try_wait()
0129 { return m_sem.try_wait(); }
0130
0131 template<class TimePoint>
0132 inline bool interprocess_semaphore::timed_wait(const TimePoint &abs_time)
0133 { return m_sem.timed_wait(abs_time); }
0134
0135 inline void interprocess_semaphore::post()
0136 { m_sem.post(); }
0137
0138 }
0139 }
0140
0141 #include <boost/interprocess/detail/config_end.hpp>
0142
0143 #endif