Skip to content

Commit 6997445

Browse files
loganeksaghul
authored andcommitted
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 6b78c7f commit 6997445

File tree

4 files changed

+103
-19
lines changed

4 files changed

+103
-19
lines changed

.github/workflows/ci.yml

+30
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,36 @@ 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: install dependencies
67+
run: |
68+
apt update && apt -y install make gcc-4.8 wget time software-properties-common
69+
# git in default ppa repository is too old to run submodule checkout
70+
add-apt-repository -y ppa:git-core/ppa
71+
apt update
72+
apt install -y git
73+
wget https://github.com/Kitware/CMake/releases/download/v3.28.0-rc5/cmake-3.28.0-rc5-linux-x86_64.sh
74+
sh cmake-3.28.0-rc5-linux-x86_64.sh --skip-license --prefix=/usr
75+
- name: checkout
76+
uses: actions/checkout@v3
77+
with:
78+
submodules: true
79+
- name: build
80+
run: |
81+
CC=gcc-4.8 make
82+
- name: stats
83+
run: |
84+
make stats
85+
- name: test
86+
run: |
87+
make test
88+
- name: test 262
89+
run: |
90+
time make test262
6191
linux-examples:
6292
runs-on: ubuntu-latest
6393
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

@@ -50454,16 +50454,16 @@ static JSValue js_atomics_op(JSContext *ctx,
5045450454

5045550455
#define OP(op_name, func_name) \
5045650456
case ATOMICS_OP_ ## op_name | (0 << 3): \
50457-
a = func_name((_Atomic(uint8_t) *)ptr, v); \
50457+
a = func_name((_Atomic uint8_t *)ptr, v); \
5045850458
break; \
5045950459
case ATOMICS_OP_ ## op_name | (1 << 3): \
50460-
a = func_name((_Atomic(uint16_t) *)ptr, v); \
50460+
a = func_name((_Atomic uint16_t *)ptr, v); \
5046150461
break; \
5046250462
case ATOMICS_OP_ ## op_name | (2 << 3): \
50463-
a = func_name((_Atomic(uint32_t) *)ptr, v); \
50463+
a = func_name((_Atomic uint32_t *)ptr, v); \
5046450464
break; \
5046550465
case ATOMICS_OP_ ## op_name | (3 << 3): \
50466-
a = func_name((_Atomic(uint64_t) *)ptr, v); \
50466+
a = func_name((_Atomic uint64_t *)ptr, v); \
5046750467
break;
5046850468
OP(ADD, atomic_fetch_add)
5046950469
OP(AND, atomic_fetch_and)
@@ -50474,42 +50474,42 @@ static JSValue js_atomics_op(JSContext *ctx,
5047450474
#undef OP
5047550475

5047650476
case ATOMICS_OP_LOAD | (0 << 3):
50477-
a = atomic_load((_Atomic(uint8_t) *)ptr);
50477+
a = atomic_load((_Atomic uint8_t *)ptr);
5047850478
break;
5047950479
case ATOMICS_OP_LOAD | (1 << 3):
50480-
a = atomic_load((_Atomic(uint16_t) *)ptr);
50480+
a = atomic_load((_Atomic uint16_t *)ptr);
5048150481
break;
5048250482
case ATOMICS_OP_LOAD | (2 << 3):
50483-
a = atomic_load((_Atomic(uint32_t) *)ptr);
50483+
a = atomic_load((_Atomic uint32_t *)ptr);
5048450484
break;
5048550485
case ATOMICS_OP_LOAD | (3 << 3):
50486-
a = atomic_load((_Atomic(uint64_t) *)ptr);
50486+
a = atomic_load((_Atomic uint64_t *)ptr);
5048750487
break;
5048850488
case ATOMICS_OP_COMPARE_EXCHANGE | (0 << 3):
5048950489
{
5049050490
uint8_t v1 = v;
50491-
atomic_compare_exchange_strong((_Atomic(uint8_t) *)ptr, &v1, rep_val);
50491+
atomic_compare_exchange_strong((_Atomic uint8_t *)ptr, &v1, rep_val);
5049250492
a = v1;
5049350493
}
5049450494
break;
5049550495
case ATOMICS_OP_COMPARE_EXCHANGE | (1 << 3):
5049650496
{
5049750497
uint16_t v1 = v;
50498-
atomic_compare_exchange_strong((_Atomic(uint16_t) *)ptr, &v1, rep_val);
50498+
atomic_compare_exchange_strong((_Atomic uint16_t *)ptr, &v1, rep_val);
5049950499
a = v1;
5050050500
}
5050150501
break;
5050250502
case ATOMICS_OP_COMPARE_EXCHANGE | (2 << 3):
5050350503
{
5050450504
uint32_t v1 = v;
50505-
atomic_compare_exchange_strong((_Atomic(uint32_t) *)ptr, &v1, rep_val);
50505+
atomic_compare_exchange_strong((_Atomic uint32_t *)ptr, &v1, rep_val);
5050650506
a = v1;
5050750507
}
5050850508
break;
5050950509
case ATOMICS_OP_COMPARE_EXCHANGE | (3 << 3):
5051050510
{
5051150511
uint64_t v1 = v;
50512-
atomic_compare_exchange_strong((_Atomic(uint64_t) *)ptr, &v1, rep_val);
50512+
atomic_compare_exchange_strong((_Atomic uint64_t *)ptr, &v1, rep_val);
5051350513
a = v1;
5051450514
}
5051550515
break;
@@ -50573,7 +50573,7 @@ static JSValue js_atomics_store(JSContext *ctx,
5057350573
}
5057450574
if (abuf->detached)
5057550575
return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
50576-
atomic_store((_Atomic(uint64_t) *)ptr, v64);
50576+
atomic_store((_Atomic uint64_t *)ptr, v64);
5057750577
} else {
5057850578
uint32_t v;
5057950579
/* XXX: spec, would be simpler to return the written value */
@@ -50588,13 +50588,13 @@ static JSValue js_atomics_store(JSContext *ctx,
5058850588
return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
5058950589
switch(size_log2) {
5059050590
case 0:
50591-
atomic_store((_Atomic(uint8_t) *)ptr, v);
50591+
atomic_store((_Atomic uint8_t *)ptr, v);
5059250592
break;
5059350593
case 1:
50594-
atomic_store((_Atomic(uint16_t) *)ptr, v);
50594+
atomic_store((_Atomic uint16_t *)ptr, v);
5059550595
break;
5059650596
case 2:
50597-
atomic_store((_Atomic(uint32_t) *)ptr, v);
50597+
atomic_store((_Atomic uint32_t *)ptr, v);
5059850598
break;
5059950599
default:
5060050600
abort();

0 commit comments

Comments
 (0)