the proper way to cancel delayed work?

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

 



  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().

  what's the proper way to close this down?  thanks.

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