Debian Codegraph @

intl/icu-patches/bug-1706949-wasi-workaround.diff

raw
1 # Handle WASI lack of support for <thread> and <atomic>.
2 #
3 # WASI issue: https://github.com/WebAssembly/wasi-sdk/issues/180
4
5 diff --git a/intl/icu/source/common/putilimp.h b/intl/icu/source/common/putilimp.h
6 index 5b95a68..7097232 100644
7 --- a/intl/icu/source/common/putilimp.h
8 +++ b/intl/icu/source/common/putilimp.h
9 @@ -105,10 +105,12 @@ typedef size_t uintptr_t;
10 #endif
11 #elif U_PLATFORM == U_PF_OS400
12 /* not defined */
13 #elif U_PLATFORM == U_PF_HAIKU
14 /* not defined */
15 +#elif defined(__wasi__)
16 + /* not defined */
17 #else
18 # define U_TZSET tzset
19 #endif
20
21 #if defined(U_TIMEZONE) || defined(U_HAVE_TIMEZONE)
22 @@ -130,10 +132,12 @@ typedef size_t uintptr_t;
23 /* not defined */
24 #elif U_PLATFORM == U_PF_OS400
25 /* not defined */
26 #elif U_PLATFORM == U_PF_IPHONE
27 /* not defined */
28 +#elif defined(__wasi__)
29 + /* not defined */
30 #else
31 # define U_TIMEZONE timezone
32 #endif
33
34 #if defined(U_TZNAME) || defined(U_HAVE_TZNAME)
35 @@ -145,10 +149,12 @@ typedef size_t uintptr_t;
36 #endif
37 #elif U_PLATFORM == U_PF_OS400
38 /* not defined */
39 #elif U_PLATFORM == U_PF_HAIKU
40 /* not defined, (well it is but a loop back to icu) */
41 +#elif defined(__wasi__)
42 + /* not defined */
43 #else
44 # define U_TZNAME tzname
45 #endif
46
47 #ifdef U_HAVE_MMAP
48 diff --git a/intl/icu/source/common/umapfile.h b/intl/icu/source/common/umapfile.h
49 index 92bd567..4ed1112 100644
50 --- a/intl/icu/source/common/umapfile.h
51 +++ b/intl/icu/source/common/umapfile.h
52 @@ -38,10 +38,12 @@ U_CFUNC void uprv_unmapFile(UDataMemory *pData);
53 #define MAP_POSIX 2
54 #define MAP_STDIO 3
55
56 #if UCONFIG_NO_FILE_IO
57 # define MAP_IMPLEMENTATION MAP_NONE
58 +#elif defined(__wasi__)
59 +# define MAP_IMPLEMENTATION MAP_STDIO
60 #elif U_PLATFORM_USES_ONLY_WIN32_API
61 # define MAP_IMPLEMENTATION MAP_WIN32
62 #elif U_HAVE_MMAP || U_PLATFORM == U_PF_OS390
63 # define MAP_IMPLEMENTATION MAP_POSIX
64 #else /* unknown platform, no memory map implementation: use stdio.h and uprv_malloc() instead */
65 diff --git a/intl/icu/source/common/umutex.cpp b/intl/icu/source/common/umutex.cpp
66 index ccbee99..6c3452c 100644
67 --- a/intl/icu/source/common/umutex.cpp
68 +++ b/intl/icu/source/common/umutex.cpp
69 @@ -43,6 +43,7 @@ U_NAMESPACE_BEGIN
70 *
71 *************************************************************************************************/
72
73 +#ifndef __wasi__
74 namespace {
75 std::mutex *initMutex;
76 std::condition_variable *initCondition;
77 @@ -55,9 +56,11 @@ std::once_flag initFlag;
78 std::once_flag *pInitFlag = &initFlag;
79
80 } // Anonymous namespace
81 +#endif
82
83 U_CDECL_BEGIN
84 static UBool U_CALLCONV umtx_cleanup() {
85 +#ifndef __wasi__
86 initMutex->~mutex();
87 initCondition->~condition_variable();
88 UMutex::cleanup();
89 @@ -66,17 +69,21 @@ static UBool U_CALLCONV umtx_cleanup() {
90 // Do not use this trick anywhere else in ICU; use umtx_initOnce, not std::call_once().
91 pInitFlag->~once_flag();
92 pInitFlag = new(&initFlag) std::once_flag();
93 +#endif
94 return true;
95 }
96
97 static void U_CALLCONV umtx_init() {
98 +#ifndef __wasi__
99 initMutex = STATIC_NEW(std::mutex);
100 initCondition = STATIC_NEW(std::condition_variable);
101 ucln_common_registerCleanup(UCLN_COMMON_MUTEX, umtx_cleanup);
102 +#endif
103 }
104 U_CDECL_END
105
106
107 +#ifndef __wasi__
108 std::mutex *UMutex::getMutex() {
109 std::mutex *retPtr = fMutex.load(std::memory_order_acquire);
110 if (retPtr == nullptr) {
111 @@ -93,14 +100,17 @@ std::mutex *UMutex::getMutex() {
112 U_ASSERT(retPtr != nullptr);
113 return retPtr;
114 }
115 +#endif
116
117 UMutex *UMutex::gListHead = nullptr;
118
119 void UMutex::cleanup() {
120 UMutex *next = nullptr;
121 for (UMutex *m = gListHead; m != nullptr; m = next) {
122 +#ifndef __wasi__
123 (*m->fMutex).~mutex();
124 m->fMutex = nullptr;
125 +#endif
126 next = m->fListLink;
127 m->fListLink = nullptr;
128 }
129 @@ -110,20 +120,24 @@ void UMutex::cleanup() {
130
131 U_CAPI void U_EXPORT2
132 umtx_lock(UMutex *mutex) {
133 +#ifndef __wasi__
134 if (mutex == nullptr) {
135 mutex = &globalMutex;
136 }
137 mutex->lock();
138 +#endif
139 }
140
141
142 U_CAPI void U_EXPORT2
143 umtx_unlock(UMutex* mutex)
144 {
145 +#ifndef __wasi__
146 if (mutex == nullptr) {
147 mutex = &globalMutex;
148 }
149 mutex->unlock();
150 +#endif
151 }
152
153
154 @@ -143,18 +157,22 @@ umtx_unlock(UMutex* mutex)
155 //
156 U_COMMON_API UBool U_EXPORT2
157 umtx_initImplPreInit(UInitOnce &uio) {
158 +#ifndef __wasi__
159 std::call_once(*pInitFlag, umtx_init);
160 std::unique_lock<std::mutex> lock(*initMutex);
161 +#endif
162 if (umtx_loadAcquire(uio.fState) == 0) {
163 umtx_storeRelease(uio.fState, 1);
164 return true; // Caller will next call the init function.
165 } else {
166 +#ifndef __wasi__
167 while (umtx_loadAcquire(uio.fState) == 1) {
168 // Another thread is currently running the initialization.
169 // Wait until it completes.
170 initCondition->wait(lock);
171 }
172 U_ASSERT(uio.fState == 2);
173 +#endif
174 return false;
175 }
176 }
177 @@ -168,11 +186,13 @@ umtx_initImplPreInit(UInitOnce &uio) {
178
179 U_COMMON_API void U_EXPORT2
180 umtx_initImplPostInit(UInitOnce &uio) {
181 +#ifndef __wasi__
182 {
183 std::unique_lock<std::mutex> lock(*initMutex);
184 umtx_storeRelease(uio.fState, 2);
185 }
186 initCondition->notify_all();
187 +#endif
188 }
189
190 U_NAMESPACE_END
191 diff --git a/intl/icu/source/common/umutex.h b/intl/icu/source/common/umutex.h
192 index 8d76b3f..c1a58db 100644
193 --- a/intl/icu/source/common/umutex.h
194 +++ b/intl/icu/source/common/umutex.h
195 @@ -20,9 +20,12 @@
196 #ifndef UMUTEX_H
197 #define UMUTEX_H
198
199 +#ifndef __wasi__
200 #include <atomic>
201 #include <condition_variable>
202 #include <mutex>
203 +#endif
204 +
205 #include <type_traits>
206
207 #include "unicode/utypes.h"
208 @@ -37,6 +40,8 @@
209 #error U_USER_ATOMICS and U_USER_MUTEX_H are not supported
210 #endif
211
212 +#ifndef __wasi__
213 +
214 // Export an explicit template instantiation of std::atomic<int32_t>.
215 // When building DLLs for Windows this is required as it is used as a data member of the exported SharedObject class.
216 // See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.
217 @@ -61,6 +66,7 @@ template struct std::atomic<std::mutex *>;
218 #endif
219 #endif
220
221 +#endif
222
223 U_NAMESPACE_BEGIN
224
225 @@ -68,10 +68,12 @@ U_NAMESPACE_BEGIN
226 *
227 * Low Level Atomic Operations, ICU wrappers for.
228 *
229 ****************************************************************************/
230
231 +#ifndef __wasi__
232 +
233 typedef std::atomic<int32_t> u_atomic_int32_t;
234
235 inline int32_t umtx_loadAcquire(u_atomic_int32_t &var) {
236 return var.load(std::memory_order_acquire);
237 }
238 @@ -86,10 +88,31 @@ inline int32_t umtx_atomic_inc(u_atomic_int32_t *var) {
239
240 inline int32_t umtx_atomic_dec(u_atomic_int32_t *var) {
241 return var->fetch_sub(1) - 1;
242 }
243
244 +#else
245 +
246 +typedef int32_t u_atomic_int32_t;
247 +
248 +inline int32_t umtx_loadAcquire(u_atomic_int32_t &var) {
249 + return var;
250 +}
251 +
252 +inline void umtx_storeRelease(u_atomic_int32_t &var, int32_t val) {
253 + var = val;
254 +}
255 +
256 +inline int32_t umtx_atomic_inc(u_atomic_int32_t *var) {
257 + return ++(*var);
258 +}
259 +
260 +inline int32_t umtx_atomic_dec(u_atomic_int32_t *var) {
261 + return --(*var);
262 +}
263 +
264 +#endif
265
266 /*************************************************************************************************
267 *
268 * UInitOnce Definitions.
269 *
270 @@ -225,21 +248,29 @@ class U_COMMON_API UMutex {
271 UMutex &operator =(const UMutex &other) = delete;
272 void *operator new(size_t) = delete;
273
274 // requirements for C++ BasicLockable, allows UMutex to work with std::lock_guard
275 void lock() {
276 +#ifndef __wasi__
277 std::mutex *m = fMutex.load(std::memory_order_acquire);
278 if (m == nullptr) { m = getMutex(); }
279 m->lock();
280 +#endif
281 + }
282 + void unlock() {
283 +#ifndef __wasi__
284 + fMutex.load(std::memory_order_relaxed)->unlock();
285 +#endif
286 }
287 - void unlock() { fMutex.load(std::memory_order_relaxed)->unlock(); }
288
289 static void cleanup();
290
291 private:
292 +#ifndef __wasi__
293 alignas(std::mutex) char fStorage[sizeof(std::mutex)] {};
294 std::atomic<std::mutex *> fMutex { nullptr };
295 +#endif
296
297 /** All initialized UMutexes are kept in a linked list, so that they can be found,
298 * and the underlying std::mutex destructed, by u_cleanup().
299 */
300 UMutex *fListLink { nullptr };
301 @@ -247,11 +278,13 @@ class U_COMMON_API UMutex {
302
303 /** Out-of-line function to lazily initialize a UMutex on first use.
304 * Initial fast check is inline, in lock(). The returned value may never
305 * be nullptr.
306 */
307 +#ifndef __wasi__
308 std::mutex *getMutex();
309 +#endif
310 };
311
312
313 /* Lock a mutex.
314 * @param mutex The given mutex to be locked. Pass NULL to specify
315
316 diff --git a/intl/icu/source/common/unifiedcache.cpp b/intl/icu/source/common/unifiedcache.cpp
317 --- a/intl/icu/source/common/unifiedcache.cpp
318 +++ b/intl/icu/source/common/unifiedcache.cpp
319 @@ -11,19 +11,23 @@
320 */
321
322 #include "unifiedcache.h"
323
324 #include <algorithm> // For std::max()
325 -#include <mutex>
326 +#ifndef __wasi__
327 + #include <mutex>
328 +#endif
329
330 #include "uassert.h"
331 #include "uhash.h"
332 #include "ucln_cmn.h"
333
334 static icu::UnifiedCache *gCache = nullptr;
335 +#ifndef __wasi__
336 static std::mutex *gCacheMutex = nullptr;
337 static std::condition_variable *gInProgressValueAddedCond;
338 +#endif
339 static icu::UInitOnce gCacheInitOnce {};
340
341 static const int32_t MAX_EVICT_ITERATIONS = 10;
342 static const int32_t DEFAULT_MAX_UNUSED = 1000;
343 static const int32_t DEFAULT_PERCENTAGE_OF_IN_USE = 100;
344 @@ -32,14 +36,16 @@ static const int32_t DEFAULT_PERCENTAGE_OF_IN_USE = 100;
345 U_CDECL_BEGIN
346 static UBool U_CALLCONV unifiedcache_cleanup() {
347 gCacheInitOnce.reset();
348 delete gCache;
349 gCache = nullptr;
350 +#ifndef __wasi__
351 gCacheMutex->~mutex();
352 gCacheMutex = nullptr;
353 gInProgressValueAddedCond->~condition_variable();
354 gInProgressValueAddedCond = nullptr;
355 +#endif
356 return true;
357 }
358 U_CDECL_END
359
360
361 @@ -70,12 +76,14 @@ CacheKeyBase::~CacheKeyBase() {
362 static void U_CALLCONV cacheInit(UErrorCode &status) {
363 U_ASSERT(gCache == nullptr);
364 ucln_common_registerCleanup(
365 UCLN_COMMON_UNIFIED_CACHE, unifiedcache_cleanup);
366
367 +#ifndef __wasi__
368 gCacheMutex = STATIC_NEW(std::mutex);
369 gInProgressValueAddedCond = STATIC_NEW(std::condition_variable);
370 +#endif
371 gCache = new UnifiedCache(status);
372 if (gCache == nullptr) {
373 status = U_MEMORY_ALLOCATION_ERROR;
374 }
375 if (U_FAILURE(status)) {
376 @@ -133,41 +141,53 @@ void UnifiedCache::setEvictionPolicy(
377 }
378 if (count < 0 || percentageOfInUseItems < 0) {
379 status = U_ILLEGAL_ARGUMENT_ERROR;
380 return;
381 }
382 +#ifndef __wasi__
383 std::lock_guard<std::mutex> lock(*gCacheMutex);
384 +#endif
385 fMaxUnused = count;
386 fMaxPercentageOfInUse = percentageOfInUseItems;
387 }
388
389 int32_t UnifiedCache::unusedCount() const {
390 +#ifndef __wasi__
391 std::lock_guard<std::mutex> lock(*gCacheMutex);
392 +#endif
393 return uhash_count(fHashtable) - fNumValuesInUse;
394 }
395
396 int64_t UnifiedCache::autoEvictedCount() const {
397 +#ifndef __wasi__
398 std::lock_guard<std::mutex> lock(*gCacheMutex);
399 +#endif
400 return fAutoEvictedCount;
401 }
402
403 int32_t UnifiedCache::keyCount() const {
404 +#ifndef __wasi__
405 std::lock_guard<std::mutex> lock(*gCacheMutex);
406 +#endif
407 return uhash_count(fHashtable);
408 }
409
410 void UnifiedCache::flush() const {
411 +#ifndef __wasi__
412 std::lock_guard<std::mutex> lock(*gCacheMutex);
413 +#endif
414
415 // Use a loop in case cache items that are flushed held hard references to
416 // other cache items making those additional cache items eligible for
417 // flushing.
418 while (_flush(false));
419 }
420
421 void UnifiedCache::handleUnreferencedObject() const {
422 +#ifndef __wasi__
423 std::lock_guard<std::mutex> lock(*gCacheMutex);
424 +#endif
425 --fNumValuesInUse;
426 _runEvictionSlice();
427 }
428
429 #ifdef UNIFIED_CACHE_DEBUG
430 @@ -182,11 +202,13 @@ void UnifiedCache::dump() {
431 }
432 cache->dumpContents();
433 }
434
435 void UnifiedCache::dumpContents() const {
436 +#ifndef __wasi__
437 std::lock_guard<std::mutex> lock(*gCacheMutex);
438 +#endif
439 _dumpContents();
440 }
441
442 // Dumps content of cache.
443 // On entry, gCacheMutex must be held.
444 @@ -222,11 +244,13 @@ UnifiedCache::~UnifiedCache() {
445 flush();
446 {
447 // Now all that should be left in the cache are entries that refer to
448 // each other and entries with hard references from outside the cache.
449 // Nothing we can do about these so proceed to wipe out the cache.
450 +#ifndef __wasi__
451 std::lock_guard<std::mutex> lock(*gCacheMutex);
452 +#endif
453 _flush(true);
454 }
455 uhash_close(fHashtable);
456 fHashtable = nullptr;
457 delete fNoValue;
458 @@ -323,11 +347,13 @@ void UnifiedCache::_putNew(
459
460 void UnifiedCache::_putIfAbsentAndGet(
461 const CacheKeyBase &key,
462 const SharedObject *&value,
463 UErrorCode &status) const {
464 +#ifndef __wasi__
465 std::lock_guard<std::mutex> lock(*gCacheMutex);
466 +#endif
467 const UHashElement *element = uhash_find(fHashtable, &key);
468 if (element != nullptr && !_inProgress(element)) {
469 _fetch(element, value, status);
470 return;
471 }
472 @@ -348,18 +374,22 @@ UBool UnifiedCache::_poll(
473 const CacheKeyBase &key,
474 const SharedObject *&value,
475 UErrorCode &status) const {
476 U_ASSERT(value == nullptr);
477 U_ASSERT(status == U_ZERO_ERROR);
478 +#ifndef __wasi__
479 std::unique_lock<std::mutex> lock(*gCacheMutex);
480 +#endif
481 const UHashElement *element = uhash_find(fHashtable, &key);
482
483 // If the hash table contains an inProgress placeholder entry for this key,
484 // this means that another thread is currently constructing the value object.
485 // Loop, waiting for that construction to complete.
486 while (element != nullptr && _inProgress(element)) {
487 +#ifndef __wasi__
488 gInProgressValueAddedCond->wait(lock);
489 +#endif
490 element = uhash_find(fHashtable, &key);
491 }
492
493 // If the hash table contains an entry for the key,
494 // fetch out the contents and return them.
495 @@ -426,13 +456,15 @@ void UnifiedCache::_put(
496 UHashElement *ptr = const_cast<UHashElement *>(element);
497 ptr->value.pointer = (void *) value;
498 U_ASSERT(oldValue == fNoValue);
499 removeSoftRef(oldValue);
500
501 +#ifndef __wasi__
502 // Tell waiting threads that we replace in-progress status with
503 // an error.
504 gInProgressValueAddedCond->notify_all();
505 +#endif
506 }
507
508 void UnifiedCache::_fetch(
509 const UHashElement *element,
510 const SharedObject *&value,
511 diff --git a/intl/icu/source/i18n/decContext.h b/intl/icu/source/i18n/decContext.h
512 index 59ab65e..20f3526 100644
513 --- a/intl/icu/source/i18n/decContext.h
514 +++ b/intl/icu/source/i18n/decContext.h
515 @@ -61,7 +61,9 @@
516 /* #include <stdint.h> */ /* C99 standard integers */
517 #endif
518 #include <stdio.h> /* for printf, etc. */
519 +#ifndef __wasi__
520 #include <signal.h> /* for traps */
521 +#endif
522
523 /* Extended flags setting -- set this to 0 to use only IEEE flags */
524 #if !defined(DECEXTFLAG)
525 diff --git a/intl/icu/source/i18n/decimfmt.cpp b/intl/icu/source/i18n/decimfmt.cpp
526 index daa1129..c8f1eda 100644
527 --- a/intl/icu/source/i18n/decimfmt.cpp
528 +++ b/intl/icu/source/i18n/decimfmt.cpp
529 @@ -480,8 +480,13 @@ DecimalFormat& DecimalFormat::operator=(const DecimalFormat& rhs) {
530 DecimalFormat::~DecimalFormat() {
531 if (fields == nullptr) { return; }
532
533 +#ifndef __wasi__
534 delete fields->atomicParser.exchange(nullptr);
535 delete fields->atomicCurrencyParser.exchange(nullptr);
536 +#else
537 + delete fields->atomicParser;
538 + delete fields->atomicCurrencyParser;
539 +#endif
540 delete fields;
541 }
542
543 @@ -1626,8 +1631,13 @@ void DecimalFormat::touch(UErrorCode& status) {
544 setupFastFormat();
545
546 // Delete the parsers if they were made previously
547 +#ifndef __wasi__
548 delete fields->atomicParser.exchange(nullptr);
549 delete fields->atomicCurrencyParser.exchange(nullptr);
550 +#else
551 + delete fields->atomicParser;
552 + delete fields->atomicCurrencyParser;
553 +#endif
554
555 // In order for the getters to work, we need to populate some fields in NumberFormat.
556 NumberFormat::setCurrency(fields->exportedProperties.currency.get(status).getISOCurrency(), status);
557 @@ -1662,7 +1672,11 @@ const numparse::impl::NumberParserImpl* DecimalFormat::getParser(UErrorCode& sta
558 }
559
560 // First try to get the pre-computed parser
561 +#ifndef __wasi__
562 auto* ptr = fields->atomicParser.load();
563 +#else
564 + auto* ptr = fields->atomicParser;
565 +#endif
566 if (ptr != nullptr) {
567 return ptr;
568 }
569 @@ -1681,6 +1695,7 @@ const numparse::impl::NumberParserImpl* DecimalFormat::getParser(UErrorCode& sta
570 // it is set to what is actually stored in the atomic
571 // if another thread beat us to computing the parser object.
572 auto* nonConstThis = const_cast<DecimalFormat*>(this);
573 +#ifndef __wasi__
574 if (!nonConstThis->fields->atomicParser.compare_exchange_strong(ptr, temp)) {
575 // Another thread beat us to computing the parser
576 delete temp;
577 @@ -1689,13 +1704,21 @@ const numparse::impl::NumberParserImpl* DecimalFormat::getParser(UErrorCode& sta
578 // Our copy of the parser got stored in the atomic
579 return temp;
580 }
581 +#else
582 + nonConstThis->fields->atomicParser = temp;
583 + return temp;
584 +#endif
585 }
586
587 const numparse::impl::NumberParserImpl* DecimalFormat::getCurrencyParser(UErrorCode& status) const {
588 if (U_FAILURE(status)) { return nullptr; }
589
590 // First try to get the pre-computed parser
591 +#ifndef __wasi__
592 auto* ptr = fields->atomicCurrencyParser.load();
593 +#else
594 + auto* ptr = fields->atomicCurrencyParser;
595 +#endif
596 if (ptr != nullptr) {
597 return ptr;
598 }
599 @@ -1710,6 +1733,7 @@ const numparse::impl::NumberParserImpl* DecimalFormat::getCurrencyParser(UErrorC
600 // Note: ptr starts as nullptr; during compare_exchange, it is set to what is actually stored in the
601 // atomic if another thread beat us to computing the parser object.
602 auto* nonConstThis = const_cast<DecimalFormat*>(this);
603 +#ifndef __wasi__
604 if (!nonConstThis->fields->atomicCurrencyParser.compare_exchange_strong(ptr, temp)) {
605 // Another thread beat us to computing the parser
606 delete temp;
607 @@ -1718,6 +1742,10 @@ const numparse::impl::NumberParserImpl* DecimalFormat::getCurrencyParser(UErrorC
608 // Our copy of the parser got stored in the atomic
609 return temp;
610 }
611 +#else
612 + nonConstThis->fields->atomicCurrencyParser = temp;
613 + return temp;
614 +#endif
615 }
616
617 void
618 diff --git a/intl/icu/source/i18n/number_mapper.h b/intl/icu/source/i18n/number_mapper.h
619 index 9ecd776..d094289 100644
620 --- a/intl/icu/source/i18n/number_mapper.h
621 +++ b/intl/icu/source/i18n/number_mapper.h
622 @@ -7,7 +7,6 @@
623 #ifndef __NUMBER_MAPPER_H__
624 #define __NUMBER_MAPPER_H__
625
626 -#include <atomic>
627 #include "number_types.h"
628 #include "unicode/currpinf.h"
629 #include "standardplural.h"
630 @@ -15,6 +14,10 @@
631 #include "number_currencysymbols.h"
632 #include "numparse_impl.h"
633
634 +#ifndef __wasi__
635 +#include <atomic>
636 +#endif
637 +
638 U_NAMESPACE_BEGIN
639 namespace number {
640 namespace impl {
641 @@ -193,10 +196,18 @@ struct DecimalFormatFields : public UMemory {
642 LocalizedNumberFormatter formatter;
643
644 /** The lazy-computed parser for .parse() */
645 +#ifndef __wasi__
646 std::atomic<::icu::numparse::impl::NumberParserImpl*> atomicParser = {};
647 +#else
648 + ::icu::numparse::impl::NumberParserImpl* atomicParser = nullptr;
649 +#endif
650
651 /** The lazy-computed parser for .parseCurrency() */
652 +#ifndef __wasi__
653 std::atomic<::icu::numparse::impl::NumberParserImpl*> atomicCurrencyParser = {};
654 +#else
655 + ::icu::numparse::impl::NumberParserImpl* atomicCurrencyParser = {};
656 +#endif
657
658 /** Small object ownership warehouse for the formatter and parser */
659 DecimalFormatWarehouse warehouse;
660 diff --git a/intl/icu/source/i18n/numrange_fluent.cpp b/intl/icu/source/i18n/numrange_fluent.cpp
661 --- a/intl/icu/source/i18n/numrange_fluent.cpp
662 +++ b/intl/icu/source/i18n/numrange_fluent.cpp
663 @@ -246,33 +246,53 @@ LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter(LocalizedNumberRang
664
665 LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter(NFS<LNF>&& src) noexcept
666 : NFS<LNF>(std::move(src)) {
667 // Steal the compiled formatter
668 LNF&& _src = static_cast<LNF&&>(src);
669 +#ifndef __wasi__
670 auto* stolen = _src.fAtomicFormatter.exchange(nullptr);
671 delete fAtomicFormatter.exchange(stolen);
672 +#else
673 + delete fAtomicFormatter;
674 + fAtomicFormatter = _src.fAtomicFormatter;
675 + _src.fAtomicFormatter = nullptr;
676 +#endif
677 }
678
679 LocalizedNumberRangeFormatter& LocalizedNumberRangeFormatter::operator=(const LNF& other) {
680 if (this == &other) { return *this; } // self-assignment: no-op
681 NFS<LNF>::operator=(static_cast<const NFS<LNF>&>(other));
682 // Do not steal; just clear
683 +#ifndef __wasi__
684 delete fAtomicFormatter.exchange(nullptr);
685 +#else
686 + delete fAtomicFormatter;
687 +#endif
688 return *this;
689 }
690
691 LocalizedNumberRangeFormatter& LocalizedNumberRangeFormatter::operator=(LNF&& src) noexcept {
692 NFS<LNF>::operator=(static_cast<NFS<LNF>&&>(src));
693 // Steal the compiled formatter
694 +#ifndef __wasi__
695 auto* stolen = src.fAtomicFormatter.exchange(nullptr);
696 delete fAtomicFormatter.exchange(stolen);
697 +#else
698 + delete fAtomicFormatter;
699 + fAtomicFormatter = src.fAtomicFormatter;
700 + src.fAtomicFormatter = nullptr;
701 +#endif
702 return *this;
703 }
704
705
706 LocalizedNumberRangeFormatter::~LocalizedNumberRangeFormatter() {
707 +#ifndef __wasi__
708 delete fAtomicFormatter.exchange(nullptr);
709 +#else
710 + delete fAtomicFormatter;
711 +#endif
712 }
713
714 LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter(const RangeMacroProps& macros, const Locale& locale) {
715 fMacros = macros;
716 fMacros.locale = locale;
717 @@ -363,11 +383,15 @@ LocalizedNumberRangeFormatter::getFormatter(UErrorCode& status) const {
718 if (U_FAILURE(status)) {
719 return nullptr;
720 }
721
722 // First try to get the pre-computed formatter
723 +#ifndef __wasi__
724 auto* ptr = fAtomicFormatter.load();
725 +#else
726 + auto* ptr = fAtomicFormatter;
727 +#endif
728 if (ptr != nullptr) {
729 return ptr;
730 }
731
732 // Try computing the formatter on our own
733 @@ -378,17 +378,22 @@ LocalizedNumberRangeFormatter::getFormatter(UErrorCode& status) const {
734
735 // Note: ptr starts as nullptr; during compare_exchange,
736 // it is set to what is actually stored in the atomic
737 // if another thread beat us to computing the formatter object.
738 auto* nonConstThis = const_cast<LocalizedNumberRangeFormatter*>(this);
739 +#ifndef __wasi__
740 if (!nonConstThis->fAtomicFormatter.compare_exchange_strong(ptr, temp.getAlias())) {
741 // Another thread beat us to computing the formatter
742 return ptr;
743 } else {
744 // Our copy of the formatter got stored in the atomic
745 return temp.orphan();
746 }
747 +#else
748 + nonConstThis->fAtomicFormatter = temp.getAlias();
749 + return temp.orphan();
750 +#endif
751
752 }
753
754
755 #endif /* #if !UCONFIG_NO_FORMATTING */
756 diff --git a/intl/icu/source/i18n/unicode/numberrangeformatter.h b/intl/icu/source/i18n/unicode/numberrangeformatter.h
757 index b9a4600..0ba2fa0 100644
758 --- a/intl/icu/source/i18n/unicode/numberrangeformatter.h
759 +++ b/intl/icu/source/i18n/unicode/numberrangeformatter.h
760 @@ -10,7 +10,6 @@
761
762 #if !UCONFIG_NO_FORMATTING
763
764 -#include <atomic>
765 #include "unicode/appendable.h"
766 #include "unicode/fieldpos.h"
767 #include "unicode/formattedvalue.h"
768 @@ -18,6 +17,10 @@
769 #include "unicode/numberformatter.h"
770 #include "unicode/unumberrangeformatter.h"
771
772 +#ifndef __wasi__
773 +#include <atomic>
774 +#endif
775 +
776 /**
777 * \file
778 * \brief C++ API: Library for localized formatting of number, currency, and unit ranges.
779 @@ -77,7 +80,9 @@ struct UFormattedNumberRangeImpl;
780 } // namespace icu::number
781 U_NAMESPACE_END
782
783 +#ifndef __wasi__
784 template struct U_I18N_API std::atomic< U_NAMESPACE_QUALIFIER number::impl::NumberRangeFormatterImpl*>;
785 +#endif
786
787 U_NAMESPACE_BEGIN
788 namespace number { // icu::number
789 @@ -546,7 +551,11 @@ class U_I18N_API LocalizedNumberRangeFormatter
790 ~LocalizedNumberRangeFormatter();
791
792 private:
793 +#ifndef __wasi__
794 std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
795 +#else
796 + impl::NumberRangeFormatterImpl* fAtomicFormatter = nullptr;
797 +#endif
798
799 const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
800