reason for code like this: "do { something } while (0)"
Hi,
I'm asking a pretty amateur question.
I've come accross code like this huge number of times in the kernel:
#define __set_task_state(tsk, state_value) \
do { (tsk)->state = (state_value); } while (0)
What is the reason to use do while loop if it is only meant to be
executed just once? Why not a simple:
#define __set_task_state(tsk, state_value) \
{ (tsk)->state = (state_value); }
TIA,
Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo [at] vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: reason for code like this: "do { something } while (0)"
> What is the reason to use do while loop if it is only meant to be
> executed just once? Why not a simple:
look at kernelnewbies faq/google/search the archives for this list.
anupam
--
In the beginning was the lambda, and the lambda was with Emacs, and
Emacs was the lambda.
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo [at] vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: reason for code like this: "do { something } while (0)"
On 15/05/06, Rajat Jain <rajat.noida.india [at] gmail.com> wrote:
> Hi,
>
> I'm asking a pretty amateur question.
>
> I've come accross code like this huge number of times in the kernel:
>
> #define __set_task_state(tsk, state_value) \
> do { (tsk)->state = (state_value); } while (0)
>
> What is the reason to use do while loop if it is only meant to be
> executed just once? Why not a simple:
>
> #define __set_task_state(tsk, state_value) \
> { (tsk)->state = (state_value); }
>
http://www.kernelnewbies.org/FAQ/DoWhile0
--
Jesper Juhl <jesper.juhl [at] gmail.com>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo [at] vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: reason for code like this: "do { something } while (0)"
------=_Part_10341_33082120.1147687678296
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
On 5/15/06, Rajat Jain <rajat.noida.india [at] gmail.com> wrote:
>
> Hi,
>
> I'm asking a pretty amateur question.
>
> I've come accross code like this huge number of times in the kernel:
>
> #define __set_task_state(tsk, state_value) \
> do { (tsk)->state =3D (state_value); } while (0)
>
> What is the reason to use do while loop if it is only meant to be
> executed just once? Why not a simple:
>
> #define __set_task_state(tsk, state_value) \
> { (tsk)->state =3D (state_value); }
>
> TIA,
>
> Rajat
>
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive: http://mail.nl.linux.org/kernelnewbies/
> FAQ: http://kernelnewbies.org/faq/
>
>
See this
http://kernelnewbies.org/FAQ/DoWhile0
Rgds,
Adil
------=_Part_10341_33082120.1147687678296
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
<br><br>
<div><span class=3D"gmail_quote">On 5/15/06, <b class=3D"gmail_sendername">=
Rajat Jain</b> <<a href=3D"mailto:rajat.noida.india [at] gmail.com">rajat.noi=
da.india [at] gmail.com</a>> wrote:</span>
<blockquote class=3D"gmail_quote" style=3D"PADDING-LEFT: 1ex; MARGIN: 0px 0=
px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi,<br><br>I'm asking a pretty a=
mateur question.<br><br>I've come accross code like this huge number of tim=
es in the kernel:
<br><br>#define __set_task_state(tsk, state_value) &=
nbsp; \<br>  =
; do { (tsk)->state =3D (state_value); } while (=
0)<br><br>What is the reason to use do while loop if it is only meant to be=
<br>executed just once? Why not a simple:
<br><br>#define __set_task_state(tsk, state_value) &=
nbsp; \<br>  =
; { (tsk)->state =3D (state_value); }<br><br>TIA=
,<br><br>Rajat<br><br>--<br>Kernelnewbies: Help each other learn about the =
Linux kernel.<br>Archive:
<a href=3D"http://mail.nl.linux.org/kernelnewbies/">http://mail.nl.linux.or=
g/kernelnewbies/</a><br>FAQ:  =
; <a href=3D"http://kernelnewbies.org/faq/">http://kernelnewbie=
s.org/faq/</a><br><br></blockquote></div>
<div><br>See this</div>
<div><a href=3D"http://kernelnewbies.org/FAQ/DoWhile0">http://kernelnewbies=
..org/FAQ/DoWhile0</a></div>
<div> </div>
<div>Rgds,</div>
<div>Adil</div>
------=_Part_10341_33082120.1147687678296--
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/kernelnewbies/
FAQ: http://kernelnewbies.org/faq/