Skip to content

Core\IO\File::lock

$file->lock(): void

Takes an exclusive lock on an open file. Only one handle can have the lock at a time.

$file->lock() works on a Core\IO\File that Core\IO::open returned. When no other handle has the lock, lock takes it and returns. When another handle already has it, lock does not wait. It throws an IOError at once. Your program can then stop, or try again later.

There is no unlock method. The lock is released when the handle is closed, with close or at the end of the request.

Use a lock when two programs, or two requests, must not change the same file at the same time. On some systems a lock does not stop a handle without the lock from reading or writing the file. So every program that changes the file should call lock first.

A closed handle throws a RuntimeError.

Return value

Returns void. Nothing. There is no unlock. The lock lasts as long as the handle, so close releases it, and so does the end of the request.

Errors

  • RuntimeErrorThe handle has already been closed.
  • IOErrorAnother handle already has the lock, or the operating system did not allow it. Whether the lock stops reads and writes by handles that do not have the lock depends on the platform.

Examples

Lock and write

<?nvs
// Shows how to take a lock on a file, write to it, and release the lock with `close`.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'stock.txt');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Write);
$file->lock();
$file->write("apples: 12\n");
// `close` also releases the lock.
$file->close();
// This prints "apples: 12".
echo Core\IO::readText($path);
Output
apples: 12

A second handle is refused

<?nvs
// Shows that a second handle cannot take a lock while the first handle has it.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'shared.txt');
Core\IO\File $first = Core\IO::open($path, Core\IO::FileMode::ReadWrite);
$first->lock();
// The second handle opens the same file. Its `lock` throws an `IOError` at once.
// This prints "the file is locked".
Core\IO\File $second = Core\IO::open($path, Core\IO::FileMode::ReadWrite);
try {
$second->lock();
} catch (IOError $e) {
echo "the file is locked\n";
}
// After the first handle is closed, the second handle gets the lock.
// This prints "the lock is free".
$first->close();
$second->lock();
echo "the lock is free\n";
$second->close();
Output
the file is locked
the lock is free

Run a job only once at a time

<?nvs
// Shows how a nightly job uses a lock file, so that two copies of the job never run at the same time.
class Job
{
// Returns `true` when this copy of the job ran, and `false` when another copy has the lock.
public static function run(string $lockPath): bool
{
Core\IO\File $lock = Core\IO::open($lockPath, Core\IO::FileMode::ReadWrite);
try {
$lock->lock();
} catch (IOError $busy) {
$lock->close();
return false;
}
echo "sending the reports\n";
$lock->close();
return true;
}
}
string $lockPath = Core\Path::join(Core\IO::createTemporaryDirectory(), 'reports.lock');
// Another copy of the job is running and has the lock. This prints "another copy is running".
Core\IO\File $other = Core\IO::open($lockPath, Core\IO::FileMode::ReadWrite);
$other->lock();
if (!Job::run($lockPath)) {
echo "another copy is running\n";
}
// The other copy finishes and closes its handle. Now this copy runs and prints "sending the reports".
$other->close();
Job::run($lockPath);
Output
another copy is running
sending the reports