%SYSTEM.AutoLock
class %SYSTEM.AutoLock extends %Library.SystemBase
Add a new class which can hold lock references and automatically release them when the oref goes out of scope. This can greatly simplify the locking code and prevent errors if the user forgets to add an error trap to make sure the lock is released in the event of an error.There are two ways to use this, the first uses a macro to get the lock and this lock is released when you leave the current stack frame:
#include %systemInclude Function() public { $$$AutoLock(^Global,"S",10) ; Lock ^Global with shared lock and a timeout of 10 seconds If '$test Write "Lock failed" Quit ; Do work here }
The alternative form explicitly returns an oref from the lock call so you can manage this oref yourself, for example:
#include %systemInclude Function() public { Set lock=$system.AutoLock.Lock("^Global","S",10) If lock=$$$NULLOREF Write "Lock failed",! Quit ; Do work }
Method Inventory
Methods
classmethod Lock(lock As %String, args As %String = "", timeout As %Integer = 0) as %SYSTEM.AutoLock
Passed a lock reference and optional args for the lock and optional timeout
and it will try to obtain this lock. If it fails then it will return "", but if it worked then it will return
an oref which will automatically release the lock when it goes out of scope.