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