Coverage Report

Created: 2026-08-13 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsAtomics.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2026  Moddable Tech, Inc.
3
 *
4
 *   This file is part of the Moddable SDK Runtime.
5
 * 
6
 *   The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
 *   it under the terms of the GNU Lesser General Public License as published by
8
 *   the Free Software Foundation, either version 3 of the License, or
9
 *   (at your option) any later version.
10
 * 
11
 *   The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *   GNU Lesser General Public License for more details.
15
 * 
16
 *   You should have received a copy of the GNU Lesser General Public License
17
 *   along with the Moddable SDK Runtime.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 */
20
21
#include "xsAll.h"
22
23
static txInteger fxCheckAtomicsIndex(txMachine* the, txInteger index, txInteger length);
24
static txSlot* fxCheckAtomicsTypedArray(txMachine* the, txBoolean onlyInt32);
25
static txSlot* fxCheckAtomicsArrayBuffer(txMachine* the, txSlot* slot, txBoolean onlyShared, txBoolean mutable);
26
static void* fxCheckAtomicsArrayBufferDetached(txMachine* the, txSlot* slot, txBoolean mutable);
27
static txSlot* fxCheckSharedArrayBuffer(txMachine* the, txSlot* slot, txString which);
28
static void fxPushAtomicsValue(txMachine* the, int i, txID id);
29
30
#define mxAtomicsHead0(TYPE,TO) \
31
0
  TYPE result = 0; \
32
0
  txBoolean lock = host->kind == XS_HOST_KIND; \
33
0
  void* data = (lock) ? host->value.host.data : fxCheckAtomicsArrayBufferDetached(the, host, XS_IMMUTABLE); \
34
0
  TYPE* address = (TYPE*)(((txByte*)data) + offset)
35
36
#define mxAtomicsHead1(TYPE,TO) \
37
0
  TYPE result = 0; \
38
0
  TYPE value = (TYPE)TO(the, slot); \
39
0
  txBoolean lock = host->kind == XS_HOST_KIND; \
40
0
  void* data = (lock) ? host->value.host.data : fxCheckAtomicsArrayBufferDetached(the, host, XS_MUTABLE); \
41
0
  TYPE* address = (TYPE*)(((txByte*)data) + offset)
42
43
#define mxAtomicsHead2(TYPE,TO) \
44
0
  TYPE result = (TYPE)TO(the, slot + 1); \
45
0
  TYPE value = (TYPE)TO(the, slot); \
46
0
  txBoolean lock = host->kind == XS_HOST_KIND; \
47
0
  void* data = (lock) ? host->value.host.data : fxCheckAtomicsArrayBufferDetached(the, host, XS_MUTABLE); \
48
0
  TYPE* address = (TYPE*)(((txByte*)data) + offset)
49
50
#ifdef mxUseGCCAtomics
51
0
  #define mxAtomicsCompareExchange() __atomic_compare_exchange(address, &result, &value, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
52
0
  #define mxAtomicsLoad() __atomic_load(address, &result, __ATOMIC_SEQ_CST)
53
0
  #define mxAtomicsAdd() result = __atomic_fetch_add(address, value, __ATOMIC_SEQ_CST)
54
0
  #define mxAtomicsAnd() result = __atomic_fetch_and(address, value, __ATOMIC_SEQ_CST)
55
0
  #define mxAtomicsExchange() __atomic_exchange(address, &value, &result, __ATOMIC_SEQ_CST)
56
0
  #define mxAtomicsOr() result = __atomic_fetch_or(address, value, __ATOMIC_SEQ_CST)
57
0
  #define mxAtomicsStore() __atomic_store(address, &value, __ATOMIC_SEQ_CST)
58
0
  #define mxAtomicsSub() result = __atomic_fetch_sub(address, value, __ATOMIC_SEQ_CST)
59
0
  #define mxAtomicsXor() result = __atomic_fetch_xor(address, value, __ATOMIC_SEQ_CST)
60
#else
61
  #define mxAtomicsCompareExchange() if (lock) fxLockSharedChunk(data); if (*address == result) *address = value; else result = *address; if (lock) fxUnlockSharedChunk(data)
62
  #define mxAtomicsLoad() if (lock) fxLockSharedChunk(data); result = *address;  if (lock) fxUnlockSharedChunk(data)
63
  #define mxAtomicsAdd() if (lock) fxLockSharedChunk(data); result = *address; *address = result + value; if (lock) fxUnlockSharedChunk(data)
64
  #define mxAtomicsAnd() if (lock) fxLockSharedChunk(data); result = *address; *address = result & value; if (lock) fxUnlockSharedChunk(data)
65
  #define mxAtomicsExchange() if (lock) fxLockSharedChunk(data); result = *address; *address = value; if (lock) fxUnlockSharedChunk(data)
66
  #define mxAtomicsOr() if (lock) fxLockSharedChunk(data); result = *address; *address = result | value; if (lock) fxUnlockSharedChunk(data)
67
  #define mxAtomicsStore() if (lock) fxLockSharedChunk(data); *address = value; if (lock) fxUnlockSharedChunk(data)
68
  #define mxAtomicsSub() if (lock) fxLockSharedChunk(data); result = *address; *address = result - value; if (lock) fxUnlockSharedChunk(data)
69
  #define mxAtomicsXor() if (lock) fxLockSharedChunk(data); result = *address; *address = result ^ value; if (lock) fxUnlockSharedChunk(data)
70
#endif  
71
72
#define mxAtomicsTail() \
73
0
  slot->kind = XS_INTEGER_KIND; \
74
0
  slot->value.integer = result
75
76
#define mxAtomicsTailBigInt64() \
77
0
  fxFromBigInt64(the, slot, result)
78
79
#define mxAtomicsTailBigUint64() \
80
0
  fxFromBigUint64(the, slot, result)
81
  
82
#define mxAtomicsTailOverflow() \
83
0
  if (result <= 0x7FFFFFFF) { \
84
0
    slot->kind = XS_INTEGER_KIND; \
85
0
    slot->value.integer = result; \
86
0
  } \
87
0
  else { \
88
0
    slot->kind = XS_NUMBER_KIND; \
89
0
    slot->value.number = result; \
90
0
  }
91
  
92
#define mxAtomicsTailWait() \
93
0
  return (result != value) ? -1 : (timeout == 0) ? 0 : 1;
94
95
#define mxAtomicsDeclarations(onlyInt32, onlyShared, mutable) \
96
0
  txSlot* dispatch = fxCheckAtomicsTypedArray(the, onlyInt32); \
97
0
  txSlot* view = dispatch->next; \
98
0
  txSlot* buffer = view->next; \
99
0
  txSlot* host = fxCheckAtomicsArrayBuffer(the, buffer, onlyShared, mutable); \
100
0
  txU2 shift = dispatch->value.typedArray.dispatch->shift; \
101
0
  txInteger length = fxGetDataViewSize(the, view, buffer) >> shift; \
102
0
  txInteger index = fxCheckAtomicsIndex(the, 1, length); \
103
0
  txInteger offset = view->value.dataView.offset + (index << shift)
