linux内核锁机制实例代码-不可睡眠锁之自旋锁之二

来源:互联网 发布:三星s7edge主题软件 编辑:程序博客网 时间:2024/05/29 19:08

spinlock.h

DEFINE_SPINLOCK

在kernel里面找到一个使用他的例子


获取锁

static inline void spin_lock(spinlock_t *lock)
{
raw_spin_lock(&lock->rlock);
}


static DEFINE_SPINLOCK(threads_lock);
struct our_data{
  int count1;
  int count2;
};


static struct our_data my_data;




#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;
}

===============

如果我们不是编译时候初始化spinlock,而是运行时初始化他。

static spinlock_t threads_lock;


static void threads_lock_init(void)
{
    //spin_lock_init
  spin_lock_init(&threads_lock);
}

static __init int minit(void)
{
// printk("testpar= %#x.\n",testpar);
printk("call %s.\n",__FUNCTION__);
// other_function();
threads_lock_init();
if(create_threads())
{
 cleanup_threads();
 return -1;
}
return 0;
}

0 0
原创粉丝点击