mailing list of musl libc
 help / color / mirror / code / Atom feed
From: IMMING <2465853002@qq.com>
To: musl <musl@lists.openwall.com>
Subject: [musl] A question about the implementation of pthread_create and start
Date: Mon, 27 May 2024 20:22:42 +0800	[thread overview]
Message-ID: <tencent_FD8896C7303E60BA2F965266BABB1E588009@qq.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2369 bytes --]

Hi,&nbsp;I would like to ask a question about the implementation of pthread_create and&nbsp;start (musl v1.2.5)

My question is as follows, here is the code:
-----------------
int&nbsp;__pthread_create(pthread_t&nbsp;*restrict&nbsp;res,&nbsp;const&nbsp;pthread_attr_t&nbsp;*restrict&nbsp;attrp,&nbsp;void&nbsp;*(*entry)(void&nbsp;*),&nbsp;void&nbsp;*restrict&nbsp;arg)

...
	if&nbsp;(ret&nbsp;<&nbsp;0)&nbsp;{ 
		ret&nbsp;=&nbsp;-EAGAIN; 
	}&nbsp;else&nbsp;if&nbsp;(attr._a_sched)&nbsp;{ 
		ret&nbsp;=&nbsp;__syscall(SYS_sched_setscheduler, 
			new-&gt;tid,&nbsp;attr._a_policy,&nbsp;&amp;attr._a_prio); 
		if&nbsp;(a_swap(&amp;args-&gt;control,&nbsp;ret&nbsp;?&nbsp;3&nbsp;:&nbsp;0)&nbsp;==&nbsp;2)  <- line 1 
			__wake(&amp;args-&gt;control,&nbsp;1,&nbsp;1);                    <- line 2 
		if&nbsp;(ret) 
			__wait(&amp;args-&gt;control,&nbsp;0,&nbsp;3,&nbsp;0);                <- line 3
 
	}
...
-----------------
static&nbsp;int&nbsp;start(void&nbsp;*p) 
 ...
	struct&nbsp;start_args&nbsp;*args&nbsp;=&nbsp;p; 
	int&nbsp;state&nbsp;=&nbsp;args-&gt;control; 
	if&nbsp;(state)&nbsp;{ 
		if&nbsp;(a_cas(&amp;args-&gt;control,&nbsp;1,&nbsp;2)&nbsp;==&nbsp;1)           <- line 4 
			__wait(&amp;args-&gt;control,&nbsp;0,&nbsp;2,&nbsp;1);              <- line 5 
		if&nbsp;(args-&gt;control)&nbsp;{ 
			__syscall(SYS_set_tid_address,&nbsp;&amp;args-&gt;control); <- line 6 
			for&nbsp;(;;)&nbsp;__syscall(SYS_exit,&nbsp;0);              <- line 7 
		} 
	} 
	__syscall(SYS_rt_sigprocmask,&nbsp;SIG_SETMASK,&nbsp;&amp;args-&gt;sig_mask,&nbsp;0,&nbsp;_NSIG/8); 
...
-----------------

I think the calling route should be like this:


1.line 4(child thread)
2.line 5(child thread wait)
3.line 1(parent thread&nbsp;if SYS_sched_setscheduler false)
4.line 2(parent thread wake child thread)
5.line 3(parent&nbsp;thread wait if SYS_sched_setscheduler false) <- Problem point
6.line 6(child thread)

7.line 6(child thread exit)



My question is, if SYS_sched_setscheduler returns an error (a non-zero value), the parent thread will remain in a wait state and I have not found a way to wake it, 
which will cause the parent thread to remain stuck in the pthread_create function and unable to return

1.Is my analysis process correct?
2.Is the situation where the parent thread gets stuck in the waite as expected?

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

             reply	other threads:[~2024-05-27 12:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-27 12:22 IMMING [this message]
2024-05-27 14:56 ` 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_FD8896C7303E60BA2F965266BABB1E588009@qq.com \
    --to=2465853002@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).