104
105
0
void fxInt8Add(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS1, fxToInteger); mxAtomicsAdd(); mxAtomicsTail(); }
106
0
void fxInt16Add(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS2, fxToInteger); mxAtomicsAdd(); mxAtomicsTail(); }
107
0
void fxInt32Add(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS4, fxToInteger); mxAtomicsAdd(); mxAtomicsTail(); }
108
0
void fxInt64Add(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS8, fxToBigInt64); mxAtomicsAdd(); mxAtomicsTailBigInt64(); }
109
0
void fxUint8Add(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU1, fxToUnsigned); mxAtomicsAdd(); mxAtomicsTail(); }
110
0
void fxUint16Add(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU2, fxToUnsigned); mxAtomicsAdd(); mxAtomicsTail(); }
111
0
void fxUint32Add(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU4, fxToUnsigned); mxAtomicsAdd(); mxAtomicsTailOverflow(); }
112
0
void fxUint64Add(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU8, fxToBigUint64); mxAtomicsAdd(); mxAtomicsTailBigUint64(); }
113
114
0
void fxInt8And(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS1, fxToInteger); mxAtomicsAnd(); mxAtomicsTail(); }
115
0
void fxInt16And(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS2, fxToInteger); mxAtomicsAnd(); mxAtomicsTail(); }
116
0
void fxInt32And(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS4, fxToInteger); mxAtomicsAnd(); mxAtomicsTail(); }
117
0
void fxInt64And(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS8, fxToBigInt64); mxAtomicsAnd(); mxAtomicsTailBigInt64(); }
118
0
void fxUint8And(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU1, fxToUnsigned); mxAtomicsAnd(); mxAtomicsTail(); }
119
0
void fxUint16And(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU2, fxToUnsigned); mxAtomicsAnd(); mxAtomicsTail(); }
120
0
void fxUint32And(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU4, fxToUnsigned); mxAtomicsAnd(); mxAtomicsTailOverflow(); }
121
0
void fxUint64And(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU8, fxToBigUint64); mxAtomicsAnd(); mxAtomicsTailBigUint64(); }
122
123
0
void fxInt8CompareExchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead2(txS1, fxToInteger); mxAtomicsCompareExchange(); mxAtomicsTail(); }
124
0
void fxInt16CompareExchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead2(txS2, fxToInteger); mxAtomicsCompareExchange(); mxAtomicsTail(); }
125
0
void fxInt32CompareExchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead2(txS4, fxToInteger); mxAtomicsCompareExchange(); mxAtomicsTail(); }
126
0
void fxInt64CompareExchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead2(txS8, fxToBigInt64); mxAtomicsCompareExchange(); mxAtomicsTailBigInt64(); }
127
0
void fxUint8CompareExchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead2(txU1, fxToUnsigned); mxAtomicsCompareExchange(); mxAtomicsTail(); }
128
0
void fxUint16CompareExchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead2(txU2, fxToUnsigned); mxAtomicsCompareExchange(); mxAtomicsTail(); }
129
0
void fxUint32CompareExchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead2(txU4, fxToUnsigned); mxAtomicsCompareExchange(); mxAtomicsTailOverflow(); }
130
0
void fxUint64CompareExchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead2(txU8, fxToBigUint64); mxAtomicsCompareExchange(); mxAtomicsTailBigUint64(); }
131
132
0
void fxInt8Exchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS1, fxToInteger); mxAtomicsExchange(); mxAtomicsTail(); }
133
0
void fxInt16Exchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS2, fxToInteger); mxAtomicsExchange(); mxAtomicsTail(); }
134
0
void fxInt32Exchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS4, fxToInteger); mxAtomicsExchange(); mxAtomicsTail(); }
135
0
void fxInt64Exchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS8, fxToBigInt64); mxAtomicsExchange(); mxAtomicsTailBigInt64(); }
136
0
void fxUint8Exchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU1, fxToUnsigned); mxAtomicsExchange(); mxAtomicsTail(); }
137
0
void fxUint16Exchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU2, fxToUnsigned); mxAtomicsExchange(); mxAtomicsTail(); }
138
0
void fxUint32Exchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU4, fxToUnsigned); mxAtomicsExchange(); mxAtomicsTailOverflow(); }
139
0
void fxUint64Exchange(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU8, fxToBigUint64); mxAtomicsExchange(); mxAtomicsTailBigUint64(); }
140
141
0
void fxInt8Load(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead0(txS1, fxToInteger); mxAtomicsLoad(); mxAtomicsTail(); }
142
0
void fxInt16Load(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead0(txS2, fxToInteger); mxAtomicsLoad(); mxAtomicsTail(); }
143
0
void fxInt32Load(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead0(txS4, fxToInteger); mxAtomicsLoad(); mxAtomicsTail(); }
144
0
void fxInt64Load(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead0(txS8, fxToBigInt64); mxAtomicsLoad(); mxAtomicsTailBigInt64(); }
145
0
void fxUint8Load(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead0(txU1, fxToUnsigned); mxAtomicsLoad(); mxAtomicsTail(); }
146
0
void fxUint16Load(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead0(txU2, fxToUnsigned); mxAtomicsLoad(); mxAtomicsTail(); }
147
0
void fxUint32Load(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead0(txU4, fxToUnsigned); mxAtomicsLoad(); mxAtomicsTailOverflow(); }
148
0
void fxUint64Load(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead0(txU8, fxToBigUint64); mxAtomicsLoad(); mxAtomicsTailBigUint64(); }
149
150
0
void fxInt8Or(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS1, fxToInteger); mxAtomicsOr(); mxAtomicsTail(); }
151
0
void fxInt16Or(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS2, fxToInteger); mxAtomicsOr(); mxAtomicsTail(); }
152
0
void fxInt32Or(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS4, fxToInteger); mxAtomicsOr(); mxAtomicsTail(); }
153
0
void fxInt64Or(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS8, fxToBigInt64); mxAtomicsOr(); mxAtomicsTailBigInt64(); }
154
0
void fxUint8Or(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU1, fxToUnsigned); mxAtomicsOr(); mxAtomicsTail(); }
155
0
void fxUint16Or(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU2, fxToUnsigned); mxAtomicsOr(); mxAtomicsTail(); }
156
0
void fxUint32Or(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU4, fxToUnsigned); mxAtomicsOr(); mxAtomicsTailOverflow(); }
157
0
void fxUint64Or(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU8, fxToBigUint64); mxAtomicsOr(); mxAtomicsTailBigUint64(); }
158
159
0
void fxInt8Store(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS1, fxToInteger); mxAtomicsStore(); mxAtomicsTail(); }
160
0
void fxInt16Store(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS2, fxToInteger); mxAtomicsStore(); mxAtomicsTail(); }
161
0
void fxInt32Store(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS4, fxToInteger); mxAtomicsStore(); mxAtomicsTail(); }
162
0
void fxInt64Store(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS8, fxToBigInt64); mxAtomicsStore(); mxAtomicsTailBigInt64(); }
163
0
void fxUint8Store(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU1, fxToUnsigned); mxAtomicsStore(); mxAtomicsTail(); }
164
0
void fxUint16Store(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU2, fxToUnsigned); mxAtomicsStore(); mxAtomicsTail(); }
165
0
void fxUint32Store(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU4, fxToUnsigned); mxAtomicsStore(); mxAtomicsTailOverflow(); }
166
0
void fxUint64Store(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU8, fxToBigUint64); mxAtomicsStore(); mxAtomicsTailBigUint64(); }
167
168
0
void fxInt8Sub(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS1, fxToInteger); mxAtomicsSub(); mxAtomicsTail(); }
169
0
void fxInt16Sub(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS2, fxToInteger); mxAtomicsSub(); mxAtomicsTail(); }
170
0
void fxInt32Sub(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS4, fxToInteger); mxAtomicsSub(); mxAtomicsTail(); }
171
0
void fxInt64Sub(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS8, fxToBigInt64); mxAtomicsSub(); mxAtomicsTailBigInt64(); }
172
0
void fxUint8Sub(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU1, fxToUnsigned); mxAtomicsSub(); mxAtomicsTail(); }
173
0
void fxUint16Sub(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU2, fxToUnsigned); mxAtomicsSub(); mxAtomicsTail(); }
174
0
void fxUint32Sub(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU4, fxToUnsigned); mxAtomicsSub(); mxAtomicsTailOverflow(); }
175
0
void fxUint64Sub(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU8, fxToBigUint64); mxAtomicsSub(); mxAtomicsTailBigUint64(); }
176
177
0
void fxInt8Xor(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS1, fxToInteger); mxAtomicsXor(); mxAtomicsTail(); }
178
0
void fxInt16Xor(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS2, fxToInteger); mxAtomicsXor(); mxAtomicsTail(); }
179
0
void fxInt32Xor(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS4, fxToInteger); mxAtomicsXor(); mxAtomicsTail(); }
180
0
void fxInt64Xor(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txS8, fxToBigInt64); mxAtomicsXor(); mxAtomicsTailBigInt64(); }
181
0
void fxUint8Xor(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU1, fxToUnsigned); mxAtomicsXor(); mxAtomicsTail(); }
182
0
void fxUint16Xor(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU2, fxToUnsigned); mxAtomicsXor(); mxAtomicsTail(); }
183
0
void fxUint32Xor(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU4, fxToUnsigned); mxAtomicsXor(); mxAtomicsTailOverflow(); }
184
0
void fxUint64Xor(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, int endian) { mxAtomicsHead1(txU8, fxToBigUint64); mxAtomicsXor(); mxAtomicsTailBigUint64(); }
185
186
0
txInteger fxInt32Wait(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, txNumber timeout) { mxAtomicsHead1(txS4, fxToInteger); mxAtomicsLoad(); mxAtomicsTailWait(); }
187
0
txInteger fxInt64Wait(txMachine* the, txSlot* host, txInteger offset, txSlot* slot, txNumber timeout) { mxAtomicsHead1(txS8, fxToBigInt64); mxAtomicsLoad(); mxAtomicsTailWait(); }
188
189
void fxBuildAtomics(txMachine* the)
190
6.53k
{
191
6.53k
  txSlot* slot;
192
  
193
6.53k
  mxPush(mxObjectPrototype);
194
6.53k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
195
6.53k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_SharedArrayBuffer_prototype_get_byteLength), C_NULL, mxID(_byteLength), XS_DONT_ENUM_FLAG);
196
6.53k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_SharedArrayBuffer_prototype_get_growable), C_NULL, mxID(_growable), XS_DONT_ENUM_FLAG);
197
6.53k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_SharedArrayBuffer_prototype_get_maxByteLength), C_NULL, mxID(_maxByteLength), XS_DONT_ENUM_FLAG);
198
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_SharedArrayBuffer_prototype_grow), 1, mxID(_grow), XS_DONT_ENUM_FLAG);
199
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_SharedArrayBuffer_prototype_slice), 2, mxID(_slice), XS_DONT_ENUM_FLAG);
200
6.53k
  slot = fxNextStringXProperty(the, slot, "SharedArrayBuffer", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
201
6.53k
  mxSharedArrayBufferPrototype = *the->stack;
202
6.53k
  slot = fxBuildHostConstructor(the, mxCallback(fx_SharedArrayBuffer), 1, mxID(_SharedArrayBuffer));
203
6.53k
  mxSharedArrayBufferConstructor = *the->stack;
204
6.53k
  slot = fxLastProperty(the, slot);
205
6.53k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_species_get), C_NULL, mxID(_Symbol_species), XS_DONT_ENUM_FLAG);
206
6.53k
  mxPop();
