Skip to content

Commit c4fef4b

Browse files
committed
Split the pthread library out of libc
1 parent 5e6053c commit c4fef4b

File tree

8 files changed

+151
-105
lines changed

8 files changed

+151
-105
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/code_size/embind_val_wasm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"a.js": 6849,
55
"a.js.gz": 2947,
66
"a.wasm": 9565,
7-
"a.wasm.gz": 4904,
7+
"a.wasm.gz": 4903,
88
"total": 16966,
9-
"total_gz": 8231
9+
"total_gz": 8230
1010
}

test/other/codesize/test_codesize_minimal_pthreads.funcs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
$__emscripten_stdout_close
12
$__emscripten_stdout_seek
2-
$__errno_location
33
$__memcpy
44
$__memset
55
$__pthread_getspecific
@@ -67,6 +67,7 @@ $em_task_queue_free
6767
$em_task_queue_is_empty
6868
$emscripten_builtin_free
6969
$emscripten_builtin_malloc
70+
$emscripten_builtin_memalign
7071
$emscripten_current_thread_process_queued_calls
7172
$emscripten_futex_wait
7273
$emscripten_futex_wake
@@ -80,7 +81,6 @@ $init_mparams
8081
$lock
8182
$main
8283
$nodtor
83-
$pthread_attr_destroy
8484
$pthread_cond_signal
8585
$pthread_mutex_destroy
8686
$pthread_setspecific
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19501
1+
19971

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
@@ -969,6 +969,138 @@ class libnoexit(Library):
969969
src_files = ['atexit_dummy.c']
970970

971971

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

11071239
ignore += LIBC_SOCKETS
11081240

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

1305-
libc_files += files_in_path(
1306-
path='system/lib/pthread',
1307-
filenames=['thread_profiler.c'])
1308-
13091352
libc_files += glob_in_path('system/lib/libc/compat', '*.c')
13101353

13111354
# Check for missing file in non_lto_files list. Do this here
@@ -2364,6 +2407,7 @@ def add_sanitizer_libs():
23642407
add_library('libmimalloc')
23652408
elif settings.MALLOC != 'none':
23662409
add_library('libmalloc')
2410+
add_library('libpthread')
23672411
add_library('libcompiler_rt')
23682412
if settings.LINK_AS_CXX:
23692413
add_library('libc++')

0 commit comments

Comments
 (0)