file_lock


An os_file_lock represents the locking of a region of bytes in a file. A process can request a read lock or a write lock for a file region. If granted, a read lock prevents other processes from obtaining a write lock for that region; and, if granted, a write lock prevents other processes from obtaining a read or a write lock for that region. If a process requests a lock that is not granted, the process blocks by default until the lock is available.

To place a lock or check for other existing locks, use the os_file class.

Library

File<ToolKit>

Declaration

#include <ospace/file/filelock.h>

class os_file_lock

Enums

enum os_file_lock::type_t
  {
  file_read_lock,
  file_write_lock,
  file_no_lock
  };

Interface

Constructor
os_file_lock( short type , os_off_t length , os_off_t offset , short whence )
Constructs a file lock of length (default 0 ) bytes of type type (default os_file_lock::file_write_lock ) starting at offset (default 0 ) bytes relative to whence (default SEEK_SET ). If length is 0 , extends the region to the current end of file.
Constructor
os_file_lock( const os_file_lock& lock )
Constructs a lock with the same value as lock .
<
bool operator<( const os_file_lock& lock ) const
Returns true if this lock is less than lock .
=
os_file_lock& operator=( const os_file_lock& lock )
Assigns this lock the value of lock .
==
bool operator==( const os_file_lock& lock ) const
Returns true if this lock is the same as lock .
length
os_off_t length() const
Returns the length of the locked region. The value 0 indicates the region continues to EOF.
offset
os_off_t offset() const
Returns the start position of the locked region. This value is relative to the whence returned by whence() . For SEEK_SET and SEEK_END , the start is positive. For SEEK_CUR , the start is either positive or negative.
print
void print( ostream& stream ) const
Prints the lock type (read/write/no lock) to stream .
read_lock
void read_lock()
Sets the lock type to read lock.

Throws: os_file_toolkit_error

set_lock
void set_lock( short type , os_off_t length , os_off_t offset , short whence )
Sets the lock type to type (default os_file_lock::file_write_lock ) with length length (default 0 ) bytes and starting offset (default 0 ) bytes relative to whence (default SEEK_SET ). If length is 0 , extends the region to the current end of file.
type
short type() const
Returns the lock type os_read_lock , os_write_lock , or os_no_lock .
unlock
void unlock()
Sets the lock type to unlocked.

Throws: os_file_toolkit_error

whence
short whence() const
Returns the whence value: SEEK_SET , SEEK_CUR , or SEEK_END , indicating the base for the relative start location.
write_lock
void write_lock()
Sets the lock type to write lock.

UNIX-Specific Interface

Constructor
os_file_lock( const os_flock_t& lock )
Constructs a lock from the os_flock_t structure lock .
(os_flock_t)
operator os_flock_t() const
Returns an os_flock_t structure.
process
os_pid_t process() const
Returns the pid of the process that currently has the lock.

Non-Member Functions

<<
ostream& operator<<( ostream& stream , const os_file_lock& lock )
Prints lock to stream .

Universal Streaming Service

#include <ospace/uss/file.h>

<<
os_bstream& operator<<( os_bstream& stream , const os_file_lock& lock )
Writes lock to stream .
>>
os_bstream& operator>>( os_bstream& stream , os_file_lock& lock )
Reads lock from stream .

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.