Tasking

Work Queue

Allow kernel code to request that a function be called at some future time.

Basic usage is shown below:

  1. Establish work queue:

    1. struct workqueue_struct is needed which is defined in <linux/workqueue.h>.

    2. There are two methods which can create the work queue:

      1. struct workqueue_struct *create_workqueue(const char *name);
        This function would return a workqueue that has a dedicated thread for each processor on the system.

      2. struct workqueue_struct *create_singlethread_workqueue(const char *name);
        This function would return a workqueue that has a single worker thread.

    3. After linux 2.6.63, there's a new mechanism called "Concurrency-managed workqueues", and there're two new function for creating the work queue:

      1. alloc_workqueue

      2. alloc_ordered_workqueue

        /**
         * alloc_ordered_workqueue - allocate an ordered workqueue
         * @name: name of the workqueue
         * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
         *
         * Allocate an ordered workqueue.  An ordered workqueue executes at
         * most one work item at any given time in the queued order.  They are
         * implemented as unbound workqueues with @max_active of one.
         *
         * RETURNS:
         * Pointer to the allocated workqueue on success, %NULL on failure.
         */
        
  2. Submit a task to a workqueue

    1. At compile time

      DECLARE_WORK(name, void (*function)(void *), void *data);
      
    2. At runtime

      1. Prepre the work_struct
        INIT_WORK(struct work_struct *work, void (*function)(void *), void *data);
        PREPARE_WORK(struct work_struct *work, void (*function)(void *), void *data);
        
      2. Submitting work to a workqueue
        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);
        

reference: http://www.makelinux.net/ldd3/chp-7-sect-6

results matching ""

    No results matching ""