all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: semantics of thread-signal (WAS: Crashing the new thread code)
@ 2016-12-12  0:16 Noam Postavsky
  2016-12-12  1:21 ` semantics of thread-signal Juliusz Chroboczek
  0 siblings, 1 reply; 6+ messages in thread
From: Noam Postavsky @ 2016-12-12  0:16 UTC (permalink / raw
  To: Juliusz Chroboczek; +Cc: Emacs developers

On Sun, Dec 11, 2016 at 5:56 PM, Juliusz Chroboczek <jch@irif.fr> wrote:
>> I would expect the thread to receive the signal as soon as it starts
>> running again.
>
> I'm not sure what the semantics of signal-thread is supposed to be.

I'm not sure either, my expectations are shaped by experience with
non-Emacs thread code.

>  The manual says:
>
>     ‘thread-signal’ will cause a thread to exit a call to ‘mutex-lock’,
>     ‘condition-wait’, or ‘thread-join’.
>
> I assumed this to mean that the condition will only be delivered when
> one of these functions is called, but your comment seems to imply that
> it's meant to deliver the condition as soon as possible.

My interpretation is that mutex-lock (and the others) block the thread
until something happens (e.g., another thread calls mutex-unlock), and
thread-signal is another thing which can end the blocking (and also
trigger a signal when the thread next runs).

>
> Which makes sense, but gives a whole new flavour to using unwind-protect
> now that conditions can be signalled asynchronously.
>
> (Aside: I'm actually not quite sure in that case that unwind-protect can
> be used safely at all.  What happens if a condition is signalled during
> the cleanup?  Say:
>
>     (let ((foo nil))
>       (unwind-protect
>            (progn
>              (setq foo (make-foo))
>              (do-stuff-with foo))
>         (when foo (destroy-foo foo))))
>
> if a condition is signalled just before the cleanup but after exiting
> the body, will we leak a foo?  End of aside.)

This can't happen, because thread are cooperative. i.e., only one
thread runs at a time.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: semantics of thread-signal
  2016-12-12  0:16 semantics of thread-signal (WAS: Crashing the new thread code) Noam Postavsky
@ 2016-12-12  1:21 ` Juliusz Chroboczek
  2016-12-12  2:06   ` Noam Postavsky
  2016-12-12  3:33   ` Eli Zaretskii
  0 siblings, 2 replies; 6+ messages in thread
