Skip to content

Commit 7613b7f

Browse files
committed
Enable support for GCC compler v < 4.9
GCCv4.8 and lower doesn't ship with stdatomic implementation (even though they don't define __STD_NO_ATOMICS__ for c11). If the code is compiled with GCCv4.8 and older, we use builtin GCC atomic operations instead. The patch was initially proposed in quickjs's mailing group: https://www.freelists.org/post/quickjs-devel/PATCH-support-for-older-gcc-versions-whitespace-changes-excluded
1 parent a6e73ca commit 7613b7f

File tree

4 files changed

+97
-19
lines changed

4 files changed

+97
-19
lines changed

.github/workflows/ci.yml

+24
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ jobs:
5858
- name: test
5959
run: |
6060
make test
61+
linux-gcc48:
62+
runs-on: ubuntu-latest
63+
container:
64+
image: ubuntu:14.04
65+
steps:
66+
- name: checkout
67+
uses: actions/checkout@v3
68+
- name: install dependencies
69+
run: |
70+
apt update && apt -y install make gcc-4.8 wget
71+
wget https://github.com/Kitware/CMake/releases/download/v3.28.0-rc5/cmake-3.28.0-rc5-linux-x86_64.sh
72+
sh cmake-3.28.0-rc5-linux-x86_64.sh --skip-license --prefix=/usr
73+
- name: build
74+
run: |
75+
CC=gcc-4.8 make
76+
- name: stats
77+
run: |
78+
make stats
79+
- name: test
80+
run: |
81+
make test
82+
- name: test 262
83+
run: |
84+
time make test262
6185
linux-examples:
6286
runs-on: ubuntu-latest
6387
steps:

