在这里插入代码片
#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);
}
rcu示例
于 2024-02-28 14:02:07 首次发布