Jump to content
willichan

File Locking with Cooperative Semaphores

Recommended Posts

i find it hard to follow the SQL code, but that's probably because i'm not an SQL expert...

anyway this UDF is a general-purpose tool (also capable of handling SQLite "lock", among many others), so i think youshould post this SQL code in one of the SQLite-related threads (or a new thread), where SQLite users can find it more easily, because the question of "locking" SQLite DB over network is repeatedly asked.

Share this post


Link to post
Share on other sites

i needed to know if, when a semaphore was created successfully, was an ancient semaphore needed to be cleaned-up.

so i added the usage of @extended in _CFS_RequestSemaphore() so it is set to 1 if cleanup of ancient semaphore was due:

Func _CFS_RequestSemaphore($sRequestedResource, $iTimeout = 300, $iClean = 900)
    Local $sCFS_Semaphore = _CFS_ResourseLockName($sRequestedResource)
    Local $iStart = TimerInit()
    Local $iExtended = 0
    While True
        If _CFS_LockAge($sCFS_Semaphore, $iClean) Then ; Too old?
            _CFS_RemoveLock($sCFS_Semaphore)
            $iExtended = 1
        EndIf
        If TimerDiff($iStart) > ($iTimeout * 1000) Then ; have we timed out?
            Return 0
        Else
            If _CFS_CreateLock($sCFS_Semaphore) Then Return SetExtended($iExtended, 1) ; did we get the lock?
        EndIf
        Sleep(100) ; didn't get a lock, pause and try again
    WEnd
EndFunc   ;==>_CFS_RequestSemaphore

Share this post


Link to post
Share on other sites

Zedna,

Sorry for the very late followup, but this scheme doesn't work reliably. You hit the problem of individual local cacheing of the remote DB by SQLite page cache in as many "clients" which have a live connection to it. There are other, lower-level issues with that which all make the scheme pretty risky.

SQLite has no clue whether the open connection is on a local drive or remote mapped drive.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Sorry that I haven't chimed in for a while here.

I am always open to improvements to the UDF.  One thing to clarify though.

Even though I initially wrote this for an SQLite project, it is for locking any resource, not just SQLite databases.  I have also used this to keep multiple scripts that share an INI file from stomping on each other, as well as for scripts running on multiple PCs writing to a single log file.

Share this post


Link to post
Share on other sites

Granted. I was only warning.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Granted. I was only warning.

​I was really more responding to Zedna, but like I said, I am always open to improvements.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...