* [musl] Pthread robust_list for non-pshared mutexes
@ 2024-05-24 7:32 AK47
2024-05-24 13:58 ` Markus Wichmann
0 siblings, 1 reply; 2+ messages in thread
From: AK47 @ 2024-05-24 7:32 UTC (permalink / raw)
To: musl
[-- Attachment #1: Type: text/plain, Size: 3800 bytes --]
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;
}
[-- Attachment #2: Type: text/html, Size: 20065 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [musl] Pthread robust_list for non-pshared mutexes
2024-05-24 7:32 [musl] Pthread robust_list for non-pshared mutexes AK47
@ 2024-05-24 13:58 ` Markus Wichmann
0 siblings, 0 replies; 2+ messages in thread
From: Markus Wichmann @ 2024-05-24 13:58 UTC (permalink / raw)
To: musl; +Cc: AK47
Hi,
not entirely sure what you mean. All non-normal mutexes get added to the
robust list, yes, but also get removed from the list in
pthread_mutex_unlock(). And the robust list is processed in
pthread_exit(), which all threads must call sooner or later. Only
exception is when a thread crashes, but in that case the entire process
dies, and non-pshared mutexes cease to matter.
If a thread is cancelled, __cancel() will call pthread_exit(). And I
know of no other way for a thread to "end" as you put it.
Ciao,
Markus
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-05-24 13:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-24 7:32 [musl] Pthread robust_list for non-pshared mutexes AK47
2024-05-24 13:58 ` Markus Wichmann
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).