Re: native busy loop and thread-terminate!

Jens Thiele <[email protected]> Thu, 27 Feb 2025 17:08:42 +0100
Newsgroups gmane.lisp.scheme.gauche
Message-ID <[email protected]>
Hi,

sorry for the late reply

Shiro Kawai <[email protected]> writes:

> The last pushed commit addresses the busy loop issue; thread-terminate!
> sends a signal and wait a bit, and if the target is still running, sends
> the signal again.  In the receiving side, if thread termination signal is
> received while the previous one hasn't been processed, it terminates itself.

tested this in a debian/trixie amd64 schroot
seems to work

#!/bin/sh
#| -*- mode: scheme; coding: utf-8; -*-
exec gosh -I. -- $0 "$@"
|#

;; native busy loops are not interrupted by thread-terminate!
;; nb: that changed in commit 7f3cfd906 (Thread termination by signal overhaul)

(use gauche.threads)
(use file.util)
(use runtime-compile)

(compile-and-load
 `((inline-stub
    (define-cproc busy-loop ()
      (let* ((i::int 0))
	(while (1)
	  ;; you would do some calculations here
	  (inc! i))))))
 '(busy-loop))

(define (num-threads)
  (guard (e
	  [else
	   +nan.0])
	 (length (directory-list "/proc/self/task" :children? #t))))

(define (main args)
  #?=(num-threads)
  (let1 t (thread-start! (make-thread (lambda()
					(busy-loop))))
    (sys-sleep 1)
    #?=(num-threads)
    (thread-terminate! t)
    #?=(num-threads)
    (guard (e [else #?=e])
	   (thread-join! t)))
  #?=(num-threads)
  (sys-sleep 1)
  #?=(num-threads)
  (sys-sleep 10)
  0)

(trixie-amd64-sbuild)karme@amalthea:/tmp/test$ ./busy-loop3.scm
#?="././busy-loop3.scm":29:(num-threads)
#?-    4
#?="././busy-loop3.scm":33:(num-threads)
#?-    5
#?="././busy-loop3.scm":35:(num-threads)
#?-    4
#?=e
#?-    #<terminated-thread-exception: #<thread #f (1) terminated 0x7 ...
#?="././busy-loop3.scm":38:(num-threads)
#?-    4
#?="././busy-loop3.scm":40:(num-threads)
#?-    4

> This does not address the cleanup issue.

ok

jens