Hi!
In neolib (C++ support library used by neoGFX) you can now profile
spinlock mutex contention:
struct i_mutex_profiler : i_service
{
template <typename ProfilerTag>
friend class recursive_spinlock;
public:
virtual bool enabled(std::chrono::microseconds& aTimeout, std::uint32_t& aMaxCount, bool& aEnhancedMetrics) const noexcept = 0;
virtual void enable(std::chrono::microseconds aTimeout = std::chrono::microseconds{ 100 }, std::uint32_t aMaxCount = 10u, bool aEnhancedMetrics = false) = 0;
virtual void disable() noexcept = 0;
public:
virtual void subscribe(i_mutex_profiler_observer& aObserver) = 0;
virtual void unsubscribe(i_mutex_profiler_observer& aObserver) = 0;
private:
virtual void notify_contention(i_lockable& aMutex, const std::chrono::microseconds& aContendedFor, mutex_lock_info const* aPreviousLocks, std::size_t aPreviousLocksCount) noexcept = 0;
public:
static uuid const& iid() { static uuid const sIid{ 0xc1546ec1, 0x9cfb, 0x4fe7, 0xb93e, { 0x1, 0xc1, 0x2a, 0x5f, 0xf1, 0x62 } }; return
sIid; }
};
static struct debug_mutexes : neolib::i_mutex_profiler_observer
{
void mutex_contended(neolib::i_lockable& aMutex, const std::chrono::microseconds& aContendedFor,
neolib::mutex_lock_info const* aPreviousLocks, std::size_t aPreviousLocksCount) noexcept final
{
}
} sDebugMutexes;
service<neolib::i_mutex_profiler>().enable(std::chrono::microseconds{ 100 }, 10u, true);
service<neolib::i_mutex_profiler>().subscribe(sDebugMutexes);
/Flibble
Pure spinlocks don't make sense in userspace.
On Mon, 22 Dec 2025 03:31:27 +0100, Bonita Montero wrote:
Pure spinlocks don't make sense in userspace.
You are assuming that I am using pure spinlocks? You know what they say about assumptions...
On 12/22/2025 1:45 PM, Mr Flibble wrote:
On Mon, 22 Dec 2025 03:31:27 +0100, Bonita Montero wrote:
Pure spinlocks don't make sense in userspace.
You are assuming that I am using pure spinlocks?-a You know what they say
about assumptions...
How about adaptive mutexs? Sure they can spin for a little while trying
to avoid a slow path call into a kernel wait.
On 12/22/2025 1:45 PM, Mr Flibble wrote:
On Mon, 22 Dec 2025 03:31:27 +0100, Bonita Montero wrote:
Pure spinlocks don't make sense in userspace.
You are assuming that I am using pure spinlocks?-a You know what they say
about assumptions...
How about adaptive mutexs? Sure they can spin for a little while
trying to avoid a slow path call into a kernel wait.
Am 22.12.2025 um 22:52 schrieb Chris M. Thomasson:
On 12/22/2025 1:45 PM, Mr Flibble wrote:
On Mon, 22 Dec 2025 03:31:27 +0100, Bonita Montero wrote:
Pure spinlocks don't make sense in userspace.
You are assuming that I am using pure spinlocks?-a You know what they say >>> about assumptions...
How about adaptive mutexs? Sure they can spin for a little while
trying to avoid a slow path call into a kernel wait.
These adaptive mutexes rarely prove effective. First, the competing threads only need to hold the mutex for a very short time for the spinning to have any chance of success.Second, the mutex must be locked and released frequently
so that locking attempts and locking situations often overlap. Both of
these
conditions don't occur often.
You are assuming that I am using pure spinlocks?
You know what they say about assumptions...
Lets take an adaptive mutex with a spin count threshold at 1. So, how
many spins avoided a kernel wait on the mutex object? I created a
different kind of mutex where a try_lock failure is an opportunity for
the thread try to do other work. The api for this try work is prefixed
with try_*. ;^)
A failed try_lock() is not a reason to burn cycles with PAUSE.
ItrCOs a signal that someone else is making progress, so yourCOre free to redirect your energy.
That is why a failed try_lock can be very useful.
Am 22.12.2025 um 22:52 schrieb Chris M. Thomasson:
On 12/22/2025 1:45 PM, Mr Flibble wrote:
On Mon, 22 Dec 2025 03:31:27 +0100, Bonita Montero wrote:
Pure spinlocks don't make sense in userspace.
You are assuming that I am using pure spinlocks?-a You know what they say >>> about assumptions...
How about adaptive mutexs? Sure they can spin for a little while
trying to avoid a slow path call into a kernel wait.
These adaptive mutexes rarely prove effective. First, the competing threads only need to hold the mutex for a very short time for the spinning to have any chance of success.Second, the mutex must be locked and released frequently
so that locking attempts and locking situations often overlap. Both of
these
conditions don't occur often.
On 12/26/2025 1:11 PM, Bonita Montero wrote:
Am 22.12.2025 um 22:52 schrieb Chris M. Thomasson:
On 12/22/2025 1:45 PM, Mr Flibble wrote:
On Mon, 22 Dec 2025 03:31:27 +0100, Bonita Montero wrote:
Pure spinlocks don't make sense in userspace.
You are assuming that I am using pure spinlocks?-a You know what
they say
about assumptions...
How about adaptive mutexs? Sure they can spin for a little while
trying to avoid a slow path call into a kernel wait.
These adaptive mutexes rarely prove effective. First, the competing
threads
only need to hold the mutex for a very short time for the spinning to
have
any chance of success.Second, the mutex must be locked and released
frequently
so that locking attempts and locking situations often overlap. Both
of these
conditions don't occur often.
We don't really know what complexity a "critical section" might
possess wrt what it does while the lock is held. We can hope its short
and sweet... This is why an adaptive mutex usually has a spin count
that can be altered. A potential nightmare is when a callback is
executed while holding a lock. I have seen this before. Uggg.
Am 27.12.2025 um 22:30 schrieb Chris M. Thomasson:
On 12/26/2025 1:11 PM, Bonita Montero wrote:
Am 22.12.2025 um 22:52 schrieb Chris M. Thomasson:
On 12/22/2025 1:45 PM, Mr Flibble wrote:
On Mon, 22 Dec 2025 03:31:27 +0100, Bonita Montero wrote:
Pure spinlocks don't make sense in userspace.
You are assuming that I am using pure spinlocks?-a You know what
they say
about assumptions...
How about adaptive mutexs? Sure they can spin for a little while
trying to avoid a slow path call into a kernel wait.
These adaptive mutexes rarely prove effective. First, the competing
threads
only need to hold the mutex for a very short time for the spinning to
have
any chance of success.Second, the mutex must be locked and released
frequently
so that locking attempts and locking situations often overlap. Both
of these
conditions don't occur often.
We don't really know what complexity a "critical section" might
possess wrt what it does while the lock is held. We can hope its short
and sweet... This is why an adaptive mutex usually has a spin count
that can be altered. A potential nightmare is when a callback is
executed while holding a lock. I have seen this before. Uggg.
Your idea bases implicitly on the assumption that the mutex is hold a very short time and its locked very frequently; otherwise spinning wouldn't make sense. Havong a task that may be completely omitted or may delay the
locking
an arbitrary time doesn't make sense.
Pleas find any project on the web that does including your idee; you won't find any.
Your idea doesn't make sense because of what I said.Your idea bases implicitly on the assumption that the mutex is hold aWrt adpative mutex we can allow one to adjust it to help fit their
very
short time and its locked very frequently; otherwise spinning
wouldn't make
sense. Havong a task that may be completely omitted or may delay the
locking
needs. So whats the problem?
an arbitrary time doesn't make sense.Are you talking about adjusting a spin count for an adaptive mutex? Or
Pleas find any project on the web that does including your idee; you
won't
find any.
my try to do other lock/wait free work after a try_lock failure?
Am 28.12.2025 um 21:26 schrieb Chris M. Thomasson:
Your idea doesn't make sense because of what I said.Your idea bases implicitly on the assumption that the mutex is hold aWrt adpative mutex we can allow one to adjust it to help fit their
very
short time and its locked very frequently; otherwise spinning
wouldn't make
sense. Havong a task that may be completely omitted or may delay the
locking
needs. So whats the problem?
an arbitrary time doesn't make sense.Are you talking about adjusting a spin count for an adaptive mutex? Or
Pleas find any project on the web that does including your idee; you
won't
find any.
my try to do other lock/wait free work after a try_lock failure?
You should show me real code where your idea applies.
On 12/28/2025 8:15 PM, Bonita Montero wrote:
Am 28.12.2025 um 21:26 schrieb Chris M. Thomasson:My idea of trying to do other lock/wait work after a try_lock, or
Your idea doesn't make sense because of what I said.Your idea bases implicitly on the assumption that the mutex is holdWrt adpative mutex we can allow one to adjust it to help fit their
a very
short time and its locked very frequently; otherwise spinning
wouldn't make
sense. Havong a task that may be completely omitted or may delay
the locking
needs. So whats the problem?
an arbitrary time doesn't make sense.Are you talking about adjusting a spin count for an adaptive mutex?
Pleas find any project on the web that does including your idee;
you won't
find any.
Or my try to do other lock/wait free work after a try_lock failure?
You should show me real code where your idea applies.
adjusting the spin count for an adaptive mutex, aka: https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setcriticalsectionspincount
Am 30.12.2025 um 01:22 schrieb Chris M. Thomasson:
On 12/28/2025 8:15 PM, Bonita Montero wrote:
Am 28.12.2025 um 21:26 schrieb Chris M. Thomasson:My idea of trying to do other lock/wait work after a try_lock, or
Your idea doesn't make sense because of what I said.Your idea bases implicitly on the assumption that the mutex is holdWrt adpative mutex we can allow one to adjust it to help fit their
a very short time and its locked very frequently; otherwise spinning >>>>> wouldn't make sense. Havong a task that may be completely omitted or >>>>> may delay the locking
needs. So whats the problem?
an arbitrary time doesn't make sense.Are you talking about adjusting a spin count for an adaptive mutex?
Pleas find any project on the web that does including your idee; you >>>>> won't find any.
Or my try to do other lock/wait free work after a try_lock failure?
You should show me real code where your idea applies.
adjusting the spin count for an adaptive mutex, aka:
https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf- synchapi-setcriticalsectionspincount
Your idea has nothing to do with that because
SetCriticalSectionSpinCount()
doesn't allow alternative tasks.
You don't need to use OS services/API for a spinlock as the spinning
happens in userland; you only involve the OS if you revert to a wait for a hybrid spinlock (what mine does).
Am 30.12.2025 um 19:21 schrieb Mr Flibble:
You don't need to use OS services/API for a spinlock as the spinning
happens in userland; you only involve the OS if you revert to a wait
for a hybrid spinlock (what mine does).
A hybrid "spinlock" isn't called spinlock.
It makes only sense if the mutex is held a very short time and the
locking occurs at a high frequency. That's are a very rare case.
On Tue, 30 Dec 2025 19:44:38 +0100, Bonita Montero wrote:
Am 30.12.2025 um 19:21 schrieb Mr Flibble:Wrong, it is called a hybrid spinlock because it spins for a short period (characterized on first use) and then reverts to a wait.
You don't need to use OS services/API for a spinlock as the spinningA hybrid "spinlock" isn't called spinlock.
happens in userland; you only involve the OS if you revert to a wait
for a hybrid spinlock (what mine does).
It makes only sense if the mutex is held a very short time and the
locking occurs at a high frequency. That's are a very rare case.
/Flibble
Am 30.12.2025 um 20:32 schrieb Mr Flibble:
On Tue, 30 Dec 2025 19:44:38 +0100, Bonita Montero wrote:
Am 30.12.2025 um 19:21 schrieb Mr Flibble:Wrong, it is called a hybrid spinlock because it spins for a short
You don't need to use OS services/API for a spinlock as the spinningA hybrid "spinlock" isn't called spinlock.
happens in userland; you only involve the OS if you revert to a wait
for a hybrid spinlock (what mine does).
It makes only sense if the mutex is held a very short time and the
locking occurs at a high frequency. That's are a very rare case.
period (characterized on first use) and then reverts to a wait.
/Flibble
Ok, you're right. But nevertheless it's really rare that its usage makes sense.
On 12/28/2025 8:15 PM, Bonita Montero wrote:^^^^^^^^^^^^^^^^^^^^^^^^^^^
Am 28.12.2025 um 21:26 schrieb Chris M. Thomasson:
Your idea doesn't make sense because of what I said.Your idea bases implicitly on the assumption that the mutex is holdWrt adpative mutex we can allow one to adjust it to help fit their
a very
short time and its locked very frequently; otherwise spinning
wouldn't make
sense. Havong a task that may be completely omitted or may delay the
locking
needs. So whats the problem?
an arbitrary time doesn't make sense.Are you talking about adjusting a spin count for an adaptive mutex?
Pleas find any project on the web that does including your idee; you
won't
find any.
Or my try to do other lock/wait free work after a try_lock failure?
You should show me real code where your idea applies.
My idea of trying to do other lock/wait work after a try_lock, or
adjusting the spin count for an adaptive mutex, aka:
https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf- synchapi-setcriticalsectionspincount
?
On Tue, 30 Dec 2025 21:00:01 +0100, Bonita Montero wrote:
Am 30.12.2025 um 20:32 schrieb Mr Flibble:
On Tue, 30 Dec 2025 19:44:38 +0100, Bonita Montero wrote:
Am 30.12.2025 um 19:21 schrieb Mr Flibble:Wrong, it is called a hybrid spinlock because it spins for a short
You don't need to use OS services/API for a spinlock as the spinning >>>>> happens in userland; you only involve the OS if you revert to a wait >>>>> for a hybrid spinlock (what mine does).A hybrid "spinlock" isn't called spinlock.
It makes only sense if the mutex is held a very short time and the
locking occurs at a high frequency. That's are a very rare case.
period (characterized on first use) and then reverts to a wait.
/Flibble
Ok, you're right. But nevertheless it's really rare that its usage makes
sense.
Wrong, hybrid spinlocks are common in:
* Language runtimes
* High-performance servers
* Game engines
On Tue, 30 Dec 2025 21:00:01 +0100, Bonita Montero wrote:
Am 30.12.2025 um 20:32 schrieb Mr Flibble:Wrong, hybrid spinlocks are common in:
On Tue, 30 Dec 2025 19:44:38 +0100, Bonita Montero wrote:Ok, you're right. But nevertheless it's really rare that its usage makes
Am 30.12.2025 um 19:21 schrieb Mr Flibble:Wrong, it is called a hybrid spinlock because it spins for a short
You don't need to use OS services/API for a spinlock as the spinning >>>>> happens in userland; you only involve the OS if you revert to a wait >>>>> for a hybrid spinlock (what mine does).A hybrid "spinlock" isn't called spinlock.
It makes only sense if the mutex is held a very short time and the
locking occurs at a high frequency. That's are a very rare case.
period (characterized on first use) and then reverts to a wait.
/Flibble
sense.
* Language runtimes
* High-performance servers
* Game engines
/Flibble
Wrong, hybrid spinlocks are common in:
* Language runtimes
* High-performance servers
* Game engines
/Flibble
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 54 |
| Nodes: | 6 (1 / 5) |
| Uptime: | 20:59:17 |
| Calls: | 742 |
| Files: | 1,218 |
| D/L today: |
6 files (8,794K bytes) |
| Messages: | 185,811 |
| Posted today: | 1 |