1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2011-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_DETAIL_WINAPI_SEMAPHORE_WRAPPER_HPP
12 #define BOOST_INTERPROCESS_DETAIL_WINAPI_SEMAPHORE_WRAPPER_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/creation_tags.hpp>
25 #include <boost/interprocess/permissions.hpp>
26 #include <boost/interprocess/detail/win32_api.hpp>
27 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
28 #include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp>
29 #include <boost/interprocess/errors.hpp>
30 #include <boost/interprocess/exceptions.hpp>
34 namespace interprocess {
37 class winapi_semaphore_functions
39 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
42 winapi_semaphore_functions(const winapi_semaphore_functions &);
43 winapi_semaphore_functions &operator=(const winapi_semaphore_functions &);
44 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
47 winapi_semaphore_functions(void *hnd)
51 void post(long count = 1)
54 winapi::release_semaphore(m_sem_hnd, count, &prev_count);
58 { return winapi_wrapper_wait_for_single_object(m_sem_hnd); }
61 { return winapi_wrapper_try_wait_for_single_object(m_sem_hnd); }
63 bool timed_wait(const boost::posix_time::ptime &abs_time)
64 { return winapi_wrapper_timed_wait_for_single_object(m_sem_hnd, abs_time); }
68 long l_count, l_limit;
69 if(!winapi::get_semaphore_info(m_sem_hnd, l_count, l_limit))
76 long l_count, l_limit;
77 if(!winapi::get_semaphore_info(m_sem_hnd, l_count, l_limit))
82 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
85 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
89 //Swappable semaphore wrapper
90 class winapi_semaphore_wrapper
91 : public winapi_semaphore_functions
93 winapi_semaphore_wrapper(const winapi_semaphore_wrapper &);
94 winapi_semaphore_wrapper &operator=(const winapi_semaphore_wrapper &);
98 //Long is 32 bits in windows
99 static const long MaxCount = long(0x7FFFFFFF);
101 winapi_semaphore_wrapper(void *hnd = winapi::invalid_handle_value)
102 : winapi_semaphore_functions(hnd)
105 ~winapi_semaphore_wrapper()
110 void *hnd = m_sem_hnd;
111 m_sem_hnd = winapi::invalid_handle_value;
116 { return m_sem_hnd; }
118 bool open_or_create( const char *name
121 , const permissions &perm
124 if(m_sem_hnd == winapi::invalid_handle_value){
125 m_sem_hnd = winapi::open_or_create_semaphore
129 , (winapi::interprocess_security_attributes*)perm.get_permissions()
131 created = winapi::get_last_error() != winapi::error_already_exists;
132 return m_sem_hnd != winapi::invalid_handle_value;
139 bool open_semaphore(const char *name)
141 if(m_sem_hnd == winapi::invalid_handle_value){
142 m_sem_hnd = winapi::open_semaphore(name);
143 return m_sem_hnd != winapi::invalid_handle_value;
152 if(m_sem_hnd != winapi::invalid_handle_value){
153 winapi::close_handle(m_sem_hnd);
154 m_sem_hnd = winapi::invalid_handle_value;
158 void swap(winapi_semaphore_wrapper &other)
159 { void *tmp = m_sem_hnd; m_sem_hnd = other.m_sem_hnd; other.m_sem_hnd = tmp; }
162 } //namespace ipcdetail {
163 } //namespace interprocess {
164 } //namespace boost {
166 #include <boost/interprocess/detail/config_end.hpp>
168 #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_SEMAPHORE_WRAPPER_HPP