I used a goto:
bool xshared_mutex::exclusive_to_shared( x2s_mode bias )
{
˙˙˙˙uint64_t ref = m_atomic.load( memory_order_relaxed );
˙˙˙˙thread_id recoverThreadId =
m_exclusiveId.load( memory_order_relaxed );
˙˙˙˙m_exclusiveId.store( thread_id(), memory_order_relaxed );
˙˙˙˙bool rdBias = (m_readerBias.load( memory_order_relaxed ) ||
(int)bias > 0) && (int)bias >= 0;
˙˙˙˙uint64_t watingExclMask = rdBias ? 0 : WaitingExclusiveMask, wakeups;
˙˙˙˙for( ; ; )
˙˙˙˙{
˙˙˙˙˙˙˙ assert(check( ref ));
˙˙˙˙˙˙˙ assert(ref & ExclusiveFlag);
˙˙˙˙˙˙˙ if( (ref & watingExclMask) )
˙˙˙˙˙˙˙ {
˙˙˙˙˙˙˙˙˙˙˙ if( (ref & WaitingSharersMask) == WaitingSharersMask )
˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ goto err;
˙˙˙˙˙˙˙˙˙˙˙ uint64_t niu = ref - WaitingExclusiveValue + WaitingSharersValue;
˙˙˙˙˙˙˙˙˙˙˙ if( !m_atomic.compare_exchange_strong( ref, niu, memory_order_release, memory_order_relaxed ) )
˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ continue;
˙˙˙˙˙˙˙˙˙˙˙ releaseExclusive();
˙˙˙˙˙˙˙˙˙˙˙ waitShared();
˙˙˙˙˙˙˙˙˙˙˙ return true;
˙˙˙˙˙˙˙ }
˙˙˙˙˙˙˙ wakeups = (ref >> WaitingSharersBase) & Mask21;
˙˙˙˙˙˙˙ if( wakeups == Mask21 )
˙˙˙˙˙˙˙˙˙˙˙ goto err;
˙˙˙˙˙˙˙ uint64_t niu = (ref & ~XclWtShrMask) + wakeups + SharerValue;
˙˙˙˙˙˙˙ if( m_atomic.compare_exchange_strong( ref, niu, memory_order_release, memory_order_relaxed ) )
˙˙˙˙˙˙˙˙˙˙˙ break;
˙˙˙˙}
˙˙˙˙if( wakeups )
˙˙˙˙˙˙˙ releaseShared( (unsigned)wakeups );
˙˙˙˙return true;
err:
˙˙˙˙m_exclusiveId.store( recoverThreadId, memory_order_relaxed );
˙˙˙˙return error<bool>();
}
For fun, when you are bored and have nothing to do... Model it in
Relacy! :^)
Am 18.11.2026 um 01:07 schrieb Chris M. Thomasson:
For fun, when you are bored and have nothing to do... Model it in
Relacy! :^)
You would even recomment to use relacy for a hello world.
I used a goto:
bool xshared_mutex::exclusive_to_shared( x2s_mode bias )
{
˙˙˙˙uint64_t ref = m_atomic.load( memory_order_relaxed );
˙˙˙˙thread_id recoverThreadId =
m_exclusiveId.load( memory_order_relaxed );
˙˙˙˙m_exclusiveId.store( thread_id(), memory_order_relaxed );
˙˙˙˙bool rdBias = (m_readerBias.load( memory_order_relaxed ) ||
(int)bias > 0) && (int)bias >= 0;
˙˙˙˙uint64_t watingExclMask = rdBias ? 0 : WaitingExclusiveMask, wakeups;
˙˙˙˙for( ; ; )
˙˙˙˙{
˙˙˙˙˙˙˙ assert(check( ref ));
˙˙˙˙˙˙˙ assert(ref & ExclusiveFlag);
˙˙˙˙˙˙˙ if( (ref & watingExclMask) )
˙˙˙˙˙˙˙ {
˙˙˙˙˙˙˙˙˙˙˙ if( (ref & WaitingSharersMask) == WaitingSharersMask )
˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ goto err;
˙˙˙˙˙˙˙˙˙˙˙ uint64_t niu = ref - WaitingExclusiveValue + WaitingSharersValue;
˙˙˙˙˙˙˙˙˙˙˙ if( !m_atomic.compare_exchange_strong( ref, niu, memory_order_release, memory_order_relaxed ) )
˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ continue;
˙˙˙˙˙˙˙˙˙˙˙ releaseExclusive();
˙˙˙˙˙˙˙˙˙˙˙ waitShared();
˙˙˙˙˙˙˙˙˙˙˙ return true;
˙˙˙˙˙˙˙ }
˙˙˙˙˙˙˙ wakeups = (ref >> WaitingSharersBase) & Mask21;
˙˙˙˙˙˙˙ if( wakeups == Mask21 )
˙˙˙˙˙˙˙˙˙˙˙ goto err;
˙˙˙˙˙˙˙ uint64_t niu = (ref & ~XclWtShrMask) + wakeups + SharerValue;
˙˙˙˙˙˙˙ if( m_atomic.compare_exchange_strong( ref, niu, memory_order_release, memory_order_relaxed ) )
˙˙˙˙˙˙˙˙˙˙˙ break;
˙˙˙˙}
˙˙˙˙if( wakeups )
˙˙˙˙˙˙˙ releaseShared( (unsigned)wakeups );
˙˙˙˙return true;
err:
˙˙˙˙m_exclusiveId.store( recoverThreadId, memory_order_relaxed );
˙˙˙˙return error<bool>();
}
| Sysop: | Tetrazocine |
|---|---|
| Location: | Melbourne, VIC, Australia |
| Users: | 3 |
| Nodes: | 8 (0 / 8) |
| Uptime: | 51:21:26 |
| Calls: | 223 |
| Files: | 21,513 |
| Messages: | 83,435 |