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