Find the answer to your Linux question:
Results 1 to 4 of 4
Hi all. Throughout the kernel I keep on finding code like this: Code: include/linux/preempt.h 46 #define preempt_enable() 47 do { 48 preempt_enable_no_resched(); 49 preempt_check_resched(); 50 } while (0) why is ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    13

    I don't understand why the kernel contains code like this.

    Hi all.

    Throughout the kernel I keep on finding code like this:

    Code:
    include/linux/preempt.h
    46 #define preempt_enable() 
    47 do { 
    48  preempt_enable_no_resched(); 
    49  preempt_check_resched(); 
    50 } while (0)
    why is this within a do-while loop, even though its expression is evaluated to false. This code will only execute once.

    Really weird. Could it redundant code? i.e the programmers couldn't be bothered to remove the do while? or could it be for testing etc.

    Thanks.

  2. #2
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,300
    Have a look at this pdf. Page 4 seems to be relavent.
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

  3. #3
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    121
    Quote Originally Posted by phil128 View Post
    why is this within a do-while loop, even though its expression is evaluated to false. This code will only execute once.
    This is a fairly common way to define a macro, since it means the macro avoids several pitfalls of macros. Since all the macro code is within a block, it can go anywhere a single statement can, and since it requires a semicolon after it, it does not confuse some language constructs. It's basically a way for a macro to safely masquerade as a function call.

    See this helpful page for details.

  4. #4
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,300
    Excellent link. Thanks.
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...