class __shared_mutex_cv
{
// Based on Howard Hinnant's reference implementation from N2406.
// The high bit of _M_state is the write-entered flag which is set to
// indicate a writer has taken the lock or is queuing to take the lock.
// The remaining bits are the count of reader locks.
//
// To take a reader lock, block on gate1 while the write-entered flag is
// set or the maximum number of reader locks is held, then increment the
// reader lock count.
// To release, decrement the count, then if the write-entered flag is set
// and the count is zero then signal gate2 to wake a queued writer,
// otherwise if the maximum number of reader locks was held signal gate1
// to wake a reader.
//
// To take a writer lock, block on gate1 while the write-entered flag is
// set, then set the write-entered flag to start queueing, then block on
// gate2 while the number of reader locks is non-zero.
// To release, unset the write-entered flag and signal gate1 to wake all
// blocked readers and writers.
//
// This means that when no reader locks are held readers and writers get
// equal priority. When one or more reader locks is held a writer gets
// priority and no more reader locks can be taken while the writer is
// queued.
// Only locked when accessing _M_state or waiting on condition variables.
mutex _M_mut;
// Used to block while write-entered is set or reader count at maximum.
condition_variable _M_gate1;
// Used to block queued writers while reader count is non-zero.
condition_variable _M_gate2;
// The write-entered flag and reader count.
unsigned _M_state;
static constexpr unsigned _S_write_entered
= 1U << (sizeof(unsigned)*__CHAR_BIT__ - 1);
static constexpr unsigned _S_max_readers = ~_S_write_entered;
// Test whether the write-entered flag is set. _M_mut must be locked.
bool _M_write_entered() const { return _M_state & _S_write_entered; }
// The number of reader locks currently held. _M_mut must be locked.
unsigned _M_readers() const { return _M_state & _S_max_readers; }
public:
__shared_mutex_cv() : _M_state(0) {}
~__shared_mutex_cv()
{
__glibcxx_assert( _M_state == 0 );
}
__shared_mutex_cv(const __shared_mutex_cv&) = delete;
__shared_mutex_cv& operator=(const __shared_mutex_cv&) = delete;
// Exclusive ownership
void
lock()
{
unique_lock<mutex> __lk(_M_mut);
// Wait until we can set the write-entered flag.
_M_gate1.wait(__lk, [=]{ return !_M_write_entered(); });
_M_state |= _S_write_entered;
// Then wait until there are no more readers.
_M_gate2.wait(__lk, [=]{ return _M_readers() == 0; });
}
bool
try_lock()
{
unique_lock<mutex> __lk(_M_mut, try_to_lock);
if (__lk.owns_lock() && _M_state == 0)
{
_M_state = _S_write_entered;
return true;
}
return false;
}
void
unlock()
{
lock_guard<mutex> __lk(_M_mut);
__glibcxx_assert( _M_write_entered() );
_M_state = 0;
// call notify_all() while mutex is held so that another thread can't
// lock and unlock the mutex then destroy *this before we make the call.
_M_gate1.notify_all();
}
// Shared ownership
void
lock_shared()
{
unique_lock<mutex> __lk(_M_mut);
_M_gate1.wait(__lk, [=]{ return _M_state < _S_max_readers; });
++_M_state;
}
bool
try_lock_shared()
{
unique_lock<mutex> __lk(_M_mut, try_to_lock);
if (!__lk.owns_lock())
return false;
if (_M_state < _S_max_readers)
{
++_M_state;
return true;
}
return false;
}
void
unlock_shared()
{
lock_guard<mutex> __lk(_M_mut);
__glibcxx_assert( _M_readers() > 0 );
auto __prev = _M_state--;
if (_M_write_entered())
{
// Wake the queued writer if there are no more readers.
if (_M_readers() == 0)
_M_gate2.notify_one();
// No need to notify gate1 because we give priority to the queued
// writer, and that writer will eventually notify gate1 after it
// clears the write-entered flag.
}
else
{
// Wake any thread that was blocked on reader overflow.
if (__prev == _S_max_readers)
_M_gate1.notify_one();
}
}
};
线程同步的要点:
1.主要变量:互斥锁(mut),第一道门(gate1),第二道门(gate2),读写锁的等待与持有状态(state)。
2.state为unsigned int,去掉最高位后最大值(2**31-1)(约为20亿),远远大于可能的线程数;最高位记录是否有写线程等待或持有写锁,其他位记录读锁持有数量。mut保护该变量的读写。
3.读线程只需要通过gate1就算持有了读锁;通过gate1的条件是没有写线程等待或已通过gate2(即state最高位为0)并且持有读锁的线程没有达到(2**31-1)(基本不可能)。
4.写线程需要通过两道门才算持有了写锁;通过gate1的条件与读线程相同,在通过后对state最高位置1;通过gate2的条件是没有读线程持有读锁(即state非最高位全部为0)。
5.读线程解锁时:1)如果有写线程等待在gate2,检查是否自己是否是最后一个持有读锁的线程,如果是则唤醒gate2。2)如果没有写线程等待在gate2,检查之前持有读锁的线程数量如果等于(2**31-1)则唤醒gate1(基本不可能)。
6.写线程解锁时由于所有线程均等待在gate1,所以唤醒gate1(notify_all)即可。这里的唤醒非写线程优先,但其会在被唤醒后等待在gate2,阻止后续的读线程通过gate1。