Skip to content

Commit 55d1430

Browse files
committed
Split the pthread library out of libc. NFC
1 parent 4b76531 commit 55d1430

File tree

6 files changed

+151
-100
lines changed

6 files changed

+151
-100
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ See docs/process.md for more on how version tagging works.
3434
on by default, but now it cannot be disabled. This setting was originally
3535
added in 2019 as a temporary measure while engines and bundlers learned to
3636
deal with `import.meta`. (#23171)
37+
- The pthread functions now live in a separate libpthread (or libpthread-stub).
38+
This library is automatically included so this change will only effect those
39+
building with `-nostdlib` or `-nolibc`. (#22735)
3740

3841
4.0.3 - 02/07/25
3942
----------------

embuilder.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
'libGL-webgl2-ofb-getprocaddr',
7676
'libGL-ww-getprocaddr',
7777
'libhtml5',
78+
'libpthread',
79+
'libpthread-stub',
80+
'libpthread-debug',
81+
'libpthread-debug-stub',
7882
'libsockets',
7983
'libsockets-ww',
8084
'libstubs',

test/test_core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4614,10 +4614,10 @@ def test_dylink_postsets_chunking(self):
46144614

46154615
@with_dylink_reversed
46164616
@parameterized({
4617-
'libcxx': ('libc,libc++,libmalloc,libc++abi',),
4617+
'libcxx': ('libc,libpthread,libc++,libmalloc,libc++abi',),
46184618
'all': ('1',),
4619-
'missing': ('libc,libmalloc,libc++abi', False, False, False),
4620-
'missing_assertions': ('libc,libmalloc,libc++abi', False, False, True),
4619+
'missing': ('libc,libpthread,libmalloc,libc++abi', False, False, False),
4620+
'missing_assertions': ('libc,libpthread,libmalloc,libc++abi', False, False, True),
46214621
})
46224622
def test_dylink_syslibs(self, syslibs, expect_pass=True, with_reversed=True, assertions=True):
46234623
# one module uses libcxx, need to force its inclusion when it isn't the main

test/test_other.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6293,9 +6293,9 @@ def test_fs_dev_random(self):
62936293
# partial list, but ok since we grab them as needed
62946294
'parial': [{'EMCC_FORCE_STDLIBS': 'libc++'}, False],
62956295
# fail! not enough stdlibs
6296-
'partial_only': [{'EMCC_FORCE_STDLIBS': 'libc++,libc,libc++abi', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, True],
6296+
'partial_only': [{'EMCC_FORCE_STDLIBS': 'libc++,libc,libpthread,libc++abi', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, True],
62976297
# force all the needed stdlibs, so this works even though we ignore the input file
6298-
'full_only': [{'EMCC_FORCE_STDLIBS': 'libc,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, False],
6298+
'full_only': [{'EMCC_FORCE_STDLIBS': 'libc,libpthread,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, False],
62996299
})
63006300
def test_only_force_stdlibs(self, env, fail):
63016301
cmd = [EMXX, test_file('hello_libcxx.cpp')]
@@ -6326,7 +6326,7 @@ def test_only_force_stdlibs_2(self):
63266326
}
63276327
}
63286328
''')
6329-
with env_modify({'EMCC_FORCE_STDLIBS': 'libc,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}):
6329+
with env_modify({'EMCC_FORCE_STDLIBS': 'libc,libpthread,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}):
63306330
self.run_process([EMXX, 'src.cpp', '-sDISABLE_EXCEPTION_CATCHING=0'])
63316331
self.assertContained('Caught exception: std::exception', self.run_js('a.out.js'))
63326332

@@ -12758,10 +12758,10 @@ def test_nostdlib(self):
1275812758
self.assertContained(err, self.expect_fail([EMCC, test_file('unistd/close.c'), '-nodefaultlibs']))
1275912759

1276012760
# Build again but with explit system libraries
12761-
libs = ['-lc', '-lcompiler_rt']
12761+
libs = ['-lc', '-lpthread', '-lcompiler_rt']
1276212762
self.run_process([EMCC, test_file('unistd/close.c'), '-nostdlib'] + libs)
1276312763
self.run_process([EMCC, test_file('unistd/close.c'), '-nodefaultlibs'] + libs)
12764-
self.run_process([EMCC, test_file('unistd/close.c'), '-nolibc', '-lc'])
12764+
self.run_process([EMCC, test_file('unistd/close.c'), '-nolibc', '-lc', '-lpthread'])
1276512765
self.run_process([EMCC, test_file('unistd/close.c'), '-nostartfiles'])
1276612766

1276712767
def test_argument_match(self):
@@ -13619,12 +13619,13 @@ def test_offset_convertor_plus_wasm2js(self):
1361913619

1362013620
def test_standard_library_mapping(self):
1362113621
# Test the `-l` flags on the command line get mapped the correct libraries variant
13622-
libs = ['-lc', '-lcompiler_rt', '-lmalloc']
13622+
libs = ['-lc', '-lpthread', '-lcompiler_rt', '-lmalloc']
1362313623
err = self.run_process([EMCC, test_file('hello_world.c'), '-pthread', '-nodefaultlibs', '-v'] + libs, stderr=PIPE).stderr
1362413624

1362513625
# Check that the linker was run with `-mt` variants because `-pthread` was passed.
1362613626
self.assertContained(' -lc-mt-debug ', err)
1362713627
self.assertContained(' -ldlmalloc-mt-debug ', err)
13628+
self.assertContained(' -lpthread-debug ', err)
1362813629
self.assertContained(' -lcompiler_rt-mt ', err)
1362913630

1363013631
def test_explicit_gl_linking(self):

tools/link.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,6 @@ def map_to_js_libs(library_name):
27522752
'dl': [],
27532753
'm': [],
27542754
'rt': [],
2755-
'pthread': [],
27562755
# This is the name of GNU's C++ standard library. We ignore it here
27572756
# for compatibility with GNU toolchains.
27582757
'stdc++': [],

tools/system_libs.py

Lines changed: 134 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,138 @@ class libnoexit(Library):
984984
src_files = ['atexit_dummy.c']
985985

986986

987+
class libpthread(MuslInternalLibrary,
988+
DebugLibrary,
989+
AsanInstrumentedLibrary):
990+
name = 'libpthread'
991+
# See libc
992+
cflags = ['-Os', '-fno-inline-functions', '-fno-builtin']
993+
cflags += ['-Wno-unused-but-set-variable',
994+
'-Wno-unused-variable',
995+
'-Wno-shift-op-parentheses',
996+
'-Wno-unused-label',
997+
'-Wno-logical-op-parentheses',
998+
'-Wno-bitwise-op-parentheses']
999+
1000+
def __init__(self, **kwargs):
1001+
self.is_stub = kwargs.pop('stub')
1002+
super().__init__(**kwargs)
1003+
1004+
def get_cflags(self):
1005+
cflags = super().get_cflags()
1006+
if not self.is_stub:
1007+
cflags += ['-pthread']
1008+
return cflags
1009+
1010+
def get_base_name(self):
1011+
name = super().get_base_name()
1012+
if self.is_stub:
1013+
name += '-stub'
1014+
return name
1015+
1016+
@classmethod
1017+
def vary_on(cls):
1018+
return super().vary_on() + ['stub']
1019+
1020+
@classmethod
1021+
def get_default_variation(cls, **kwargs):
1022+
return super().get_default_variation(stub=not settings.PTHREADS, **kwargs)
1023+
1024+
def get_files(self):
1025+
files = files_in_path(
1026+
path='system/lib/pthread',
1027+
filenames=['thread_profiler.c'])
1028+
1029+
if self.is_stub:
1030+
# Include just a subset of the thread directory
1031+
files += files_in_path(
1032+
path='system/lib/libc/musl/src/thread',
1033+
filenames=[
1034+
'pthread_self.c',
1035+
'pthread_cleanup_push.c',
1036+
'pthread_attr_init.c',
1037+
'pthread_attr_destroy.c',
1038+
'pthread_attr_get.c',
1039+
'pthread_attr_setdetachstate.c',
1040+
'pthread_attr_setguardsize.c',
1041+
'pthread_attr_setinheritsched.c',
1042+
'pthread_attr_setschedparam.c',
1043+
'pthread_attr_setschedpolicy.c',
1044+
'pthread_attr_setscope.c',
1045+
'pthread_attr_setstack.c',
1046+
'pthread_attr_setstacksize.c',
1047+
'pthread_getconcurrency.c',
1048+
'pthread_getcpuclockid.c',
1049+
'pthread_getschedparam.c',
1050+
'pthread_setschedprio.c',
1051+
'pthread_setconcurrency.c',
1052+
'default_attr.c',
1053+
# C11 thread library functions
1054+
'call_once.c',
1055+
'tss_create.c',
1056+
'tss_delete.c',
1057+
'tss_set.c',
1058+
'cnd_broadcast.c',
1059+
'cnd_destroy.c',
1060+
'cnd_init.c',
1061+
'cnd_signal.c',
1062+
'cnd_timedwait.c',
1063+
'cnd_wait.c',
1064+
'mtx_destroy.c',
1065+
'mtx_init.c',
1066+
'mtx_lock.c',
1067+
'mtx_timedlock.c',
1068+
'mtx_trylock.c',
1069+
'mtx_unlock.c',
1070+
'thrd_create.c',
1071+
'thrd_exit.c',
1072+
'thrd_join.c',
1073+
'thrd_sleep.c',
1074+
'thrd_yield.c',
1075+
])
1076+
files += files_in_path(
1077+
path='system/lib/pthread',
1078+
filenames=[
1079+
'library_pthread_stub.c',
1080+
'pthread_self_stub.c',
1081+
'proxying_stub.c',
1082+
])
1083+
else:
1084+
files += glob_in_path(
1085+
path='system/lib/libc/musl/src/thread',
1086+
glob_pattern='*.c',
1087+
excludes=[
1088+
'clone.c',
1089+
'pthread_create.c',
1090+
'pthread_kill.c', 'pthread_sigmask.c',
1091+
'__set_thread_area.c', 'synccall.c',
1092+
'__syscall_cp.c', '__tls_get_addr.c',
1093+
'__unmapself.c',
1094+
# Empty files, simply ignore them.
1095+
'syscall_cp.c', 'tls.c',
1096+
# TODO: Support these. See #12216.
1097+
'pthread_setname_np.c',
1098+
'pthread_getname_np.c',
1099+
])
1100+
files += files_in_path(
1101+
path='system/lib/pthread',
1102+
filenames=[
1103+
'library_pthread.c',
1104+
'em_task_queue.c',
1105+
'proxying.c',
1106+
'proxying_legacy.c',
1107+
'thread_mailbox.c',
1108+
'pthread_create.c',
1109+
'pthread_kill.c',
1110+
'emscripten_thread_init.c',
1111+
'emscripten_thread_state.S',
1112+
'emscripten_futex_wait.c',
1113+
'emscripten_futex_wake.c',
1114+
'emscripten_yield.c',
1115+
])
1116+
return files
1117+
1118+
9871119
class libc(MuslInternalLibrary,
9881120
DebugLibrary,
9891121
AsanInstrumentedLibrary,
@@ -1098,7 +1230,7 @@ def get_files(self):
10981230
ignore = [
10991231
'ipc', 'passwd', 'signal', 'sched', 'time', 'linux',
11001232
'aio', 'exit', 'legacy', 'mq', 'setjmp',
1101-
'ldso', 'malloc'
1233+
'ldso', 'malloc', 'thread'
11021234
]
11031235

11041236
# individual files
@@ -1120,91 +1252,6 @@ def get_files(self):
11201252

11211253
ignore += LIBC_SOCKETS
11221254

1123-
if self.is_mt:
1124-
ignore += [
1125-
'clone.c',
1126-
'pthread_create.c',
1127-
'pthread_kill.c', 'pthread_sigmask.c',
1128-
'__set_thread_area.c', 'synccall.c',
1129-
'__syscall_cp.c', '__tls_get_addr.c',
1130-
'__unmapself.c',
1131-
# Empty files, simply ignore them.
1132-
'syscall_cp.c', 'tls.c',
1133-
# TODO: Support these. See #12216.
1134-
'pthread_setname_np.c',
1135-
'pthread_getname_np.c',
1136-
]
1137-
libc_files += files_in_path(
1138-
path='system/lib/pthread',
1139-
filenames=[
1140-
'library_pthread.c',
1141-
'em_task_queue.c',
1142-
'proxying.c',
1143-
'proxying_legacy.c',
1144-
'thread_mailbox.c',
1145-
'pthread_create.c',
1146-
'pthread_kill.c',
1147-
'emscripten_thread_init.c',
1148-
'emscripten_thread_state.S',
1149-
'emscripten_futex_wait.c',
1150-
'emscripten_futex_wake.c',
1151-
'emscripten_yield.c',
1152-
])
1153-
else:
1154-
ignore += ['thread']
1155-
libc_files += files_in_path(
1156-
path='system/lib/libc/musl/src/thread',
1157-
filenames=[
1158-
'pthread_self.c',
1159-
'pthread_cleanup_push.c',
1160-
'pthread_attr_init.c',
1161-
'pthread_attr_destroy.c',
1162-
'pthread_attr_get.c',
1163-
'pthread_attr_setdetachstate.c',
1164-
'pthread_attr_setguardsize.c',
1165-
'pthread_attr_setinheritsched.c',
1166-
'pthread_attr_setschedparam.c',
1167-
'pthread_attr_setschedpolicy.c',
1168-
'pthread_attr_setscope.c',
1169-
'pthread_attr_setstack.c',
1170-
'pthread_attr_setstacksize.c',
1171-
'pthread_getconcurrency.c',
1172-
'pthread_getcpuclockid.c',
1173-
'pthread_getschedparam.c',
1174-
'pthread_setschedprio.c',
1175-
'pthread_setconcurrency.c',
1176-
'default_attr.c',
1177-
# C11 thread library functions
1178-
'call_once.c',
1179-
'tss_create.c',
1180-
'tss_delete.c',
1181-
'tss_set.c',
1182-
'cnd_broadcast.c',
1183-
'cnd_destroy.c',
1184-
'cnd_init.c',
1185-
'cnd_signal.c',
1186-
'cnd_timedwait.c',
1187-
'cnd_wait.c',
1188-
'mtx_destroy.c',
1189-
'mtx_init.c',
1190-
'mtx_lock.c',
1191-
'mtx_timedlock.c',
1192-
'mtx_trylock.c',
1193-
'mtx_unlock.c',
1194-
'thrd_create.c',
1195-
'thrd_exit.c',
1196-
'thrd_join.c',
1197-
'thrd_sleep.c',
1198-
'thrd_yield.c',
1199-
])
1200-
libc_files += files_in_path(
1201-
path='system/lib/pthread',
1202-
filenames=[
1203-
'library_pthread_stub.c',
1204-
'pthread_self_stub.c',
1205-
'proxying_stub.c',
1206-
])
1207-
12081255
# These files are in libc directories, but only built in libc_optz.
12091256
ignore += [
12101257
'pow_small.c', 'log_small.c', 'log2_small.c'
@@ -1322,10 +1369,6 @@ def get_files(self):
13221369
if settings.RELOCATABLE:
13231370
libc_files += files_in_path(path='system/lib/libc', filenames=['dynlink.c'])
13241371

1325-
libc_files += files_in_path(
1326-
path='system/lib/pthread',
1327-
filenames=['thread_profiler.c'])
1328-
13291372
libc_files += glob_in_path('system/lib/libc/compat', '*.c')
13301373

13311374
# Check for missing file in non_lto_files list. Do this here
@@ -2378,6 +2421,7 @@ def add_sanitizer_libs():
23782421
if settings.ALLOW_UNIMPLEMENTED_SYSCALLS:
23792422
add_library('libstubs')
23802423
if not options.nolibc:
2424+
add_library('libpthread')
23812425
if not settings.EXIT_RUNTIME:
23822426
add_library('libnoexit')
23832427
add_library('libc')

0 commit comments

Comments
 (0)