Re: [PATCH v4 14/18] static_call: Add static_cond_call()

Peter Zijlstra
2020-05-05T09:36:50Z

HJ, Nick,

Any chance any of you can see a way to make your respective compilers
not emit utter junk for this?

On Mon, May 04, 2020 at 10:14:45PM +0200, Peter Zijlstra wrote:

> https://godbolt.org/z/SDRG2q
>
> ---
> #include <stddef.h>
>
>
> #define READ_ONCE(var) (*((volatile typeof(var) *)&(var)))
> #define WRITE_ONCE(var, val) (*((volatile typeof(var) *)&(var)) = (val))
>
> struct static_call_key {
> void *func;
> };
>
> #define DECLARE_STATIC_CALL(name, func) \
> extern struct static_call_key name; \
> extern typeof(func) __SCT__##name;
>
> #define DEFINE_STATIC_COND_CALL(name, _func) \
> DECLARE_STATIC_CALL(name, _func) \
> struct static_call_key name = { \
> .func = NULL, \
> }
>
> static void __static_call_nop(void)
> {
> }
>
> #define __static_cond_call(name) \
> ({ \
> void *func = READ_ONCE(name.func); \
> if (!func) \
> func = &__static_call_nop; \
> (typeof(__SCT__##name)*)func; \
> })
>
> #define static_cond_call(name) (void)__static_cond_call(name)
>
> static void inline static_call_update(struct static_call_key *call, void *func)
> {
> WRITE_ONCE(call->func, func);
> }
>
> volatile int _x;
>
> void bar(int x)
> {
> _x = x;
> }
>
> DEFINE_STATIC_COND_CALL(foo, bar);
>
> void ponies(int x)
> {
> static_cond_call(foo)(x);
> }