quickjs-c-atomics.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* QuickJS C atomics definitions
3+
*
4+
* Copyright (c) 2023 Marcin Kolny
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#if (defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__)
26+
// Use GCC builtins for version < 4.9
27+
# if((__GNUC__ << 16) + __GNUC_MINOR__ < ((4) << 16) + 9)
28+
# define GCC_BUILTIN_ATOMICS
29+
# endif
30+
#endif
31+
32+
#ifdef GCC_BUILTIN_ATOMICS
33+
#define atomic_fetch_add(obj, arg) \
34+
__atomic_fetch_add(obj, arg, __ATOMIC_SEQ_CST)
35+
#define atomic_compare_exchange_strong(obj, expected, desired) \
36+
__atomic_compare_exchange_n(obj, expected, desired, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
37+
#define atomic_exchange(obj, desired) \
38+
__atomic_exchange_n (obj, desired, __ATOMIC_SEQ_CST)
39+
#define atomic_load(obj) \
40+
__atomic_load_n(obj, __ATOMIC_SEQ_CST)
41+
#define atomic_store(obj, desired) \
42+
__atomic_store_n(obj, desired, __ATOMIC_SEQ_CST)
43+
#define atomic_fetch_or(obj, arg) \
44+
__atomic_fetch_or(obj, arg, __ATOMIC_SEQ_CST)
45+
#define atomic_fetch_xor(obj, arg) \
46+
__atomic_fetch_xor(obj, arg, __ATOMIC_SEQ_CST)
47+
#define atomic_fetch_and(obj, arg) \
48+
__atomic_fetch_and(obj, arg, __ATOMIC_SEQ_CST)
49+
#define atomic_fetch_sub(obj, arg) \
50+
__atomic_fetch_sub(obj, arg, __ATOMIC_SEQ_CST)
51+
#define _Atomic
52+
#else
53+
#include <stdatomic.h>
54+
#endif

quickjs-libc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ typedef sig_t sighandler_t;
6464

6565
#ifdef USE_WORKER
6666
#include <pthread.h>
67-
#include <stdatomic.h>
67+
#include "quickjs-c-atomics.h"
6868
#endif
6969

7070
#include "cutils.h"
@@ -3142,7 +3142,7 @@ static JSContext *(*js_worker_new_context_func)(JSRuntime *rt);
31423142

31433143
static int atomic_add_int(int *ptr, int v)
31443144
{
3145-
return atomic_fetch_add((_Atomic(uint32_t) *)ptr, v) + v;
3145+
return atomic_fetch_add((_Atomic uint32_t*)ptr, v) + v;
31463146
}
31473147

31483148
/* shared array buffer allocator */

quickjs.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103

104104
#ifdef CONFIG_ATOMICS
105105
#include <pthread.h>
106-
#include <stdatomic.h>
106+
#include "quickjs-c-atomics.h"
107107
#include <errno.h>
108108
#endif
109109

@@ -50106,16 +50106,16 @@ static JSValue js_atomics_op(JSContext *ctx,
5010650106

5010750107
#define OP(op_name, func_name) \
5010850108
case ATOMICS_OP_ ## op_name | (0 << 3): \
50109-
a = func_name((_Atomic(uint8_t) *)ptr, v); \
50109+
a = func_name((_Atomic uint8_t *)ptr, v); \
5011050110
break; \
5011150111
case ATOMICS_OP_ ## op_name | (1 << 3): \
50112-
a = func_name((_Atomic(uint16_t) *)ptr, v); \
50112+
a = func_name((_Atomic uint16_t *)ptr, v); \
5011350113
break; \
5011450114
case ATOMICS_OP_ ## op_name | (2 << 3): \
50115-
a = func_name((_Atomic(uint32_t) *)ptr, v); \
50115+
a = func_name((_Atomic uint32_t *)ptr, v); \
5011650116
break; \
5011750117
case ATOMICS_OP_ ## op_name | (3 << 3): \
50118-
a = func_name((_Atomic(uint64_t) *)ptr, v); \
50118+
a = func_name((_Atomic uint64_t *)ptr, v); \
5011950119
break;
5012050120
OP(ADD, atomic_fetch_add)
5012150121
OP(AND, atomic_fetch_and)
@@ -50126,42 +50126,42 @@ static JSValue js_atomics_op(JSContext *ctx,
5012650126
#undef OP
5012750127

5012850128
case ATOMICS_OP_LOAD | (0 << 3):
50129-
a = atomic_load((_Atomic(uint8_t) *)ptr);
50129+
a = atomic_load((_Atomic uint8_t *)ptr);
5013050130
break;
5013150131
case ATOMICS_OP_LOAD | (1 << 3):
50132-
a = atomic_load((_Atomic(uint16_t) *)ptr);
50132+
a = atomic_load((_Atomic uint16_t *)ptr);
5013350133
break;
5013450134
case ATOMICS_OP_LOAD | (2 << 3):
50135-
a = atomic_load((_Atomic(uint32_t) *)ptr);
50135+
a = atomic_load((_Atomic uint32_t *)ptr);
5013650136
break;
5013750137
case ATOMICS_OP_LOAD | (3 << 3):
50138-
a = atomic_load((_Atomic(uint64_t) *)ptr);
50138+
a = atomic_load((_Atomic uint64_t *)ptr);
5013950139
break;
5014050140
case ATOMICS_OP_COMPARE_EXCHANGE | (0 << 3):
5014150141
{
5014250142
uint8_t v1 = v;
50143-
atomic_compare_exchange_strong((_Atomic(uint8_t) *)ptr, &v1, rep_val);
50143+
atomic_compare_exchange_strong((_Atomic uint8_t *)ptr, &v1, rep_val);
5014450144
a = v1;
5014550145
}
5014650146
break;
5014750147
case ATOMICS_OP_COMPARE_EXCHANGE | (1 << 3):
5014850148
{
5014950149
uint16_t v1 = v;
50150-
atomic_compare_exchange_strong((_Atomic(uint16_t) *)ptr, &v1, rep_val);
50150+
atomic_compare_exchange_strong((_Atomic uint16_t *)ptr, &v1, rep_val);
5015150151
a = v1;
5015250152
}
5015350153
break;
5015450154
case ATOMICS_OP_COMPARE_EXCHANGE | (2 << 3):
5015550155
{
5015650156
uint32_t v1 = v;
50157-
atomic_compare_exchange_strong((_Atomic(uint32_t) *)ptr, &v1, rep_val);
50157+
atomic_compare_exchange_strong((_Atomic uint32_t *)ptr, &v1, rep_val);
5015850158
a = v1;
5015950159
}
5016050160
break;
5016150161
case ATOMICS_OP_COMPARE_EXCHANGE | (3 << 3):
5016250162
{
5016350163
uint64_t v1 = v;
50164-
atomic_compare_exchange_strong((_Atomic(uint64_t) *)ptr, &v1, rep_val);
50164+
atomic_compare_exchange_strong((_Atomic uint64_t *)ptr, &v1, rep_val);
5016550165
a = v1;
5016650166
}
5016750167
break;
@@ -50225,7 +50225,7 @@ static JSValue js_atomics_store(JSContext *ctx,
5022550225
}
5022650226
if (abuf->detached)
5022750227
return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
50228-
atomic_store((_Atomic(uint64_t) *)ptr, v64);
50228+
atomic_store((_Atomic uint64_t *)ptr, v64);
5022950229
} else {
5023050230
uint32_t v;
5023150231
/* XXX: spec, would be simpler to return the written value */
@@ -50240,13 +50240,13 @@ static JSValue js_atomics_store(JSContext *ctx,
5024050240
return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
5024150241
switch(size_log2) {
5024250242
case 0:
50243-
atomic_store((_Atomic(uint8_t) *)ptr, v);
50243+
atomic_store((_Atomic uint8_t *)ptr, v);
5024450244
break;
5024550245
case 1:
50246-
atomic_store((_Atomic(uint16_t) *)ptr, v);
50246+
atomic_store((_Atomic uint16_t *)ptr, v);
5024750247
break;
5024850248
case 2:
50249-
atomic_store((_Atomic(uint32_t) *)ptr, v);
50249+
atomic_store((_Atomic uint32_t *)ptr, v);
5025050250
break;
5025150251
default:
5025250252
abort();

0 commit comments

Comments
 (0)