diff --git a/libraries/wutnewlib/wut_lock.c b/libraries/wutnewlib/wut_lock.c index 0da87d1c3..b16987a7a 100644 --- a/libraries/wutnewlib/wut_lock.c +++ b/libraries/wutnewlib/wut_lock.c @@ -1,12 +1,12 @@ #include "wut_newlib.h" #include -#include +#include #define MAX_LOCKS 16 static OSMutex sLibcLocks[MAX_LOCKS]; -static uint32_t sLibcLockUsedMask = 0; +static volatile uint32_t sLibcLockUsedMask = 0; static inline bool __wut_is_lock_valid(int *lock) @@ -24,12 +24,12 @@ __wut_lock_init(int *lock, int slot; uint32_t new_mask; - uint32_t cur_mask = __atomic_load_n(&sLibcLockUsedMask, __ATOMIC_SEQ_CST); + uint32_t cur_mask = sLibcLockUsedMask; do { slot = __builtin_ffs(~cur_mask)-1; if (slot < 0 || slot >= MAX_LOCKS) break; new_mask = cur_mask | (1U << slot); - } while (!__atomic_compare_exchange_n(&sLibcLockUsedMask, &cur_mask, new_mask, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); + } while (!OSCompareAndSwapAtomicEx(&sLibcLockUsedMask, cur_mask, new_mask, &cur_mask)); if (slot < 0 || slot >= MAX_LOCKS) { return -1; @@ -48,10 +48,10 @@ __wut_lock_close(int *lock) } uint32_t new_mask; - uint32_t cur_mask = __atomic_load_n(&sLibcLockUsedMask, __ATOMIC_SEQ_CST); + uint32_t cur_mask = sLibcLockUsedMask; do { new_mask = cur_mask &~ (1U << *lock); - } while (!__atomic_compare_exchange_n(&sLibcLockUsedMask, &cur_mask, new_mask, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); + } while (!OSCompareAndSwapAtomicEx(&sLibcLockUsedMask, cur_mask, new_mask, &cur_mask)); *lock = -1; return 0; diff --git a/libraries/wutnewlib/wut_sbrk.c b/libraries/wutnewlib/wut_sbrk.c index e9d11c8e4..2226f2584 100644 --- a/libraries/wutnewlib/wut_sbrk.c +++ b/libraries/wutnewlib/wut_sbrk.c @@ -14,17 +14,17 @@ void * __wut_sbrk_r(struct _reent *r, ptrdiff_t incr) { - uint32_t oldSize, newSize; + uint32_t newSize; + uint32_t oldSize = sHeapSize; do { - oldSize = sHeapSize; newSize = oldSize + incr; if (newSize > sHeapMaxSize) { r->_errno = ENOMEM; return (void *)-1; } - } while (!OSCompareAndSwapAtomic(&sHeapSize, oldSize, newSize)); + } while (!OSCompareAndSwapAtomicEx(&sHeapSize, oldSize, newSize, &oldSize)); return ((uint8_t *)sHeapBase) + oldSize; } diff --git a/libraries/wutstdc++/wut_gthread_once.cpp b/libraries/wutstdc++/wut_gthread_once.cpp index 5e7a03c57..52a2f4eb2 100644 --- a/libraries/wutstdc++/wut_gthread_once.cpp +++ b/libraries/wutstdc++/wut_gthread_once.cpp @@ -11,9 +11,8 @@ __wut_once(__wut_once_t *once, __WUT_ONCE_VALUE_STARTED, &value)) { func(); - OSCompareAndSwapAtomic(once, - __WUT_ONCE_VALUE_STARTED, - __WUT_ONCE_VALUE_DONE); + OSSwapAtomic(once, + __WUT_ONCE_VALUE_DONE); } else if (value != __WUT_ONCE_VALUE_DONE) { while (!OSCompareAndSwapAtomic(once, __WUT_ONCE_VALUE_DONE,