什么是事件句柄?

发布于 2024-08-23 12:37:50 字数 856 浏览 2 评论 0 原文

我在一些继承的 C# winforms 代码中遇到了泄漏句柄问题(“没有足够的配额来处理此命令。”),因此我使用了 Sysinternals 的 句柄工具来追踪它。事实证明是事件句柄泄漏了,所以我尝试用谷歌搜索它(尝试了几次才找到查询未返回“您的意思是:事件处理程序?”)。根据张俊峰,事件句柄是通过使用 Monitor 生成的,就事件句柄处理和同步原语而言,可能存在一些奇怪的规则。

我不完全确定泄漏句柄的来源完全是由于长寿命对象调用大量同步内容所致,因为此代码还处理 HID 接口和大量 win32 封送和互操作,并且没有执行任何操作我知道的同步。不管怎样,我只是要在 Windbg 中运行它并开始追踪句柄的来源,并且还花费大量时间学习这部分代码,但是我很难找到有关事件的信息手柄是第一位的。

事件内核对象的 msdn 页面只是链接到通用同步概述...那么什么是事件句柄,它们与互斥体/信号量/其他什么有什么不同?

I had a leaking handle problem ("Not enough quota available to process this command.") in some inherited C# winforms code, so I went and used Sysinternals' Handle tool to track it down. Turns out it was Event Handles that were leaking, so I tried googled it (took a couple tries to find a query that didn't return "Did you mean: event handler?"). According to Junfeng Zhang, event handles are generated by the use of Monitor, and there may be some weird rules as far as event handle disposal and the synchonization primitives.

I'm not entirely sure that the source of my leaking handles are entirely due to simply long-lived objects calling lots of synchronization stuff, as this code is also dealing with HID interfaces and lots of win32 marshaling and interop, and was not doing any synchronization that I was aware of. Either way, I'm just going to run this in windbg and start tracing down where the handles are originating from, and also spend a lot of time learning this section of the code, but I had a very hard time finding information about what event handles are in the first place.

The msdn page for the event kernel object just links to the generic synchronization overview... so what are event handles, and how are they different from mutexes/semaphores/whatever?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚你发端 2024-08-30 12:37:50

NT 内核使用事件对象来允许信号传输到等待该信号的实体。互斥体和信号量也是可等待的内核对象(内核调度程序对象),但具有不同的语义。我唯一一次遇到它们是在驱动程序中等待 IO 完成时。

所以我对你的问题的理论可能是驱动程序有缺陷,或者你是否依赖于专门的硬件?

编辑:更多信息(来自Windows Internals 5th Edition - 第 3 章系统机制)

一些内核调度程序对象(例如互斥量、信号量)具有概念所有权。因此,当发出释放信号时,一个等待线程将被释放,从而抢占这些资源。而其他人则必须继续等待。事件不被拥有,因此可以由任何线程重置。

还有三种类型的事件:

  • 通知:发出信号时,所有等待线程都被释放
  • 同步:发出信号时,释放一个等待线程,但事件被重置
  • 按键:发出信号时同一进程中的一个等待线程信号器被释放。

我了解到的另一件有趣的事情是 关键部分 (c# 中的锁原语) 实际上不是内核对象,而是根据需要从键控事件、互斥锁或信号量中实现。

The NT kernel uses event objects to allow signals to transferred to entities that wait on the signal. A mutex and a semaphore are also waitable kernel objects (Kernel Dispatcher Objects), but with different semantics. The only time I ever came across them was when waiting for IO to complete in drivers.

So my theory on your problem is possibly a faulty driver, or are you relying on specialised hardware?

Edit: More info (from Windows Internals 5th Edition - Chapter 3 System Mechanics)

Some Kernel Dispatcher Objects (e.g. mutex, semaphore) have the of concept ownership. So when signalled the released one waiting thread will be released will grab these resources. And others will have to continue to wait. Events are not owned hence are available to be reset by any thread.

Also there are three types of events:

  • Notification : On signalled all waiting threads are released
  • Synchronisation : On signalled one waiting thread is released but the event is reset
  • Keyed : On signalled one waiting thread in the same process as the signaller is released.

Another interesting thing that I've learned is that critical sections (the lock primitive in c#) are actually not kernel objects, rather they are implemented out of a keyed event, or mutex or semaphore as required.

流绪微梦 2024-08-30 12:37:50

如果您正在谈论内核事件对象,那么事件句柄将是系统保留在该对象上的句柄(Int),以便其他对象可以引用它。 IE 保留它的“句柄”。

希望这有帮助!

If you're talking about kernel Event Objects, then an event handle will be a handle (Int) that the system keeps on this object so other objects can reference it. IE Keep a 'handle' on it.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文