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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值