Am 03.06.2026 um 23:50 schrieb Chris M. Thomasson:
On 6/2/2026 9:36 PM, Bonita Montero wrote:
bool xshared_mutex::re_share()
{
˙˙˙˙˙uint64_t cmp = m_atomic.load( memory_order_relaxed ), niu;
˙˙˙˙˙bool lastSharer;
˙˙˙˙˙do
˙˙˙˙˙{
˙˙˙˙˙˙˙˙ assert(check( cmp ));
˙˙˙˙˙˙˙˙ assert(cmp & SharersMask);
˙˙˙˙˙˙˙˙ if( !(cmp & WaitingExclusiveMask) )
˙˙˙˙˙˙˙˙˙˙˙˙ return false;
˙˙˙˙˙˙˙˙ if( (cmp & WaitingSharersMask) == WaitingSharersMask )
˙˙˙˙˙˙˙˙˙˙˙˙ throw overflow();
˙˙˙˙˙˙˙˙ niu = cmp - SharerValue + WaitingSharersValue;
˙˙˙˙˙˙˙˙ uint64_t niuLast = (niu - WaitingExclusiveValue) |
ExclusiveFlag;
˙˙˙˙˙˙˙˙ lastSharer = !(niu & SharersMask);
˙˙˙˙˙˙˙˙ niu = lastSharer ? niuLast : niu;
˙˙˙˙˙} while( !m_atomic.compare_exchange_strong( cmp, niu,
memory_order_release, memory_order_relaxed ) );
˙˙˙˙˙if( lastSharer )
˙˙˙˙˙˙˙˙ releaseExclusive();
˙˙˙˙˙waitShared();
˙˙˙˙˙return true;
}
You are going to me more explicit, and clarify. Perhaps an atomic hand
off? Not sure.
The reader's writer lock I wrote has a 64 bit word with three 21
bit counters. The lowest 21 bit count the number of currently active
sharers. The next 21 bit are the numbers of threads which are apply-
ing to share the lock. The next 21 bit are the number of threads
which apply for an exlusive ownership. Bit 63 is a bit that there's
a thread which owns the lock in exclusive mode.
Usually the lock is write preferring (controlled via a bit), i.e. if
there are a number of threads which are currently sharing and a threads
applies for exclusive mode the last mentioned counter is incremented
and all further threads which want to share are delayed and the counter
of threads which apply for sharing is incremented.
If mutiple threads are constantly locking and unlocking the lock in
shared mode to allow writers to proceed this may result in a lot of
cachline traffic. The above function allows a thread to remain in
shared mode and poll if there are threads which want to apply for
exlusive mode. If there were no such threads the cacheline holding
the mentioned counters and flags is usually remained in shared mode,
i.e. there's usually no additional cacheline traffic.
If there are threads which are waiting for exclusive mode the thread
executing the above code switches from sharer mode to waitinf for
shared access mode. If it is the last sharer that does this a thread
waiting for exclusive mode is awakened.
--- PyGate Linux v1.5.15
* Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)