pthread_mutex_trylock:未使用的互斥锁上的EBUSY?

时间:2017-03-10 00:46:36

标签: linux locking pthreads mutex deadlock

线程锁定了我。首先执行trylock,然后执行阻塞锁定(这大部分已完成,因此我可以记录此时其他东西是否有锁定)。 trylock失败,我们永远阻止pthread_mutex_lock()。事实是,互斥锁未被使用。

相关的代码片段:

#define LOCK_SESSION(s)                         \
do {                                            \
    int _i_ = pthread_mutex_trylock(&s->s_lock);\
    if (_i_) {                                  \
        logn(LOG_INFO2|LOGF_LOCKS,              \
            "WARNING: Failed to acquire session lock for (%p)->(%p).  (rv: %d:%s)  Fix this.  Going to do a blocking lock.", \
            s, &s->s_lock, _i_, strerror(_i_)); \
        pthread_mutex_lock(&s->s_lock);         \
    }                                           \
    LOG_LOCK(&s->s_lock);                       \
} while (0)

gdb输出:

#0  0x0000003cdc40e334 in __lll_lock_wait () from /lib64/libpthread.so.0
#1  0x0000003cdc4095f3 in _L_lock_892 () from /lib64/libpthread.so.0
#2  0x0000003cdc4094d7 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3  0x000000000046837c in _fas_make_session_or_acquire (fd=21, shutdown=1) at modules.c:6732

查看相关的互斥锁:

#3  0x000000000046837c in _fas_make_session_or_acquire (fd=21, shutdown=1) at modules.c:6732
6732    in modules.c
(gdb) p s->s_lock
$17 = {__data = {__lock = 0, __count = 0, __owner = 0, __nusers = 0, __kind = 1, __spins = 0, __list = {__prev = 0x0, __next = 0x0}},
__size = '\000' <repeats 16 times>, "\001", '\000' <repeats 22 times>, __align = 0}
(gdb) p _i_
$18 = 16

_i_保存pthread_mutex_trylock的返回值,即EAGAIN。 请注意,互斥锁类型(1)是递归的。

因此,即使互斥锁完全未使用,trylock也会失败,即使互斥锁完全未使用,然后我们进入pthread_mutex_lock()的黑洞,永远不会返回。我应该注意到这种情况非常严重。

我无法解释这里发生了什么。有什么想法吗?

0 个答案:

没有答案