免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2648 | 回复: 4
打印 上一主题 下一主题

请教较新版本内核中mutex的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-01 13:01 |只看该作者 |倒序浏览
在较新版本中,如2.6.27中,内核有专门的metux子系统,而不是之前使用的binary semaphore.
在文档中,有这样的描述:
'struct mutex' semantics are well-defined and are enforced if
   CONFIG_DEBUG_MUTEXES is turned on. Semaphores on the other hand have
   virtually no debugging code or instrumentation. The mutex subsystem
   checks and enforces the following rules:

   * - only one task can hold the mutex at a time
   * - only the owner can unlock the mutex
   * - multiple unlocks are not permitted
   * - recursive locking is not permitted
   * - a mutex object must be initialized via the API
   * - a mutex object must not be initialized via memset or copying
   * - task may not exit with mutex held
   * - memory areas where held locks reside must not be freed
   * - held mutexes must not be reinitialized
   * - mutexes may not be used in hardware or software interrupt
   *   contexts such as tasklets and timers

为了测试这些metux的feature,我把内核的CONFIG_DEBUG_MUTEXES 打开并打开了CONFIG_LOCKDEP_SUPPORT.但是并没有得到描述中的结果。其中,关于metex是否能被不同进程lock/unlock的问题,我有以下两个内核线程:
struct mutex mutex;

static int mysthread1(void * data){
  int i;
  i= 5;  
  while(i -- > 0){
                mutex_lock(&mutex);
    printk("this is thread1\n");
    msleep(5000);
  }
        return 0;
}

static int mysthread2(void * data){
  int i;
  i= 5;  
  while(i -- > 0){
    printk("this is thread2\n");
    msleep(5000);
                mutex_lock(&mutex);
  }
        return 0;
}

在模块中启动两个线程后,并没有出现什么异常,每个线程都输出了5条信息。因此这个和文档描述矛盾。哪个兄弟能解释下?
谢谢

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
2 [报告]
发表于 2009-06-01 13:34 |只看该作者
锁在哪初始化的啊?

论坛徽章:
0
3 [报告]
发表于 2009-06-01 13:51 |只看该作者

回复 #2 yjh777 的帖子

在init函数初始化的。上面没有写出来,不好意思。
看了下代码,并调试了下。
在__mutex_unlock_common_slowpath中(由mutex_unlock调用)会调用检查mutex那些feathure的函数:debug_mutex_unlock
void debug_mutex_unlock(struct mutex *lock)
{
        if (unlikely(!debug_locks))
                return;

        DEBUG_LOCKS_WARN_ON(lock->magic != lock);
        DEBUG_LOCKS_WARN_ON(lock->owner != current_thread_info());
        DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
        DEBUG_LOCKS_WARN_ON(lock->owner != current_thread_info());
}
我调试到这里时,发现debug_locks为0.
而debug_locks在debug_locks.c中赋值:int debug_locks = 1;
难道有些config选项不对?

论坛徽章:
0
4 [报告]
发表于 2014-11-03 19:00 |只看该作者
结论是啥啊?
后续呢?

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
5 [报告]
发表于 2014-11-04 22:53 |只看该作者
真不明白你想干嘛。
首先,“关于metex是否能被不同进程lock/unlock的问题”,这个问题你前文并没提到呀。在内核态对调度而言本来就没有进程/线程的区别。如果不能被多个执行流同时执行,那要锁来干嘛。
其次,你这个案例,不是明显会死锁吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP