|
Locking File
|
|
|
Locking a region of a file gives the locking process exclusive access to the specified region, that is, denies all other processes
both read and write access to the region. Locking is used typically in situations when multiple processes or threads have access
to the same file and therefore can interfere. When file region is locked, access to it is serialized, guaranteeing that data is
processed in a consistent manner and will not be corrupted. IXFile supports both automatic and manual locking that can be used separately
or collectively, depending on your needs.
Automatic file locking is specified during initialization and can be activated for reading, writing or both;
see Initialize for information on how to do this. When automatic locking is enabled, region in file of appropriate
size and position is locked before read or write operation takes place. After data is accessed and operation completes, region is unlocked.
Automatic locking is always performed with regard to data buffer which can contain partial data. If data is entirely in buffer,
no additional locking takes place because region is locked when buffer is flushed or refilled. If data does not fit or is partially in buffer,
entire data region is locked before read or write operation takes place, and lock remains active until all requested data is completely read or written.
Please note that locking is performed whenever file data needs to be accessed, regardless of actual user activity - locking is enforced not only on
Get and Put operations but also on Flush, SetSize, Shrink, Expand, Find and all other activities which explicitly
or implicitly access file data. Automatic locking always concerns single operation of reading or writing data and therefore cannot be used for exclusive read-modify-write operation;
for such processing manual locking must be used.
Automatic locking makes use of multiple locking (see below) to allow developers to combine it with manual locking.
Manual locking is used whenever specific file region must be locked, and stay locked as long as required. With LockByte,
LockShort, LockLong, LockFloat,
LockDouble, LockText, LockUnicodeText,
LockVariant and LockBinary methods, you can lock data regions of various types
supported by IXFile. Size of file region is calculated internally based on data type and number of elements to lock. All methods return lock handle that
identifies locked region (its size and position) and this handle should be finally passed to UnLock method for unlocking.
Manual locking can be used with or without multiple locking feature (see below). When multiple locking is used, file regions can overlap and automatic locking
can be used collectively if desired. When disabled, regions cannot overlap and are always directly mapped to physical locks. In such case, however, it is user responsibility
to properly handle them.
Multiple locking allows locking the same region or its parts many times; single IXFile object can lock as many overlapping file regions
as needed, without regard to their influence on each other. In other words you can lock a region that is already locked and then properly unlock it.
This is possible because map of locked regions is maintained internally by the object to guarantee correct order of unlocking.
File region is physically unlocked only when there are no dependent locks active. Because of it, however, unlocking may not result
in physical unlocking of file region; in other words you must assume that after calling UnLock method,
specified region can be still physically locked. Please note that multiple locking is allowed only for the same IXFile object -
you cannot lock region that overlapps region locked by another application, thread or other IXFile objects.
Locked region should be unlocked as soon as possible to allow other processes perform their operations and avoid deadlocks.
To unlock file region use UnLock method, passing lock handle obtained from one of the locking methods.
When file is being closed or detached from the object, all its locks are automatically removed.
Examples
C++
int main(int, char**)
{
IFile ixf;
long lock1, lock2, lock3;
short data;
char txt[256];
ixf.SetLicenseKey("YOURLICENSEKEY");
ixf.Initialize();
ixf.Open("test.bin");
lock1 = ixf.LockByte(10, 100, FALSE);
lock2 = ixf.LockByte(150, 100, FALSE);
lock3 = ixf.LockByte(0, 200, TRUE);
.
.
.
ixf.UnLock(lock1);
ixf.UnLock(lock3);
ixf.UnLock(lock2);
.
.
.
ixf.Initialize(IXF_MODE_READ | IXF_MODE_READLOCK | IXF_MODE_CLOSE);
ixf.Open("test.bin");
lock1 = ixf.LockByte(10, 100, FALSE);
ixf.GetText(txt, 256, 50, IXF_TEXT_NULLTERM);
ixf.GetShort(&data, 1, 75);
ixf.UnLock(lock1);
return(0);
}
BASIC
Sub Main()
Dim ixf As IXFile
Dim lock1 As Long, lock2 As Long, lock3 As Long
Dim data as Integer
Dim txt As String
Set ixf = New IXFile
ixf.SetLicenseKey "YOURLICENSEKEY"
ixf.Initialize
ixf.Open "test.bin"
lock1 = ixf.LockByte(10, 100, False)
lock2 = ixf.LockByte(150, 100, False)
lock3 = ixf.LockByte(0, 200, True)
.
.
.
ixf.UnLock lock1
ixf.UnLock lock3
ixf.UnLock lock2
.
.
.
ixf.Initialize IXF_MODE_READ Or IXF_MODE_READLOCK Or IXF_MODE_CLOSE
ixf.Open "test.bin"
lock1 = ixf.LockByte(10, 100, False)
ixf.GetText txt, 256, 50, IXF_TEXT_NULLTERM
ixf.GetShort data, 1, 75
ixf.UnLock lock1
End Sub
See Also
LockByte,
LockShort,
LockLong,
LockFloat,
LockDouble,
LockText,
LockUnicodeText,
LockVariant,
LockBinary,
UnLock