Analysis and use of INIT_DELAYED_WORK ()

  • Application scenarios It
    is very dangerous to handle too many operations in an interrupt, which has a great impact on the timely response of the interrupt. In Linux, we often use INIT_DELAYED_WORK to handle the operations that the interrupt needs to respond to.
  • Function function
    Work queue (work queue) is a mechanism for deferring the execution of operations in the Linux kernel. INIT_DELAYED_WORK () is a macro
  • Function location
    \ kernel \ include \ linux \ workqueue.h
  • Function analysis
    • Function prototype
#define INIT_DELAYED_WORK(_work, _func)	\
 __INIT_DELAYED_WORK(_work, _func, 0)

#define __INIT_DELAYED_WORK(_work, _func, _tflags)	\
 do {	\
  INIT_WORK(&(_work)->work, (_func));	\
  __setup_timer(&(_work)->timer, delayed_work_timer_fn,	\
         (unsigned long)(_work),	\
         (_tflags) | TIMER_IRQSAFE);	\
 } while (0)

#ifdef CONFIG_LOCKDEP
#define __INIT_WORK(_work, _func, _onstack)	\
 do {	\
  static struct lock_class_key __key;	\
         \
  __init_work((_work), _onstack);	\
  (_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
  lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0); \
  INIT_LIST_HEAD(&(_work)->entry);	\
  (_work)->func = (_func);	\
 } while (0)
#else
#define __INIT_WORK(_work, _func, _onstack)	\
 do {	\
  __init_work((_work), _onstack);	\
  (_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
  INIT_LIST_HEAD(&(_work)->entry);	\
  (_work)->func = (_func);	\
 } while (0)
#endif
    • Function analysis
  • demo

#define READ_VDELAY 3000    //30S
struct xxx_device{
    struct delayed_work xxx_delay_work;
};

static void read_work({struct work_struct *work)
{
    
    struct xxx_device *bq = container_of(work,
            struct xxx_device, read_work.work);

schedule_delayed_work(&bq->read_work,
    READ_VDELAY );
}

probe
{
    struct xxx_device *bq;
    ...
    INIT_DELAYED_WORK(&bq->xxx_delay_work, read_work);
    ...
}
  • Reference link
    http://blog.chinaunix.net/uid-28639221-id-5065472.html
    https://blog.csdn.net/lcqlw123/article/details/46892147
Published 162 original articles · praised 183 · 120,000 views

Guess you like

Origin blog.csdn.net/qq_31339221/article/details/105057978