0

Inside include/linux/sched.h one of the important task state changing functions is:

#define __set_task_state(tsk, state_value)      \
    do { (tsk)->state = (state_value); } while (0)
#define set_task_state(tsk, state_value)        \
    set_mb((tsk)->state, (state_value))

As you can see there is a do while() structure inside #define __set_task_state(...).

Now my question is, is it just like any other C do-while loop code - or does it have special properties as it is defined inside #define macro?

Thanks

Griwes
  • 8,805
  • 2
  • 43
  • 70
Ace
  • 1,501
  • 4
  • 30
  • 49
  • It is just like any other do-while loop. – Kerrek SB Jan 24 '14 at 22:32
  • Note that it's condition is while(0), so it's not really a loop since its body will only ever execute once. It's used merely to ensure that the code in the body will nest properly within other code. – ooga Jan 24 '14 at 22:34
  • See http://stackoverflow.com/questions/19116171/dowhile0-macro-safety (and many other stackoverflow questions about this) – Filipe Gonçalves Jan 24 '14 at 22:35
  • @ooga Thats interesting I would expect just having (tsk)->state = (state_value); to nest not worse than the do-while() structure above. To me the 'nestability' does not seem too obvious. – Ace Jan 24 '14 at 22:37
  • @Anup, I agree. In this case it should not be necessary. But that's what the construct is usually used for. Perhaps there was originally another line of code and the do/while was just left in. BTW, the compiler will not generate any code for it, so there's no loss of efficiency. Also, I'm not sure why simple braces aren't used. – ooga Jan 24 '14 at 22:39
  • Only one word: **scope!** – ichramm Jan 24 '14 at 22:52
  • @ichramm Scope? What do you mean...Can you please explain a little further... – Ace Jan 24 '14 at 23:01
  • the other answer explains far better then me but I use them not only to force the usage of semicolon but also because it allows me to create local variables without collision. – ichramm Jan 25 '14 at 02:44

0 Answers0