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