Skip to content

Commit 3685900

Browse files
kateinoigakukunAnka
authored andcommitted
[wasm][stdlib] HACK: Add pthread stub to avoid linking error
This is a hack to avoid missing pthread symbols error when linking. But we now have `SWIFT_STDLIB_SINGLE_THREADED_RUNTIME` mode so the hack should not be necessary anymore.
1 parent a64ba3a commit 3685900

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

stdlib/public/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ if(SWIFT_BUILD_STDLIB)
142142
add_subdirectory(core)
143143
add_subdirectory(SwiftOnoneSupport)
144144

145+
if(WASI IN_LIST SWIFT_SDKS)
146+
add_subdirectory(WASI)
147+
endif()
148+
145149
if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING)
146150
add_subdirectory(Differentiation)
147151
endif()

stdlib/public/Resources/wasi/static-executable-args.lnk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-static
22
-lswiftSwiftOnoneSupport
3+
-lswiftWasiPthread
34
-ldl
45
-lstdc++
56
-lm

stdlib/public/WASI/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_swift_target_library(swiftWasiPthread STATIC IS_STDLIB
2+
Pthread.cpp
3+
TARGET_SDKS WASI
4+
INSTALL_IN_COMPONENT stdlib)

stdlib/public/WASI/Pthread.cpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// SPDX-License-Identifier: 0BSD
2+
// prototypes taken from opengroup
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
#include <pthread.h>
6+
#include <semaphore.h>
7+
8+
#define STUB() do {fprintf(stderr, "FakePthread: unsupported %s\n", __func__);abort();}while(0)
9+
10+
// mutexes: just no-ops
11+
12+
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) {
13+
return 0;
14+
}
15+
16+
int pthread_mutex_destroy(pthread_mutex_t *mutex) {
17+
return 0;
18+
}
19+
20+
int pthread_mutexattr_init(pthread_mutexattr_t *attr) {
21+
return 0;
22+
}
23+
24+
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) {
25+
return 0;
26+
}
27+
28+
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type) {
29+
return 0;
30+
}
31+
32+
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) {
33+
return 0;
34+
}
35+
36+
int pthread_mutex_lock(pthread_mutex_t *mutex) {
37+
return 0;
38+
}
39+
40+
int pthread_mutex_trylock(pthread_mutex_t *mutex) {
41+
return 0;
42+
}
43+
44+
int pthread_mutex_unlock(pthread_mutex_t *mutex) {
45+
return 0;
46+
}
47+
48+
// pthread_cond: STUB
49+
50+
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) {
51+
return 0;
52+
}
53+
54+
int pthread_cond_destroy(pthread_cond_t *cond) {
55+
return 0;
56+
}
57+
58+
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
59+
STUB();
60+
}
61+
62+
int pthread_cond_timedwait(pthread_cond_t *cond,
63+
pthread_mutex_t *mutex, const struct timespec *abstime) {
64+
STUB();
65+
}
66+
67+
int pthread_cond_broadcast(pthread_cond_t *cond) {
68+
return 0;
69+
}
70+
71+
int pthread_cond_signal(pthread_cond_t *cond) {
72+
return 0;
73+
}
74+
75+
// tls
76+
77+
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)) {
78+
STUB();
79+
}
80+
81+
void *pthread_getspecific(pthread_key_t key) {
82+
STUB();
83+
}
84+
85+
int pthread_setspecific(pthread_key_t key, const void *value) {
86+
STUB();
87+
}
88+
89+
// threads
90+
91+
pthread_t pthread_self() {
92+
return (pthread_t)1234;
93+
}
94+
95+
#undef pthread_equal
96+
97+
int pthread_equal(pthread_t t1, pthread_t t2) {
98+
return t1 == t2;
99+
}
100+
101+
int pthread_join(pthread_t thread, void **value_ptr) {
102+
STUB();
103+
}
104+
105+
int pthread_detach(pthread_t thread) {
106+
STUB();
107+
}
108+
109+
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) {
110+
return 0;
111+
}
112+
113+
// once
114+
115+
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) {
116+
STUB();
117+
}
118+
119+
// rwlock
120+
121+
int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) {
122+
return 0;
123+
}
124+
125+
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) {
126+
return 0;
127+
}
128+
129+
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) {
130+
return 0;
131+
}
132+
133+
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock) {
134+
return 0;
135+
}
136+
137+
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) {
138+
return 0;
139+
}
140+
141+
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock) {
142+
return 0;
143+
}
144+
145+
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) {
146+
return 0;
147+
}
148+
149+
// named semaphores
150+
151+
sem_t *sem_open(const char *name, int oflag, ...) {
152+
STUB();
153+
}

0 commit comments

Comments
 (0)