Skip to content

Commit

Permalink
Fix race conditions in NotePlayHandleManager (LMMS#4966)
Browse files Browse the repository at this point in the history
NotePlayHandleManager::acquire uses a read lock unless the pool is empty.
If two threads try to acquire NotePlayHandle simultaneously
when the value of s_availableIndex is 1, one thread will try to read s_available[-1].
If the acquire action and the release action are done at the same time,
NotePlayHandleManager::acquire may try to read data
before NotePlayHandleManager::release actually writes.

This commit prevents them by always using the write lock when acquiring a NotePlayHandle.
  • Loading branch information
PhysSong authored May 8, 2019
1 parent 9a3413a commit b1ae9db
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/core/NotePlayHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,9 @@ NotePlayHandle * NotePlayHandleManager::acquire( InstrumentTrack* instrumentTrac
int midiEventChannel,
NotePlayHandle::Origin origin )
{
if( s_availableIndex < 0 )
{
s_mutex.lockForWrite();
if( s_availableIndex < 0 ) extend( NPH_CACHE_INCREMENT );
s_mutex.unlock();
}
s_mutex.lockForRead();
// TODO: use some lockless data structures
s_mutex.lockForWrite();
if (s_availableIndex < 0) { extend(NPH_CACHE_INCREMENT); }
NotePlayHandle * nph = s_available[ s_availableIndex.fetchAndAddOrdered( -1 ) ];
s_mutex.unlock();

Expand Down

0 comments on commit b1ae9db

Please sign in to comment.