Strange issue with Slim Reader Writer lock.

Windows specific forum
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Strange issue with Slim Reader Writer lock.

Post by RichAlgeni »

I am getting strange problems with PB 5.45 on Windows 2008 Server using Microsoft's Slim Reader Writer library. So far, I have been unable to recreate the problems, but they occur about one every two weeks. The error number does not appear to be one easily searched.

Here are some examples:

Code: Select all

Error Message:   Unknown error code
Error Code:      C0000264
Code Address:    776E8078
Sourcecode line: 1885
Sourcecode file: M:\Development\PureBasic\Services\OTS_CADData_service.pb

Error Message:   Unknown error code
Error Code:      C0000264
Code Address:    776E8078
Sourcecode line: 1900
Sourcecode file: M:\Development\PureBasic\Services\OTS_CADData_service.pb
The lines are:

Code: Select all

1875    Repeat
1876        WaitForSingleObject_(thisEvent(eventNumber), monitorTime)
1877        If shutDownNow
1878            Break
1879        EndIf
1880 
1881 ; get the data from the list we have built
1882 
1883         AcquireSRWLockShared(*runNumberUpdates)
1884         runListSize = ListSize(runUpdates())
1885         ReleaseSRWLockShared(*runNumberUpdates)
1886 
1887         If runListSize = 0
1888             Continue
1889         EndIf
1890 
1891         ClearList(updateList())
1892 
1893         While Not TryAcquireSRWLockExclusive(*runNumberUpdates)
1894             Delay(100)
1895         Wend
1896 
1897         CopyList(runUpdates(), updateList())
1898         ClearList(runUpdates())
1899 
1900        ReleaseSRWLockExclusive(*runNumberUpdates)
The error happens on the release, afters thousands of successful releases. Does anyone have a clue as to what the error might be?? The service is about 6000 lines long with all of the includes.

Thanks!
Rich
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Strange issue with Slim Reader Writer lock.

Post by RichAlgeni »

May have found the issue with this, I have removed the ability to use 'AcquireSRW...' and will now only use the 'TryAcquire...' functions. I had assumed the 'AcquireSRW...' would block if unsuccessful. I am no longer sure this is the case.

Code: Select all

; see if we are 64 bit or not

CompilerIf #PB_Processor_x64
    Import "..\Windows_libs\x64\kernel32.lib"
        InitializeSRWLock(*globalPointer)
        TryAcquireSRWLockShared(*globalPointer)
        ReleaseSRWLockShared(*globalPointer)
        TryAcquireSRWLockExclusive(*globalPointer)
        ReleaseSRWLockExclusive(*globalPointer)
    EndImport

; else we are 32 bit

CompilerElse
    Import "..\Windows_libs\x86\kernel32.lib"
        InitializeSRWLock(*globalPointer)
        TryAcquireSRWLockShared(*globalPointer)
        ReleaseSRWLockShared(*globalPointer)
        TryAcquireSRWLockExclusive(*globalPointer)
        ReleaseSRWLockExclusive(*globalPointer)
    EndImport
CompilerEndIf

Code: Select all

While Not TryAcquireSRWLockShared(*globalPointer)
    Delay(20)
Wend
...
ReleaseSRWLockShared(*globalPointer)

While Not TryAcquireSRWLockExclusive(*globalPointer)
    Delay(50)
Wend
...
ReleaseSRWLockExclusive(*globalPointer)
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Strange issue with Slim Reader Writer lock.

Post by RichAlgeni »

I think I have figured this out. The error is STATUS_RESOURCE_NOT_OWNED.

It looks like the issue is that you cannot move immediately from a shared lock to an exclusive lock? This doesn't make a lot of sense to me, but when I changed my code to using exclusive locks only, the problem seems to have disappeared.

Here are some helpful links:
http://alax.info/blog/1163
https://docs.microsoft.com/en-us/window ... -not-owned
https://gist.github.com/mbrownnycnyc/24 ... fa37bf9063

Here is my old code, that threw the exception. You'll see I do the shared lock first, then the exclusive lock:

Code: Select all

; get the data from the list we have built

Repeat
    While Not TryAcquireSRWLockShared(*runNumberUpdates)
        Delay(20)
    Wend

    runListSize = ListSize(runUpdates())

    ReleaseSRWLockShared(*runNumberUpdates)

    If runListSize = 0
        Continue
    EndIf

    While Not TryAcquireSRWLockExclusive(*runNumberUpdates)
        Delay(20)
    Wend

    CopyList(runUpdates(), updateList())
    ClearList(runUpdates())

    ReleaseSRWLockExclusive(*runNumberUpdates)

    ... do work here
Forever
Here is the new code that has not thrown the C0000264, STATUS_RESOURCE_NOT_OWNED exception.

Code: Select all

; get the data from the list we have built

Repeat
    While Not TryAcquireSRWLockExclusive(*runNumberUpdates)
        Delay(20)
    Wend

    runListSize = ListSize(runUpdates())

    If runListSize > 0
        CopyList(runUpdates(), updateList())
        ClearList(runUpdates())
    EndIf

    ReleaseSRWLockExclusive(*runNumberUpdates)

    If runListSize = 0
        Continue
    EndIf

    ... do work here
Forever
Note that code that executes shared locks on their own, seem to work fine.
Post Reply