内核驱动的一些技术处理函数

//***********************************************************
1./* 工作队列: */
step 1)/* 定义和初始化: */
static struct workqueue_struct *wq = NULL;
static struct work_struct work_delayed;

wq = create_workqueue("skeleton_dev_wq")
INIT_WORK(&work_delayed, work_queue_func, NULL);
static void work_queue_func(struct work_struct *work_wq)
{
}
step 2)/* 使用 */
queue_delayed_work(wq, &work_delayed, 50)

step 3)/* 注销 */
cancel_delayed_work(&work_delayed)
destroy_workqueue(wq)

2./* 定时器: */
step 1)/* 定义和初始化 */
struct timer_list tmr;
init_timer(&tmr);
tmr.function = TestTimerCallback;
static void TestTimerCallback(unsigned long data)
{
}

step 2)/* 使用: */
tmr.expires = jiffies + 100
add_timer(&tmr)

step 3)/* 注销 */
del_timer(&tmr)

3./* tasklet */
step 1)/* 定义和初始化 */
#include <linux/interrupt.h>
//声明一个名叫tasklet_test的tasklet,它的处理函数为tasklet_test_handle()
DECLARE_TASKLET(tasklet_test, tasklet_test_handle, 0);
static void tasklet_test_handle(unsigned long arg)
{
}
step 2)/* 使用 */
//调度这个tasklet: 调度一下执行一次
tasklet_schedule(&tasklet_test);
step 3)/* 注销 */
//禁用这个tasklet
tasklet_kill(&tasklet_test);

4./* wait queue */
step 1)/* 定义和初始化 */
#include <linux/workqueue.h>
static wait_queue_head_t wqChgEvent;
init_waitqueue_head(&wqChgEvent);
step 2)/* 使用 */
wait_event_interruptible_timeout() /* 等待 */
wake_up_interruptible(&wqChgEvent) /* 被唤醒 */
step 3)/* 注销 */

5./* thread */
step 1)/* 定义和初始化 */
static int test_kernel_thread(void *dummy)
{
}
step 2)/* 使用 */
kthread_run(test_kernel_thread, chgInfo, "kernel thread test")
step 3)/* 注销 */

//***********************************************************
1./* spinlock => 死等,不会引起进程间切换 */
step 1)/* 定义和初始化 */
static spinlock_t lockEvent;
step 2)/* 使用 */
spin_lock_irqsave(&lockEvent, flags)
// 临界资源: 如一个变量
spin_unlock_irqrestore(lockEvent, flags
step 3)/* 注销 */

2./* semaphore => 可能引起进程间切换 */
step 1)/* 定义和初始化 */
#include <linux/semaphore.h>
step 2)/* 使用 */
up()
// 临界资源
down()
step 3)/* 注销 */

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值