2018年2月13日 星期二

Linux kernel - tasklet範例

#include <linux/init.h>
#include <linux/module.h>
//#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/delay.h>

MODULE_LICENSE("Dual BSD/GPL");

static void f(unsigned long name);

static DECLARE_TASKLET(t1, f, (unsigned long)"t1");
static DECLARE_TASKLET_DISABLED(t2, f, (unsigned long)"t2");

static struct tasklet_struct *t3;

static void f(unsigned long name)
{
 printk(KERN_INFO "%s, on cpu %d\n", (char *)name, smp_processor_id());
}

static void f3(unsigned long name)
{
 static u32 c = 0;

 tasklet_schedule(t3);
 
 //msleep(1000);
 //mdelay(1000);
 if (!(c++ % 2000000))
  printk(KERN_INFO "%s, on cpu %d\n", (char *)name, smp_processor_id());
}

static int hello_init(void)
{

 printk(KERN_INFO "Hello kernel\n");
 t3 = kzalloc(sizeof (struct tasklet_struct), GFP_KERNEL);
 tasklet_init(t3, f3, (unsigned long)"t3");

 tasklet_schedule(&t1);
 tasklet_schedule(&t2);
 tasklet_schedule(t3);

 tasklet_enable(&t2);

 return 0;
}

static void hello_exit(void)
{
 tasklet_kill(&t1);
 tasklet_kill(&t2);
 tasklet_kill(t3);
 printk(KERN_INFO "Goodbye kernel\n");
}

module_init(hello_init);
module_exit(hello_exit);

沒有留言:

張貼留言