%SYSTEM.AutoLock
Class %SYSTEM.AutoLock Extends %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 }
This will obtain a shared 'S' lock on ^Global with a timeout of 10 seconds and if it fails to get the lock it will write that it failed and exit, otherwise it will hold the lock until after the end of the function where the stack frame is removed. If the work throws an error then even though this routine has no error trapping the lock will still be removed correctly.
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 }
This obtains the same lock but puts the oref into 'lock' local variable, sometimes this can be useful as it can be returned to the caller for it to manage rather than always having the lock removed when you go back from this stack frame as the other method of using this does.
Properties
Args
Property Args As %String [ Private ];
Lock
Property Lock As %String [ Private ];
Methods
%OnNew
Method %OnNew(lock As %String, args As %String = "") As %Status [ Private ]
%OnClose
Method %OnClose() As %Status [ Private ]
Lock
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.