CodyChan CodyChan - 1 year ago 96
Linux Question

what does "static int function(...) __acquires(..) __releases(...){" mean?

I recently got a snippet of code in Linux kernel:

static int
fb_mmap(struct file *file, struct vm_area_struct * vma)

What confused me is the two __funtions following
static int fb_mmap()
right before

What are the purpose of the two __funtions?

Why in that position?

Why do they have the prefix

Are there other examples similar to this?

Answer Source

Not everything ending with a pair of parenthesis is a function (call). In this case they are parameterized macro expansions. The macros are defined as

#define __acquires(x)  __attribute__((context(x,0,1)))
#define __releases(x)  __attribute__((context(x,1,0)))

in file include/linux/compiler.h in the kernel build tree.

The purpose of those macros expanding into attribute definitions is to annotate the function symbols with information about which locking structures the function will acquire (i.e. lock) and release (i.e. unlock). The purpose of those in particular is debugging locking mechanisms (the Linux kernel contains some code that allows it to detect potential deadlock situations and report on this).

__attribute__ is a keyword specific to the GCC compiler, that allows to assign, well, attributes to a given symbol

Since macros are expanded at the text level, before the compiler is even looking at it, the result for your particular snippet, that the actual compilers sees would be

static int
fb_mmap(struct file *file, struct vm_area_struct * vma)

Those macros start with a double underscore __ to indicate, that they are part of the compiler environment. All identifiers starting with one or two underscores are reserved for the compiler environment implementation. In the case of the Linux kernel, because Linux is a operating system kernel that does not (because it simply is not availible) use the standard library, it's natural for it, do define it's own compiler environment definitions, private to it. Hence the two underscores to indicate, that this is compiler environment/implementation specific stuff.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download