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_SEMAPHORE_HPP
12#define BOOST_INTERPROCESS_SEMAPHORE_HPP
13
14#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
15
16#ifndef BOOST_CONFIG_HPP
17# include <boost/config.hpp>
18#endif
19#
20#if defined(BOOST_HAS_PRAGMA_ONCE)
21# pragma once
22#endif
23
24#include <boost/interprocess/detail/config_begin.hpp>
25#include <boost/interprocess/detail/workaround.hpp>
26
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>
31
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)
38 //Experimental...
39 #include <boost/interprocess/sync/windows/semaphore.hpp>
40 #define BOOST_INTERPROCESS_SEMAPHORE_USE_WINAPI
41#else
42 //spin_semaphore is used
43 #include <boost/interprocess/sync/spin/semaphore.hpp>
44#endif
45
46#endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
47
48//!\file
49//!Describes a interprocess_semaphore class for inter-process synchronization
50
51namespace boost {
52namespace interprocess {
53
54//!Wraps a interprocess_semaphore that can be placed in shared memory and can be
55//!shared between processes. Allows timed lock tries
56class interprocess_semaphore
57{
58 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
59 //Non-copyable
60 interprocess_semaphore(const interprocess_semaphore &);
61 interprocess_semaphore &operator=(const interprocess_semaphore &);
62 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
63 public:
64 //!Creates a interprocess_semaphore with the given initial count.
65 //!interprocess_exception if there is an error.*/
66 interprocess_semaphore(unsigned int initialCount);
67
68 //!Destroys the interprocess_semaphore.
69 //!Does not throw
70 ~interprocess_semaphore();
71
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.
75 void post();
76
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.
80 void wait();
81
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.
85 bool try_wait();
86
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);
94
95 //!Returns the interprocess_semaphore count
96// int get_count() const;
97 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
98 private:
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;
103 #else
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
108};
109
110} //namespace interprocess {
111} //namespace boost {
112
113namespace boost {
114namespace interprocess {
115
116inline interprocess_semaphore::interprocess_semaphore(unsigned int initialCount)
117 : m_sem(initialCount)
118{}
119
120inline interprocess_semaphore::~interprocess_semaphore(){}
121
122inline void interprocess_semaphore::wait()
123{
124 ipcdetail::lock_to_wait<internal_sem_t> ltw(m_sem);
125 timeout_when_locking_aware_lock(m&: ltw);
126}
127
128inline bool interprocess_semaphore::try_wait()
129{ return m_sem.try_wait(); }
130
131template<class TimePoint>
132inline bool interprocess_semaphore::timed_wait(const TimePoint &abs_time)
133{ return m_sem.timed_wait(abs_time); }
134
135inline void interprocess_semaphore::post()
136{ m_sem.post(); }
137
138} //namespace interprocess {
139} //namespace boost {
140
141#include <boost/interprocess/detail/config_end.hpp>
142
143#endif //BOOST_INTERPROCESS_SEMAPHORE_HPP
144

source code of boost/libs/interprocess/include/boost/interprocess/sync/interprocess_semaphore.hpp