Site Tools


do-while-0

Table of Contents

do { ... } while (0)

In C programming language,do { ... } while (0) is a loop which does a single iteration. A compiler will optimize it away completely, so the syntax produces no actual machine code.

Most commonly, it's used to make function-macros.

Example:

/*
 * - clearing psr.i is implicitly serialized (visible by next insn)
 * - setting psr.i requires data serialization
 * - we need a stop-bit before reading PSR because we sometimes
 *   write a floating-point register right before reading the PSR
 *   and that writes to PSR.mfl
 */
#define __local_irq_save(x)         \
do {                    \
    ia64_stop();                \
    (x) = ia64_getreg(_IA64_REG_PSR);   \
    ia64_stop();                \
    ia64_rsm(IA64_PSR_I);           \
} while (0)
do-while-0.txt ยท Last modified: (external edit)