Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/kthread.h>
- #define CPU_COUNT 8
- atomic_t count;
- static struct task_struct *thread_init(int (*fn)(void *data),
- void *data, int cpu)
- {
- struct task_struct *ts;
- ts = kthread_create(fn, data, "per_cpu_thread");
- kthread_bind(ts, cpu);
- if (!IS_ERR(ts)) {
- wake_up_process(ts);
- } else {
- printk(KERN_ERR "Failed to bind thread to CPU %d\n", cpu);
- }
- return ts;
- }
- static void thread_sync(void)
- {
- atomic_inc(&count);
- while (atomic_read(&count) != CPU_COUNT);
- }
- static void do_common_stuff(void) {
- int cpu = smp_processor_id();
- unsigned long flags = 0;
- printk(KERN_INFO "Syncing %d...\n", cpu);
- local_irq_save(flags);
- thread_sync();
- printk(KERN_INFO "Syncing done %d.\n", cpu);
- local_irq_restore(flags);
- }
- static int per_cpu_thread_fn(void *data) {
- do_common_stuff();
- return 0;
- }
- static int main_thread_fn(void *data) {
- int i;
- atomic_set(&count, 0);
- for (i = 1; i < CPU_COUNT; ++i) {
- printk("Create thread on %d\n", i);
- thread_init(per_cpu_thread_fn, NULL, i);
- }
- do_common_stuff();
- return 0;
- }
- static int __init sync_init(void)
- {
- thread_init(main_thread_fn, NULL, 0);
- return 0;
- }
- static void __exit sync_exit(void)
- {
- }
- module_init(sync_init);
- module_exit(sync_exit);
- MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement