mORMot2 DailyDaily read of upstream commits

Native futex for TOSLightLock and ICU deadlock fix

Today's commits bring a major evolution to the mORMot 2 core synchronization primitives. TOSLightLock now leverages native futex on Linux and WaitOnAddress on Windows 8+, promising significant contention performance gains. In addition, an ICU library deadlock was fixed and the async networking polling got a boundary check.

Worth a lookfix or feature you may want
Build2.4.16618 → 2.4.16628
Commits10
Lines+404 -312
Read2 min

TL;DR

  • TOSLightLock uses native futex on Linux and WaitOnAddress on Windows 8+.
  • TIcuLibrary fixes a deadlock during shared converter creation.
  • TAsyncConnections fixes a potential out-of-range bug in thread polling.
  • Adaptive spin-wait mechanisms are extracted to the core base unit.

Themes

Futex-based TOSLightLock

The TOSLightLock structure was heavily refactored across several commits. On Linux, it now uses a 32-bit futex syscall instead of falling back to thread yielding. On Windows 8 and later, it replaces the Slim Reader/Writer (SRW) locks with the faster WaitOnAddress API. A fallback to TRTLCriticalSection remains for older kernels or Android.

Adaptive spinning refactoring

The CPU-specific adaptive spin loop functions were centralized. NextSpin() and a new raw DoPause procedure are now public in mormot.core.base, allowing better code reuse across the framework.

Categories

Fixes4
New features1
Performance2
Under the hood2
Housekeeping1

Fixes

4

Fixed a deadlock when loading ICU converters

core: fix deadlock in TIcuLibrary

What changed

Corrects the unlock logic in TIcuLibrary.SharedUcnvGet(). The previous code mistakenly released a converter-specific lock instead of the main library lock after populating the converter cache.

Impact

Prevents deadlocks when multiple threads request text converters from the ICU library simultaneously.

Fixed TOSLightLock fallback on Android and Delphi POSIX

core: another pass of TOsLightLock

What changed

Restores the TRTLCriticalSection fallback for TOSLightLock on platforms without native futex or pthread_mutex support, like Android and Delphi POSIX. The previous commits inadvertently broke this fallback.

Impact

Applications running on Android or compiled with Delphi for POSIX will compile and run correctly again. No impact on Windows or FPC Linux.

Fixed late loading of Windows 8 synchronization API

core: fixed API-MS-Win-Core-Synch-l1-2-0.dll loading

What changed

Moves the runtime loading of API-MS-Win-Core-Synch-l1-2-0.dll to happen after the OS version has been properly populated into OSVersion32.

Impact

Resolves a bug where the new futex API was not initialized correctly on modern Windows versions because the version check failed at initialization time.

Fixed potential bounds error in asynchronous connections

net: fixed potential out of range in TAsyncConnections.ThreadPollingWakeupLocked;

What changed

Updates TAsyncConnections.ThreadPollingWakeupLocked to check for <= 0 instead of exactly 0 when inspecting spare events.

Impact

Prevents potential out-of-range errors or negative event counters from causing issues in the async polling thread loop.

New features

1

Published a raw DoPause procedure

core: let publish a raw DoPause procedure

What changed

Extracts the CPU pause logic (the pause opcode on x86, yield on ARM) into a globally available DoPause procedure in mormot.core.base.

Impact

Developers can now explicitly call DoPause in custom spin loops to reduce CPU power consumption and cache traffic.

Public API

  • DoPauseadded — A cross-platform procedure that invokes CPU pause or yield instructions.

Performance

2

TOSLightLock uses WaitOnAddress on Windows 8+

core: TOSLightLock now uses WaitOnAddress/WakeByAddressSingle on Win8+

What changed

Upgrades TOSLightLock on Windows to use the WaitOnAddress and WakeByAddressSingle API (introduced in Windows 8), moving away from Slim Reader/Writer (SRW) locks.

Impact

Faster lock acquisition on Windows 8 and newer, especially under high contention, by avoiding SRW lock overhead.

TOSLightLock upgraded to use native futex on Linux

core: TOsLightLock uses a futex on Linux

What changed

Replaces the underlying Linux implementation of TOSLightLock with a direct 32-bit futex syscall instead of a TRTLCriticalSection. It falls back to spin-and-wait on older kernels.

Impact

Significantly improves requests-per-second and threading stability on Linux backends. It is now as fast as TLightLock on the uncontended fast path.

Under the hood

2

Moved NextSpin to mormot.core.base

core: moved NextSpin() to mormot.core.base

What changed

Moves the adaptive spin-wait function NextSpin() and its associated SPIN_FACTOR and SPIN_COUNT constants from mormot.core.os into the lower-level mormot.core.base unit.

Impact

No direct impact on application code. It allows simpler units without OS dependencies to use the adaptive spin logic.

Refactored TIcuLibrary internal locking fields

core: fixed TIcuLibrary locking code

What changed

Changes the internal locking fields in TIcuLibrary from PtrUInt to cardinal to match the expected types for TLightLock primitives.

Impact

No impact on application code, internal refactoring only.

Housekeeping

1

Updated TOSLightLock usage comments

core: fixed TOSLightLock usage comments in code

What changed

Updates code comments referencing TOSLightLock across several units to reflect the new Windows 8+ and Linux futex behaviors.

Impact

No impact on application code, internal refactoring only.

Upgrade advice

Teams pushing heavy multithreaded workloads on Linux or Windows 8+ should benefit from the TOSLightLock upgrade. The ICU deadlock fix is recommended for applications dealing with internationalization.

Notes

Everything reviewed cleanly.