using threads in mode line modules to avoid blocking

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

using threads in mode line modules to avoid blocking

Joseph Mingrone
Hi,

I've been playing around writing some mode line modules.  I quickly
realized that anything that blocks, blocks the whole window manager.
Using threads improves the situation, but there are still hiccups.
These hiccups mean that hitting the prefix key results in a pause before
the rat shows up.  This can be annoying because no one waits for the
rat before pressing the subsequent keys.

Below is some code that checks a remote maildir directory for new
messages.  The hiccups occur when the connection to the remote host is
broken.  Why this is?  I hoped that threading would have meant a
smoother experience.

Joseph

(defun fmt-maildir-modeline (ml)
  "Return the number of new mails"
  (declare (ignore ml))
  (let ((now (/ (get-internal-real-time) internal-time-units-per-second)))
    (when (or (= 0 *maildir-prev-time*) (>= (- now *maildir-prev-time*) 60))
      (setf *maildir-prev-time* now)
      (sb-thread:make-thread
       (lambda ()
         (setf *new-mail-count*
               (string-trim
                '(#\Space #\Tab #\Newline)
                (stumpwm::run-prog-collect-output
                 stumpwm::*shell-program* "-c"
                 (concatenate 'string "/usr/bin/ssh -p " *port*
                              " -x -o ConnectTimeout=1 " *user*
                              "@" *host* " 'ls " *path*
                              " | wc -l'"))))))))
  (when (string= *new-mail-count* "0")
    (setf *new-mail-count* ""))
  (format nil "~a" *new-mail-count*))


_______________________________________________
Stumpwm-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/stumpwm-devel
Reply | Threaded
Open this post in threaded view
|

Re: using threads in mode line modules to avoid blocking

David Bjergaard
Thinking out loud (this is beyond my expertise), could it be that your
thread isn't being terminated properly if you lose connection to the
remote host? If stumpwm (or the lisp kernel) polls your thread and gets
no response, it probably just hangs there waiting.

Someone with more experience may have better insights.

    Dave

Joseph Mingrone <[hidden email]> writes:

> Hi,
>
> I've been playing around writing some mode line modules.  I quickly
> realized that anything that blocks, blocks the whole window manager.
> Using threads improves the situation, but there are still hiccups.
> These hiccups mean that hitting the prefix key results in a pause before
> the rat shows up.  This can be annoying because no one waits for the
> rat before pressing the subsequent keys.
>
> Below is some code that checks a remote maildir directory for new
> messages.  The hiccups occur when the connection to the remote host is
> broken.  Why this is?  I hoped that threading would have meant a
> smoother experience.
>
> Joseph
>
> (defun fmt-maildir-modeline (ml)
>   "Return the number of new mails"
>   (declare (ignore ml))
>   (let ((now (/ (get-internal-real-time) internal-time-units-per-second)))
>     (when (or (= 0 *maildir-prev-time*) (>= (- now *maildir-prev-time*) 60))
>       (setf *maildir-prev-time* now)
>       (sb-thread:make-thread
>        (lambda ()
> (setf *new-mail-count*
>       (string-trim
> '(#\Space #\Tab #\Newline)
> (stumpwm::run-prog-collect-output
> stumpwm::*shell-program* "-c"
> (concatenate 'string "/usr/bin/ssh -p " *port*
>      " -x -o ConnectTimeout=1 " *user*
>      "@" *host* " 'ls " *path*
>      " | wc -l'"))))))))
>   (when (string= *new-mail-count* "0")
>     (setf *new-mail-count* ""))
>   (format nil "~a" *new-mail-count*))
>
>
> _______________________________________________
> Stumpwm-devel mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/stumpwm-devel

_______________________________________________
Stumpwm-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/stumpwm-devel
Reply | Threaded
Open this post in threaded view
|

Re: using threads in mode line modules to avoid blocking

Joseph Mingrone
David Bjergaard <[hidden email]> writes:
> Thinking out loud (this is beyond my expertise), could it be that your
> thread isn't being terminated properly if you lose connection to the
> remote host? If stumpwm (or the lisp kernel) polls your thread and gets
> no response, it probably just hangs there waiting.
>
> Someone with more experience may have better insights.
>
>     Dave

I tested a new version that only creates one thread and keeps it
running.  It causes the same choppiness, so I don't think that is the
problem.

Joseph

;; swm-maildir-modline.lisp
;;
;; Put %m in your modeline format string to show number of new mails
;;

(in-package #:swm-maildir-modeline)

(defvar *checking-flag* nil)
(defvar *host* "myhost")
(defvar *interval* 60)
(defvar *maildir-prev-time* 0)
(defvar *new-mail-count* "")
(defvar *port* "22")
(defvar *path* "mail/new")
(defvar *user* "me")

(defun check-new-messages ()
  (sb-thread:make-thread
    (lambda ()
      (loop
         (setf *new-mail-count*
               (string-trim
                '(#\Space #\Tab #\Newline)
                (stumpwm::run-prog-collect-output
                 stumpwm::*shell-program* "-c"
                 (concatenate 'string "/usr/bin/ssh -p " *port*
                              " -x -o ConnectTimeout=1 " *user*
                              "@" *host* " 'ls " *path*
                              " | wc -l'"))))
      (sleep *interval*)))
    :name "check-new-messages-thread"))

(defun fmt-maildir-modeline (ml)
  "Return the number of new mails"
  (when (not *checking-flag*)
    (setf *checking-flag* t)
    (check-new-messages))
  (when (string= *new-mail-count* "0")
    (setf *new-mail-count* ""))
  (format nil "~a" *new-mail-count*))

;; Install formatter
(stumpwm::add-screen-mode-line-formatter #\m #'fmt-maildir-modeline)


_______________________________________________
Stumpwm-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/stumpwm-devel