rcu示例

在这里插入代码片

#include <linux/rcupdate.h>
#include <linux/slab.h>
#include "test_state.h"

struct test_state {
   int test_status;
    struct rcu_head rcu;
};

static struct test_state *test_sta __rcu;

int test_state_read(void)
{
	int ret = test_DEAD;
	struct test_state *read = NULL;

	rcu_read_lock();
	read = rcu_dereference(test_sta);
	if (read)
		ret = read->test_status;
	rcu_read_unlock();

	return ret;
}

static void test_state_rcu_del(struct rcu_head *rh)
{
	struct test_state *p = container_of(rh, struct test_state, rcu);
    kfree(p);
}

void test_state_write(test_status_s change)
{
	struct test_state *old;
	struct test_state *new_ptr;
	int test_status_new;

	test_status_new = change;
	new_ptr = kmalloc(sizeof(struct test_state), GFP_KERNEL);
	old = test_sta;
	*new_ptr = *old;
	new_ptr->test_status = test_status_new;
	rcu_assign_pointer(test_sta, new_ptr);
	call_rcu(&old->rcu, test_state_rcu_del);
}

int test_state_init(void)
{
	test_sta = kmalloc(sizeof(struct test_state), GFP_KERNEL);
	if (!test_sta)
        return -ENOMEM;
	test_sta->test_status = 0;
	return 0;
}

void test_state_exit(void)
{
	synchronize_rcu();
	if (test_sta)
		kfree(test_sta);
}

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值