Skip to content

Commit

Permalink
feat(io): basic locking functions
Browse files Browse the repository at this point in the history
  • Loading branch information
abougouffa committed Jun 22, 2023
1 parent e86a19a commit 75a28ba
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
20 changes: 19 additions & 1 deletion core/me-loaddefs.el
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,25 @@ The executable for \"single-file\" which is used archive HTML pages.")
Save URL into OUT-FILE as a standalone HTML file.
(fn URL OUT-FILE)")
(register-definition-prefixes "../elisp/+io" '("+html2pdf-default-backend" "+save-as-pdf-filename"))
(autoload '+lockedp "../elisp/+io" "\
Return non-nil if the resource NAME is locked.
(fn NAME)")
(autoload '+locked-by-this-process-p "../elisp/+io" "\
Return non-nil if the resource NAME locked by this Emacs instance.
(fn NAME)")
(autoload '+lock "../elisp/+io" "\
Lock the resource named NAME.
(fn NAME)")
(autoload '+unlock "../elisp/+io" "\
Unlock the resource named NAME if locked by this process.
If FORCE-P is non-nil, force unlocking even if the resource is not locked by the
current process.
(fn NAME &optional FORCE-P)")
(register-definition-prefixes "../elisp/+io" '("+html2pdf-default-backend" "+lock--" "+save-as-pdf-filename"))


;;; Generated autoloads from ../elisp/+keybinding.el
Expand Down
40 changes: 40 additions & 0 deletions elisp/+io.el
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,45 @@ When MAIL-MODE-P is non-nil, treat INFILE as a mail."
url out-file))
(user-error "Please set `+single-file-executable' accordingly.")))

(defun +lock--file (name)
(expand-file-name (format "minemacs-%s.lock" name) temporary-file-directory))

(defun +lock--locker-pid (name)
(let ((fname (+lock--file name)))
(and (file-exists-p fname) (string-to-number (+file-read-to-string fname)))))

;;;###autoload
(defun +lockedp (name)
"Return non-nil if the resource NAME is locked."
(when-let ((pid (+lock--locker-pid name)))
(and (process-attributes pid) t)))

;;;###autoload
(defun +locked-by-this-process-p (name)
"Return non-nil if the resource NAME locked by this Emacs instance."
(and (+lockedp name) (equal (emacs-pid) (+lock--locker-pid name))))

;;;###autoload
(defun +lock (name)
"Lock the resource named NAME."
(if (+lockedp name)
(progn (+info! "Resource `%s' already locked!" name) nil)
(+info! "Created lock file for resource `%s'!" name)
(+shutup!
(with-temp-buffer
(insert (format "%d" (emacs-pid)))
(write-file (+lock--file name))))
t))

;;;###autoload
(defun +unlock (name &optional force-p)
"Unlock the resource named NAME if locked by this process.
If FORCE-P is non-nil, force unlocking even if the resource is not locked by the
current process."
(when (or force-p (+locked-by-this-process-p name))
(+info! "Resource `%s' unlocked" name)
(delete-file (+lock--file name))
t))


;;; +io.el ends here

0 comments on commit 75a28ba

Please sign in to comment.