1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_SEMAPHORE_HPP
12 #define BOOST_INTERPROCESS_SEMAPHORE_HPP
14 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
16 #ifndef BOOST_CONFIG_HPP
17 # include <boost/config.hpp>
20 #if defined(BOOST_HAS_PRAGMA_ONCE)
24 #include <boost/interprocess/detail/config_begin.hpp>
25 #include <boost/interprocess/detail/workaround.hpp>
27 #include <boost/interprocess/creation_tags.hpp>
28 #include <boost/interprocess/exceptions.hpp>
29 #include <boost/interprocess/sync/detail/locks.hpp>
30 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
32 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
33 defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && \
34 defined(BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES)
35 #include <boost/interprocess/sync/posix/semaphore.hpp>
36 #define BOOST_INTERPROCESS_SEMAPHORE_USE_POSIX
37 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
39 #include <boost/interprocess/sync/windows/semaphore.hpp>
40 #define BOOST_INTERPROCESS_SEMAPHORE_USE_WINAPI
42 //spin_semaphore is used
43 #include <boost/interprocess/sync/spin/semaphore.hpp>
46 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
49 //!Describes a interprocess_semaphore class for inter-process synchronization
52 namespace interprocess {
54 //!Wraps a interprocess_semaphore that can be placed in shared memory and can be
55 //!shared between processes. Allows timed lock tries
56 class interprocess_semaphore
58 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
60 interprocess_semaphore(const interprocess_semaphore &);
61 interprocess_semaphore &operator=(const interprocess_semaphore &);
62 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
64 //!Creates a interprocess_semaphore with the given initial count.
65 //!interprocess_exception if there is an error.*/
66 interprocess_semaphore(unsigned int initialCount);
68 //!Destroys the interprocess_semaphore.
70 ~interprocess_semaphore();
72 //!Increments the interprocess_semaphore count. If there are processes/threads blocked waiting
73 //!for the interprocess_semaphore, then one of these processes will return successfully from
74 //!its wait function. If there is an error an interprocess_exception exception is thrown.
77 //!Decrements the interprocess_semaphore. If the interprocess_semaphore value is not greater than zero,
78 //!then the calling process/thread blocks until it can decrement the counter.
79 //!If there is an error an interprocess_exception exception is thrown.
82 //!Decrements the interprocess_semaphore if the interprocess_semaphore's value is greater than zero
83 //!and returns true. If the value is not greater than zero returns false.
84 //!If there is an error an interprocess_exception exception is thrown.
87 //!Decrements the interprocess_semaphore if the interprocess_semaphore's value is greater
88 //!than zero and returns true. Otherwise, waits for the interprocess_semaphore
89 //!to the posted or the timeout expires. If the timeout expires, the
90 //!function returns false. If the interprocess_semaphore is posted the function
91 //!returns true. If there is an error throws sem_exception
92 template<class TimePoint>
93 bool timed_wait(const TimePoint &abs_time);
95 //!Returns the interprocess_semaphore count
96 // int get_count() const;
97 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
99 #if defined(BOOST_INTERPROCESS_SEMAPHORE_USE_POSIX)
100 typedef ipcdetail::posix_semaphore internal_sem_t;
101 #elif defined(BOOST_INTERPROCESS_SEMAPHORE_USE_WINAPI)
102 typedef ipcdetail::winapi_semaphore internal_sem_t;
104 typedef ipcdetail::spin_semaphore internal_sem_t;
105 #endif //#if defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
106 internal_sem_t m_sem;
107 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
110 } //namespace interprocess {
111 } //namespace boost {
114 namespace interprocess {
116 inline interprocess_semaphore::interprocess_semaphore(unsigned int initialCount)
117 : m_sem(initialCount)
120 inline interprocess_semaphore::~interprocess_semaphore(){}
122 inline void interprocess_semaphore::wait()
124 ipcdetail::lock_to_wait<internal_sem_t> ltw(m_sem);
125 timeout_when_locking_aware_lock(ltw);
128 inline bool interprocess_semaphore::try_wait()
129 { return m_sem.try_wait(); }
131 template<class TimePoint>
132 inline bool interprocess_semaphore::timed_wait(const TimePoint &abs_time)
133 { return m_sem.timed_wait(abs_time); }
135 inline void interprocess_semaphore::post()
138 } //namespace interprocess {
139 } //namespace boost {
141 #include <boost/interprocess/detail/config_end.hpp>
143 #endif //BOOST_INTERPROCESS_SEMAPHORE_HPP