1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2011. 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_WINDOWS_SEMAPHORE_HPP
12 #define BOOST_INTERPROCESS_DETAIL_WINDOWS_SEMAPHORE_HPP
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
21 #include <boost/interprocess/detail/win32_api.hpp>
22 #include <boost/interprocess/detail/intermodule_singleton.hpp>
23 #include <boost/interprocess/sync/windows/sync_utils.hpp>
24 #include <boost/interprocess/exceptions.hpp>
28 namespace interprocess {
31 class windows_semaphore
33 windows_semaphore(const windows_semaphore &);
34 windows_semaphore &operator=(const windows_semaphore &);
37 windows_semaphore(unsigned int initialCount);
40 void post(long release_count = 1);
43 bool timed_wait(const boost::posix_time::ptime &abs_time);
49 inline windows_semaphore::windows_semaphore(unsigned int initialCount)
52 sync_handles &handles =
53 intermodule_singleton<sync_handles>::get();
54 //Force smeaphore creation with the initial count
56 handles.obtain_semaphore(this->id_, initialCount, &open_or_created);
57 //The semaphore must be created, never opened
58 assert(open_or_created);
59 assert(open_or_created && winapi::get_last_error() != winapi::error_already_exists);
60 (void)open_or_created;
63 inline windows_semaphore::~windows_semaphore()
65 sync_handles &handles =
66 intermodule_singleton<sync_handles>::get();
67 handles.destroy_handle(this->id_);
70 inline void windows_semaphore::wait(void)
72 sync_handles &handles =
73 intermodule_singleton<sync_handles>::get();
75 void *hnd = handles.obtain_semaphore(this->id_, 0);
76 unsigned long ret = winapi::wait_for_single_object(hnd, winapi::infinite_time);
77 if(ret == winapi::wait_failed){
78 error_info err(winapi::get_last_error());
79 throw interprocess_exception(err);
83 inline bool windows_semaphore::try_wait(void)
85 sync_handles &handles =
86 intermodule_singleton<sync_handles>::get();
88 void *hnd = handles.obtain_semaphore(this->id_, 0);
89 unsigned long ret = winapi::wait_for_single_object(hnd, 0);
90 if(ret == winapi::wait_failed){
91 error_info err(winapi::get_last_error());
92 throw interprocess_exception(err);
94 return ret != winapi::wait_timeout;
97 inline bool windows_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
99 if(abs_time == boost::posix_time::pos_infin){
103 boost::posix_time::ptime now
104 = boost::posix_time::microsec_clock::universal_time();
106 unsigned long ms = (unsigned long)(abs_time-now).total_milliseconds();
107 sync_handles &handles =
108 intermodule_singleton<sync_handles>::get();
110 void *hnd = handles.obtain_semaphore(this->id_, 0);
111 unsigned long ret = winapi::wait_for_single_object(hnd, ms);
112 if(ret == winapi::wait_failed){
113 error_info err(winapi::get_last_error());
114 throw interprocess_exception(err);
116 return ret != winapi::wait_timeout;
119 inline void windows_semaphore::post(long release_count)
121 sync_handles &handles =
122 intermodule_singleton<sync_handles>::get();
124 void *hnd = handles.obtain_semaphore(this->id_, 0);
126 int ret = winapi::release_semaphore(hnd, release_count, &prev_count);
131 } //namespace ipcdetail {
132 } //namespace interprocess {
133 } //namespace boost {
135 #include <boost/interprocess/detail/config_end.hpp>
137 #endif //BOOST_INTERPROCESS_DETAIL_WINDOWS_SEMAPHORE_HPP