Merge pull request #6541 from akallabeth/streampool_lock_fix

Unify StreamPool locking.
This commit is contained in:
Martin Fleisz 2020-10-30 10:42:04 +01:00 committed by GitHub
commit e72c35447c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,6 +41,26 @@ struct _wStreamPool
size_t defaultSize; size_t defaultSize;
}; };
/**
* Lock the stream pool
*/
static INLINE void StreamPool_Lock(wStreamPool* pool)
{
if (pool->synchronized)
EnterCriticalSection(&pool->lock);
}
/**
* Unlock the stream pool
*/
static INLINE void StreamPool_Unlock(wStreamPool* pool)
{
if (pool->synchronized)
LeaveCriticalSection(&pool->lock);
}
static BOOL StreamPool_EnsureCapacity(wStreamPool* pool, size_t count, BOOL usedOrAvailable) static BOOL StreamPool_EnsureCapacity(wStreamPool* pool, size_t count, BOOL usedOrAvailable)
{ {
size_t new_cap = 0; size_t new_cap = 0;
@ -166,8 +186,7 @@ wStream* StreamPool_Take(wStreamPool* pool, size_t size)
SSIZE_T foundIndex; SSIZE_T foundIndex;
wStream* s = NULL; wStream* s = NULL;
if (pool->synchronized) StreamPool_Lock(pool);
EnterCriticalSection(&pool->lock);
if (size == 0) if (size == 0)
size = pool->defaultSize; size = pool->defaultSize;
@ -206,8 +225,7 @@ wStream* StreamPool_Take(wStreamPool* pool, size_t size)
} }
out_fail: out_fail:
if (pool->synchronized) StreamPool_Unlock(pool);
LeaveCriticalSection(&pool->lock);
return s; return s;
} }
@ -221,33 +239,13 @@ void StreamPool_Return(wStreamPool* pool, wStream* s)
if (!s) if (!s)
return; return;
if (pool->synchronized) StreamPool_Lock(pool);
EnterCriticalSection(&pool->lock);
StreamPool_EnsureCapacity(pool, 1, FALSE); StreamPool_EnsureCapacity(pool, 1, FALSE);
pool->aArray[(pool->aSize)++] = s; pool->aArray[(pool->aSize)++] = s;
StreamPool_RemoveUsed(pool, s); StreamPool_RemoveUsed(pool, s);
if (pool->synchronized) StreamPool_Unlock(pool);
LeaveCriticalSection(&pool->lock);
}
/**
* Lock the stream pool
*/
static void StreamPool_Lock(wStreamPool* pool)
{
EnterCriticalSection(&pool->lock);
}
/**
* Unlock the stream pool
*/
static void StreamPool_Unlock(wStreamPool* pool)
{
LeaveCriticalSection(&pool->lock);
} }
/** /**
@ -306,7 +304,7 @@ wStream* StreamPool_Find(wStreamPool* pool, BYTE* ptr)
} }
} }
LeaveCriticalSection(&pool->lock); StreamPool_Unlock(pool);
return (found) ? s : NULL; return (found) ? s : NULL;
} }
@ -345,8 +343,7 @@ void StreamPool_Release(wStreamPool* pool, BYTE* ptr)
void StreamPool_Clear(wStreamPool* pool) void StreamPool_Clear(wStreamPool* pool)
{ {
if (pool->synchronized) StreamPool_Lock(pool);
EnterCriticalSection(&pool->lock);
while (pool->aSize > 0) while (pool->aSize > 0)
{ {
@ -360,8 +357,7 @@ void StreamPool_Clear(wStreamPool* pool)
Stream_Free(pool->uArray[pool->uSize], TRUE); Stream_Free(pool->uArray[pool->uSize], TRUE);
} }
if (pool->synchronized) StreamPool_Unlock(pool);
LeaveCriticalSection(&pool->lock);
} }
/** /**