Skip to content

Commit 54151f7

Browse files
committed
Split the pthread library out of libc
1 parent bdde3cd commit 54151f7

File tree

5 files changed

+146
-100
lines changed

5 files changed

+146
-100
lines changed

embuilder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
'libsanitizer_common_rt',
115115
'libubsan_rt',
116116
'libwasm_workers-debug-stub',
117+
'libpthread-debug',
118+
'libpthread-debug-stub',
117119
'libfetch',
118120
'libfetch-mt',
119121
'libwasmfs',

test/test_core.py

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

46264626
@with_dylink_reversed
46274627
@parameterized({
4628-
'libcxx': ('libc,libc++,libmalloc,libc++abi',),
4628+
'libcxx': ('libc,libpthread,libc++,libmalloc,libc++abi',),
46294629
'all': ('1',),
4630-
'missing': ('libc,libmalloc,libc++abi', False, False, False),
4631-
'missing_assertions': ('libc,libmalloc,libc++abi', False, False, True),
4630+
'missing': ('libc,libpthread,libmalloc,libc++abi', False, False, False),
4631+
'missing_assertions': ('libc,libpthread,libmalloc,libc++abi', False, False, True),
46324632
})
46334633
def test_dylink_syslibs(self, syslibs, expect_pass=True, with_reversed=True, assertions=True):
46344634
# 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
@@ -5998,9 +5998,9 @@ def test_bad_lookup(self):
59985998
# partial list, but ok since we grab them as needed
59995999
'parial': [{'EMCC_FORCE_STDLIBS': 'libc++'}, False],
60006000
# fail! not enough stdlibs
6001-
'partial_only': [{'EMCC_FORCE_STDLIBS': 'libc++,libc,libc++abi', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, True],
6001+
'partial_only': [{'EMCC_FORCE_STDLIBS': 'libc++,libc,libpthread,libc++abi', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, True],
60026002
# force all the needed stdlibs, so this works even though we ignore the input file
6003-
'full_only': [{'EMCC_FORCE_STDLIBS': 'libc,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, False],
6003+
'full_only': [{'EMCC_FORCE_STDLIBS': 'libc,libpthread,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}, False],
60046004
})
60056005
def test_only_force_stdlibs(self, env, fail):
60066006
cmd = [EMXX, test_file('hello_libcxx.cpp')]
@@ -6031,7 +6031,7 @@ def test_only_force_stdlibs_2(self):
60316031
}
60326032
}
60336033
''')
6034-
with env_modify({'EMCC_FORCE_STDLIBS': 'libc,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}):
6034+
with env_modify({'EMCC_FORCE_STDLIBS': 'libc,libpthread,libc++abi,libc++,libmalloc', 'EMCC_ONLY_FORCED_STDLIBS': '1'}):
60356035
self.run_process([EMXX, 'src.cpp', '-sDISABLE_EXCEPTION_CATCHING=0'])
60366036
self.assertContained('Caught exception: std::exception', self.run_js('a.out.js'))
60376037

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

1227912279
# Build again but with explit system libraries
12280-
libs = ['-lc', '-lcompiler_rt']
12280+
libs = ['-lc', '-lpthread', '-lcompiler_rt']
1228112281
self.run_process([EMCC, test_file('unistd/close.c'), '-nostdlib'] + libs)
1228212282
self.run_process([EMCC, test_file('unistd/close.c'), '-nodefaultlibs'] + libs)
12283-
self.run_process([EMCC, test_file('unistd/close.c'), '-nolibc', '-lc'])
12283+
self.run_process([EMCC, test_file('unistd/close.c'), '-nolibc', '-lc', '-lpthread'])
1228412284
self.run_process([EMCC, test_file('unistd/close.c'), '-nostartfiles'])
1228512285

1228612286
def test_argument_match(self):
@@ -13174,12 +13174,13 @@ def test_offset_convertor_plus_wasm2js(self):
1317413174

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

1318013180
# Check that the linker was run with `-mt` variants because `-pthread` was passed.
1318113181
self.assertContained(' -lc-mt-debug ', err)
1318213182
self.assertContained(' -ldlmalloc-mt ', err)
13183+
self.assertContained(' -lpthread-debug ', err)
1318313184
self.assertContained(' -lcompiler_rt-mt ', err)
1318413185

1318513186
def test_explicit_gl_linking(self):

tools/link.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2721,7 +2721,6 @@ def map_to_js_libs(library_name):
27212721
'dl': [],
27222722
'm': [],
27232723
'rt': [],
2724-
'pthread': [],
27252724
# This is the name of GNU's C++ standard library. We ignore it here
27262725
# for compatibility with GNU toolchains.
27272726
'stdc++': [],

tools/system_libs.py

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

976976

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

10941226
# individual files
@@ -1111,91 +1243,6 @@ def get_files(self):
11111243

11121244
ignore += LIBC_SOCKETS
11131245

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

1310-
libc_files += files_in_path(
1311-
path='system/lib/pthread',
1312-
filenames=['thread_profiler.c'])
1313-
13141357
libc_files += glob_in_path('system/lib/libc/compat', '*.c')
13151358

13161359
# Check for missing file in non_lto_files list. Do this here
@@ -2372,6 +2415,7 @@ def add_sanitizer_libs():
23722415
if settings.ALLOW_UNIMPLEMENTED_SYSCALLS:
23732416
add_library('libstubs')
23742417
if '-nolibc' not in args:
2418+
add_library('libpthread')
23752419
if not settings.EXIT_RUNTIME:
23762420
add_library('libnoexit')
23772421
add_library('libc')

0 commit comments

Comments
 (0)