unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: Po Lu <luangruo@yahoo.com>
Cc: Ruijie Yu <ruijie@netyu.xyz>, emacs-devel@gnu.org
Subject: Re: Catch arbitrary signal and throw it later in C
Date: Thu, 13 Apr 2023 15:18:19 -0700	[thread overview]
Message-ID: <DBE232B8-8681-4793-A51C-5CD8C7E710BB@gmail.com> (raw)
In-Reply-To: <87edookx7s.fsf@yahoo.com>



> On Apr 13, 2023, at 1:22 AM, Po Lu <luangruo@yahoo.com> wrote:
> 
> Yuan Fu <casouri@gmail.com> writes:
> 
>> unwind-protect (and its internal C counterpart) pushes the cleanup
>> form onto specpdl. Supposingly that form is later eval’ed? I want to
>> run some C code which obviously it doesn’t support. I need to
>> understand how this whole unwinding thing work before I can maybe
>> modify or write something that does what I want. Hopefully some one
>> can enlighten me :-)
> 
> Typically, you do this:
> 
>  specpdl_ref count;
>  struct localcontext context;
> 
>  count = SPECPDL_INDEX ();
>  record_unwind_protect_ptr (cleanup_func, &context);
> 
>  /* Code which may signal.  */
> 
>  unbind_to (count, Qnil);
> 
> where cleanup_func is:
> 
>  void
>  cleanup_func (void *arg)
> 
> Once a signal happens between the `record_unwind_protect_ptr', and
> `unbind_to', `cleanup_func' will be called with `&context' as an
> argument.  This happens before any longjmp, so it is safe to use
> references to variables with automatic storage duration within unwind
> protects you record.
> 
> Note that `record_unwind_protect_ptr' itself may also signal if the
> specpdl is full, calling the unwind function in the process.
> 
> If your cleanup function needs arguments that aren't pointers, you can
> also use `record_unwind_protect_int', `record_unwind_protect', or
> `record_unwind_protect_void'.

Thanks, this is very clear and helpful. So the signal routine will unwind the pdl and run my cleanup function, right? And if no signal happens, my cleanup function is removed from the pdl by unbind_to.

> 
> Saving a signal, only to throw it again later, is not something we do in
> Emacs.  I suggest you make your cleanup code work as described above.

Right. What you’ve shown is actually exactly what I need. As long as the cleanup runs and the signal is thrown, I’m good.

Yuan


  reply	other threads:[~2023-04-13 22:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-13  7:24 Catch arbitrary signal and throw it later in C Yuan Fu
2023-04-13  7:33 ` Ruijie Yu via Emacs development discussions.
2023-04-13  8:12   ` Yuan Fu
2023-04-13  8:20     ` Eli Zaretskii
2023-04-13  8:22     ` Po Lu
2023-04-13 22:18       ` Yuan Fu [this message]
2023-04-13 23:48         ` Po Lu
2023-04-13  8:24     ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DBE232B8-8681-4793-A51C-5CD8C7E710BB@gmail.com \
    --to=casouri@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=luangruo@yahoo.com \
    --cc=ruijie@netyu.xyz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).