Skip to content

Commit

Permalink
Stop using base-class's lock
Browse files Browse the repository at this point in the history
  • Loading branch information
tshino committed Dec 26, 2023
1 parent 83de162 commit a3b1f35
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/softcamcore/DShowSoftcam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ FrameBuffer* Softcam::getFrameBuffer()
return nullptr;
}

CAutoLock lock(&m_cStateLock);
CAutoLock lock(&m_critsec);
if (!m_frame_buffer)
{
auto fb = FrameBuffer::open();
Expand All @@ -393,7 +393,7 @@ FrameBuffer* Softcam::getFrameBuffer()
void
Softcam::releaseFrameBuffer()
{
CAutoLock lock(&m_cStateLock);
CAutoLock lock(&m_critsec);
m_frame_buffer.release();
}

Expand Down
1 change: 1 addition & 0 deletions src/softcamcore/DShowSoftcam.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Softcam : public CSource, public IAMStreamConfig
static void disableDefaultBlankImage();

private:
CCritSec m_critsec;
FrameBuffer m_frame_buffer;
DefaultImage m_default_image;
const bool m_valid;
Expand Down
38 changes: 38 additions & 0 deletions tests/core_tests/DShowSoftcamTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1003,5 +1003,43 @@ TEST_F(SoftcamStream, CSourceStreamGetMediaTypeNormal)
checkMediaType320x240(&mt);
}

TEST_F(SoftcamStream, getFrameBuffer_must_not_lock_the_filter_state)
{
auto fb = createFrameBufer(320, 240, 60);
SetUpSoftcamStream();

CAutoLock lock(m_softcam->pStateLock()); // 1st lock
std::atomic<int> done{false};
std::thread th([&]
{
// A deadlock occurs if this code attempts to get lock.
m_softcam->getFrameBuffer();
done = true;
});

for (int i = 0; !done && i < 20; i++) { sc::Timer::sleep(0.050f); }
ASSERT_TRUE( done );
th.join();
}

TEST_F(SoftcamStream, releaseFrameBuffer_must_not_lock_the_filter_state)
{
auto fb = createFrameBufer(320, 240, 60);
SetUpSoftcamStream();

CAutoLock lock(m_softcam->pStateLock()); // 1st lock
std::atomic<int> done{false};
std::thread th([&]
{
// A deadlock occurs if this code attempts to get lock.
m_softcam->releaseFrameBuffer();
done = true;
});

for (int i = 0; !done && i < 20; i++) { sc::Timer::sleep(0.050f); }
ASSERT_TRUE( done );
th.join();
}


} //namespace

0 comments on commit a3b1f35

Please sign in to comment.