简体   繁体   English

什么“静态int函数(...)__ acquires(...)__releases(...){”是什么意思?

[英]what does “static int function(…) __acquires(..) __releases(…){” mean?

I recently got a snippet of code in Linux kernel: 我最近在Linux内核中得到了一段代码:

static int
fb_mmap(struct file *file, struct vm_area_struct * vma)
__acquires(&info->lock)
__releases(&info->lock)
{
...
}

What confused me is the two __funtions following static int fb_mmap() right before "{" , 令我困惑的是在"{"之前的static int fb_mmap()之后的两个__funtions,

a). What are the purpose of the two __funtions? 这两个__funtions的目的是什么?

b). Why in that position? 为什么在那个位置?

c). Why do they have the prefix "__" ? 为什么他们有前缀"__"

d). Are there other examples similar to this? 还有其他类似的例子吗?

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. 在内核构建树中的文件include/linux/compiler.h中。

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 (ie lock) and release (ie 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). 这些的目的尤其是调试锁定机制(Linux内核包含一些代码,允许它检测潜在的死锁情况并报告此情况)。

https://en.wikipedia.org/wiki/Sparse https://en.wikipedia.org/wiki/Sparse

__attribute__ is a keyword specific to the GCC compiler, that allows to assign, well, attributes to a given symbol http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function-Attributes __attribute__是特定于GCC编译器的关键字,允许将属性分配给给定符号http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function-Attributes

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)
__attribute__((context(&info->lock,0,1)))
__attribute__((context(&info->lock,1,0)))
{
…
}

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. 对于Linux内核,因为Linux是一个操作系统内核(因为它根本不可用)使用标准库,所以很自然地定义它自己的编译器环境定义,它是私​​有的。 Hence the two underscores to indicate, that this is compiler environment/implementation specific stuff. 因此,两个下划线表明,这是编译器环境/实现特定的东西。

They're probably macros defined with #define . 它们可能是使用#define定义的宏。 You should look for the definition of such macros and see what they expand to. 您应该查找此类宏的定义并查看它们扩展到的内容。 They might expand to some pragma giving hints to the compiler; 它们可能扩展到一些pragma器,给编译器提示 ; they might expand to nothing giving hints to the developers or some analysis tool. 他们可能会扩展到没有给开发人员或某些分析工具提示 The meaning might vary 意思可能会有所不同

The __attribute__ these macros evaluate to are compiler-specific features. 这些宏评估的__attribute__是特定于编译器的功能。 man gcc explains some of the uses. man gcc解释了一些用途。

The prefix __ typically is used to avoid name clashes; 前缀__通常用于避免名称冲突; double underscore as prefix and postfix mark an identifier as being used by the compiler itself. 双下划线作为前缀,后缀标记编译器本身使用的标识符。

More on gcc attributes can be found here . 有关gcc属性的更多信息,请点击此处

More on the kernel use of these can be found here . 关于内核的更多内容可以在这里找到。

Those are macro's defined as 这些是宏的定义为

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

in Linux/include/linux/compiler.h 在Linux / include / linux / compiler.h中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM