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/detail/posix_time_types_wrk.hpp>
30 #include <boost/interprocess/sync/detail/locks.hpp>
31 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
33 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
34 (defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && defined(BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES))
35 #include <boost/interprocess/sync/posix/semaphore.hpp>
36 #define BOOST_INTERPROCESS_USE_POSIX
38 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
39 #include <boost/interprocess/sync/windows/semaphore.hpp>
40 #define BOOST_INTERPROCESS_USE_WINDOWS
41 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
42 #include <boost/interprocess/sync/spin/semaphore.hpp>
43 #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
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 bool timed_wait(const boost::posix_time::ptime &abs_time);
94 //!Returns the interprocess_semaphore count
95 // int get_count() const;
96 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
98 #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
99 #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
100 typedef ipcdetail::spin_semaphore internal_sem_t;
101 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
102 #undef BOOST_INTERPROCESS_USE_WINDOWS
103 typedef ipcdetail::windows_semaphore internal_sem_t;
105 #undef BOOST_INTERPROCESS_USE_POSIX
106 typedef ipcdetail::posix_semaphore internal_sem_t;
107 #endif //#if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
108 internal_sem_t m_sem;
109 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
112 } //namespace interprocess {
113 } //namespace boost {
116 namespace interprocess {
118 inline interprocess_semaphore::interprocess_semaphore(unsigned int initialCount)
119 : m_sem(initialCount)
122 inline interprocess_semaphore::~interprocess_semaphore(){}
124 inline void interprocess_semaphore::wait()
126 ipcdetail::lock_to_wait<internal_sem_t> ltw(m_sem);
127 timeout_when_locking_aware_lock(ltw);
130 inline bool interprocess_semaphore::try_wait()
131 { return m_sem.try_wait(); }
133 inline bool interprocess_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
134 { return m_sem.timed_wait(abs_time); }
136 inline void interprocess_semaphore::post()
139 } //namespace interprocess {
140 } //namespace boost {
142 #include <boost/interprocess/detail/config_end.hpp>
144 #endif //BOOST_INTERPROCESS_SEMAPHORE_HPP