207
  
208
6.53k
  mxPush(mxObjectPrototype);
209
6.53k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
210
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_add), 3, mxID(_add), XS_DONT_ENUM_FLAG);
211
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_and), 3, mxID(_and), XS_DONT_ENUM_FLAG);
212
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_compareExchange), 4, mxID(_compareExchange), XS_DONT_ENUM_FLAG);
213
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_exchange), 3, mxID(_exchange), XS_DONT_ENUM_FLAG);
214
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_isLockFree), 1, mxID(_isLockFree), XS_DONT_ENUM_FLAG);
215
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_load), 2, mxID(_load), XS_DONT_ENUM_FLAG);
216
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_or), 3, mxID(_or), XS_DONT_ENUM_FLAG);
217
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_notify), 3, mxID(_notify), XS_DONT_ENUM_FLAG);
218
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_store), 3, mxID(_store), XS_DONT_ENUM_FLAG);
219
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_sub), 3, mxID(_sub), XS_DONT_ENUM_FLAG);
220
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_wait), 4, mxID(_wait), XS_DONT_ENUM_FLAG);
221
6.53k
#if mxECMAScript2024
222
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_waitAsync), 4, mxID(_waitAsync), XS_DONT_ENUM_FLAG);
223
6.53k
#endif
224
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_notify), 3, mxID(_wake), XS_DONT_ENUM_FLAG);
225
6.53k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Atomics_xor), 3, mxID(_xor), XS_DONT_ENUM_FLAG);
226
6.53k
  slot = fxNextStringXProperty(the, slot, "Atomics", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
227
6.53k
  mxPull(mxAtomicsObject);
228
6.53k
}
229
230
txSlot* fxCheckAtomicsArrayBuffer(txMachine* the, txSlot* slot, txBoolean onlyShared, txBoolean mutable)
231
0
{
232
0
  if ((!slot) || (!mxIsReference(slot)))
233
0
    mxTypeError("typedArray.buffer: not an object");
234
0
  slot = slot->value.reference->next;
235
0
  if (slot && (slot->kind == XS_HOST_KIND) && (slot->value.host.variant.destructor == fxReleaseSharedChunk))
236
0
    return slot;
237
0
  if (onlyShared)
238
0
    mxTypeError("typedArray.buffer: not a SharedArrayBuffer instance");
239
0
  if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_ARRAY_BUFFER_KIND)) {
240
0
    if (slot->value.arrayBuffer.address == C_NULL)
241
0
      mxTypeError("typedArray.buffer: detached");
242
0
    if (mutable && (slot->flag & XS_DONT_SET_FLAG))
243
0
      mxTypeError("typedArray.buffer: read-only");
244
0
    return slot;
245
0
  }
246
0
  mxTypeError("typedArray.buffer: not a SharedArrayBuffer instance, not an ArrayBuffer instance");
247
0
  return C_NULL;
