summaryrefslogtreecommitdiffstats
path: root/lockdir.tl
diff options
context:
space:
mode:
Diffstat (limited to 'lockdir.tl')
-rw-r--r--lockdir.tl19
1 files changed, 19 insertions, 0 deletions
diff --git a/lockdir.tl b/lockdir.tl
new file mode 100644
index 0000000..3d6fe0f
--- /dev/null
+++ b/lockdir.tl
@@ -0,0 +1,19 @@
+(defun acquire-lock (path)
+ (let ((lock `@path.#lock#`)
+ (start-time (time)))
+ (while (< (- (time) start-time) 30)
+ (catch (if (mkdir lock #o700)
+ (return-from acquire-lock lock))
+ (file-error (e)
+ (unless (= 17 (errno))
+ (throw 'file-error e))
+ (usleep 1000000))))
+ (error "acquire-lock: unable to acquire ~a" lock)))
+
+(defun release-lock (lock)
+ (ignerr (remove-path lock)))
+
+(defmacro with-lock (path . body)
+ (let ((lock (gensym)))
+ ^(with-resources ((,lock (acquire-lock ,path) (release-lock ,lock)))
+ ,*body)))