mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-08-25 00:34:48 +03:00
move lock code to atomic.h
This commit is contained in:
parent
0b3cd51249
commit
f93fb900b7
8 changed files with 92 additions and 174 deletions
|
@ -242,50 +242,3 @@ void _mi_prim_thread_associate_default_heap(mi_heap_t* heap) {
|
|||
|
||||
}
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// Locks
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#if defined(MI_USE_PTHREADS)
|
||||
|
||||
bool _mi_prim_lock(mi_lock_t* lock) {
|
||||
return (pthread_mutex_lock(lock) == 0);
|
||||
}
|
||||
|
||||
bool _mi_prim_try_lock(mi_lock_t* lock) {
|
||||
return (pthread_mutex_trylock(lock) == 0);
|
||||
}
|
||||
|
||||
void _mi_prim_unlock(mi_lock_t* lock) {
|
||||
pthread_mutex_unlock(lock);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <emscripten.h>
|
||||
|
||||
// fall back to poor man's locks.
|
||||
bool _mi_prim_lock(mi_lock_t* lock) {
|
||||
for(int i = 0; i < 1000; i++) { // for at most 1 second?
|
||||
if (_mi_prim_try_lock(lock)) return true;
|
||||
if (i < 25) {
|
||||
mi_atomic_yield(); // first yield a bit
|
||||
}
|
||||
else {
|
||||
emscripten_sleep(1); // then sleep for 1ms intervals
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _mi_prim_try_lock(mi_lock_t* lock) {
|
||||
uintptr_t expected = 0;
|
||||
return mi_atomic_cas_strong_acq_rel(lock,&expected,(uintptr_t)1);
|
||||
}
|
||||
|
||||
void _mi_prim_unlock(mi_lock_t* lock) {
|
||||
mi_atomic_store_release(lock,(uintptr_t)0);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue