开源力量学习笔记
自旋锁(spinlock)简介
自旋锁是内核的一种同步机制,在同一时刻只能被最多一个内核任务持有,所以一个时刻只有一个线程允许存在于临界区中。
内核可以通过自旋锁占有某些资源,直到使用完后再释放锁。如果该资源已经被其他的任务占有了,那内核会不断等待,直到获得该资源。
这点可以应用在多处理机器、或运行在单处理器上的抢占式内核中需要的锁定服务。
自旋锁(spinlock)的基本形式API
初始化:
在编译时定义:DEFINE _SPINLOCK();
在运行时初始化:spin_lock_init();
持有锁和释放锁:
spin_lock();
//临界区
spin_unlock();
自旋锁代码编程示例:
运行结果:
阅读(648) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
自旋锁(spinlock)简介
自旋锁是内核的一种同步机制,在同一时刻只能被最多一个内核任务持有,所以一个时刻只有一个线程允许存在于临界区中。
内核可以通过自旋锁占有某些资源,直到使用完后再释放锁。如果该资源已经被其他的任务占有了,那内核会不断等待,直到获得该资源。
这点可以应用在多处理机器、或运行在单处理器上的抢占式内核中需要的锁定服务。
自旋锁(spinlock)的基本形式API
初始化:
在编译时定义:DEFINE _SPINLOCK();
在运行时初始化:spin_lock_init();
持有锁和释放锁:
spin_lock();
//临界区
spin_unlock();
自旋锁代码编程示例:
- #include <linux/module.h>
- #include <linux/kthread.h>
- #include <linux/delay.h>
-
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Chen");
- MODULE_DESCRIPTION("The module is only used for test.");
-
- //static DEFINE_SPINLOCK(threads_lock);
- static spinlock_t threads_lock;
-
- static void threads_lock_init(void)
- {
- spin_lock_init(&threads_lock);
- }
-
- struct our_data{
- int count1;
- int count2;
- };
-
- static struct our_data my_data;
-
- static void show_my_data(void)
- {
- printk("count1 %d,count2 %d\n", my_data.count1, my_data.count2);
- }
-
- #define MAX_KTHREAD 10
-
- static struct task_struct *threads[MAX_KTHREAD];
-
- static int thread_do(void *data)
- {
- printk("run ...\n");
- while(!kthread_should_stop()){
- spin_lock(&threads_lock);
- my_data.count1++;
- my_data.count2 += 10;
- spin_unlock(&threads_lock);
-
- msleep(10);
- }
- return 0;
- }
-
- static int create_threads(void)
- {
- int i;
- for(i=0 ; i < MAX_KTHREAD; i++)
- {
- struct task_struct *thread;
- thread = kthread_run(thread_do, NULL, "thread-%d", i);
- if(IS_ERR(thread))
- return -1;
-
- threads[i] = thread;
- }
- return 0;
- }
-
- static void cleanup_threads(void)
- {
- int i;
-
- for (i = 0; i < MAX_KTHREAD; i++)
- if(threads[i])
- kthread_stop(threads[i]);
- }
-
- static __init int minit(void)
- {
- printk("call %s.\n",__FUNCTION__);
- threads_lock_init();
- if (create_threads())
- goto err;
- return 0;
-
- err:
- cleanup_threads();
- return -1;
- }
-
- static __exit void mfini(void)
- {
- printk("call %s.\n",__FUNCTION__);
- cleanup_threads();
- show_my_data();
- }
-
- module_init(minit);
- module_exit(mfini);
- chen@ubuntu:~/mygit/module/exam1$ dmesg
- [ 3475.623885] call minit.
- [ 3475.625348] run ...
- [ 3475.625363] run ...
- [ 3475.625375] run ...
- [ 3475.625386] run ...
- [ 3475.625397] run ...
- [ 3475.625546] run ...
- [ 3475.625562] run ...
- [ 3475.625574] run ...
- [ 3475.625594] run ...
- [ 3475.627184] run ...
- [ 3480.215762] call mfini.
- [ 3480.353601] count1 2899,count2 28990
相关热门文章
给主人留下些什么吧!~~
评论热议