From: Juliusz Chroboczek @ 2016-12-12  1:21 UTC (permalink / raw
  To: emacs-devel

>> I assumed this to mean that the condition will only be delivered when
>> one of these functions is called, but your comment seems to imply that
>> it's meant to deliver the condition as soon as possible.

> My interpretation is that mutex-lock (and the others) block the thread
> until something happens (e.g., another thread calls mutex-unlock), and
> thread-signal is another thing which can end the blocking (and also
> trigger a signal when the thread next runs).

Reasonable enough.  Perhaps somebody could clarify the docs?

>> if a condition is signalled just before the cleanup but after exiting
>> the body, will we leak a foo?

> This can't happen, because thread are cooperative.

The manual says:

    However, the Emacs thread support has been designed in a way to
    later allow more fine-grained concurrency, and correct programs
    should not rely on cooperative threading.

So if thread-signal can be delivered asynchronously, this will cause
trouble when Emacs moves to kernel threads.

(And this does happen.  Viz. SBCL, CCL, ACL, all of which switched from
userspace threading to kernel threads sometime in the last 20 years or
so.)

-- Juliusz




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: semantics of thread-signal
  2016-12-12  1:21 ` semantics of thread-signal Juliusz Chroboczek
@ 2016-12-12  2:06   ` Noam Postavsky
  2016-12-12 16:08     ` Eli Zaretskii
  2016-12-12  3:33   ` Eli Zaretskii
  1 sibling, 1 reply; 6+ messages in thread
From: Noam Postavsky @ 2016-12-12  2:06 UTC (permalink / raw
  To: Juliusz Chroboczek; +Cc: Emacs developers

On Sun, Dec 11, 2016 at 8:21 PM, Juliusz Chroboczek <jch@irif.fr> wrote:
>>> I assumed this to mean that the condition will only be delivered when
>>> one of these functions is called, but your comment seems to imply that
>>> it's meant to deliver the condition as soon as possible.
>
>> My interpretation is that mutex-lock (and the others) block the thread
>> until something happens (e.g., another thread calls mutex-unlock), and
>> thread-signal is another thing which can end the blocking (and also
>> trigger a signal when the thread next runs).
>
> Reasonable enough.  Perhaps somebody could clarify the docs?
>

Something like this?

--- i/doc/lispref/threads.texi
+++ w/doc/lispref/threads.texi
@@ -82,9 +82,11 @@ Basic Thread Functions
 @defun thread-signal thread error-symbol data
 Like @code{signal} (@pxref{Signaling Errors}), but the signal is
 delivered in the thread @var{thread}.  If @var{thread} is the current
-thread, then this just calls @code{signal} immediately.
-@code{thread-signal} will cause a thread to exit a call to
-@code{mutex-lock}, @code{condition-wait}, or @code{thread-join}.
+thread, then this just calls @code{signal} immediately.  Otherwise,
+@var{thread} will receive the signal as soon as it becomes current.
+If @var{thread} was blocked by a call to @code{mutex-lock},
+@code{condition-wait}, or @code{thread-join}; @code{thread-signa} will
+unblock it.
 @end defun


>
> The manual says:
>
>     However, the Emacs thread support has been designed in a way to
>     later allow more fine-grained concurrency, and correct programs
>     should not rely on cooperative threading.
>
> So if thread-signal can be delivered asynchronously, this will cause
> trouble when Emacs moves to kernel threads.

Hmm, if we really want to be safe for preemptive threads, we would
probably need to add some way to block asynch signals, and
unwind-protect would use that during the unwind forms. It would also
be needed between calling make-foo and setting the value of foo.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: semantics of thread-signal
  2016-12-12  1:21 ` semantics of thread-signal Juliusz Chroboczek
  2016-12-12  2:06   ` Noam Postavsky
@ 2016-12-12  3:33   ` Eli Zaretskii
  1 sibling, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2016-12-12  3:33 UTC (permalink / raw
  To: Juliusz Chroboczek; +Cc: emacs-devel

> From: Juliusz Chroboczek <jch@irif.fr>
> Date: Mon, 12 Dec 2016 02:21:38 +0100
> 
>     However, the Emacs thread support has been designed in a way to
>     later allow more fine-grained concurrency, and correct programs
>     should not rely on cooperative threading.
> 
> So if thread-signal can be delivered asynchronously, this will cause
> trouble when Emacs moves to kernel threads.
> 
> (And this does happen.  Viz. SBCL, CCL, ACL, all of which switched from
> userspace threading to kernel threads sometime in the last 20 years or
> so.)

The current implementation already uses kernel threads, it just makes
sure only one of them runs Lisp at any given time.  All the rest are
stuck either in system calls or trying to acquire a global lock.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: semantics of thread-signal
  2016-12-12  2:06   ` Noam Postavsky
@ 2016-12-12 16:08     ` Eli Zaretskii
  2016-12-13  2:46       ` Noam Postavsky
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2016-12-12 16:08 UTC (permalink / raw
  To: Noam Postavsky; +Cc: jch, emacs-devel

> From: Noam Postavsky <npostavs@users.sourceforge.net>
> Date: Sun, 11 Dec 2016 21:06:38 -0500
> Cc: Emacs developers <emacs-devel@gnu.org>
> 
> --- i/doc/lispref/threads.texi
> +++ w/doc/lispref/threads.texi
> @@ -82,9 +82,11 @@ Basic Thread Functions
>  @defun thread-signal thread error-symbol data
>  Like @code{signal} (@pxref{Signaling Errors}), but the signal is
>  delivered in the thread @var{thread}.  If @var{thread} is the current
> -thread, then this just calls @code{signal} immediately.
> -@code{thread-signal} will cause a thread to exit a call to
> -@code{mutex-lock}, @code{condition-wait}, or @code{thread-join}.
> +thread, then this just calls @code{signal} immediately.  Otherwise,
> +@var{thread} will receive the signal as soon as it becomes current.
> +If @var{thread} was blocked by a call to @code{mutex-lock},
> +@code{condition-wait}, or @code{thread-join}; @code{thread-signa} will
> +unblock it.                                         ^^^^^^^^^^^^
>  @end defun

Thanks, please push after fixing the typo.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: semantics of thread-signal
  2016-12-12 16:08     ` Eli Zaretskii
@ 2016-12-13  2:46       ` Noam Postavsky
  0 siblings, 0 replies; 6+ messages in thread
From: Noam Postavsky @ 2016-12-13  2:46 UTC (permalink / raw
  To: Eli Zaretskii; +Cc: Juliusz Chroboczek, Emacs developers

On Mon, Dec 12, 2016 at 11:08 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>
> Thanks, please push after fixing the typo.

Done, pushed as c78f872



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-12-13  2:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-12  0:16 semantics of thread-signal (WAS: Crashing the new thread code) Noam Postavsky
2016-12-12  1:21 ` semantics of thread-signal Juliusz Chroboczek
2016-12-12  2:06   ` Noam Postavsky
2016-12-12 16:08     ` Eli Zaretskii
2016-12-13  2:46       ` Noam Postavsky
2016-12-12  3:33   ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.