248
0
}
249
250
void* fxCheckAtomicsArrayBufferDetached(txMachine* the, txSlot* slot, txBoolean mutable)
251
0
{
252
0
  if (slot->value.arrayBuffer.address == C_NULL)
253
0
    mxTypeError("typedArray.buffer: detached");
254
0
  if (mutable && (slot->flag & XS_DONT_SET_FLAG))
255
0
    mxTypeError("typedArray.buffer: read-only");
256
0
  return slot->value.arrayBuffer.address;
257
0
}
258
259
txInteger fxCheckAtomicsIndex(txMachine* the, txInteger i, txInteger length)
260
0
{
261
0
  txSlot *slot = (mxArgc > i) ? mxArgv(i) : C_NULL;
262
0
  if (slot && (XS_INTEGER_KIND == slot->kind)) {
263
0
    int index = slot->value.integer;
264
0
    if ((0 <= index) && (index < length))
265
0
      return index;
266
0
  }
267
268
0
  txNumber index = slot ? c_trunc(fxToNumber(the, slot)) : C_NAN; 
269
0
  if (c_isnan(index))
270
0
    index = 0;
271
0
  if (index < 0)
272
0
    mxRangeError("invalid index");
273
0
  else if (index >= length)
274
0
    mxRangeError("invalid index");
275
0
  return (txInteger)index;
276
0
}
277
278
txSlot* fxCheckAtomicsTypedArray(txMachine* the, txBoolean onlyInt32)
279
0
{
280
0
  txSlot* slot = (mxArgc > 0) ? mxArgv(0) : C_NULL;
281
0
  txID id;
282
0
  if ((!slot) || (!mxIsReference(slot)))
283
0
    mxTypeError("typedArray: not an object");
284
0
  slot = slot->value.reference->next;
285
0
  if ((!slot) || ((slot->kind != XS_TYPED_ARRAY_KIND)))
286
0
    mxTypeError("typedArray: not a TypedArray instance");
287
0
  id = slot->value.typedArray.dispatch->constructorID;
288
0
  if (onlyInt32) {
289
0
    if ((id != _Int32Array) && (id != _BigInt64Array))
290
0
      mxTypeError("typedArray: not an Int32Array instance");
291
0
  }
292
0
  else {
293
0
    if (id == _Float32Array)
294
0
      mxTypeError("typedArray: Float32Array instance");
295
0
    else if (id == _Float64Array)
296
0
      mxTypeError("typedArray: Float64Array instance");
297
0
    else if (id == _Uint8ClampedArray)
298
0
      mxTypeError("typedArray: Uint8ClampedArray instance");
299
0
  #if mxFloat16
300
0
    else if (id == _Float16Array)
301
0
      mxTypeError("typedArray: Float16Array instance");
302
0
  #endif
303
0
  }
304
0
  return slot;
305
0
}
306
307
txSlot* fxCheckSharedArrayBuffer(txMachine* the, txSlot* slot, txString which)
308
0
{
309
0
  if ((!slot) || (!mxIsReference(slot)))
310
0
    mxTypeError("%s: not an object", which);
311
0
  slot = slot->value.reference->next;
312
0
  if ((!slot) || (slot->kind != XS_HOST_KIND) || (slot->value.host.variant.destructor != fxReleaseSharedChunk))
313
0
    mxTypeError("%s: not a SharedArrayBuffer instance", which);
314
0
  return slot;
315
0
}
316
317
void fxPushAtomicsValue(txMachine* the, int i, txID id)
318
0
{
319
0
  txSlot* slot;
320
0
  if (mxArgc > i)
321
0
    mxPushSlot(mxArgv(i));
322
0
  else
323
0
    mxPushUndefined();
324
0
  slot = the->stack;
325
0
  if ((id == _BigInt64Array) || (id == _BigUint64Array))
326
0
    fxBigIntCoerce(the, slot);
327
0
  else if (XS_INTEGER_KIND != slot->kind) {
328
0
    txNumber value;
329
0
    fxNumberCoerce(the, slot);
330
0
    value = c_trunc(slot->value.number); 
331
0
    if (c_isnan(value) || (value == -0))
332
0
      value = 0;
333
0
    slot->value.number = value;
334
0
  }
335
0
}
336
337
338
void fx_SharedArrayBuffer(txMachine* the)
339
0
{
340
0
  txSlot* instance;
341
0
  txS8 byteLength;
342
0
  txS8 maxByteLength = -1;
343
0
  txSlot* property;
344
0
  if (!mxHasTarget)
345
0
    mxTypeError("call: SharedArrayBuffer");
346
0
  byteLength = fxArgToSafeByteLength(the, 0, 0);
347
0
  if ((mxArgc > 1) && mxIsReference(mxArgv(1))) {
348
0
    mxPushSlot(mxArgv(1));
349
0
    mxGetID(mxID(_maxByteLength));
350
0
    mxPullSlot(mxArgv(1));
351
0
    maxByteLength = fxArgToSafeByteLength(the, 1, -1);
352
0
  }
353
0
  if (maxByteLength >= 0) {
354
0
    if (byteLength > maxByteLength)
355
0
      mxRangeError("byteLength > maxByteLength");
356
0
  }
357
0
  mxPushSlot(mxTarget);
358
0
  fxGetPrototypeFromConstructor(the, &mxSharedArrayBufferPrototype);
359
0
  instance = fxNewSlot(the);
360
0
  instance->kind = XS_INSTANCE_KIND;
361
0
  instance->value.instance.garbage = C_NULL;
362
0
  instance->value.instance.prototype = the->stack->value.reference;
363
0
  the->stack->value.reference = instance;
364
0
  the->stack->kind = XS_REFERENCE_KIND;
365
0
  if (byteLength > 0x7FFFFFFF)
366
0
    mxRangeError("byteLength too big");
367
0
  if (maxByteLength > 0x7FFFFFFF)
368
0
    mxRangeError("maxByteLength too big");
369
0
  property = instance->next = fxNewSlot(the);
370
0
  property->flag = XS_INTERNAL_FLAG;
371
0
  property->kind = XS_HOST_KIND;
372
0
  property->value.host.data = fxCreateSharedChunk((txInteger)byteLength);
373
0
  if (!property->value.host.data) {
374
0
    property->value.host.variant.destructor = NULL;
375
0
    mxRangeError("cannot allocate SharedArrayBuffer instance");
376
0
  }
377
0
  property->value.host.variant.destructor = fxReleaseSharedChunk;
378
0
  property = property->next = fxNewSlot(the);
379
0
  property->flag = XS_INTERNAL_FLAG;
380
0
  property->kind = XS_BUFFER_INFO_KIND;
381
0
  property->value.bufferInfo.length = (txInteger)byteLength;
382
0
  property->value.bufferInfo.maxLength = (txInteger)maxByteLength;
383
0
  mxPullSlot(mxResult);
384
0
}
385
386
void fx_SharedArrayBuffer_prototype_get_byteLength(txMachine* the)
387
0
{
388
0
  txSlot* host = fxCheckSharedArrayBuffer(the, mxThis, "this");
389
0
  txSlot* bufferInfo = host->next; 
390
0
  mxResult->kind = XS_INTEGER_KIND;
391
0
  mxResult->value.integer = bufferInfo->value.bufferInfo.length;
392
0
}
393
394
void fx_SharedArrayBuffer_prototype_get_growable(txMachine* the)
395
0
{
396
0
  txSlot* host = fxCheckSharedArrayBuffer(the, mxThis, "this");
397
0
  txSlot* bufferInfo = host->next;
398
0
  mxResult->kind = XS_BOOLEAN_KIND;
399
0
  mxResult->value.boolean = (bufferInfo->value.bufferInfo.maxLength >= 0) ? 1 : 0;
400
0
}
401
402
void fx_SharedArrayBuffer_prototype_get_maxByteLength(txMachine* the)
403
0
{
404
0
  txSlot* host = fxCheckSharedArrayBuffer(the, mxThis, "this");
405
0
  txSlot* bufferInfo = host->next; 
406
0
  mxResult->kind = XS_INTEGER_KIND;
407
0
  if (bufferInfo->value.bufferInfo.maxLength >= 0)
408
0
    mxResult->value.integer = bufferInfo->value.bufferInfo.maxLength;
409
0
  else
410
0
    mxResult->value.integer = bufferInfo->value.bufferInfo.length;
411
0
}
412
413
void fx_SharedArrayBuffer_prototype_grow(txMachine* the)
414
0
{
415
0
  txSlot* host = fxCheckSharedArrayBuffer(the, mxThis, "this");
416
0
  txSlot* bufferInfo = host->next; 
417
0
  txInteger maxByteLength, oldByteLength, newByteLength;
418
0
  maxByteLength = bufferInfo->value.bufferInfo.maxLength;
419
0
  if (maxByteLength < 0)
420
0
    mxTypeError("this: not resizable");
421
0
  oldByteLength = bufferInfo->value.bufferInfo.length;
422
0
  newByteLength = fxArgToByteLength(the, 0, 0);
423
0
  if (newByteLength < oldByteLength)
424
0
    mxRangeError("newLength < byteLength");
425
0
  if (newByteLength > maxByteLength)
426
0
    mxRangeError("newLength > maxByteLength");
427
0
  mxRangeError("cannot grow SharedArrayBuffer instance");
428
0
}
429
430
void fx_SharedArrayBuffer_prototype_slice(txMachine* the)
431
0
{
432
0
  txSlot* host = fxCheckSharedArrayBuffer(the, mxThis, "this");
433
0
  txSlot* bufferInfo = host->next; 
434
0
  txInteger length = bufferInfo->value.bufferInfo.length;
435
0
  txInteger start = fxArgToIndexInteger(the, 0, 0, length);
436
0
  txInteger stop = fxArgToIndexInteger(the, 1, length, length);
437
0
  txSlot* result;
438
0
  if (stop < start) 
439
0
    stop = start;
440
0
  length = stop - start;
441
0
  mxPushSlot(mxThis);
442
0
  mxGetID(mxID(_constructor));
443
0
  fxToSpeciesConstructor(the, &mxSharedArrayBufferConstructor);
444
0
  mxNew();
445
0
  mxPushInteger(length);
446
0
  mxRunCount(1);
447
0
  mxPullSlot(mxResult);
448
0
  result = fxCheckSharedArrayBuffer(the, mxResult, "result");
449
0
  if (result == host)
450
0
    mxTypeError("result: same SharedArrayBuffer instance");
451
0
  bufferInfo = result->next; 
452
0
  if (bufferInfo->value.bufferInfo.length < length)
453
0
    mxTypeError("result: smaller SharedArrayBuffer instance");
454
0
  c_memcpy(result->value.host.data, ((txByte*)host->value.host.data + start), stop - start);
455
0
}
456
457
void fx_Atomics_add(txMachine* the)
458
0
{
459
0
  mxAtomicsDeclarations(0, 0, 1);
460
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
461
0
  (*dispatch->value.typedArray.atomics->add)(the, host, offset, the->stack, 0);
462
0
  mxPullSlot(mxResult);
463
0
}
464
465
void fx_Atomics_and(txMachine* the)
466
0
{
467
0
  mxAtomicsDeclarations(0, 0, 1);
468
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
469
0
  (*dispatch->value.typedArray.atomics->and)(the, host, offset, the->stack, 0);
470
0
  mxPullSlot(mxResult);
471
0
}
472
473
void fx_Atomics_compareExchange(txMachine* the)
474
0
{
475
0
  mxAtomicsDeclarations(0, 0, 1);
476
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
477
0
  fxPushAtomicsValue(the, 3, dispatch->value.typedArray.dispatch->constructorID);
478
0
  (*dispatch->value.typedArray.atomics->compareExchange)(the, host, offset, the->stack, 0);
479
0
  mxPullSlot(mxResult);
480
0
  mxPop();
481
0
}
482
483
void fx_Atomics_exchange(txMachine* the)
484
0
{
485
0
  mxAtomicsDeclarations(0, 0, 1);
486
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
487
0
  (*dispatch->value.typedArray.atomics->exchange)(the, host, offset, the->stack, 0);
488
0
  mxPullSlot(mxResult);
489
0
}
490
491
void fx_Atomics_isLockFree(txMachine* the)
492
0
{
493
0
  txInteger size = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
494
0
  mxResult->value.boolean = (size == 4) ? 1 : 0;
495
0
  mxResult->kind = XS_BOOLEAN_KIND;
496
0
}
497
498
void fx_Atomics_load(txMachine* the)
499
0
{
500
0
  mxAtomicsDeclarations(0, 0, 0);
501
0
  (*dispatch->value.typedArray.atomics->load)(the, host, offset, mxResult, 0);
502
0
}
503
504
void fx_Atomics_or(txMachine* the)
505
0
{
506
0
  mxAtomicsDeclarations(0, 0, 1);
507
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
508
0
  (*dispatch->value.typedArray.atomics->or)(the, host, offset, the->stack, 0);
509
0
  mxPullSlot(mxResult);
510
0
}
511
512
void fx_Atomics_notify(txMachine* the)
513
0
{
514
0
  mxAtomicsDeclarations(1, 0, 0);
515
0
  txInteger count = ((mxArgc > 2) && !mxIsUndefined(mxArgv(2))) ? fxToInteger(the, mxArgv(2)) : 20;
516
0
  if (count < 0)
517
0
    count = 0;
518
0
  if (host->kind == XS_ARRAY_BUFFER_KIND) {
519
0
    mxResult->value.integer = 0;
520
0
  }
521
0
  else {
522
0
    mxResult->value.integer = fxNotifySharedChunk(the, (txByte*)host->value.host.data + offset, count);
523
0
  }
524
0
  mxResult->kind = XS_INTEGER_KIND;
525
0
}
526
527
void fx_Atomics_store(txMachine* the)
528
0
{
529
0
  mxAtomicsDeclarations(0, 0, 1);
530
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
531
0
  *mxResult = *the->stack;
532
0
  (*dispatch->value.typedArray.atomics->store)(the, host, offset, the->stack, 0);
533
0
  mxPop();
534
0
}
535
536
void fx_Atomics_sub(txMachine* the)
537
0
{
538
0
  mxAtomicsDeclarations(0, 0, 1);
539
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
540
0
  (*dispatch->value.typedArray.atomics->sub)(the, host, offset, the->stack, 0);
541
0
  mxPullSlot(mxResult);
542
0
}
543
544
void fx_Atomics_wait(txMachine* the)
545
0
{
546
0
  mxAtomicsDeclarations(1, 1, 0);
547
0
  txNumber timeout;
548
0
  txInteger result;
549
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
550
0
  timeout = (mxArgc > 3) ? fxToNumber(the, mxArgv(3)) : C_NAN;
551
0
  if (c_isnan(timeout))
552
0
    timeout = C_INFINITY;
553
0
  else if (timeout < 0)
554
0
    timeout = 0;
555
0
  result = (*dispatch->value.typedArray.atomics->wait)(the, host, offset, the->stack, timeout);
556
0
  if (result < 0)
557
0
    mxPushStringX("not-equal");
558
0
  else {
559
0
    result = fxWaitSharedChunk(the, (txByte*)host->value.host.data + offset, timeout, C_NULL);
560
0
    if (result == 0)
561
0
      mxPushStringX("timed-out");
562
0
    else
563
0
      mxPushStringX("ok");
564
0
  }
565
0
  mxPullSlot(mxResult);
566
0
}
567
568
#if mxECMAScript2024
569
void fx_Atomics_waitAsync(txMachine* the)
570
0
{
571
0
  mxAtomicsDeclarations(1, 1, 0);
572
0
  txNumber timeout;
573
0
  txInteger result;
574
0
  txSlot* slot;
575
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
576
0
  timeout = (mxArgc > 3) ? fxToNumber(the, mxArgv(3)) : C_NAN;
577
0
  if (c_isnan(timeout))
578
0
    timeout = C_INFINITY;
579
0
  else if (timeout < 0)
580
0
    timeout = 0;
581
0
  result = (*dispatch->value.typedArray.atomics->wait)(the, host, offset, the->stack, timeout);
582
  
583
0
  mxPush(mxObjectPrototype);
584
0
  slot = fxLastProperty(the, fxNewObjectInstance(the));
585
0
  slot = fxNextBooleanProperty(the, slot, (result <= 0) ? 0 : 1, mxID(_async), XS_NO_FLAG);
586
0
  if (result < 0)
587
0
    fxNextStringXProperty(the, slot, "not-equal", mxID(_value), XS_NO_FLAG);
588
0
  else if (result == 0)
589
0
    fxNextStringXProperty(the, slot, "timed-out", mxID(_value), XS_NO_FLAG);
590
0
  else {
591
0
    txSlot* resolveFunction;
592
0
    txSlot* rejectFunction;
593
0
    mxTemporary(resolveFunction);
594
0
    mxTemporary(rejectFunction);
595
0
    mxPush(mxPromiseConstructor);
596
0
    fxNewPromiseCapability(the, resolveFunction, rejectFunction);
597
0
    fxNextSlotProperty(the, slot, the->stack, mxID(_value), XS_NO_FLAG);
598
0
    mxPop(); // promise
599
0
    fxWaitSharedChunk(the, (txByte*)host->value.host.data + offset, timeout, resolveFunction);
600
0
    mxPop(); // rejectFunction
601
0
    mxPop(); // resolveFunction
602
0
  }
603
0
  mxPullSlot(mxResult);
604
0
}
605
#endif
606
607
void fx_Atomics_xor(txMachine* the)
608
0
{
609
0
  mxAtomicsDeclarations(0, 0, 1);
610
0
  fxPushAtomicsValue(the, 2, dispatch->value.typedArray.dispatch->constructorID);
611
0
  (*dispatch->value.typedArray.atomics->xor)(the, host, offset, the->stack, 0);
612
0
  mxPullSlot(mxResult);
613
0
}
614
615
#ifdef mxUseDefaultSharedChunks
616
617
#if defined(mxUsePOSIXThreads)
618
  #define mxThreads 1
