use mutexlocker class for nicer code

This commit is contained in:
Volker Fischer 2008-08-15 06:56:49 +00:00
parent 7445505c12
commit d2dc9f6834

View file

@ -611,6 +611,8 @@ CChannel::CChannel() : sName ( "" ),
void CChannel::SetEnable ( const bool bNEnStat ) void CChannel::SetEnable ( const bool bNEnStat )
{ {
QMutexLocker locker ( &Mutex );
// set internal parameter // set internal parameter
bIsEnabled = bNEnStat; bIsEnabled = bNEnStat;
@ -624,8 +626,8 @@ void CChannel::SetEnable ( const bool bNEnStat )
void CChannel::SetNetwInBlSiFactAndCompr ( const int iNewBlockSizeFactor, void CChannel::SetNetwInBlSiFactAndCompr ( const int iNewBlockSizeFactor,
const CAudioCompression::EAudComprType eNewAudComprType ) const CAudioCompression::EAudComprType eNewAudComprType )
{ {
Mutex.lock(); QMutexLocker locker ( &Mutex );
{
// store new value // store new value
iCurNetwInBlSiFact = iNewBlockSizeFactor; iCurNetwInBlSiFact = iNewBlockSizeFactor;
@ -640,13 +642,11 @@ void CChannel::SetNetwInBlSiFactAndCompr ( const int iNewBlockSizeFactor,
// socket buffer must be adjusted // socket buffer must be adjusted
SetSockBufSizeIntern ( GetSockBufSize() ); SetSockBufSizeIntern ( GetSockBufSize() );
} }
Mutex.unlock();
}
void CChannel::SetNetwBufSizeFactOut ( const int iNewNetwBlSiFactOut ) void CChannel::SetNetwBufSizeFactOut ( const int iNewNetwBlSiFactOut )
{ {
Mutex.lock(); QMutexLocker locker ( &Mutex );
{
// store new value // store new value
iCurNetwOutBlSiFact = iNewNetwBlSiFactOut; iCurNetwOutBlSiFact = iNewNetwBlSiFactOut;
@ -657,8 +657,6 @@ void CChannel::SetNetwBufSizeFactOut ( const int iNewNetwBlSiFactOut )
// init conversion buffer // init conversion buffer
ConvBuf.Init ( iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES ); ConvBuf.Init ( iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES );
} }
Mutex.unlock();
}
void CChannel::SetAudioCompressionOut ( const CAudioCompression::EAudComprType eNewAudComprTypeOut ) void CChannel::SetAudioCompressionOut ( const CAudioCompression::EAudComprType eNewAudComprTypeOut )
{ {
@ -673,13 +671,10 @@ void CChannel::SetAudioCompressionOut ( const CAudioCompression::EAudComprType e
void CChannel::SetSockBufSize ( const int iNumBlocks ) void CChannel::SetSockBufSize ( const int iNumBlocks )
{ {
// this opperation must be done with mutex QMutexLocker locker ( &Mutex ); // this opperation must be done with mutex
Mutex.lock();
{
SetSockBufSizeIntern ( iNumBlocks ); SetSockBufSizeIntern ( iNumBlocks );
} }
Mutex.unlock();
}
void CChannel::SetSockBufSizeIntern ( const int iNumBlocks ) void CChannel::SetSockBufSizeIntern ( const int iNumBlocks )
{ {
@ -697,32 +692,28 @@ void CChannel::SetSockBufSizeIntern ( const int iNumBlocks )
void CChannel::SetGain ( const int iChanID, const double dNewGain ) void CChannel::SetGain ( const int iChanID, const double dNewGain )
{ {
Mutex.lock(); QMutexLocker locker ( &Mutex );
{
// set value (make sure channel ID is in range) // set value (make sure channel ID is in range)
if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) ) if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) )
{ {
vecdGains[iChanID] = dNewGain; vecdGains[iChanID] = dNewGain;
} }
} }
Mutex.unlock();
}
double CChannel::GetGain ( const int iChanID ) double CChannel::GetGain ( const int iChanID )
{ {
double dReturnVal = 0; QMutexLocker locker ( &Mutex );
Mutex.lock();
{
// get value (make sure channel ID is in range) // get value (make sure channel ID is in range)
if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) ) if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) )
{ {
dReturnVal = vecdGains[iChanID]; return vecdGains[iChanID];
} }
else
{
return 0;
} }
Mutex.unlock();
return dReturnVal;
} }
void CChannel::SetName ( const QString strNewName ) void CChannel::SetName ( const QString strNewName )
@ -751,15 +742,9 @@ QString CChannel::GetName()
{ {
// make sure the string is not written at the same time when it is // make sure the string is not written at the same time when it is
// read here -> use mutex to secure access // read here -> use mutex to secure access
QString strReturn; QMutexLocker locker ( &Mutex );
Mutex.lock(); return sName;
{
strReturn = sName;
}
Mutex.unlock();
return strReturn;
} }
void CChannel::OnSendProtMessage ( CVector<uint8_t> vecMessage ) void CChannel::OnSendProtMessage ( CVector<uint8_t> vecMessage )
@ -799,25 +784,19 @@ void CChannel::OnChangeChanName ( QString strName )
bool CChannel::GetAddress(CHostAddress& RetAddr) bool CChannel::GetAddress(CHostAddress& RetAddr)
{ {
bool bReturnFlag; QMutexLocker locker ( &Mutex );
Mutex.lock();
{
if ( IsConnected() ) if ( IsConnected() )
{ {
RetAddr = InetAddr; RetAddr = InetAddr;
bReturnFlag = true; return true;
} }
else else
{ {
RetAddr = CHostAddress(); RetAddr = CHostAddress();
bReturnFlag = false; return false;
} }
} }
Mutex.unlock();
return bReturnFlag;
}
EPutDataStat CChannel::PutData ( const CVector<unsigned char>& vecbyData, EPutDataStat CChannel::PutData ( const CVector<unsigned char>& vecbyData,
int iNumBytes ) int iNumBytes )
@ -946,11 +925,11 @@ EPutDataStat CChannel::PutData ( const CVector<unsigned char>& vecbyData,
EGetDataStat CChannel::GetData ( CVector<double>& vecdData ) EGetDataStat CChannel::GetData ( CVector<double>& vecdData )
{ {
QMutexLocker locker ( &Mutex );
// init with ok flag // init with ok flag
EGetDataStat eGetStatus = GS_BUFFER_OK; EGetDataStat eGetStatus = GS_BUFFER_OK;
Mutex.lock(); // get mutex lock
{
if ( !SockBuf.Get ( vecdData ) ) if ( !SockBuf.Get ( vecdData ) )
{ {
// decrease time-out counter // decrease time-out counter
@ -975,20 +954,18 @@ EGetDataStat CChannel::GetData ( CVector<double>& vecdData )
eGetStatus = GS_CHAN_NOT_CONNECTED; eGetStatus = GS_CHAN_NOT_CONNECTED;
} }
} }
}
Mutex.unlock(); // get mutex unlock
return eGetStatus; return eGetStatus;
} }
CVector<unsigned char> CChannel::PrepSendPacket ( const CVector<short>& vecsNPacket ) CVector<unsigned char> CChannel::PrepSendPacket ( const CVector<short>& vecsNPacket )
{ {
QMutexLocker locker ( &Mutex );
// if the block is not ready we have to initialize with zero length to // if the block is not ready we have to initialize with zero length to
// tell the following network send routine that nothing should be sent // tell the following network send routine that nothing should be sent
CVector<unsigned char> vecbySendBuf ( 0 ); CVector<unsigned char> vecbySendBuf ( 0 );
Mutex.lock(); // get mutex lock
{
// use conversion buffer to convert sound card block size in network // use conversion buffer to convert sound card block size in network
// block size // block size
if ( ConvBuf.Put ( vecsNPacket ) ) if ( ConvBuf.Put ( vecsNPacket ) )
@ -997,8 +974,6 @@ CVector<unsigned char> CChannel::PrepSendPacket ( const CVector<short>& vecsNPac
vecbySendBuf.Init ( iAudComprSizeOut ); vecbySendBuf.Init ( iAudComprSizeOut );
vecbySendBuf = AudioCompressionOut.Encode ( ConvBuf.Get() ); vecbySendBuf = AudioCompressionOut.Encode ( ConvBuf.Get() );
} }
}
Mutex.unlock(); // get mutex unlock
return vecbySendBuf; return vecbySendBuf;
} }