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.