lockDirectory: Lock and unlock directories

View source: R/lockDirectory.R

lockDirectoryR Documentation

Lock and unlock directories

Description

Lock and unlock the package and version directories for thread-safe processing.

Usage

lockDirectory(path, ...)

unlockDirectory(lock.info, clear = TRUE, ...)

Arguments

path

String containing the path to a versioned directory. The dirname should be the package cache while the basename should be a version number.

...

For lockDirectory, further arguments to pass to lock.

For unlockDirectory, further arguments to pass to clearDirectories.

lock.info

The list returned by lockDirectory.

clear

Logical scalar indicating whether to remove expired versions via clearDirectories.

Details

lockDirectory actually creates two locks:

  • The first ā€œVā€ lock is applied to the versioned directory (i.e., basename(path)) within the package cache (i.e., dirname(path)). This provides thread-safe read/write on its contents, protecting against other processes that want to write to the same versioned directory. If the caller is only reading from path, they can set exclusive=FALSE in ... to define a shared lock for concurrent reads across multiple processes.

  • The second ā€œPā€ lock is applied to the package cache and is always a shared lock, regardless of the contents of .... This provides thread-safe access to the lock file used by the V lock, protecting it from deletion when the relevant directory expires in clearDirectories.

If dirname(path) does not exist, it will be created by lockDirectory.

clearDirectories is called in unlockDirectory as the former needs to hold an exclusive lock on the package cache. Thus, the clearing can only be performed after the P lock created by lockDirectory is released.

Value

lockDirectory returns a list of locking information, including lock handles generated by the filelock package.

unlockDirectory unlocks the handles generated by lockDirectory. If clear=TRUE, versioned directories that have expired are removed by clearDirectories. It returns a NULL invisibly.

Author(s)

Aaron Lun

Examples

# Creating the relevant directories.
cache.dir <- tempfile(pattern="expired_demo")
version <- package_version("1.11.0")

handle <- lockDirectory(file.path(cache.dir, version))
handle
unlockDirectory(handle)

list.files(cache.dir)


LTLA/dir.expiry documentation built on Oct. 20, 2024, 8:09 a.m.