File indexing completed on 2025-01-18 09:38:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_DETAIL_WINDOWS_SEMAPHORE_HPP
0012 #define BOOST_INTERPROCESS_DETAIL_WINDOWS_SEMAPHORE_HPP
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 # pragma once
0020 #endif
0021
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 #include <boost/interprocess/detail/win32_api.hpp>
0025 #include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
0026 #include <boost/interprocess/sync/windows/sync_utils.hpp>
0027 #include <boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp>
0028 #include <boost/interprocess/exceptions.hpp>
0029 #include <boost/assert.hpp>
0030
0031
0032 namespace boost {
0033 namespace interprocess {
0034 namespace ipcdetail {
0035
0036 class winapi_semaphore
0037 {
0038 winapi_semaphore(const winapi_semaphore &);
0039 winapi_semaphore &operator=(const winapi_semaphore &);
0040 public:
0041
0042 winapi_semaphore(unsigned int initialCount);
0043 ~winapi_semaphore();
0044
0045 void post(unsigned int release_count = 1);
0046 void wait();
0047 bool try_wait();
0048 template<class TimePoint> bool timed_wait(const TimePoint &abs_time);
0049
0050 private:
0051 const sync_id id_;
0052 const unsigned initial_count_;
0053 };
0054
0055 inline winapi_semaphore::winapi_semaphore(unsigned int initialCount)
0056 : id_(), initial_count_(initialCount)
0057 {
0058 sync_handles &handles =
0059 windows_intermodule_singleton<sync_handles>::get();
0060
0061 bool open_or_created;
0062 handles.obtain_semaphore(this->id_, this, initialCount, &open_or_created);
0063
0064 BOOST_ASSERT(open_or_created);
0065 BOOST_ASSERT(open_or_created && winapi::get_last_error() != winapi::error_already_exists);
0066 (void)open_or_created;
0067 }
0068
0069 inline winapi_semaphore::~winapi_semaphore()
0070 {
0071 sync_handles &handles =
0072 windows_intermodule_singleton<sync_handles>::get();
0073 handles.destroy_handle(this->id_, this);
0074 }
0075
0076 inline void winapi_semaphore::wait()
0077 {
0078 sync_handles &handles =
0079 windows_intermodule_singleton<sync_handles>::get();
0080
0081 winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, this, initial_count_));
0082 sem.wait();
0083 }
0084
0085 inline bool winapi_semaphore::try_wait()
0086 {
0087 sync_handles &handles =
0088 windows_intermodule_singleton<sync_handles>::get();
0089
0090 winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, this, initial_count_));
0091 return sem.try_wait();
0092 }
0093
0094 template<class TimePoint>
0095 inline bool winapi_semaphore::timed_wait(const TimePoint &abs_time)
0096 {
0097 sync_handles &handles =
0098 windows_intermodule_singleton<sync_handles>::get();
0099
0100 winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, this, initial_count_));
0101 return sem.timed_wait(abs_time);
0102 }
0103
0104 inline void winapi_semaphore::post(unsigned release_count)
0105 {
0106 sync_handles &handles =
0107 windows_intermodule_singleton<sync_handles>::get();
0108 winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, this, initial_count_));
0109 sem.post(static_cast<long>(release_count));
0110 }
0111
0112
0113 }
0114 }
0115 }
0116
0117 #include <boost/interprocess/detail/config_end.hpp>
0118
0119 #endif