分类: LINUX
2006-06-01 23:49:51
#include #include #include #include #include typedef struct _simple_timeout{ struct timer_list timer; int (*function) (void*); void *args; unsigned long time; } simple_timeout; void simple_timeout_add(simple_timeout *timeout, unsigned long time, int (*function)(void*), void *args); int simple_timeout_del(simple_timeout *timeout); |
#include #include #include #include #include #include "simple_timeout.h" MODULE_LICENSE("GPL"); static void simple_timeout_helper(unsigned long arg) { simple_timeout *st = (simple_timeout*)arg; if(st->function(st->args) == 0){ st->timer.expires = jiffies + st->time; add_timer(&st->timer); }else{ del_timer_sync(&st->timer); } } void simple_timeout_add(simple_timeout *timeout, unsigned long time, int (*function)(void*), void *args) { timeout->time = time; timeout->function = function; timeout->args = args; init_timer(&timeout->timer); timeout->timer.function = simple_timeout_helper; timeout->timer.data = (unsigned long)timeout; timeout->timer.expires = jiffies + time; add_timer(&timeout->timer); } int simple_timeout_del(simple_timeout *timeout) { return del_timer_sync(&timeout->timer); } EXPORT_SYMBOL_GPL(simple_timeout_add); EXPORT_SYMBOL_GPL(simple_timeout_del); |
#include #include #include #include #include #include "simple_timeout.h" MODULE_LICENSE("GPL"); simple_timeout timeout; unsigned long arg = 0; int sample(void *args) { unsigned long *arg = (unsigned long*)args; *arg = *arg + 1; printk("timeout sample:%lu\n", *arg); if(*arg == 10){ return -1; }else{ return 0; } } static int __init sample_init(void) { simple_timeout_add(&timeout, 1 * HZ, &sample, &arg); return 0; } static void __exit sample_exit(void) { if(simple_timeout_del(&timeout)) printk("Active timer deactivated.\n"); else printk("No timer active.\n"); } module_init(sample_init); module_exit(sample_exit); |