Linux内核线程

内核线程创建

/**
 * kthread_run - create and wake a thread.
 * @threadfn: the function to run until signal_pending(current).线程函数
 * @data: data ptr for @threadfn.参数
 * @namefmt: printf-style name for the thread.创建的线程名称
 *
 * Description: Convenient wrapper for kthread_create() followed by
 * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
 * 返回一个struct task_struct对象指针,用于操作线程
 */
#define kthread_run(threadfn, data, namefmt, ...)                          \
({                                                                         \
        struct task_struct *__k                                            \
                = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
        if (!IS_ERR(__k))                                                  \
                wake_up_process(__k);                                      \
        __k;                                                               \
})

内核线程实现

static struct task_struct *uc1628_kthread = NULL;
/**
 * @brief 内核线程
 * 
 * @param data 
 * @return int 
 */
static int kthread_uc1628_func(void *data)
{
	struct fb_info *info = (struct fb_info *)data;
	while (!kthread_should_stop())
	{
		uc1628fb_update_display(info->par);
		msleep(300);
	}
	return 0;
}
static int kthread_uc1628_init(struct fb_info *info)
{
	uc1628_kthread = kthread_run(kthread_uc1628_func, (void *)info, "kthread-uc1628");
	if (!uc1628_kthread)
	{
		printk("kthread_run fail\n");
		return -1;
	}

	return 0;
}

内核线程退出

/**
 * @brief kthread_stop()发送should_stop标志给内核线程。
 * kthread_stop()参数是创建内核线程时返回的struct task_struct指针
 * 返回值是内核线程的返回值。
 * kthread_stop()会阻塞等待,直到内核线程k退出为止
 * 
 * @param struct task_struct
 * @return int 
 */
int kthread_stop(struct task_struct *k);

/**
 * @brief kthread_stop()发送should_stop标志后的返回值为1,否则返回值为0
 * 内核线程根据kthread_should_stop()的返回值来决定是否退出内核线程,以保证在调用kthread_stop()前,内核线程没有退出
 * 
 * @param void
 * @return bool
 */
bool kthread_should_stop(void);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值