619
  typedef pthread_cond_t txCondition;
620
  typedef pthread_mutex_t txMutex;
621
  typedef pthread_t txThread;
622
0
  #define mxCreateCondition(CONDITION) pthread_cond_init(CONDITION,NULL)
623
6.53k
  #define mxCreateMutex(MUTEX) pthread_mutex_init(MUTEX,NULL)
624
6.53k
  #define mxCurrentThread() pthread_self()
625
0
  #define mxDeleteCondition(CONDITION) pthread_cond_destroy(CONDITION)
626
6.53k
  #define mxDeleteMutex(MUTEX) pthread_mutex_destroy(MUTEX)
627
6.53k
  #define mxLockMutex(MUTEX) pthread_mutex_lock(MUTEX)
628
6.53k
  #define mxUnlockMutex(MUTEX) pthread_mutex_unlock(MUTEX)
629
0
  #define mxWakeCondition(CONDITION) pthread_cond_signal(CONDITION)
630
#elif defined(mxUseFreeRTOSTasks)
631
  #define mxThreads 1
632
633
  #include "FreeRTOS.h"
634
#if ESP32
635
  #include "freertos/queue.h"
636
  #include "freertos/semphr.h"
637
#else
638
  #include "queue.h"
639
  #include "semphr.h"
640
#endif
641
  typedef TaskHandle_t txCondition;
