Skip to content

Commit b72c608

Browse files
committed
[ADD] Update syncthing buffer asynchronously
Update the syncthing buffer asynchronously using threads (when supported). The URL library plays well with Emacs' cooperative threading so this change entirely avoids blocking Emacs while retrieving information from syncthing (without needing to rewrite all the url retrieval calls to be async). - Every thread gets its own current buffer, match state, etc. so this doesn't conflict with other threads. - Emacs threads are actually cooperative so there are no races here. This may change in the future but this seems relatively safe for now.
1 parent 16a28be commit b72c608

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

syncthing-draw.el

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,16 @@
776776
(defun syncthing--draw (server)
777777
"Setup buffer and draw widgets for SERVER."
778778
(syncthing-trace)
779+
(cond
780+
;; Threading isn't supported, continue.
781+
((not (bound-and-true-p main-thread)))
782+
;; We are the sync thread, continue.
783+
((eq (current-thread) (syncthing-buffer-update-thread syncthing-buffer)))
784+
;; We are the main thread, wait for syncing to complete before drawing.
785+
((eq (current-thread)
786+
(when-let* ((thread (syncthing-buffer-update-thread syncthing-buffer)))
787+
(thread-join thread))))
788+
(t (error "Can only draw in the syncthing buffer from the update thread or the main thread")))
779789
(when (syncthing-buffer-initialized syncthing-buffer)
780790
(setf (syncthing-buffer-point syncthing-buffer) (point)))
781791
(let ((inhibit-read-only t))

syncthing-state.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
name collapse-after-start initialized
3333
fold-folders skip-fold-folders
3434
fold-devices skip-fold-devices
35-
point)
35+
point update-thread)
3636

3737
(defvar-local syncthing-watcher nil
3838
"Buffer-local instance for event poller.")

syncthing-update.el

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,27 @@
1515
(require 'syncthing-watcher)
1616

1717

18-
(defun syncthing--update (&rest _)
19-
"Update function for every refresh iteration."
20-
(syncthing-trace)
21-
(when-let* ((buf (syncthing-buffer-name syncthing-buffer)))
22-
(with-current-buffer buf
18+
(defun syncthing--update-synchronous (buffer)
19+
"Synchronously update the specified syncthing BUFFER."
20+
(when (buffer-live-p buffer)
21+
(with-current-buffer buffer
2322
(syncthing--ping syncthing-server)
2423
(syncthing--server-update syncthing-server)
2524
(syncthing--init-state)
2625
(syncthing--draw syncthing-server)
27-
(setf (syncthing-buffer-collapse-after-start syncthing-buffer) nil))))
26+
(setf (syncthing-buffer-collapse-after-start syncthing-buffer) nil
27+
(syncthing-buffer-update-thread syncthing-buffer) nil))))
28+
29+
(defun syncthing--update (&rest _)
30+
"Update function for every refresh iteration."
31+
(syncthing-trace)
32+
(when-let* ((buf (current-buffer)))
33+
(if (and (bound-and-true-p main-thread)
34+
(not (and-let* ((thread (syncthing-buffer-update-thread syncthing-buffer)))
35+
(thread-live-p thread))))
36+
(setf (syncthing-buffer-update-thread syncthing-buffer)
37+
(make-thread (lambda () (syncthing--update-synchronous buf)) "Syncthing Update"))
38+
(syncthing--update-synchronous buf))))
2839

2940
(defun syncthing--calc-speed (server data)
3041
"Calculate upload and download rate DATA for SERVER."

0 commit comments

Comments
 (0)