| 1 |
|
| 2 |
the$lock_registry <- new.env(parent = emptyenv()) |
| 3 |
|
| 4 |
renv_lock_acquire <- function(path) { |
| 5 |
|
| 6 |
# normalize path |
| 7 |
path <- renv_lock_path(path) |
| 8 |
dlog("lock", "%s [acquiring lock]", renv_path_pretty(path)) |
| 9 |
|
| 10 |
# if we already have this lock, increment our counter |
| 11 |
count <- the$lock_registry[[path]] %||% 0L |
| 12 |
if (count > 0L) { |
| 13 |
the$lock_registry[[path]] <- count + 1L |
| 14 |
return(TRUE) |
| 15 |
} |
| 16 |
|
| 17 |
# make sure parent directory exists |
| 18 |
ensure_parent_directory(path) |
| 19 |
|
| 20 |
# suppress warnings in this scope |
| 21 |
renv_scope_options(warn = -1L) |
| 22 |
|
| 23 |
# loop until we acquire the lock |
| 24 |
repeat tryCatch( |
| 25 |
renv_lock_acquire_impl(path) && break, |
| 26 |
error = function(cnd) Sys.sleep(0.2) |
| 27 |
) |
| 28 |
|
| 29 |
# mark this path as locked by us |
| 30 |
the$lock_registry[[path]] <- 1L |
| 31 |
|
| 32 |
# notify the watchdog |
| 33 |
renv_watchdog_notify("LockAcquired", list(path = path)) |
| 34 |
|
| 35 |
# TRUE to mark successful lock |
| 36 |
dlog("lock", "%s [lock acquired]", renv_path_pretty(path)) |
| 37 |
TRUE |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
# https://rcrowley.org/2010/01/06/things-unix-can-do-atomically.html |
| 42 |
renv_lock_acquire_impl <- function(path) { |
| 43 |
|
| 44 |
# check for orphaned locks |
| 45 |
if (renv_lock_orphaned(path)) { |
| 46 |
dlog("lock", "%s: removing orphaned lock", path) |
| 47 |
unlink(path, recursive = TRUE, force = TRUE) |
| 48 |
} |
| 49 |
|
| 50 |
# attempt to create the lock |
| 51 |
dir.create(path, mode = "0755", showWarnings = FALSE) |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
renv_lock_release <- function(path) { |
| 56 |
|
| 57 |
# normalize path |
| 58 |
path <- renv_lock_path(path) |
| 59 |
|
| 60 |
# decrement our lock count |
| 61 |
count <- the$lock_registry[[path]] <- the$lock_registry[[path]] - 1L |
| 62 |
|
| 63 |
# remove the lock if we have no more locks |
| 64 |
if (count == 0L) { |
| 65 |
dlog("lock", "%s [lock released]", renv_path_pretty(path)) |
| 66 |
renv_lock_release_impl(path) |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
renv_lock_release_impl <- function(path) { |
| 72 |
unlink(path, recursive = TRUE, force = TRUE) |
| 73 |
remaining <- intersect(path, ls(envir = the$lock_registry, all.names = TRUE)) |
| 74 |
rm(list = remaining, envir = the$lock_registry) |
| 75 |
renv_watchdog_notify("LockReleased", list(path = path)) |
| 76 |
} |
| 77 |
|
| 78 |
renv_lock_orphaned <- function(path) { |
| 79 |
|
| 80 |
timeout <- getOption("renv.lock.timeout", default = 60L) |
| 81 |
if (timeout <= 0L) |
| 82 |
return(TRUE) |
| 83 |
|
| 84 |
info <- renv_file_info(path) |
| 85 |
if (is.na(info$isdir)) |
| 86 |
return(FALSE) |
| 87 |
|
| 88 |
diff <- difftime(Sys.time(), info$mtime, units = "secs") |
| 89 |
diff >= timeout |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
renv_lock_refresh <- function(lock) { |
| 94 |
Sys.setFileTime(lock, Sys.time()) |
| 95 |
} |
| 96 |
|
| 97 |
renv_lock_unload <- function() { |
| 98 |
locks <- ls(envir = the$lock_registry, all.names = TRUE) |
| 99 |
unlink(locks, recursive = TRUE, force = TRUE) |
| 100 |
} |
| 101 |
|
| 102 |
renv_lock_path <- function(path) { |
| 103 |
|
| 104 |
file.path( |
| 105 |
renv_path_normalize(dirname(path), mustWork = TRUE), |
| 106 |
basename(path) |
| 107 |
) |
| 108 |
|
| 109 |
} |