642
  typedef struct {
643
#if nrf52
644
    SemaphoreHandle_t sem;
645
#else
646
    QueueHandle_t handle;
647
    StaticSemaphore_t buffer;
648
#endif
649
  } txMutex;
650
  typedef TaskHandle_t txThread;
651
  #define mxCreateCondition(CONDITION) *(CONDITION) = xTaskGetCurrentTaskHandle()
652
#if nrf52
653
  #define mxCreateMutex(MUTEX) (MUTEX)->sem = xSemaphoreCreateMutex()
654
#else
655
  #define mxCreateMutex(MUTEX) (MUTEX)->handle = xSemaphoreCreateMutexStatic(&((MUTEX)->buffer))
656
#endif
657
  #define mxCurrentThread() xTaskGetCurrentTaskHandle()
658
  #define mxDeleteCondition(CONDITION) *(CONDITION) = NULL
659
#if nrf52
660
  #define mxDeleteMutex(MUTEX) vSemaphoreDelete((MUTEX)->sem)
661
  #define mxLockMutex(MUTEX) xSemaphoreTake((MUTEX)->sem, portMAX_DELAY)
662
  #define mxUnlockMutex(MUTEX) xSemaphoreGive((MUTEX)->sem)
663
#else
664
  #define mxDeleteMutex(MUTEX) vSemaphoreDelete((MUTEX)->handle)
665
  #define mxLockMutex(MUTEX) xSemaphoreTake((MUTEX)->handle, portMAX_DELAY)
666
  #define mxUnlockMutex(MUTEX) xSemaphoreGive((MUTEX)->handle)
667
#endif
668
  #define mxWakeCondition(CONDITION) xTaskNotifyGive(*(CONDITION));
669
#elif mxWindows
670
  #define mxThreads 1
671
  typedef CONDITION_VARIABLE txCondition;
672
  typedef CRITICAL_SECTION txMutex;
673
  typedef DWORD txThread;
674
  #define mxCreateCondition(CONDITION) InitializeConditionVariable(CONDITION)
675
  #define mxCreateMutex(MUTEX) InitializeCriticalSection(MUTEX)
676
  #define mxCurrentThread() GetCurrentThreadId()
677
  #define mxDeleteCondition(CONDITION) (void)(CONDITION)
678
  #define mxDeleteMutex(MUTEX) DeleteCriticalSection(MUTEX)
679
  #define mxLockMutex(MUTEX) EnterCriticalSection(MUTEX)
