1- --- a/Makefile
2- +++ b/Makefile
3- @@ -36,6 +36,11 @@ OBJ_FILES := $(filter-out $(NOT_OBJ_FILES), $(SRC_FILES:.c=.o))
4- override CFLAGS += -g -I. -I./vendor -I./src/postgres/include -Wall -Wno-unused-function -Wno-unused-value -Wno-unused-variable -fno-strict-aliasing -fwrapv -fPIC
1+ Backport of the PostgreSQL 14 s_lock.h fallback that uses __sync_lock_test_and_set()
2+ when there is no platform-specific TAS. PG13 has none for wasm32, so without this
3+ Emscripten hits "#error PostgreSQL does not have native spinlock support".
4+
5+ diff --git a/src/postgres/include/storage/s_lock.h b/src/postgres/include/storage/s_lock.h
6+ index 6b368a5..d8d0bff 100644
7+ --- a/src/postgres/include/storage/s_lock.h
8+ +++ b/src/postgres/include/storage/s_lock.h
9+ @@ -748,6 +748,50 @@ tas(volatile slock_t *lock)
10+ typedef unsigned char slock_t;
11+ #endif
512
6- override PG_CONFIGURE_FLAGS += -q --without-readline --without-zlib
713+
8- + # Disable spinlocks when building with Emscripten
9- + ifdef EMSCRIPTEN
10- + override PG_CONFIGURE_FLAGS += --disable-spinlocks
11- + endif
12-
13- override TEST_CFLAGS += -I. -I./vendor -g
14- override TEST_LDFLAGS += -pthread
15- @@ -149,6 +154,9 @@ extract_source: $(PGDIR)
16- echo "#undef LOCALE_T_IN_XLOCALE" >> ./src/postgres/include/pg_config.h
17- echo "#undef WCSTOMBS_L_IN_XLOCALE" >> ./src/postgres/include/pg_config.h
18- # Support 32-bit systems without reconfiguring
19- + ifdef EMSCRIPTEN
20- + echo "#undef HAVE_SPINLOCKS" >> ./src/postgres/include/pg_config.h
21- + endif
22- echo "#undef PG_INT128_TYPE" >> ./src/postgres/include/pg_config.h
23- # Support gcc earlier than 4.6.0 without reconfiguring
24- echo "#undef HAVE__STATIC_ASSERT" >> ./src/postgres/include/pg_config.h
14+ + /*
15+ + * If we have no platform-specific knowledge, but we found that the compiler
16+ + * provides __sync_lock_test_and_set(), use that. Prefer the int-width
17+ + * version over the char-width version if we have both, on the rather dubious
18+ + * grounds that that's known to be more likely to work in the ARM ecosystem.
19+ + * (But we dealt with ARM above.)
20+ + */
21+ + #if !defined(HAS_TEST_AND_SET)
22+ +
23+ + #if defined(HAVE_GCC__SYNC_INT32_TAS)
24+ + #define HAS_TEST_AND_SET
25+ +
26+ + #define TAS(lock) tas(lock)
27+ +
28+ + typedef int slock_t;
29+ +
30+ + static __inline__ int
31+ + tas(volatile slock_t *lock)
32+ + {
33+ + return __sync_lock_test_and_set(lock, 1);
34+ + }
35+ +
36+ + #define S_UNLOCK(lock) __sync_lock_release(lock)
37+ +
38+ + #elif defined(HAVE_GCC__SYNC_CHAR_TAS)
39+ + #define HAS_TEST_AND_SET
40+ +
41+ + #define TAS(lock) tas(lock)
42+ +
43+ + typedef char slock_t;
44+ +
45+ + static __inline__ int
46+ + tas(volatile slock_t *lock)
47+ + {
48+ + return __sync_lock_test_and_set(lock, 1);
49+ + }
50+ +
51+ + #define S_UNLOCK(lock) __sync_lock_release(lock)
52+ +
53+ + #endif /* HAVE_GCC__SYNC_INT32_TAS */
54+ +
55+ + #endif /* !defined(HAS_TEST_AND_SET) */
56+ +
57+ /*
58+ * Default implementation of S_UNLOCK() for gcc/icc.
59+ *
0 commit comments