Coverage Report

Created: 2026-06-10 07:01

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