680
  #define mxUnlockMutex(MUTEX) LeaveCriticalSection(MUTEX)
681
  #define mxWakeCondition(CONDITION) WakeConditionVariable(CONDITION)
682
#else
683
  #define mxThreads 0
684
  typedef void* txThread;
685
  #define mxCurrentThread() C_NULL
686
#endif
687
688
typedef struct sxSharedChunk txSharedChunk;
689
typedef struct sxSharedCluster txSharedCluster;
690
typedef struct sxSharedWaiter txSharedWaiter;
691
692
typedef void (*txTimerCallback)(void* timer, void *refcon, txInteger refconSize);
693
extern void fxRescheduleTimer(void* timer, txNumber timeout, txNumber interval);
694
extern void* fxScheduleTimer(txNumber timeout, txNumber interval, txTimerCallback callback, void* refcon, txInteger refconSize);
695
extern void fxUnscheduleTimer(void* timer);
696
697
struct sxSharedChunk {
698
#if mxThreads && !defined(mxUseGCCAtomics)
699
  txMutex mutex;
700
#endif
701
  txSize size;
702
  txSize usage;
703
};
704
705
struct sxSharedCluster {
706
  txThread mainThread;
707
  txSize usage;
708
#if mxThreads
709
  txSharedWaiter* first; 
710
  txMutex waiterMutex; 
711
#endif
712
};
713
714
struct sxSharedWaiter {
715
  txSharedWaiter* next;
716
  txMachine* the;
717
  void* data;
718
  void* condition;
719
  void* timer;
720
  txSlot resolve;
721
  txBoolean ok;
722
};
723
724
txSharedCluster* gxSharedCluster = C_NULL;
725
726
void fxInitializeSharedCluster(txMachine* the)
727
6.53k
{
728
6.53k
  if (gxSharedCluster) {
729
0
    gxSharedCluster->usage++;
730
0
  }
731
6.53k
  else {
732
6.53k
    gxSharedCluster = c_calloc(sizeof(txSharedCluster), 1);
733
6.53k
    if (gxSharedCluster) {
734
6.53k
      gxSharedCluster->mainThread = mxCurrentThread();
735
6.53k
      gxSharedCluster->usage++;
736
6.53k
    #if mxThreads
737
6.53k
      mxCreateMutex(&gxSharedCluster->waiterMutex);
738
6.53k
    #endif
739
6.53k
    #ifdef mxInitializeSharedTimers
740
6.53k
      mxInitializeSharedTimers();
741
6.53k
    #endif
742
6.53k
    }
743
6.53k
  }
744
6.53k
}
745
746
void fxTerminateSharedCluster(txMachine* the)
747
6.53k
{
748
6.53k
  if (gxSharedCluster) {
749
6.53k
  #ifdef mxUnscheduleSharedTimer
750
6.53k
    if (the) {
751
6.53k
      txSharedWaiter** address;
752
6.53k
      txSharedWaiter* waiter;
753
6.53k
      mxLockMutex(&gxSharedCluster->waiterMutex);
754
6.53k
      address = &(gxSharedCluster->first);
755
6.53k
      while ((waiter = *address)) {
756
0
        if (waiter->the == the) {
757
0
          *address = waiter->next;
758
0
          if (waiter->timer)
759
0
            mxUnscheduleSharedTimer(waiter->timer);
760
0
          c_free(waiter);         
761
0
        }
762
0
        else
763
0
          address = &(waiter->next);
764
0
      }
765
6.53k
      mxUnlockMutex(&gxSharedCluster->waiterMutex);
766
6.53k
    }
767
6.53k
  #endif
768
6.53k
    gxSharedCluster->usage--;
769
6.53k
    if (gxSharedCluster->usage == 0) {
770
6.53k
    #ifdef mxTerminateSharedTimers
771
6.53k
      mxTerminateSharedTimers();
772
6.53k
    #endif
773
6.53k
    #if mxThreads
774
6.53k
      mxDeleteMutex(&gxSharedCluster->waiterMutex);
775
6.53k
    #endif
776
6.53k
      c_free(gxSharedCluster);
777
6.53k
      gxSharedCluster = C_NULL;
778
6.53k
    }
779
6.53k
  }
780
6.53k
}
781
782
void* fxCreateSharedChunk(txInteger size)
783
0
{
784
0
  txSharedChunk* chunk = c_malloc(sizeof(txSharedChunk) + size);
785
0
  if (chunk) {
786
0
    void* data = (((txByte*)chunk) + sizeof(txSharedChunk));
787
  #if mxThreads && !defined(mxUseGCCAtomics)
788
    mxCreateMutex(&(chunk->mutex));
789
  #endif
790
0
    chunk->size = size;
791
0
    chunk->usage = 1;
792
0
    c_memset(data, 0, size);
793
0
    return data;
794
0
  }
795
0
  return C_NULL;
796
0
}
797
798
void fxLockSharedChunk(void* data)
799
0
{
800
#if mxThreads && !defined(mxUseGCCAtomics)
801
  txSharedChunk* chunk = (txSharedChunk*)(((txByte*)data) - sizeof(txSharedChunk));
802
    mxLockMutex(&(chunk->mutex));
803
#endif
804
0
}
805
806
txInteger fxMeasureSharedChunk(void* data)
807
0
{
808
0
  txSharedChunk* chunk = (txSharedChunk*)(((txByte*)data) - sizeof(txSharedChunk));
809
0
  return chunk->size;
810
0
}
811
812
txInteger fxNotifySharedChunk(txMachine* the, void* data, txInteger count)
813
0
{
814
0
  txInteger result = 0;
815
0
  if (gxSharedCluster) {
816
0
  #if mxThreads
817
0
    txSharedWaiter* first = C_NULL;
818
0
    txSharedWaiter* last = C_NULL;
819
0
    txSharedWaiter** address;
820
0
    txSharedWaiter* waiter;
821
0
    mxLockMutex(&gxSharedCluster->waiterMutex);
822
0
    address = &(gxSharedCluster->first);
823
0
    while ((waiter = *address)) {
824
0
      if (waiter->data == data) {
825
0
        if (count == 0)
826
0
          break;
827
0
        count--;
828
0
        if (first)
829
0
          last->next = waiter;
830
0
        else
831
0
          first = waiter;
832
0
        last = waiter;
833
0
        *address = waiter->next;
834
0
        waiter->next = C_NULL;
835
0
      }
836
0
      else
837
0
        address = &(waiter->next);
838
0
    }
839
0
    waiter = first;
840
0
    while (waiter) {
841
0
      waiter->data = C_NULL;
842
0
      if (waiter->condition) {
843
0
        mxWakeCondition((txCondition*)waiter->condition);
844
0
      }
845
0
      else {
846
0
        waiter->ok = 1;
847
0
      #ifdef mxRescheduleSharedTimer
848
0
        mxRescheduleSharedTimer(waiter->timer, 0, 0);
849
      #else
850
        fxAbort(the, XS_DEAD_STRIP_EXIT);
851
      #endif
852
0
      }
853
0
      result++;
854
0
      waiter = waiter->next;
855
0
    }
856
0
    mxUnlockMutex(&gxSharedCluster->waiterMutex);
857
0
  #endif  
858
0
  }
859
0
  return result;
860
0
}
861
862
void* fxRetainSharedChunk(void* data)
863
0
{
864
0
  txSharedChunk* chunk = (txSharedChunk*)(((txByte*)data) - sizeof(txSharedChunk));
865
0
  txS4 result = 0;
866
0
  txS4 value = 1;
867
0
  txS4* address = &(chunk->usage);
868
#ifndef mxUseGCCAtomics
869
  txBoolean lock = 1;
870
#endif
871
0
  mxAtomicsAdd();
872
0
  if (result == 0)
873
0
    return C_NULL;
874
0
  return data;
875
0
}
876
877
void fxReleaseSharedChunk(void* data)
878
0
{
879
0
  txSharedChunk* chunk = (txSharedChunk*)(((txByte*)data) - sizeof(txSharedChunk));
880
0
  txS4 result = 0;
881
0
  txS4 value = 1;
882
0
  txS4* address = &(chunk->usage);
883
#ifndef mxUseGCCAtomics
884
  txBoolean lock = 1;
885
#endif
886
0
  mxAtomicsSub();
887
0
  if (result == 1) {
888
0
    c_free(chunk);
889
0
  }
890
0
}
891
892
void fxUnlockSharedChunk(void* data)
893
0
{
894
#if mxThreads && !defined(mxUseGCCAtomics)
895
  txSharedChunk* chunk = (txSharedChunk*)(((txByte*)data) - sizeof(txSharedChunk));
896
    mxUnlockMutex(&(chunk->mutex));
897
#endif
898
0
}
899
900
#if mxThreads
901
void fxWaitSharedChunkCallback(void* timer, void* refcon, txInteger refconSize)
902
0
{
903
0
  txSharedWaiter** address = (txSharedWaiter**)refcon;
904
0
  txSharedWaiter* waiter = *address;
905
0
  txSharedWaiter* link;
906
0
  txMachine* the;
907
0
  mxLockMutex(&gxSharedCluster->waiterMutex);
908
0
  address = &(gxSharedCluster->first);
909
0
  while ((link = *address)) {
910
0
    if (link == waiter)
911
0
      *address = link->next;
912
0
    else
913
0
      address = &(link->next);
914
0
  }
915
0
  mxUnlockMutex(&gxSharedCluster->waiterMutex);
916
0
  the = waiter->the;
917
0
  fxBeginHost(waiter->the);
918
0
  mxTry(the) {
919
0
    mxPushUndefined();
920
0
    mxPush(waiter->resolve);
921
0
    mxCall();
922
0
    if (waiter->ok)
923
0
      mxPushStringX("ok");
924
0
    else
925
0
      mxPushStringX("timed-out");
926
0
    mxRunCount(1);
927
0
    mxPop();
928
0
  }
929
0
  mxCatch(the) {
930
0
  }
931
0
  fxForget(the, &waiter->resolve);
932
0
  fxEndHost(the);
933
0
  c_free(waiter);
934
0
}
935
#endif
936
937
txInteger fxWaitSharedChunk(txMachine* the, void* data, txNumber timeout, txSlot* resolveFunction)
938
0
{
939
0
  txInteger result = 1;
940
0
  if (gxSharedCluster) {
941
0
  #if mxThreads
942
0
    txSharedWaiter* waiter;
943
0
    txSharedWaiter** address;
944
0
    txSharedWaiter* link;
945
0
    waiter = c_calloc(1, sizeof(txSharedWaiter));
946
0
    if (!waiter)
947
0
      fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
948
0
    waiter->the = the;
949
0
    waiter->data = data;
950
0
    mxLockMutex(&gxSharedCluster->waiterMutex);
951
0
    address = &(gxSharedCluster->first);
952
0
    while ((link = *address))
953
0
      address = &(link->next);
954
0
    *address = waiter;
955
    
956
0
    if (resolveFunction) {
957
0
      mxUnlockMutex(&gxSharedCluster->waiterMutex);
958
0
      waiter->resolve = *resolveFunction;
959
0
      fxRemember(the, &waiter->resolve);
960
0
    #ifdef mxScheduleSharedTimer
961
0
      waiter->timer = mxScheduleSharedTimer(timeout, 0, (txSharedTimerCallback)fxWaitSharedChunkCallback, &waiter, sizeof(txSharedWaiter*));
962
0
      if (!waiter->timer)
963
0
        fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
964
    #else
965
      fxAbort(the, XS_DEAD_STRIP_EXIT);
966
    #endif
967
0
    }
968
0
    else if (gxSharedCluster->mainThread != mxCurrentThread()) {
969
0
      txCondition condition;
970
0
      mxCreateCondition(&condition);
971
0
      waiter->condition = &condition;
972
0
      if (timeout == C_INFINITY) {
973
0
      #if defined(mxUsePOSIXThreads)
974
0
        while (waiter->data == data)
975
0
          pthread_cond_wait(&condition, &gxSharedCluster->waiterMutex);
976
      #elif defined(mxUseFreeRTOSTasks)
977
        mxUnlockMutex(&gxSharedCluster->waiterMutex);
978
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
979
        mxLockMutex(&gxSharedCluster->waiterMutex);
980
      #else
981
        while (waiter->data == data)
982
          SleepConditionVariableCS(&condition, &gxSharedCluster->waiterMutex, INFINITE);
983
      #endif
984
0
      }
985
0
      else {
986
0
      #if defined(mxUsePOSIXThreads)
987
0
        struct timespec ts;
988
0
        timeout += fxDateNow();
989
0
        ts.tv_sec = c_floor(timeout / 1000);
990
0
        ts.tv_nsec = c_fmod(timeout, 1000) * 1000000;
991
0
        while (waiter->data == data) {
992
0
          result = (pthread_cond_timedwait(&condition, &gxSharedCluster->waiterMutex, &ts) == ETIMEDOUT) ? 0 : 1;
993
0
          if (!result)
994
0
            break;
995
0
        }
996
      #elif defined(mxUseFreeRTOSTasks)
997
        mxUnlockMutex(&gxSharedCluster->waiterMutex);
998
        ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(timeout));
999
        mxLockMutex(&gxSharedCluster->waiterMutex);
1000
        result = (waiter->data == data) ? 0 : 1;
1001
      #else
1002
        timeout += fxDateNow();
1003
        while (waiter->data == data) {
1004
          result = (SleepConditionVariableCS(&condition, &gxSharedCluster->waiterMutex, (DWORD)(timeout - fxDateNow()))) ? 1 : 0;
1005
          if (!result)
1006
            break;
1007
        }
1008
      #endif
1009
0
      }
1010
0
      address = &(gxSharedCluster->first);
1011
0
      while ((link = *address)) {
1012
0
        if (link == waiter)
1013
0
          *address = link->next;
1014
0
        else
1015
0
          address = &(link->next);
1016
0
      }
1017
0
      mxUnlockMutex(&gxSharedCluster->waiterMutex);
1018
0
      c_free(waiter);
1019
0
      mxDeleteCondition(&condition);
1020
0
    }
1021
0
    else {
1022
0
      mxTypeError("main thread cannot wait");
1023
0
    }
1024
0
  #endif  
1025
0
  }
1026
0
  else {
1027
0
    mxTypeError("no shared cluster");
1028
0
  }
1029
0
  return result;
1030
0
}
1031
1032
#endif /* mxUseDefaultSharedChunks */
1033
1034