【发布时间】:2018-04-15 06:38:44
【问题描述】:
在浏览Linux内核源码时,我发现hlist_bl_for_each_entry_rcu宏。下面是它的定义
for (pos = hlist_bl_first_rcu(head); \
pos && \
({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1; }); \
pos = rcu_dereference_raw(pos->next))
此宏用于__d_lookup() 获取dentry。我不明白的是这条线
({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1; });
它获得了 tpos。 1 在这里有什么用?在for循环中如何理解这个条件?
【问题讨论】:
-
该行使用GCC extension known as a statement expression。将该行视为一个函数,其返回值始终为
1。 -
@user3386109- 这意味着 for 循环中的条件是“pos && 1”,而与 tpos 的值无关?
-
是的。由于short-circuit evaluation,
tpos只会在pos不为 NULL 时更新。当pos不为NULL 时,循环体将始终 执行。循环条件不检查tpos是否为 NULL。要么保证tpos在pos不为NULL 时始终有效,要么循环体需要处理tpos为NULL 的情况。
标签: c linux-kernel kernel