rwlock.cpp
来自「The library provides supports for run-ti」· C++ 代码 · 共 66 行
CPP
66 行
#include "pi/RWLock.h"#include "pi/Atomic.h"//#include "pi/Thread.h"#define RWLOCK AtomicExchange(&m_lock,1)#define RWUNLOCK AtomicExchange(&m_lock,0)bool RWLock::WriteLock( bool try_once ){_CFE_; int cnt = SL_MAX_WAIT; while( --cnt>0 ){ if( !RWLOCK ){ if( !m_cnt ) m_cnt--; RWUNLOCK; if( m_cnt<0 || try_once ) return m_cnt<0; } RWLOCK_YIELD; } return false;}bool RWLock::WriteUnlock( ){_CFE_; int cnt = SL_MAX_WAIT; while( --cnt>0 ){ if( !RWLOCK ){ bool res = false; if( m_cnt==-1 ) m_cnt++,res=true; RWUNLOCK; return res; } RWLOCK_YIELD; } return false;}bool RWLock::Lock( bool try_once ){_CFE_; int cnt = SL_MAX_WAIT; while( --cnt>0 ){ if( !RWLOCK ){ if( m_cnt>=0 ) m_cnt++; RWUNLOCK; if( m_cnt>0 || try_once ) return m_cnt>0; } RWLOCK_YIELD; } return false;}bool RWLock::Unlock( ){_CFE_; int cnt = SL_MAX_WAIT; while( --cnt>0 ){ if( !RWLOCK ){ bool res = false; if( m_cnt>0 ) m_cnt--,res=true; RWUNLOCK; return res; } RWLOCK_YIELD; } return false;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?