Subject: Re: bug#23483: 24.5; cygwin emacs w32 doesn not ask to save files when windows shuts down

From: Eli Zaretskii <[email-masked]>
Date: Tue, 10 May 2016 19:32:26 +0300

> Cc: [email-masked]
> From: Ken Brown <[email-masked]>
> Date: Tue, 10 May 2016 11:16:44 -0400
> 
> > If someone submits patches that listen to system shutdown messages,
> > and auto-save files when they arrive, this can be fixed.  Patches
> > welcome.
> 
> I asked about this on the Cygwin list (https://www.cygwin.com/ml/cygwin/2016-05/msg00085.html).  Based on the response I got, I tried the following, which seems to work:
> 
> diff --git a/src/w32fns.c b/src/w32fns.c
> index ede8f6b..705b978 100644
> --- a/src/w32fns.c
> +++ b/src/w32fns.c
> @@ -4795,6 +4795,9 @@ w32_wnd_proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
>        my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
>        return 0;
> 
> +    case WM_ENDSESSION:
> +      terminate_due_to_signal (SIGTERM, 0);
> +

Thanks.

However, w32_wnd_proc runs in a separate thread, so I don't think you
can safely call terminate_due_to_signal from there, because the latter
will call Lisp.  IOW, the above can crash in "interesting" ways.

What I think we need to do instead is send a message to the main
thread that will produce a special event, and then bind that event to
kill-emacs (probably in special-event-map).