Directshow Transform 过滤器的实现会暴露数据竞争吗? [已解决]

发布于 2025-01-17 12:04:08 字数 1758 浏览 0 评论 0 原文

我正在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文