Kernel Work Queue Workqueue

Source: Internet
Author: User

LDD3: The work queue is, on the surface, similar to taskets; They allow kernel code to request that a function be called at a later time. However, there are several notable differences between these 2, including:

(1) The result of Tasklet running in the context of software interrupts is that all Tasklet code must be atomic. Instead, the work queue function runs in a special kernel process context; As a result, they have more flexibility. In particular, the work queue function is able to sleep.

(2) Tasklet are often run on the processor on which they were originally submitted. Work queues work in the same way, by default.

(3) The kernel code can request that the work queue function be delayed by an explicit interval.

The key difference between the two is that the Tasklet executes quickly, in short periods, and in an atomic state, while the work queue function may have high cycles but does not need to be atomic. Each mechanism has a situation where it fits.

Use

The Task Force column has a struct workqueue_struct type, defined in <linux/workqueue.h>.

Create: struct workqueue_struct *create_workqueue (const char *name); struct workqueue_struct *create_singlethread_ Workqueue (const char *name);

If you use Create_workqueue, you get a task queue that has a dedicated thread on each processor in the system. In many cases, all of these threads are simply over-behaving; If a single worker thread is sufficient, use Create_singlethread_workqueue.

Statement:

Declare_work (name, void (*function) (void *), void *data);

Run-time initialization:

static struct workqueue_struct * WORKQUEUE;


There is also a init_delayed_work:

#define Init_delayed_work (_work, _func) \

do{\

Init_work (& (_work)->work, (_func)); \

Init_timer (& (_work)->timer); \

}while (0)

Join queue:

int queue_work (struct workqueue_struct *queue, struct work_struct *work);
int queue_delayed_work (struct workqueue_struct *queue, struct work_struct *work, unsigned long delay);

If Queue_delay_work is used, however, the actual work is not carried out until at least the delay jiffies has passed. The return value from these functions is 0 if the work is successfully added to the queue; A non-0 result means that the WORK_STRUCT structure is already waiting in the queue and is not joined for the 2nd time.

If you need to cancel a pending task in the list, you can call:

int cancel_delayed_work (struct work_struct*work);

The return value is non-0 if the entry is canceled before it starts executing. The kernel guarantees that execution of a given portal will not be initialized after the call to Cancel_delay_work. If Cancel_delay_work returns 0, however, the portal may already be running on a different processor and may still be running after calling Cancel_delayed_work. To be absolutely sure that the work function is not running anywhere after Cancel_delayed_work returns 0, you must follow this call to invoke:

void Flush_workqueue (Structworkqueue_struct *queue);

After Flush_workqueue returns, no function that was committed before the call runs anywhere in the system.

When you run out of a work queue, you can remove it and use:

void Destroy_workqueue (Structworkqueue_struct *queue);

Example:

The global definition of the static struct workqueue_struct *rohm_workqueue; in the initialization function: Rohm_workqueue = Create_singlethread_workqueue ("Rohm_  Workqueue "); The queue is not yet working if (!rohm_workqueue) {return (-ENOMEM);}  Destroy function: Destroy_workqueue (rohm_workqueue); start: Init_work (&ps_als->work, Ps_als_work_func);        Initialization has not yet worked to set a timer:/* Timer process */Hrtimer_init (&ps_als->timer, Clock_monotonic, Hrtimer_mode_rel); Ps_als->timer.function = Ps_als_timer_func; Notice there is no start timer, this statement has a code below: Init_delayed_work (&ps_als->input_ work, Ps_als_work_func); The Ps_als_work_func will start the timer.    Timer handler function: Static enum Hrtimer_restart ps_als_timer_func (struct Hrtimer *timer) {ps_als_data *ps_als;    int result;    Ps_als = container_of (timer, ps_als_data, timer);    result = Queue_work (Rohm_workqueue, &ps_als->work);        if (result = = 0) {PRINTK (kern_err "can ' t register que.\n");    PRINTK (kern_err "result = 0x%x\n", result); } return (Hrtimer_norestart);} static void Ps_als_work_func (sTruct work_struct *work) {/* The setting value from Application */Get_timer = ps_als->delay_time;        /* 125MS (8HZ) at least */wait_sec = (get_timer/sm_time_unit);        Wait_nsec = ((Get_timer-(WAIT_SEC * sm_time_unit)) * mn_time_unit);        result = Hrtimer_start (&ps_als->timer, Ktime_set (Wait_sec, wait_nsec), Hrtimer_mode_rel);            if (Result! = 0) {PRINTK (kern_err "can ' t start timer\n");        Return }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Kernel Work Queue Workqueue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.