<div>Aha |thread-state| - neat! Thank you for clarifying, was not aware of it.</div><div><br></div><div>Well it implements the critical logics really, so extending it would be rather for convenience than necessity.</div><div>

<br></div><div><br></div><div>(Reasoning:</div><div><br></div><div>Without the awareness of |thread-state|, what brought me to suggest this ##thread-status which is indeed a |thread-state| with continuation capturing, was this reasoning:</div>

<div><br></div><div> * As a user you want to be able to know what is going on in Gambit, in the sense, you want to be able to get a ,be printout of every running thread.</div><div>   (And to know if it's initializing/running/ended with a result value and if so which/ended with exception and if so which, though that's completely secondary here)</div>

<div><br></div><div> * Getting the ,be requires catching the thread's current continuation.</div><div><br></div><div> * Catching the continuation of an uninitialized thread gives SIGSEGV</div><div><br></div><div> * Knowing if a thread is initialized or not requires access to thread-internal structures using macros that only Gambit is compiled with. To extract this information reliably, we need to extend Gambit with some kind of getter for it.</div>

<div><br></div><div> * Alas: You need a checker for thread status, as to ensure it was initialized so you don't attempt to capture its continuation unless it was, and this checker needs to be implemented in Gambit</div>

<div><br></div><div>This line of reasoning brought |##thread-status| which checks for every possible thread state, and optionally captures the thread's continuation if the thread is running.</div><div><br></div><div>
<br>
</div><div>|thread-state| returns all of this info, so the only thing that could be added to it would be that if thread is active it catches its continuation and returns it - and then the special logic that makes this a bit complex, which would be the central reason for making |thread-state| do it:</div>

<div><br></div><div>Catching the continuation can only be done while Gambit's threading system is running, and this means that catching the continuation can fail as the thread may end during |thread-state|'s course of execution. Thus, |thread-state| needs to have the logics to detect this, and start over from the beginning if it happens.</div>

<div><br></div><div>Perhaps with future Gambit versions this logic will change, that could be a good reason to extend |thread-state| with it instead of requiring the user to implement it.</div><div><br></div><div>Also perhaps it could be said that this continuation dumping mechanism is within the definition of what Gambit is supposed to do out of the box, so that it should be included by this reason.</div>

<div><br></div><div>Now that Gambit provides the critical logics, no hacks that bring unstability are needed anymore, so from my side I'm good with either way.</div><div><br></div><div><br></div><div><div>That |thread-state| reports what IO object it's waiting for and with what timeout is neat!</div>

<div><br></div><div><br></div><div>Anyhow so by this my question transformed to:</div><div>)</div><div><br></div><div><br></div><div><div>What about adding to the |thread-state-active| structure a slot |continuation| or |current-continuation|, and making |thread-state| capture the continuation or not depending on an optional argument (capture-continuation? #t) etc.?</div>

</div><div><br></div><div><br></div><div>Best regards,</div><div>Mikael</div><div><br></div><div class="gmail_quote">2013/7/31 Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca" target="_blank">feeley@iro.umontreal.ca</a>></span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">There is already a thread-state procedure with similar functionality.  Can you explain what is lacking in that procedure and extend it with the functionality you need?<br>


<br>
Marc<br>
<div><div class="h5"><br>
On 2013-07-12, at 6:00 AM, Mikael <<a href="mailto:mikael.rcv@gmail.com">mikael.rcv@gmail.com</a>> wrote:<br>
<br>
> Three fixes to code from previous post.<br>
><br>
> Sent a pull request for it for inclusion of this procedure, <a href="https://github.com/feeley/gambit/pull/38" target="_blank">https://github.com/feeley/gambit/pull/38</a> .<br>
><br>
> Test:<br>
><br>
> > (define t (make-thread (lambda () (thread-sleep! 10) 'over)))<br>
> > (##thread-status t)<br>
> (created)<br>
> > (begin (thread-start! t) (##thread-status t))<br>
> (starting)<br>
> > (##thread-status t)<br>
> (running)<br>
> > (##thread-status t #t)<br>
> (running #<continuation #2>)<br>
> > (##thread-status t)<br>
> (ended over)<br>
> > (define t (thread-start! (make-thread (lambda () (raise "error")))))<br>
> > (##thread-status t)<br>
> (ended/exception "error")<br>
><br>
> To it included in your Gambit binary until Marc recompiles the C files in the next Gambit release, you need to do:<br>
><br>
> ./configure<br>
> make<br>
> cd lib<br>
> ../gsc/gsc -:~~bin=../bin,~~lib=../lib,~~include=../include -c  _thread.scm<br>
> ../gsc/gsc -:~~bin=../bin,~~lib=../lib,~~include=../include -f -link -flat -o _gambc.c _kernel.c _system.c _num.c _std.c _eval.c _io.c _nonstd.c _thread.c _repl.c<br>
> cd ..<br>
> make<br>
><br>
> ; (##thread-status thread #!optional capture-continuation-if-running?)<br>
> ; => list ('created) = The thread has not yet been started<br>
> ;    list ('starting) = The thread has been started but execution has not yet begun<br>
> ;    list ('running [continuation-object]) = The thread is running<br>
> ;    list ('ended result) = The thread terminated ordinarily, with the return value result<br>
> ;    list ('ended/exception exception) = The thread terminated with the exception exception<br>
> (define (##thread-status thread #!optional capture-continuation-if-running?)<br>
><br>
>   (let loop ()<br>
><br>
>     ; Make snapshot of thread state<br>
>     ; Quantum manipulation just to ensure get a consistent snapshot<br>
>     (define quantum-pre (thread-quantum (current-thread)))<br>
>     (thread-quantum-set! (current-thread) +inf.0)<br>
>     (let* ((end-condvar (macro-thread-end-condvar thread))<br>
>            (exception?  (macro-thread-exception?  thread))<br>
>            (result      (macro-thread-result      thread)))<br>
>       (thread-quantum-set! (current-thread) quantum-pre)<br>
><br>
>       ; Generate return value<br>
>       (cond ((not end-condvar)<br>
>              ;; Thread has terminated<br>
>              (if exception?<br>
>                  ;; Thread has terminated with exception<br>
>                  (list 'ended/exception result)<br>
>                  ;; Thread has terminated with result<br>
>                  (list 'ended result)))<br>
>             (exception?<br>
>              ;; Thread has never run and is not terminated<br>
>              (if (not result)<br>
>                  ;; Thread is not yet started<br>
>                  (list 'created)<br>
>                  ;; Thread is started but has not yet run<br>
>                  (list 'starting)))<br>
>             (else<br>
>              (if capture-continuation-if-running?<br>
>                  ; continuation = #f here indicates that the thread state changed during the course<br>
>                  ;                of execution of this procedure, in such a way that the thread no<br>
>                  ;                longer has a continuation. Thisis the case if the thread ended.<br>
>                  (let ((continuation<br>
>                         (with-exception-catcher<br>
>                          (lambda (e) #f)<br>
>                          (lambda () (##thread-continuation-capture thread)))))<br>
>                    (if continuation<br>
>                        (list 'running continuation)<br>
>                        (loop)))<br>
>                  (list 'running)))))))<br>
><br>
><br>
> 2013/6/23 Mikael <<a href="mailto:mikael.rcv@gmail.com">mikael.rcv@gmail.com</a>><br>
> (dump-threads) uses ##thread-continuation-capture to get the current continuation of a target thread.<br>
><br>
> This presumes that the target thread has been started, if it has not ##thread-continuation-capture causes SIGSEGV.<br>
><br>
> Checking for if it has been started requires access to thread object fields that are not exported currently, and thus to do this right now you need to make a hack consisting of copying out the thread structure's definition into your code and doing the check there - this is not version safe.<br>


><br>
> I propose the introduction in Gambit of a procedure<br>
><br>
> (##thread-status thread)<br>
> => list ('created) = The thread has not yet been started<br>
>    list ('starting) = The thread has been started but execution has not yet begun<br>
>    list ('running) = The thread is running<br>
>    list ('ended result) = The thread terminated ordinarily, with the return value result<br>
>    list ('ended/exception exception) = The thread terminated with the exception exception<br>
><br>
> as the definition is quite short and its presence enables the programmer to without hacks reliably dump threads, which is like a core thing for debug.<br>
><br>
> Here follows a definition for inclusion into Gambit, based on an email Marc sent 2011-12-16, quoted below:<br>
><br>
> (define (##thread-status thread #!optional capture-continuation-if-running?)<br>
><br>
>   (let loop ()<br>
><br>
>     ; Make snapshot of thread state<br>
>     ; Quantum manipulation just to ensure get a consistent snapshot<br>
>     (define quantum-pre (thread-quantum (current-thread)))<br>
>     (thread-quantum-set! (current-thread) +inf.0)<br>
>     (let* ((end-condvar (macro-thread-end-condvar thread))<br>
>            (exception?  (macro-thread-exception?  thread))<br>
>            (result      (macro-thread-result      thread)))<br>
>       (thread-quantum-set! (current-thread) quantum-pre)<br>
><br>
>       ; Generate return value<br>
>       (cond ((not end-condvar)<br>
>              ;; Thread has terminated<br>
>              (if exception?<br>
>                  ;; Thread has terminated with exception<br>
>                  (list 'ended/exception result)<br>
>                  ;; Thread has terminated with result<br>
>                  (list 'ended result)))<br>
>             (exception?<br>
>              ;; Thread has never run and is not terminated<br>
>              (if (not result)<br>
>                  ;; Thread is not yet started<br>
>                  (list 'created)<br>
>                  ;; Thread is started but has not yet run<br>
>                  (list 'starting)))<br>
>             (else<br>
>              (if capture-continuation-if-running?<br>
>                  (let ((continuation<br>
>                         (with-exception-catcher<br>
>                          (lambda (e) (loop))<br>
>                          (lambda () (capture-continuation-if-running? thread)))))<br>
>                    (list 'running continuation)<br>
>                    (list 'running))))))))<br>
><br>
> Use would look something like:<br>
><br>
> > (define t (make-thread (lambda () (thread-sleep! 5))))<br>
> > (##thread-status t)<br>
> (created)<br>
> > (thread-start! t)<br>
> > (##thread-status t)<br>
> (running)<br>
> > (thread-sleep! 5)<br>
> > (##thread-status t)<br>
> (ended #!void)<br>
><br>
> With such a definition, a (dump-threads) could be defined as easy as:<br>
><br>
> (define (dump-threads #!key threads thread-group thread #!rest display-continuation-backtrace-args)<br>
>   (map (lambda (thread)<br>
>          (let loop ()<br>
>            (let ((status (##thread-status thread #t)))<br>
>              (if (eq? (car status) 'running)<br>
>                  (list 'running (call-with-output-string<br>
>                                  '()<br>
>                                  (lambda (port) (apply display-continuation-backtrace<br>
>                                                        `(,(cadr status) ; = continuation<br>
>                                                          ,port<br>
>                                                          .,display-continuation-backtrace-args)))))<br>
>                  status))))<br>
>        (or threads (thread-group->thread-list (or thread-group (thread-thread-group (or thread (current-thread))))))))<br>
><br>
> Please feel free to provide feedback.<br>
><br>
> Brgds, Mikael<br>
><br>
> 2011/12/16 Marc Feeley<br>
><br>
> On 2011-12-15, at 6:22 PM, Mikael wrote:<br>
><br>
> [..]<br>
> > Aha, that is good to be aware of - before running ##thread-continuation-capture on a thread, how can i check it's started (as to safeguard from this)?<br>
><br>
> The attached code checks for all possible states of a thread.  Please try to dump the state of your threads with it to see if the segmentation fault goes away.<br>
><br>
> (##include "~~lib/_gambit#.scm")<br>
><br>
> (define (dump-thread thread)<br>
><br>
>   (write thread)<br>
><br>
>   (let* ((end-condvar (macro-thread-end-condvar thread))<br>
>          (exception? (macro-thread-exception? thread))<br>
>          (result (macro-thread-result thread)))<br>
>     (cond ((not end-condvar)<br>
>            ;; thread has terminated<br>
>            (if exception?<br>
>                (begin<br>
>                  ;; thread has terminated with exception<br>
>                  (display " is terminated with exception:\n")<br>
>                  (display-exception result))<br>
>                (begin<br>
>                  ;; thread has terminated with result<br>
>                  (display " is terminated with result:\n")<br>
>                  (pretty-print result))))<br>
>           (exception?<br>
>            ;; thread has never run and is not terminated<br>
>            (if (not result)<br>
>                (begin<br>
>                  ;; thread is not yet started<br>
>                  (display " is not yet started\n"))<br>
>                (begin<br>
>                  ;; thread is started but has not yet run<br>
>                  (display " is started but has not yet run\n"))))<br>
>           (else<br>
>            ;; thread is started and has run<br>
>            (let ((c (##thread-continuation-capture thread)))<br>
>              (display " is running with continuation:\n")<br>
>              (display-continuation-backtrace c)))))<br>
><br>
>   (newline))<br>
><br>
> (define t2 (make-thread (lambda () 222)))<br>
> (define t3 (make-thread (lambda () 333)))<br>
> (define t4 (make-thread (lambda () (/ 1 0))))<br>
> (define t5 (make-thread (lambda () 555)))<br>
><br>
> (thread-start! t3)<br>
> (thread-start! t4)<br>
><br>
> (thread-sleep! 0.01)<br>
><br>
> (dump-thread (current-thread))<br>
> (dump-thread t2)<br>
> (dump-thread t3)<br>
> (dump-thread t4)<br>
> (thread-start! t5)<br>
> (dump-thread t5)<br>
><br>
> ;; % gsi dump-thread.scm<br>
> ;; #<thread #1 primordial> is running with continuation:<br>
> ;; 0  dump-thread             "dump-thread.scm"@32:21 (##thread-continuation...<br>
> ;; 1  (interaction)           "dump-thread.scm"@48:1  (dump-thread (current-...<br>
> ;; 2  ##load<br>
> ;; 3  ##load<br>
> ;; 4  ##main<br>
> ;;<br>
> ;; #<thread #2> is not yet started<br>
> ;;<br>
> ;; #<thread #3> is terminated with result:<br>
> ;; 333<br>
> ;;<br>
> ;; #<thread #4> is terminated with exception:<br>
> ;; Divide by zero<br>
> ;; (/ 1 0)<br>
> ;;<br>
> ;; #<thread #5> is started but has not yet run<br>
><br>
><br>
</div></div>> _______________________________________________<br>
> Gambit-list mailing list<br>
> <a href="mailto:Gambit-list@iro.umontreal.ca">Gambit-list@iro.umontreal.ca</a><br>
> <a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
<br>
</blockquote></div><br></div>