19 #ifndef ROSTLAB_FLOCK_RESOURCE 20 #define ROSTLAB_FLOCK_RESOURCE 40 std::string _filename;
56 file_lock_resource(
const std::string& __file,
const std::string& __mode =
"r",
int __cmd = F_SETLKW,
short __type = F_RDLCK,
bool __dbg =
false ) : _fh(NULL), dbg(__dbg)
58 acquire( __file, __mode, __cmd, __type, __dbg );
61 inline void acquire(
const std::string& __file,
const std::string& __mode =
"r",
int __cmd = F_SETLKW,
short __type = F_RDLCK,
bool __dbg =
false ) throw (
runtime_error,
wouldblock )
67 _fh = fopen( _filename.c_str(), __mode.c_str() );
68 if( !_fh )
throw runtime_error( strerror( errno ) );
73 lock.l_whence = SEEK_SET;
77 if( fcntl( fileno(_fh), __cmd, &lock ) )
79 int fcntl_errno = errno;
80 fclose(_fh); _fh = NULL;
81 if( fcntl_errno == EACCES || fcntl_errno == EAGAIN )
throw wouldblock();
82 else throw runtime_error( strerror( fcntl_errno ) );
84 else if(dbg) std::cerr <<
"LOCK_" << __mode <<
" " << __cmd <<
" " << __type <<
" '" << _filename <<
"'\n";
92 if( fclose(_fh) )
throw runtime_error( strerror( errno ) );
93 if(dbg) std::cerr <<
"LOCK_UN '" << _filename <<
"'\n";
103 operator FILE* () {
return _fh; }
105 inline const std::string&
111 #endif // ROSTLAB_FLOCK_RESOURCE
virtual ~file_lock_resource()
file_lock_resource(const std::string &__file, const std::string &__mode="r", int __cmd=F_SETLKW, short __type=F_RDLCK, bool __dbg=false)
void acquire(const std::string &__file, const std::string &__mode="r", int __cmd=F_SETLKW, short __type=F_RDLCK, bool __dbg=false)
const std::string & filename() const