原子操作之间是否存在
happens before
关系,例如原子也需要内存顺序?代码:
static int zero = 0;
static int proc_enter(struct proc_context* ctx, struct lkf_node* node)
{
__atomic_store_n(&node->next, NULL, __ATOMIC_RELAXED); // 0
__atomic_store_n(&ctx->list.tail, &node->next, __ATOMIC_RELEASE); // 1
int n = (int) __atomic_compare_exchange_n(&ctx->stat, &zero, 1, false, __ATOMIC_RELEASE, // 2
__ATOMIC_RELAXED);
return n - 1;
}
static int proc_leave(struct proc_context* ctx)
{
__atomic_store_n(&ctx->stat, 0, __ATOMIC_RELAXED); // 3
__atomic_thread_fence(__ATOMIC_ACQ_REL); // 4
if (__atomic_load_n(&ctx->list.tail, __ATOMIC_RELAXED) == &ctx->list.root.next) { // 5
return 0;
}
int n = (int) __atomic_compare_exchange_n(&ctx->stat, &zero, 1, false, __ATOMIC_RELAXED,
__ATOMIC_RELAXED);
return n - 1;
}
几个问题:
// 0
发生在// 1
之前?// 1
在 // 2
之前发生?// 5
发生在// 3
// 5
发生在// 3
之前的唯一方法吗(如果有效)?最后一个问题,我的逻辑是:
我需要的是:
step 3
禁止 step 4
和 step 2
禁止 step 1
下降