0

I found this arcane construct inside include/linux/wait.h

 #define DEFINE_WAIT_FUNC(name, function)                                \
        wait_queue_t name = {                                            \
                 .private        = current,                              \
                 .func           = function,                             \
                 .task_list      = LIST_HEAD_INIT((name).task_list),     \
        }

I know good amount on macros and preproc directives in general, but I am absolutely lost on this one. Can someone please explain the above code structure in detail including the '\' at the end of the line. Thanks.

Note: I dont need to know what it does in linux, only the syntactic meaning behind it.

artless noise
  • 21,212
  • 6
  • 68
  • 105
Ace
  • 1,501
  • 4
  • 30
  • 49

3 Answers3

2

The \ character in a macros is a line continuation character. It simply allows the macro to span multiple lines.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Thanks for the answer, appreciate it. What about the rest. Its very imp for me to understand the wait_queue_t name = { .private = current,.....} part inside the body of #define? – Ace Jan 31 '14 at 04:12
  • 2
    @AnupSaumithri That doesn't have to do with macros, but is C99 syntax. [What does dot (.) mean in a struct initializer?](https://stackoverflow.com/questions/8047261/what-does-dot-mean-in-a-struct-initializer) –  Jan 31 '14 at 04:21
1

The macro (presumably) is asociating an structure with a function pointer and doing common initialization. Lest say you want to add those structures to a list and then (during an execution step) call different functions. A better question would at least include wait_queue_t definition.

marc
  • 56
  • 4
1

As per others (and many references on-line), the \ character continues any line via the c-preprocessor. As for the rest,

#define DEFINE_WAIT_FUNC(name, function) \

Definition of the macro.

   wait_queue_t name = {                                            \

Declares a wait_queue_t with the macro substitution name.

            .private        = current,                              \

Initialize the private wait_queue_t structure member with the current task pointer. This is also a macro (perhaps inline assembler) defined by each architecture in the Linux tree.

            .func           = function,                             \

Set the func member to the function parameter.

            .task_list      = LIST_HEAD_INIT((name).task_list),     \

Initializes the list as empty. task_list points to itself.

The . notation is used through-out the kernel source and is a gcc feature (and later C99), called designated intializers. Instead of having to set all members of a structure, only the named ones are initialized with the others set to zero. This allows people to extend the structure without modifying all the declarations. It is not a c-preprocessor feature, but a 'C' language (extension).

Community
  • 1
  • 1
artless noise
  • 21,212
  • 6
  • 68
  • 105