Linux SRCU Notifier机制学习

#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/notifier.h>

struct srcu_notifier_head srcu_event;

static int notifier_call_test_event(struct notifier_block *nb,
        unsigned long event, void *data)
{
  printk(KERN_ALERT "notifier_call_test_event start\n");

  printk(KERN_ALERT "%s\n", (char *)data);
  return 0;
}

struct notifier_block nb = {
  .notifier_call = notifier_call_test_event,
};

static int srcu_notifier_head_init(void)
{
  srcu_init_notifier_head(&srcu_event);
  srcu_notifier_chain_register(&srcu_event, &nb);
  printk(KERN_ALERT "srcu_notifier_head_init start\n");
  return 0;
}

static void srcu_notifier_head_exit(void)
{
  printk(KERN_ALERT "GoodBye srcu_notifier_head Test\n");
}


module_init(srcu_notifier_head_init);
module_exit(srcu_notifier_head_exit);
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/notifier.h>

extern struct srcu_notifier_head srcu_event;

static int srcu_notifier_device_init(void)
{
  srcu_notifier_call_chain(&srcu_event, 1, (void *)"hanhao");

  printk(KERN_ALERT "srcu_notifier_device_init start\n");
  return 0;
}

static void srcu_notifier_device_exit(void)
{
  printk(KERN_ALERT "GoodBye wake up source Test\n");
}


late_initcall(srcu_notifier_device_init);
module_exit(srcu_notifier_device_exit);

运行结果如下:

\par \cf0\highlight0 [    4.439727][    T1] srcu_notifier_head_init start\cf1\highlight2 
\par \cf0\highlight0 [    5.020833][    T1] notifier_call_test_event start\cf1\highlight2 
\par \cf0\highlight0 [    5.020841][    T1] hanhao\cf1\highlight2 
\par \cf0\highlight0 [    5.020846][    T1] srcu_notifier_device_init start\cf1\highlight2 

srcu_notifier_chain_register函数用于向系统注册一个SRCU notifier链。它的原型如下: ```c int srcu_notifier_chain_register(struct srcu_notifier_head *nh, struct notifier_block *n) ``` 其中,nh是一个指向srcu_notifier_head结构体的指针,表示需要注册的SRCU notifier链;n是一个指向notifier_block结构体的指针,表示需要注册的notifier。 以下是一个使用srcu_notifier_chain_register函数的例子: ```c #include <linux/kernel.h> #include <linux/module.h> #include <linux/notifier.h> #include <linux/srcu.h> static struct srcu_notifier_head my_chain; static int my_notifier_func(struct notifier_block *nb, unsigned long val, void *data) { printk(KERN_INFO "my_notifier_func called with val=%lu\n", val); return NOTIFY_OK; } static struct notifier_block my_notifier = { .notifier_call = my_notifier_func, }; static int __init my_module_init(void) { int ret; ret = srcu_notifier_chain_register(&my_chain, &my_notifier); if (ret) { printk(KERN_ERR "Failed to register srcu notifier chain: %d\n", ret); return ret; } return 0; } static void __exit my_module_exit(void) { srcu_notifier_chain_unregister(&my_chain, &my_notifier); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Example SRCU notifier chain module"); ``` 在这个例子中,我们首先定义了一个srcu_notifier_head结构体my_chain和一个notifier_block结构体my_notifier。然后,在模块初始化函数my_module_init中,我们调用了srcu_notifier_chain_register函数将my_notifier注册到my_chain中。在模块退出函数my_module_exit中,我们调用了srcu_notifier_chain_unregister函数将my_notifier从my_chain中注销。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新柯兰永久

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值