Skip to content

Commit 93e1434

Browse files
committed
use srw lock on windows
1 parent f3d83e5 commit 93e1434

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

include/mimalloc/atomic.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -402,28 +402,34 @@ static inline void mi_atomic_yield(void) {
402402

403403

404404
// ----------------------------------------------------------------------
405-
// Locks are only used for abandoned segment visiting in `arena.c`
405+
// Locks
406+
// These do not have to be recursive and should be light-weight
407+
// in-process only locks. Only used for reserving arena's and to
408+
// maintain the abandoned list.
406409
// ----------------------------------------------------------------------
410+
#if _MSC_VER
411+
#pragma warning(disable:26110) // unlock with holding lock
412+
#endif
407413

408414
#if defined(_WIN32)
409415

410-
#define mi_lock_t CRITICAL_SECTION
416+
#define mi_lock_t SRWLOCK // slim reader-writer lock
411417

412418
static inline bool mi_lock_try_acquire(mi_lock_t* lock) {
413-
return TryEnterCriticalSection(lock);
419+
return TryAcquireSRWLockExclusive(lock);
414420
}
415421
static inline bool mi_lock_acquire(mi_lock_t* lock) {
416-
EnterCriticalSection(lock);
422+
AcquireSRWLockExclusive(lock);
417423
return true;
418424
}
419425
static inline void mi_lock_release(mi_lock_t* lock) {
420-
LeaveCriticalSection(lock);
426+
ReleaseSRWLockExclusive(lock);
421427
}
422428
static inline void mi_lock_init(mi_lock_t* lock) {
423-
InitializeCriticalSection(lock);
429+
InitializeSRWLock(lock);
424430
}
425431
static inline void mi_lock_done(mi_lock_t* lock) {
426-
DeleteCriticalSection(lock);
432+
// nothing
427433
}
428434

429435

@@ -447,14 +453,13 @@ static inline void mi_lock_done(mi_lock_t* lock) {
447453
pthread_mutex_destroy(lock);
448454
}
449455

450-
/*
451456
#elif defined(__cplusplus)
452457

453458
#include <mutex>
454459
#define mi_lock_t std::mutex
455460

456461
static inline bool mi_lock_try_acquire(mi_lock_t* lock) {
457-
return lock->lock_try_acquire();
462+
return lock->try_lock();
458463
}
459464
static inline bool mi_lock_acquire(mi_lock_t* lock) {
460465
lock->lock();
@@ -469,7 +474,6 @@ static inline void mi_lock_init(mi_lock_t* lock) {
469474
static inline void mi_lock_done(mi_lock_t* lock) {
470475
(void)(lock);
471476
}
472-
*/
473477

474478
#else
475479

0 commit comments

Comments
 (0)