Skip to content

Commit bd92f11

Browse files
committed
Split the pthread library out of libc. NFC
1 parent d7f8277 commit bd92f11

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
@@ -52,6 +52,9 @@ See docs/process.md for more on how version tagging works.
5252
- The `POLYFILL_OLD_MATH_FUNCTIONS` setting was removed. The browser versions
5353
that require these polyfills are no longer supported by emscripten so the
5454
polyfills should never be needed. (#23262)
55+
- The pthread APIs now live in a separate libpthread (or libpthread-stub). This
56+
library is automatically included so this change will only effect those
57+
building with `-nostdlib` or `-nolibc`. (#22735)
5558

5659
3.1.74 - 12/14/24
5760
-----------------

embuilder.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
'libGL-webgl2-ofb-getprocaddr',
7272
'libGL-ww-getprocaddr',
7373
'libhtml5',
74+
'libpthread',
75+
'libpthread-stub',
76+
'libpthread-debug',
77+
'libpthread-debug-stub',
7478
'libsockets',
7579
'libsockets-ww',
7680
'libstubs',

test/test_core.py

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

45904590
@with_dylink_reversed
45914591
@parameterized({
4592-
'libcxx': ('libc,libc++,libmalloc,libc++abi',),
4592+
'libcxx': ('libc,libpthread,libc++,libmalloc,libc++abi',),
45934593
'all': ('1',),
4594-
'missing': ('libc,libmalloc,libc++abi', False, False, False),
4595-
'missing_assertions': ('libc,libmalloc,libc++abi', False, False, True),
4594+
'missing': ('libc,libpthread,libmalloc,libc++abi', False, False, False),
4595+
'missing_assertions': ('libc,libpthread,libmalloc,libc++abi', False, False, True),
45964596
})
45974597
def test_dylink_syslibs(self, syslibs, expect_pass=True, with_reversed=True, assertions=True):
45984598
# 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
@@ -6104,9 +6104,9 @@ def test_fs_dev_random(self):
61046104
# partial list, but ok since we grab them as needed
61056105
'parial': [{'EMCC_FORCE_STDLIBS': 'libc++'}, False],
61066106
# fail! not enough stdlibs
6107-
'partial_only': [{'EMCC_FORCE_STDLIBS': 'libc++,libc,libc++abi', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, True],
6107+
'partial_only': [{'EMCC_FORCE_STDLIBS': 'libc++,libc,libpthread,libc++abi', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, True],
61086108
# force all the needed stdlibs, so this works even though we ignore the input file
6109-
'full_only': [{'EMCC_FORCE_STDLIBS': 'libc,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, False],
6109+
'full_only': [{'EMCC_FORCE_STDLIBS': 'libc,libpthread,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, False],
61106110
})
61116111
def test_only_force_stdlibs(self, env, fail):
61126112
cmd = [EMXX, test_file('hello_libcxx.cpp')]
@@ -6137,7 +6137,7 @@ def test_only_force_stdlibs_2(self):
61376137
}
61386138
}
61396139
''')
6140-
with env_modify({'EMCC_FORCE_STDLIBS': 'libc,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}):
6140+
with env_modify({'EMCC_FORCE_STDLIBS': 'libc,libpthread,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}):
61416141
self.run_process([EMXX, 'src.cpp', '-sDISABLE_EXCEPTION_CATCHING=0'])
61426142
self.assertContained('Caught exception: std::exception', self.run_js('a.out.js'))
61436143

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

1245912459
# Build again but with explit system libraries
12460-
libs = ['-lc', '-lcompiler_rt']
12460+
libs = ['-lc', '-lpthread', '-lcompiler_rt']
1246112461
self.run_process([EMCC, test_file('unistd/close.c'), '-nostdlib'] + libs)
1246212462
self.run_process([EMCC, test_file('unistd/close.c'), '-nodefaultlibs'] + libs)
12463-
self.run_process([EMCC, test_file('unistd/close.c'), '-nolibc', '-lc'])
12463+
self.run_process([EMCC, test_file('unistd/close.c'), '-nolibc', '-lc', '-lpthread'])
1246412464
self.run_process([EMCC, test_file('unistd/close.c'), '-nostartfiles'])
1246512465

1246612466
def test_argument_match(self):
@@ -13359,12 +13359,13 @@ def test_offset_convertor_plus_wasm2js(self):
1335913359

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

1336513365
# Check that the linker was run with `-mt` variants because `-pthread` was passed.
1336613366
self.assertContained(' -lc-mt-debug ', err)
1336713367
self.assertContained(' -ldlmalloc-mt ', err)
13368+
self.assertContained(' -lpthread-debug ', err)
1336813369
self.assertContained(' -lcompiler_rt-mt ', err)
1336913370

1337013371
def test_explicit_gl_linking(self):

tools/link.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2776,7 +2776,6 @@ def map_to_js_libs(library_name):
27762776
'dl': [],
27772777
'm': [],
27782778
'rt': [],
2779-
'pthread': [],
27802779
# This is the name of GNU's C++ standard library. We ignore it here
27812780
# for compatibility with GNU toolchains.
27822781
'stdc++': [],

tools/system_libs.py

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

972972

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

10891221
# individual files
@@ -1105,91 +1237,6 @@ def get_files(self):
11051237

11061238
ignore += LIBC_SOCKETS
11071239

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

1308-
libc_files += files_in_path(
1309-
path='system/lib/pthread',
1310-
filenames=['thread_profiler.c'])
1311-
13121355
libc_files += glob_in_path('system/lib/libc/compat', '*.c')
13131356

13141357
# Check for missing file in non_lto_files list. Do this here
@@ -2377,6 +2420,7 @@ def add_sanitizer_libs():
23772420
if settings.ALLOW_UNIMPLEMENTED_SYSCALLS:
23782421
add_library('libstubs')
23792422
if '-nolibc' not in args:
2423+
add_library('libpthread')
23802424
if not settings.EXIT_RUNTIME:
23812425
add_library('libnoexit')
23822426
add_library('libc')

0 commit comments

Comments
 (0)