File indexing completed on 2025-01-18 09:38:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_FILE_LOCK_HPP
0012 #define BOOST_INTERPROCESS_FILE_LOCK_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/exceptions.hpp>
0025 #include <boost/interprocess/detail/os_file_functions.hpp>
0026 #include <boost/interprocess/detail/os_thread_functions.hpp>
0027 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
0028 #include <boost/interprocess/sync/detail/locks.hpp>
0029 #include <boost/move/utility_core.hpp>
0030
0031
0032
0033
0034 namespace boost {
0035 namespace interprocess {
0036
0037
0038
0039
0040
0041
0042
0043 class file_lock
0044 {
0045 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0046
0047 BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock)
0048 #endif
0049
0050 public:
0051
0052
0053 file_lock() BOOST_NOEXCEPT
0054 : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
0055 {}
0056
0057
0058
0059 file_lock(const char *name);
0060
0061 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0062
0063
0064
0065
0066
0067 file_lock(const wchar_t *name);
0068 #endif
0069
0070
0071
0072
0073 file_lock(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT
0074 : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
0075 { this->swap(moved); }
0076
0077
0078
0079
0080 file_lock &operator=(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT
0081 {
0082 file_lock tmp(boost::move(moved));
0083 this->swap(tmp);
0084 return *this;
0085 }
0086
0087
0088 ~file_lock();
0089
0090
0091
0092 void swap(file_lock &other) BOOST_NOEXCEPT
0093 {
0094 file_handle_t tmp = m_file_hnd;
0095 m_file_hnd = other.m_file_hnd;
0096 other.m_file_hnd = tmp;
0097 }
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111 void lock();
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 bool try_lock();
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 template<class TimePoint>
0139 bool timed_lock(const TimePoint &abs_time);
0140
0141
0142
0143 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0144 { return this->timed_lock(abs_time); }
0145
0146
0147
0148 template<class Duration> bool try_lock_for(const Duration &dur)
0149 { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
0150
0151
0152
0153
0154 void unlock();
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168 void lock_sharable();
0169
0170
0171
0172 void lock_shared()
0173 { this->lock_sharable(); }
0174
0175
0176
0177
0178
0179
0180
0181 bool try_lock_sharable();
0182
0183
0184
0185 bool try_lock_shared()
0186 { return this->try_lock_sharable(); }
0187
0188
0189
0190
0191
0192
0193 template<class TimePoint>
0194 bool timed_lock_sharable(const TimePoint &abs_time);
0195
0196
0197
0198 template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
0199 { return this->timed_lock_sharable(abs_time); }
0200
0201
0202
0203 template<class Duration> bool try_lock_shared_for(const Duration &dur)
0204 { return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
0205
0206
0207
0208
0209 void unlock_sharable();
0210
0211
0212
0213 void unlock_shared()
0214 { this->unlock_sharable(); }
0215
0216 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0217 private:
0218 file_handle_t m_file_hnd;
0219
0220 #endif
0221 };
0222
0223 inline file_lock::file_lock(const char *name)
0224 {
0225 m_file_hnd = ipcdetail::open_existing_file(name, read_write);
0226
0227 if(m_file_hnd == ipcdetail::invalid_file()){
0228 error_info err(system_error_code());
0229 throw interprocess_exception(err);
0230 }
0231 }
0232
0233 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0234
0235 inline file_lock::file_lock(const wchar_t *name)
0236 {
0237 m_file_hnd = ipcdetail::open_existing_file(name, read_write);
0238
0239 if(m_file_hnd == ipcdetail::invalid_file()){
0240 error_info err(system_error_code());
0241 throw interprocess_exception(err);
0242 }
0243 }
0244
0245 #endif
0246
0247 inline file_lock::~file_lock()
0248 {
0249 if(m_file_hnd != ipcdetail::invalid_file()){
0250 ipcdetail::close_file(m_file_hnd);
0251 m_file_hnd = ipcdetail::invalid_file();
0252 }
0253 }
0254
0255 inline void file_lock::lock()
0256 {
0257 if(!ipcdetail::acquire_file_lock(m_file_hnd)){
0258 error_info err(system_error_code());
0259 throw interprocess_exception(err);
0260 }
0261 }
0262
0263 inline bool file_lock::try_lock()
0264 {
0265 bool result;
0266 if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){
0267 error_info err(system_error_code());
0268 throw interprocess_exception(err);
0269 }
0270 return result;
0271 }
0272
0273 template<class TimePoint>
0274 inline bool file_lock::timed_lock(const TimePoint &abs_time)
0275 { return ipcdetail::try_based_timed_lock(*this, abs_time); }
0276
0277 inline void file_lock::unlock()
0278 {
0279 if(!ipcdetail::release_file_lock(m_file_hnd)){
0280 error_info err(system_error_code());
0281 throw interprocess_exception(err);
0282 }
0283 }
0284
0285 inline void file_lock::lock_sharable()
0286 {
0287 if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){
0288 error_info err(system_error_code());
0289 throw interprocess_exception(err);
0290 }
0291 }
0292
0293 inline bool file_lock::try_lock_sharable()
0294 {
0295 bool result;
0296 if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){
0297 error_info err(system_error_code());
0298 throw interprocess_exception(err);
0299 }
0300 return result;
0301 }
0302
0303 template<class TimePoint>
0304 inline bool file_lock::timed_lock_sharable(const TimePoint &abs_time)
0305 {
0306 ipcdetail::lock_to_sharable<file_lock> lsh(*this);
0307 return ipcdetail::try_based_timed_lock(lsh, abs_time);
0308 }
0309
0310 inline void file_lock::unlock_sharable()
0311 {
0312 if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){
0313 error_info err(system_error_code());
0314 throw interprocess_exception(err);
0315 }
0316 }
0317
0318 }
0319 }
0320
0321 #include <boost/interprocess/detail/config_end.hpp>
0322
0323 #endif