1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2011. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_INTERPROCESS_FILE_LOCK_HPP
#define BOOST_INTERPROCESS_FILE_LOCK_HPP
#if (defined _MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/detail/os_file_functions.hpp>
#include <boost/interprocess/detail/os_thread_functions.hpp>
#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
#include <boost/move/move.hpp>
//!\file
//!Describes a class that wraps file locking capabilities.
namespace boost {
namespace interprocess {
//!A file lock, is a mutual exclusion utility similar to a mutex using a
//!file. A file lock has sharable and exclusive locking capabilities and
//!can be used with scoped_lock and sharable_lock classes.
//!A file lock can't guarantee synchronization between threads of the same
//!process so just use file locks to synchronize threads from different processes.
class file_lock
{
/// @cond
//Non-copyable
BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock)
/// @endcond
public:
//!Constructs an empty file mapping.
//!Does not throw
file_lock()
: m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
{}
//!Opens a file lock. Throws interprocess_exception if the file does not
//!exist or there are no operating system resources.
file_lock(const char *name);
//!Moves the ownership of "moved"'s file mapping object to *this.
//!After the call, "moved" does not represent any file mapping object.
//!Does not throw
file_lock(BOOST_RV_REF(file_lock) moved)
: m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
{ this->swap(moved); }
//!Moves the ownership of "moved"'s file mapping to *this.
//!After the call, "moved" does not represent any file mapping.
//!Does not throw
file_lock &operator=(BOOST_RV_REF(file_lock) moved)
{
file_lock tmp(boost::move(moved));
this->swap(tmp);
return *this;
}
//!Closes a file lock. Does not throw.
~file_lock();
//!Swaps two file_locks.
//!Does not throw.
void swap(file_lock &other)
{
file_handle_t tmp = m_file_hnd;
m_file_hnd = other.m_file_hnd;
other.m_file_hnd = tmp;
}
//Exclusive locking
//!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
//! and if another thread has exclusive, or sharable ownership of
//! the mutex, it waits until it can obtain the ownership.
//!Throws: interprocess_exception on error.
void lock();
//!Effects: The calling thread tries to acquire exclusive ownership of the mutex
//! without waiting. If no other thread has exclusive, or sharable
//! ownership of the mutex this succeeds.
//!Returns: If it can acquire exclusive ownership immediately returns true.
//! If it has to wait, returns false.
//!Throws: interprocess_exception on error.
bool try_lock();
//!Effects: The calling thread tries to acquire exclusive ownership of the mutex
//! waiting if necessary until no other thread has exclusive, or sharable
//! ownership of the mutex or abs_time is reached.
//!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
//!Throws: interprocess_exception on error.
bool timed_lock(const boost::posix_time::ptime &abs_time);
//!Precondition: The thread must have exclusive ownership of the mutex.
//!Effects: The calling thread releases the exclusive ownership of the mutex.
//!Throws: An exception derived from interprocess_exception on error.
void unlock();
//Sharable locking
//!Effects: The calling thread tries to obtain sharable ownership of the mutex,
//! and if another thread has exclusive ownership of the mutex, waits until
//! it can obtain the ownership.
//!Throws: interprocess_exception on error.
void lock_sharable();
//!Effects: The calling thread tries to acquire sharable ownership of the mutex
//! without waiting. If no other thread has exclusive ownership of the
//! mutex this succeeds.
//!Returns: If it can acquire sharable ownership immediately returns true. If it
//! has to wait, returns false.
//!Throws: interprocess_exception on error.
bool try_lock_sharable();
//!Effects: The calling thread tries to acquire sharable ownership of the mutex
//! waiting if necessary until no other thread has exclusive ownership of
//! the mutex or abs_time is reached.
//!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
//!Throws: interprocess_exception on error.
bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
//!Precondition: The thread must have sharable ownership of the mutex.
//!Effects: The calling thread releases the sharable ownership of the mutex.
//!Throws: An exception derived from interprocess_exception on error.
void unlock_sharable();
/// @cond
private:
file_handle_t m_file_hnd;
bool timed_acquire_file_lock
(file_handle_t hnd, bool &acquired, const boost::posix_time::ptime &abs_time)
{
//Obtain current count and target time
boost::posix_time::ptime now = microsec_clock::universal_time();
using namespace boost::detail;
if(now >= abs_time) return false;
do{
if(!ipcdetail::try_acquire_file_lock(hnd, acquired))
return false;
if(acquired)
return true;
else{
now = microsec_clock::universal_time();
if(now >= abs_time){
acquired = false;
return true;
}
// relinquish current time slice
ipcdetail::thread_yield();
}
}while (true);
}
bool timed_acquire_file_lock_sharable
(file_handle_t hnd, bool &acquired, const boost::posix_time::ptime &abs_time)
{
//Obtain current count and target time
boost::posix_time::ptime now = microsec_clock::universal_time();
using namespace boost::detail;
if(now >= abs_time) return false;
do{
if(!ipcdetail::try_acquire_file_lock_sharable(hnd, acquired))
return false;
if(acquired)
return true;
else{
now = microsec_clock::universal_time();
if(now >= abs_time){
acquired = false;
return true;
}
// relinquish current time slice
ipcdetail::thread_yield();
}
}while (true);
}
/// @endcond
};
inline file_lock::file_lock(const char *name)
{
m_file_hnd = ipcdetail::open_existing_file(name, read_write);
if(m_file_hnd == ipcdetail::invalid_file()){
error_info err(system_error_code());
throw interprocess_exception(err);
}
}
inline file_lock::~file_lock()
{
if(m_file_hnd != ipcdetail::invalid_file()){
ipcdetail::close_file(m_file_hnd);
m_file_hnd = ipcdetail::invalid_file();
}
}
inline void file_lock::lock()
{
if(!ipcdetail::acquire_file_lock(m_file_hnd)){
error_info err(system_error_code());
throw interprocess_exception(err);
}
}
inline bool file_lock::try_lock()
{
bool result;
if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){
error_info err(system_error_code());
throw interprocess_exception(err);
}
return result;
}
inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time)
{
if(abs_time == boost::posix_time::pos_infin){
this->lock();
return true;
}
bool result;
if(!this->timed_acquire_file_lock(m_file_hnd, result, abs_time)){
error_info err(system_error_code());
throw interprocess_exception(err);
}
return result;
}
inline void file_lock::unlock()
{
if(!ipcdetail::release_file_lock(m_file_hnd)){
error_info err(system_error_code());
throw interprocess_exception(err);
}
}
inline void file_lock::lock_sharable()
{
if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){
error_info err(system_error_code());
throw interprocess_exception(err);
}
}
inline bool file_lock::try_lock_sharable()
{
bool result;
if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){
error_info err(system_error_code());
throw interprocess_exception(err);
}
return result;
}
inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time)
{
if(abs_time == boost::posix_time::pos_infin){
this->lock_sharable();
return true;
}
bool result;
if(!this->timed_acquire_file_lock_sharable(m_file_hnd, result, abs_time)){
error_info err(system_error_code());
throw interprocess_exception(err);
}
return result;
}
inline void file_lock::unlock_sharable()
{
if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){
error_info err(system_error_code());
throw interprocess_exception(err);
}
}
} //namespace interprocess {
} //namespace boost {
#include <boost/interprocess/detail/config_end.hpp>
#endif //BOOST_INTERPROCESS_FILE_LOCK_HPP
|