kthread_run

头文件
include/linux/kthread.h

创建并启动

/**
 * 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).
 */
#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;                                       \
})

创建

/**
* kthread_create - create a kthread.
* @threadfn: the function to run until signal_pending(current).
* @data: data ptr for @threadfn.
* @namefmt: printf-style name for the thread.
*
* Description: This helper function creates and names a kernel
* thread. The thread will be stopped: use wake_up_process() to start
* it. See also kthread_run(), kthread_create_on_cpu().
*
* When woken, the thread will run @threadfn() with @data as its
* argument. @threadfn can either call do_exit() directly if it is a
* standalone thread for which noone will call kthread_stop(), or
* return when 'kthread_should_stop()' is true (which means
* kthread_stop() has been called). The return value should be zero
* or a negative error number; it will be passed to kthread_stop().
*
* Returns a task_struct or ERR_PTR(-ENOMEM).
*/
struct task_struct *kthread_create(int (*threadfn)(void *data),
       void *data,
       const char namefmt[],
       ...)
{
    struct kthread_create_info create;
    DECLARE_WORK(work, keventd_create_kthread, &create);

    create.threadfn = threadfn;
    create.data = data;
    init_completion(&create.started);
    init_completion(&create.done);

    /*
    * The workqueue needs to start up first:
    */
    if (!helper_wq)
       work.func(work.data);
    else {
       queue_work(helper_wq, &work);
       wait_for_completion(&create.done);
    }
    if (!IS_ERR(create.result)) {
       va_list args;
       va_start(args, namefmt);
       vsnprintf(create.result->comm, sizeof(create.result->comm),
         namefmt, args);
       va_end(args);
    }

    return create.result;
}
EXPORT_SYMBOL(kthread_create);

结束

int kthread_stop(struct task_struct *k);

检测

int kthread_should_stop(void);

返回should_stop标志。它用于创建的线程检查结束标志,并决定是否退出

举例

static int hello_init(void)  
{  
    printk(KERN_INFO "Hello, world!\n");  

    tsk = kthread_run(thread_function, NULL, "mythread%d", 1);  
    if (IS_ERR(tsk)) {  
        printk(KERN_INFO "create kthread failed!\n");  
    }  
    else {  
        printk(KERN_INFO "create ktrhead ok!\n");  
    }  
    return 0;  
}  

static void hello_exit(void)  
{  
    printk(KERN_INFO "Hello, exit!\n");  
    if (!IS_ERR(tsk)){  
        int ret = kthread_stop(tsk);  
        printk(KERN_INFO "thread function has run %ds\n", ret);  
    }  
}  

module_init(hello_init);  
module_exit(hello_exit);  
posted @ 2016-03-13 11:16  thomas_blog  阅读(674)  评论(0编辑  收藏  举报