File indexing completed on 2025-01-18 09:38:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_WINDOWS_NAMED_SEMAPHORE_HPP
0012 #define BOOST_INTERPROCESS_WINDOWS_NAMED_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/creation_tags.hpp>
0025 #include <boost/interprocess/permissions.hpp>
0026 #include <boost/interprocess/detail/interprocess_tester.hpp>
0027 #include <boost/interprocess/sync/windows/named_sync.hpp>
0028 #include <boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp>
0029
0030 namespace boost {
0031 namespace interprocess {
0032 namespace ipcdetail {
0033
0034
0035
0036 class winapi_named_semaphore
0037 {
0038 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0039
0040
0041 winapi_named_semaphore();
0042 winapi_named_semaphore(const winapi_named_semaphore &);
0043 winapi_named_semaphore &operator=(const winapi_named_semaphore &);
0044 #endif
0045
0046 public:
0047 winapi_named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
0048
0049 winapi_named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
0050
0051 winapi_named_semaphore(open_only_t, const char *name);
0052
0053 winapi_named_semaphore(create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
0054
0055 winapi_named_semaphore(open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
0056
0057 winapi_named_semaphore(open_only_t, const wchar_t *name);
0058
0059 ~winapi_named_semaphore();
0060
0061 void post();
0062 void wait();
0063 bool try_wait();
0064 template<class TimePoint> bool timed_wait(const TimePoint &abs_time);
0065
0066 static bool remove(const char *name);
0067 static bool remove(const wchar_t *name);
0068
0069 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0070 private:
0071 friend class interprocess_tester;
0072 void dont_close_on_destruction();
0073 winapi_semaphore_wrapper m_sem_wrapper;
0074 windows_named_sync m_named_sync;
0075
0076 class named_sem_callbacks : public windows_named_sync_interface
0077 {
0078 public:
0079 typedef __int64 sem_count_t;
0080 named_sem_callbacks(winapi_semaphore_wrapper &sem_wrapper, sem_count_t sem_cnt)
0081 : m_sem_wrapper(sem_wrapper), m_sem_count(sem_cnt)
0082 {}
0083
0084 virtual std::size_t get_data_size() const BOOST_OVERRIDE
0085 { return sizeof(sem_count_t); }
0086
0087 virtual const void *buffer_with_final_data_to_file() BOOST_OVERRIDE
0088 { return &m_sem_count; }
0089
0090 virtual const void *buffer_with_init_data_to_file() BOOST_OVERRIDE
0091 { return &m_sem_count; }
0092
0093 virtual void *buffer_to_store_init_data_from_file() BOOST_OVERRIDE
0094 { return &m_sem_count; }
0095
0096 virtual bool open(create_enum_t, const char *id_name) BOOST_OVERRIDE
0097 {
0098 std::string aux_str = "Global\\bipc.sem.";
0099 aux_str += id_name;
0100
0101 permissions sem_perm;
0102 sem_perm.set_unrestricted();
0103 bool created;
0104 return m_sem_wrapper.open_or_create
0105 ( aux_str.c_str(), static_cast<long>(m_sem_count)
0106 , winapi_semaphore_wrapper::MaxCount, sem_perm, created);
0107 }
0108
0109 virtual bool open(create_enum_t, const wchar_t *id_name) BOOST_OVERRIDE
0110 {
0111 std::wstring aux_str = L"Global\\bipc.sem.";
0112 aux_str += id_name;
0113
0114 permissions sem_perm;
0115 sem_perm.set_unrestricted();
0116 bool created;
0117 return m_sem_wrapper.open_or_create
0118 ( aux_str.c_str(), static_cast<long>(m_sem_count)
0119 , winapi_semaphore_wrapper::MaxCount, sem_perm, created);
0120 }
0121
0122 virtual void close() BOOST_OVERRIDE
0123 {
0124 m_sem_wrapper.close();
0125 }
0126
0127 virtual ~named_sem_callbacks() BOOST_OVERRIDE
0128 {}
0129
0130 private:
0131 winapi_semaphore_wrapper& m_sem_wrapper;
0132 sem_count_t m_sem_count;
0133 };
0134
0135 #endif
0136 };
0137
0138 inline winapi_named_semaphore::~winapi_named_semaphore()
0139 {
0140 named_sem_callbacks callbacks(m_sem_wrapper, m_sem_wrapper.value());
0141 m_named_sync.close(callbacks);
0142 }
0143
0144 inline void winapi_named_semaphore::dont_close_on_destruction()
0145 {}
0146
0147 inline winapi_named_semaphore::winapi_named_semaphore
0148 (create_only_t, const char *name, unsigned int initial_count, const permissions &perm)
0149 : m_sem_wrapper()
0150 {
0151 named_sem_callbacks callbacks(m_sem_wrapper, initial_count);
0152 m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
0153 }
0154
0155 inline winapi_named_semaphore::winapi_named_semaphore
0156 (open_or_create_t, const char *name, unsigned int initial_count, const permissions &perm)
0157 : m_sem_wrapper()
0158 {
0159 named_sem_callbacks callbacks(m_sem_wrapper, initial_count);
0160 m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
0161 }
0162
0163 inline winapi_named_semaphore::winapi_named_semaphore(open_only_t, const char *name)
0164 : m_sem_wrapper()
0165 {
0166 named_sem_callbacks callbacks(m_sem_wrapper, 0);
0167 m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
0168 }
0169
0170 inline winapi_named_semaphore::winapi_named_semaphore
0171 (create_only_t, const wchar_t *name, unsigned int initial_count, const permissions &perm)
0172 : m_sem_wrapper()
0173 {
0174 named_sem_callbacks callbacks(m_sem_wrapper, initial_count);
0175 m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
0176 }
0177
0178 inline winapi_named_semaphore::winapi_named_semaphore
0179 (open_or_create_t, const wchar_t *name, unsigned int initial_count, const permissions &perm)
0180 : m_sem_wrapper()
0181 {
0182 named_sem_callbacks callbacks(m_sem_wrapper, initial_count);
0183 m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
0184 }
0185
0186 inline winapi_named_semaphore::winapi_named_semaphore(open_only_t, const wchar_t *name)
0187 : m_sem_wrapper()
0188 {
0189 named_sem_callbacks callbacks(m_sem_wrapper, 0);
0190 m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
0191 }
0192
0193 inline void winapi_named_semaphore::post()
0194 {
0195 m_sem_wrapper.post();
0196 }
0197
0198 inline void winapi_named_semaphore::wait()
0199 {
0200 m_sem_wrapper.wait();
0201 }
0202
0203 inline bool winapi_named_semaphore::try_wait()
0204 {
0205 return m_sem_wrapper.try_wait();
0206 }
0207
0208 template<class TimePoint>
0209 inline bool winapi_named_semaphore::timed_wait(const TimePoint &abs_time)
0210 {
0211 return m_sem_wrapper.timed_wait(abs_time);
0212 }
0213
0214 inline bool winapi_named_semaphore::remove(const char *name)
0215 {
0216 return windows_named_sync::remove(name);
0217 }
0218
0219 inline bool winapi_named_semaphore::remove(const wchar_t *name)
0220 {
0221 return windows_named_sync::remove(name);
0222 }
0223
0224 }
0225 }
0226 }
0227
0228 #include <boost/interprocess/detail/config_end.hpp>
0229
0230 #endif