|
Message-ID: <tencent_BE588B7CA4E262C6E32E66D442F187644F05@qq.com>
Date: Fri, 24 May 2024 15:32:47 +0800
From: "AK47" <250200715@...com>
To: "musl" <musl@...ts.openwall.com>
Subject: Pthread robust_list for non-pshared mutexes
Hi all,
Now for a normal non-Robust mutex, like a normal mutex with type PTHREAD_MUTEX_RECURSIVE. I found it also will be add to robust_list in pthread in __pthread_mutex_trylock_owner:
volatile void *next = self->robust_list.head; m->_m_next = next;
m->_m_prev = &self->robust_list.head;
if (next != &self->robust_list.head) *(volatile void *volatile *)
((char *)next - sizeof(void *)) = &m->_m_next;
self->robust_list.head = &m->_m_next;
self->robust_list.pending = 0;
However, it seems that for non-Robust mutexes added to the pthread robust_list, only when the pthread exits we will do a traversal wake-up operation in __pthread_exit:
volatile void *volatile *rp; while ((rp=self->robust_list.head) && rp != &self->robust_list.head) {
pthread_mutex_t *m = (void *)((char *)rp
- offsetof(pthread_mutex_t, _m_next));
int waiters = m->_m_waiters;
int priv = (m->_m_type & 128) ^ 128;
self->robust_list.pending = rp;
self->robust_list.head = *rp;
int cont = a_swap(&m->_m_lock, 0x40000000);
self->robust_list.pending = 0;
if (cont < 0 || waiters)
__wake(&m->_m_lock, 1, priv);
}
And taking a regular recursive mutex as an example, it will be added to the robust_list, if the thread ends but it is still not fully released, The robust_list still contains this mutex. We will swap the _m_lock to owner died (0x40000000) and wake up one of the waiting thread. But due to this code in __pthread_mutex_trylock_owner:
if (own == 0x3fffffff) return ENOTRECOVERABLE;
if (own || (old && !(type & 4))) return EBUSY;
The waiting thread which has been waked up will nerver get the recursive mutex, the deadlock still occurred. So my question is, why do we need to add some non-Roubus mutexes, such as PTHREAD-MUTEX-RECURSIVE, to the robust list and try to do wake up in pthread_exit when the mutex isn't released by this thread.
I hope to receive your answer, thank you.
JinCheng Li
PS: This is a simple case for deadlock but musl still do something with robust_list such as waking up the waiters:
pthread_mutex_t mutex;
void *dl_test1()
{
pthread_mutex_lock(&mutex);
sleep(10);
return 0;
}
void *dl_test2()
{
pthread_mutex_lock(&mutex);
return 0;
}
int main()
{
void *res;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, &attr);
pthread_t thd1;
pthread_t thd2;
pthread_create(&thd1, NULL, dl_test1, NULL);
pthread_create(&thd2, NULL, dl_test2, NULL);
pthread_join(thd1, &res);
pthread_join(thd2, &res);
return 0;
}
Content of type "text/html" skipped
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.