Re: the proper way to cancel delayed work?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 19 May 2007, pradeep singh wrote:

> On 5/19/07, Robert P. J. Day <rpjday@xxxxxxxxxxxxxx> wrote:
> >
> >   as a short demo of using a delayed work queue, i wrote the following
> > module which, every second, just does a printk() to /var/log/messages,
> > and stops when you unload it.
> >
> > ==========================================================================
> > #include <linux/module.h>       // for all modules
> > #include <linux/init.h>         // for entry/exit macros
> > #include <linux/kernel.h>       // for printk macros
> > #include <linux/workqueue.h>
> >
> >
> > static void work_func(struct work_struct *unused);
> > static DECLARE_DELAYED_WORK(work, work_func);
> >
> > static int count ;
> >
> > static void
> > work_func(struct work_struct *unused)
> > {
> >         printk(KERN_INFO "yee ha timer, %d.\n", ++count);
> >         schedule_delayed_work(&work, HZ);
> > }
> >
> > static int hi(void)
> > {
> >         printk(KERN_INFO "Hello from timer.\n");
> >         schedule_delayed_work(&work, HZ);
> >         count = 0 ;
> >         return 0;
> > }
> >
> > static void bye(void)
> > {
> >         printk(KERN_INFO "Goodbye from timer.\n");
> >         cancel_delayed_work(&work);
> >         flush_scheduled_work();
> > }
> >
> > module_init(hi);
> > module_exit(bye);
> >
> > MODULE_LICENSE("Dual BSD/GPL");
> > =======================================================================
> >
> >   the module works just fine, but i'm not sure if i'm closing down the
> > work queue properly upon module unloading.  note that i'm doing both:
> >
> >         cancel_delayed_work(&work);
> >         flush_scheduled_work();
> >
> > is it necessary to do both?  and i've seen code that is even more
> > careful to do:
> >
> >         if (!cancel_delayed_work(&work))
> >                 flush_scheduled_work();
> >
> > just in case the work callback function is still running on return
> > from cancel_delayed_work().

> Robert, i ran into this situation days back and i remember it worked
> fine for me. But at the same time if you observe closely, value
> returned by cancel_delayed_work(&work) is not checked at a lot of
> places in the kernel, why i have no idea? Perhaps because it always
> results in current work handler to complete(if it is in between).
> Saying that i guess why would you need flush_scheduled_work() then?
> Maybe you want to do a flush_workqueue() instead?

i really wish there was some up-to-date coverage of stuff like this.
this really is a case where your only option is to RTFS to figure out
what's really going on.

rday
-- 
========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA

http://fsdev.net/wiki/index.php?title=Main_Page
========================================================================

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux