未验证 提交 2dc15985 编写于 作者: O openharmony_ci 提交者: Gitee

!916 Optimize pthread_once performance

Merge pull request !916 from yinchuang/opt_pthread_once
......@@ -2188,6 +2188,7 @@ musl_src_porting_file = [
"src/conf/legacy.c",
"src/conf/sysconf.c",
"src/env/getenv.c",
"src/thread/pthread_once.c",
]
musl_inc_hook_files = [
......
......@@ -393,6 +393,15 @@ template("musl_libs") {
}
defines = []
if (musl_arch == "arm") {
defines += [ "MUSL_ARM_ARCH" ]
}
if (musl_arch == "aarch64") {
defines += [ "MUSL_AARCH64_ARCH" ]
}
if (musl_arch == "x86_64") {
defines += [ "MUSL_X86_64_ARCH" ]
}
if (musl_secure_level > 0) {
defines += [ "MALLOC_FREELIST_HARDENED" ]
}
......
#include "pthread_impl.h"
static void undo(void *control)
{
/* Wake all waiters, since the waiter status is lost when
* resetting control to the initial state. */
if (a_swap(control, 0) == 3)
__wake(control, -1, 1);
}
hidden int __pthread_once_full(pthread_once_t *control, void (*init)(void))
{
/* Try to enter initializing state. Four possibilities:
* 0 - we're the first or the other cancelled; run init
* 1 - another thread is running init; wait
* 2 - another thread finished running init; just return
* 3 - another thread is running init, waiters present; wait */
for (;;) switch (a_cas(control, 0, 1)) {
case 0:
pthread_cleanup_push(undo, control);
init();
pthread_cleanup_pop(0);
if (a_swap(control, 2) == 3)
__wake(control, -1, 1);
return 0;
case 1:
/* If this fails, so will __wait. */
a_cas(control, 1, 3);
case 3:
__wait(control, 0, 3, 1);
continue;
case 2:
return 0;
}
}
#if defined(MUSL_AARCH64_ARCH) || defined(MUSL_ARM_ARCH)
int __pthread_once(pthread_once_t *control, void (*init)(void))
{
/* Return immediately if init finished before, use load aquire to ensure that
* effects of the init routine are visible to the caller. */
if (a_ll((volatile int *)control) == 2) {
return 0;
}
return __pthread_once_full(control, init);
}
#else
int __pthread_once(pthread_once_t *control, void (*init)(void))
{
/* Return immediately if init finished before, but ensure that
* effects of the init routine are visible to the caller. */
if (*(volatile int *)control == 2) {
a_barrier();
return 0;
}
return __pthread_once_full(control, init);
}
#endif
weak_alias(__pthread_once, pthread_once);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册