- Okay so...listen up. First off: When you acquire a lock such as a fast mutex, you...
authorAleksey Bragin <aleksey@reactos.org>
Mon, 12 Nov 2007 19:00:26 +0000 (19:00 +0000)
committerAleksey Bragin <aleksey@reactos.org>
Mon, 12 Nov 2007 19:00:26 +0000 (19:00 +0000)
commit0b529c1bb5c978beaaeffa30d73304dc9e7a8d9e
tree84bbc073cbb63caa70ddc4ad40f1f6ff90b8609c
parent2653004ec0be246955dfd9521133c1bebd3cfe62
- Okay so...listen up. First off: When you acquire a lock such as a fast mutex, you should never acquire it recursively.
  For example, when you handle a page fault in a section, then page fault while handling that page fault (which is perfectly okay),
  you shouldn't be trying to re-acquire the address space lock that you're already holding. After this fix, this scenario works
  and countless others. Apps like QTInfo now work and load, and PictureViewer doesn't BSOD the system anymore. I've fixed this by changing
  the lock to a pushlock. It not only increases speed inside the memory manager significantly (such as during page fault handling), but
  does allow recursive acquisition without any problems.
- Now if that wasn't bad enough, here's a couple more tips. Fast Mutexes actually require APC_LEVEL to be effective. If you're going
  to be using a Fast Mutex and calling it with the "Unsafe" version, then don't expect anything to work. Also, using functions like
  "CcTryToAcquireBrokenMutex" where correct code is duplicated then hacked to work isn't a big help either. And that's not all. Fast Mutex
  disables kernel APCs by setting the KernelApcDisable flag on, and it's expected that the count inside the fast mutex will match the count
  inside the thread. In other words, LOCK ACQUISITION AND RELEASE MUST BE ORDERED. You can't acquire LOCK A and B, and then release lock A
  and B, because that leads to deadlocks and other issues. So of course, the Cache Manager acquired a view lock, then acquired a segment lock,
  then released the view lock, then released the segment lock, then re-acquired the view lock. Uh, no, that won't work. You know what else
  doesn't work so well? Disabling APCs about 6-9 times to acquire a single lock, and using spinlocks in the same code path as well. Just how
  paranoid are you about thread safety, but still manage to get it wrong? Okay, so we've got recursion, out-of-order lock acquision and
  release, made-up "broken" acquire functions, and using a lock that depends on APC_LEVEL at PASSIVE_LEVEL. The best part is when Cc builds
  an array of cache segments, and locks each of them... then during release, the list gets parsed head-first, so the first acquired locks
  get released first. So locks a, b, c, d get acquired, then a, b, c, d get released. Great! Sounds about right for ReactOS's Cache Manager
  design. I've changed the view lock to a guarded mutex -- which actually properly disables APCs and works at PASSIVE_LEVEL, and changed the
  segment locks to be push locks. First it'll be 10 times faster then acquiring a bazillion fast mutexes, especially since APCs have already
  been disabled at this point, and it also allows you to do most of the stupid things the Cache Manager does. Out-of-order release is still
  not going to work well, so eventually on a multi-processor machine the code will completely die -- but at least it'll work on UP for now.
  In the end, this makes things like the Inkscape installer and Quicktime Installer to work, and probably countless other things that generated
  ASSERTS in the fast mutex code.
  -- Alex Ionescu

svn path=/trunk/; revision=30401
reactos/ntoskrnl/cc/fs.c
reactos/ntoskrnl/cc/pin.c
reactos/ntoskrnl/cc/view.c
reactos/ntoskrnl/include/internal/cc.h
reactos/ntoskrnl/include/internal/ex.h
reactos/ntoskrnl/include/internal/ke.h
reactos/ntoskrnl/mm/aspace.c
reactos/ntoskrnl/mm/section.c