kthread_run总结

1、

kthread_run是一个宏定义,功能是创建并启动内核线程
struct task_struct *ion_comm_kthread;
wait_queue_head_t ion_comm_wq;
atomic_t ion_comm_event = ATOMIC_INIT(0);
atomic_t ion_comm_cache_event = ATOMIC_INIT(0);
#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;                                                     \
})

 

使用例子:

int ion_comm_init(void)
{
	struct sched_param param = { .sched_priority = 0 };

	init_waitqueue_head(&ion_comm_wq);
	ion_comm_kthread = kthread_run(ion_comm_cache_pool, NULL, "%s",
				       "ion_comm_pool");
	if (IS_ERR(ion_comm_kthread)) {
		IONMSG("%s: creating thread for ion history\n", __func__);
		return PTR_RET(ion_comm_kthread);
	}

	sched_setscheduler(ion_comm_kthread, SCHED_IDLE, &param);
	wake_up_process(ion_comm_kthread);

	return 0;
}
static int ion_comm_cache_pool(void *data)
{
	int req_cache_size = 0;
	int cached_size = 0;
	int cache_buffer = 0;
	unsigned int gfp_flags = __GFP_HIGHMEM | __GFP_MOVABLE;
	struct ion_buffer *buffer = NULL;
	struct ion_heap *ion_cam_heap;

	ion_cam_heap = ion_drv_get_heap(g_ion_device,
					ION_HEAP_TYPE_MULTIMEDIA_FOR_CAMERA,
					1);
	while (1) {
		if (kthread_should_stop()) {=====》使用模式都是这样,要终止kthread_run()创建的线程,可以调用kthread_stop()。都是使用kthread_stop必现保证线程函数没有退出,所以线程使用while循环等待,这样线程就不会退出,直到其他线程执行kthread_stop
			IONMSG("stop ion history threak\n");
			break;
		}
		wait_event_interruptible(ion_comm_wq,
					 atomic_read(&ion_comm_event));
		req_cache_size = atomic_read(&ion_comm_event);
		cache_buffer = atomic_read(&ion_comm_cache_event);
		atomic_set(&ion_comm_event, 0);

		cached_size = ion_mm_heap_pool_size(ion_cam_heap,
						    gfp_flags,
						    cache_buffer);
		buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
		if ((!buffer) || (cached_size >= req_cache_size)) {
			kfree(buffer);
			buffer = NULL;
			atomic_set(&ion_comm_event, 0);
			IONMSG("%s is ready: req %d, cached %d\n", __func__,
			       req_cache_size, cached_size);
			continue;
		}

		IONMSG("%s alloc start, req %d, cached %d\n", __func__,
		       req_cache_size, cached_size);
		buffer->heap = ion_cam_heap;
		buffer->flags = 3;
		if (cache_buffer == 0)
			buffer->flags = 0;
		buffer->size = req_cache_size;
		ion_mm_heap_cache_allocate(ion_cam_heap,
					   buffer,
					   buffer->size,
					   0,
					   0);
		IONMSG("%s push buffer to cam_pool\n", __func__);
		ion_mm_heap_cache_free(buffer);
		kfree(buffer);
		buffer = NULL;

		IONMSG("%s alloc done\n", __func__);
	}

	return 0;
}

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值