git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Use macro HANDLER_WRAPPER in sigchain to wrap clean function of sigchain

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 13, 2012, 18:11 UTC
Message-ID
<7vmx7kweus.fsf@alter.siamese.dyndns.org>
In-Reply-To
<CAMocUqRsEvwnoV32Cr05dJeUj7iNDj1cLP5kAzgyMNEo1O0kCw@mail.gmail.com>
徐迪 <xudifsd@gmail.com> writes:
Show 8 quoted lines
> The sigchain APIs require user to code like:
>
> void clean_foo_on_signal(int sig)
> {
>  clean_foo();
>  sigchain_pop(sig);
>  raise(sig);
> }

I can see the repetition, but I do not think your macro is a very good way to reduce it. Can't we fix the sigchain API a bit, perhaps first by making a bit more state available to the sigchain stack?

	typedef void (*sigchain_fn)(int, void *);
        int sigchain_push(int sig, sigchain_fn fn, void *cb_data);
        int sigchain_pop(int sig);
        void sigchain_push_common(sigchain_fn fn, void *cb_data);

And then make the repetitive one into a single copy of a helper function, while giving a new external interface to register a on_signal handler:

        static void relay_fn(int sig, void *cb_data)
        {
                void (*fn)(void) = cb_data; /* [*1*] */
        	fn();
                sigchain_pop(sig);
                raise(sig);
        }
        void sigchain_add_relayed(void (*fn)(void))
        {
		sigchain_push_common(relay_fn, fn);
        }
The code that wants to register a clean-up action would do:
	sigchain_add_relayed(clean_foo);
instead of
	sigchain_push_common(clean_foo_on_signal);
with repeated definition of clean_xxx_on_signal.
Hmm?
[Footnote]

*1* Yes, I know, this casts between a data pointer and a function pointer that is not portable, but the purpose of this pseudo-code is primarily to illustrate the high level view of the idea. We would probably want to be able to pass callback value to the clean_foo() function and at that point, we would likely to be passing a pointer to a struct, and we could declare the first element of such a struct is a pointer to a sigchain_fn, or something.

Previous: 徐迪Next: Jeff King
Message 2 of 5 in “Use macro HANDLER_WRAPPER in sigchain to wrap clean function of sigchain”
  1. Use macro HANDLER_WRAPPER in sigchain to wrap clean function of sigchain徐迪, Mar 13, 2012
  2. Junio C HamanoMar 13, 2012
  3. Jeff KingMar 13, 2012
  4. Junio C HamanoMar 13, 2012
  5. 徐迪Mar 15, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.