Linux spin Lock

Source: Internet
Author: User

#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/delay.h>
static int i=0,j=100;
struct Task_struct *mythread1=null;
struct Task_struct *mythread2=null;
static int myVar = 0;
static int count = 0;
spinlock_t lock;
static void Setmyvar (int input)
{
Spin_lock (&lock);
if (count)
{
PRINTK ("Busy setmyvar\n");
}
count++;
MyVar = input;
PRINTK ("Setmyvar is%d\n", MyVar);
Spin_unlock (&lock);
count--;
}
static int Getmyvar (void)
{
int res = 0;
Spin_lock (&lock);
if (count)
{
PRINTK ("Busy setmyvar\n");
}
count++;
res = MyVar;
PRINTK ("Getmyvar is%d\n", res);
Spin_unlock (&lock);
count--;
return 0;
}
static int print1 (void *data)
{
while (!kthread_should_stop ())
{
PRINTK ("This is thread1......\n");
Getmyvar ();
Setmyvar (i);
Ssleep (1);
i++;
}
return 0;
}
static int print2 (void *data)
{
while (!kthread_should_stop ())
{
PRINTK ("This is thread2......\n");
Getmyvar ();
Setmyvar (j);
Ssleep (1);
j + +;
}
return 0;
}

static int __init hello_init (void) {
Spin_lock_init (&lock);
MyThread1 = Kthread_run (Print1,null, "mythread1");
MyThread2 = Kthread_run (Print2,null, "mythread2");
return 0;
}
static void __exit
Hello_exit (void) {
if (MYTHREAD1)
{
PRINTK ("Kthread1 stop....\n");
Kthread_stop (MYTHREAD1);
Mythread1=null;
}
if (MyThread2)
{
PRINTK ("Kthread2 stop....\n");
Kthread_stop (MYTHREAD2);
Mythread2=null;
}

}
Module_init (Hello_init);
Module_exit (Hello_exit);
Module_license ("GPL");
Module_author ("Valerie Henson [email protected]");
Module_description ("\" Hello, world!\ "minimal module");
Module_version ("PRINTK");

Linux spin Lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.