您的位置:首页 > 运维架构 > Linux

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

2014-05-12 09:14 519 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: