1

In the linux kernel(v6.0) source code /kernel/sched/sched.h, I found a macro:

#define SCHED_WARN_ON(x)      ({ (void)(x), 0; })

My question is that how to understand the expression ({ (void)(x), 0; }). In my knowledge, the return value of that expression is 0. The (void)(x) part seems neglectable here, as the return value of { (void)(x), 0; } always depends on , 0; } part, which returns 0.

Any help would be appreciated!

nakeydoln
  • 43
  • 5
  • `(void)` is used to suppress warnings about not using the return value of `x` – Barmar Oct 19 '22 at 16:34
  • I got you, but what confuses me is more about the whole part of ```({ (void)(x), 0; })```, as I think the return value of the code block is always ```0```. – nakeydoln Oct 19 '22 at 16:55
  • @nakeydoln yes, it is always `0`. What's the problem with that? – Marco Bonelli Oct 19 '22 at 16:55
  • @Marco Bonelli I just wonder then what is the ```x``` for? As I first see this macro function there is some boolean operation in it like ```SCHED_WARN_ON( [boolean operation] ).``` – nakeydoln Oct 19 '22 at 17:02
  • 2
    It's not the only definition of the macro. It's a version for when a flag (`CONFIG_SCHED_DEBUG`) requests no diagnostics. The version that warns, actually does something with `x`. This version simply suppresses all warnings. – StoryTeller - Unslander Monica Oct 19 '22 at 17:12
  • The code in `(x)` will be executed for its side effects. E.g. `SCHED_WARN_ON(printf("some message"))` – Barmar Oct 19 '22 at 20:07

0 Answers0