Menu ▾ ▴

#2732 InitializeCriticalSectionEx() Patch

open
patch (2)
5
5 days ago
6 days ago
No

I have a proposed patch for Windows critical sections in your C library. The Threads.c and picture of use of WINVER and WINNTVISTA macros make sure that InitializeCriticalSectionEx() is compiled only when the code is expected to run on Windows Vista and later, when InitializeCriticalSectionEx() became defined in the Windows API. Even better is that when Threads.c is compiled as Release, InitializeCriticalSectionEx() makes sure that debug info is not added to the Critical Section. Finally, since InitializeCriticalSectionEx() does not throw exceptions, checking the BOOL return value of InitializeCriticalSectionEx() is all that is required to determine memory allocation failure for Critical Sections.

Patch is also visible in my project's SVN Trunk.

https://sourceforge.net/p/stick2xyz/stick2xyz/1126/#diff-2

2 Attachments

Discussion

  • Kevin McBride

    Kevin McBride - 6 days ago

    This newer source file has the _WIN32_WINNT_WINXP macro to ensure that InitializeCriticalSection() is available from the Windows API headers if the Ex() is not available due to compiling for Windows version less than Vista.

     
  • Kevin McBride

    Kevin McBride - 6 days ago

    I wonder if:
    if ( !InitializeCriticalSectionEx(p, 0, LZMA_PRIV_CS_DEBUGINFO) )
    return ERROR_NOT_ENOUGH_MEMORY;

    should return statement on error of Ex() be instead:
    if ( !InitializeCriticalSectionEx(p, 0, LZMA_PRIV_CS_DEBUGINFO) )
    return GetError();

     
  • Igor Pavlov

    Igor Pavlov - 6 days ago

    why that InitializeCriticalSectionEx?
    It's possible that runtime DEBUG_INFO inside CRITICAL_SECTION can be useful for better performance.
    If we don't want exception handling, maybe InitializeCriticalSectionEx(p, 0) can be used instead.

     
    • Kevin McBride

      Kevin McBride - 6 days ago

      I wonder if you meant InitializeCriticalSectionAndSpinCount(p, 0). That too works and like InitializeCriticalSection(), is also available on Windows XP. However, InitializeCriticalSectionEx() is needed if you want to keep debug info out of critical sections during release builds.

       
  • Kevin McBride

    Kevin McBride - 5 days ago

    I have added a macro-based conditional to my copy of the lzma Threads.c that allows debug info to be kept in critical sections even for release builds, if a person compiling the code desires it.

    https://sourceforge.net/p/stick2xyz/stick2xyz/1129/#diff-2

     

Log in to post a comment.