mailing list of musl libc
 help / color / mirror / code / Atom feed
From: AK47 <250200715@qq.com>
To: musl <musl@lists.openwall.com>
Subject: [musl] Pthread robust_list for non-pshared mutexes
Date: Fri, 24 May 2024 15:32:47 +0800	[thread overview]
Message-ID: <tencent_BE588B7CA4E262C6E32E66D442F187644F05@qq.com> (raw)

[-- 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&nbsp;&nbsp;__pthread_mutex_trylock_owner:
      volatile void *next = self-&gt;robust_list.head;      m-&gt;_m_next = next;
      m-&gt;_m_prev = &amp;self-&gt;robust_list.head;
      if (next != &amp;self-&gt;robust_list.head) *(volatile void *volatile *)
            ((char *)next - sizeof(void *)) = &amp;m-&gt;_m_next;
      self-&gt;robust_list.head = &amp;m-&gt;_m_next;
      self-&gt;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&nbsp;a traversal wake-up operation in __pthread_exit:

      volatile void *volatile *rp;      while ((rp=self-&gt;robust_list.head) &amp;&amp; rp != &amp;self-&gt;robust_list.head) {
            pthread_mutex_t *m = (void *)((char *)rp
                  - offsetof(pthread_mutex_t, _m_next));
            int waiters = m-&gt;_m_waiters;
            int priv = (m-&gt;_m_type &amp; 128) ^ 128;
            self-&gt;robust_list.pending = rp;
            self-&gt;robust_list.head = *rp;
            int cont = a_swap(&amp;m-&gt;_m_lock, 0x40000000);
            self-&gt;robust_list.pending = 0;
            if (cont < 0 || waiters)
                  __wake(&amp;m-&gt;_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&nbsp;__pthread_mutex_trylock_owner:



      if (own == 0x3fffffff) return ENOTRECOVERABLE;
      if (own || (old &amp;&amp; !(type &amp; 4))) return EBUSY;



The waiting thread which has been waked up will nerver get the&nbsp;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&nbsp;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:
&nbsp;     pthread_mutex_t mutex;
void *dl_test1()
{
      pthread_mutex_lock(&amp;mutex);

      sleep(10);
      return 0;
}
      void *dl_test2()

{
      pthread_mutex_lock(&amp;mutex);

      return 0;
}
int main()
{
      void *res;

      pthread_mutexattr_t attr;

      pthread_mutexattr_init(&amp;attr);

      pthread_mutexattr_settype(&amp;attr, PTHREAD_MUTEX_RECURSIVE);

      pthread_mutex_init(&amp;mutex,&nbsp;&amp;attr);

      pthread_t thd1;

      pthread_t thd2;

      pthread_create(&amp;thd1, NULL, dl_test1, NULL);

      pthread_create(&amp;thd2, NULL, dl_test2, NULL);

      pthread_join(thd1, &amp;res);

      pthread_join(thd2, &amp;res);

      return 0;
}

[-- Attachment #2: Type: text/html, Size: 20065 bytes --]

             reply	other threads:[~2024-05-24  7:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-24  7:32 AK47 [this message]
2024-05-24 13:58 ` Markus Wichmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_BE588B7CA4E262C6E32E66D442F187644F05@qq.com \
    --to=250200715@qq.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).