我正在 Directshow 中编写一个转换过滤器。我查看了 转换过滤器实施。
他们使用 1 个 filter_lock
来保护过滤器的状态,并使用另一个名为 streaming_lock
的锁来保护流线程中使用的资源。但在流线程中,默认实现无需 filter_lock
即可访问过滤器的状态。例如: CTransformInputPin::Receive
>CBaseInputPin::Receive
>CTransformInputPin::CheckStreaming
>CBasePin ::IsStopped
>正在访问 m_State
。
如果他们在 Running_State 并检查流线程中的 m_State
时,rel="nofollow noreferrer">Pause()
?
P/s:我认为避免 m_state 中数据竞争的简单解决方案是使其原子化或为 m_state 创建单独的锁。这也保持了现有的逻辑基本不变。例如:
CTransformInputPin::CheckStreaming() {
// check something...
// check m_state
{
CAutoLock lck1(m_filter->stateLock);
if (IsStopped()) {
return VFW_E_WRONG_STATE;
}
}
}
CTransformFilter::Pause() // or Stop(), Run()
{
CAutoLock lck(&m_csFilter);
// logic....
// when need to set m_state
{
CAutoLock lck1(stateLock);
m_State = State_Paused;
}
}
更新:Windows 似乎将保证对诸如 m_state
之类的单词/双字大小的变量进行原子访问。所以现在对我来说是一种解脱。
I'm writing a transform filter in Directshow. I've looked at the Transform filter implementation.
They use 1 filter_lock
for protecting filter's state and another lock called streaming_lock
for protecting resources used in streaming thread. But in the streaming thread, the default implementation accesses the filter's state without filter_lock
. eg: CTransformInputPin::Receive
>CBaseInputPin::Receive
>CTransformInputPin::CheckStreaming
>CBasePin::IsStopped
>accessing m_State
.
Isn't that data race if they change m_State
in Pause()
while the filter's is in Running_State
and checking the m_State
in streaming thread?
P/s: I think the simple solution to avoid data race in the m_state
is making it atomic or creating separate lock for m_state
. This also keeps the existing logic basically unchanged. eg:
CTransformInputPin::CheckStreaming() {
// check something...
// check m_state
{
CAutoLock lck1(m_filter->stateLock);
if (IsStopped()) {
return VFW_E_WRONG_STATE;
}
}
}
CTransformFilter::Pause() // or Stop(), Run()
{
CAutoLock lck(&m_csFilter);
// logic....
// when need to set m_state
{
CAutoLock lck1(stateLock);
m_State = State_Paused;
}
}
Update: It seems that Windows will guarantee atomic access to word/dword-sized variables like m_state
. So it is now a relief to me.
发布评论