Lock
("[name]")
Permits
one and only one thread/channel/device to execute inside a block of statements.
Parameters
[name]
Name of the lock created.
Remarks
- By
executing the operation, shared resources ensure that only atomic manipulation is performed on their values.
- Lock()
creates a mutex by this name and locks it.
- Caveat against misuse of this function:
- Minimise the lines of code in the Lock()-Unlock() block.
- Built-in Expressions, String & Number manipulation functions, Database access and the atomic functions can be used in the block.
- Other plugins, sub-functions and voice device dependant commands like Play, Record, Input etc. *SHOULD NOT* be used in code between Lock() and Unlock().
- ONSYSTEMERROR and ONRUNTIMEOUT will result in an immediate jump to these labels. Thus all locks of that port will be automatically released and the entire Lock()-Unlock() block *MAY NOT* execute properly. Hence, no function that may result in an ONSYSTEMERROR jump should be used between Lock() and Unlock().
- ONHANGUP and ONTIMEOUT labels *DO NOT* take effect when locked.
- Coding GOTO or IF THEN GOTO commands after a Lock() will cause an Unlock() of all locks to happen.
- All locks are automatically released when HANGUP command is executed.
Example
:MAIN
.......
.......
// The database operation is done inside a lock - unlock block
// so that only one thread will access it and all other threads
// will wait in Lock() to get its turn.
Lock()
$sql = "select * from EMP where status = 'Q'"
$result = db.RunSQL($db,$sql)
if $result = 0
// query failed.
Unlock()
goto MAIN
endif
// The field status is modified to 'X' so that this record will not be
// taken by any other threads.
$sql = format("update EMP set status = 'X' where AUTOID = %d",$result.AUTOID)
$stat = db.RunSQL($db,$sql)
Unlock()
........
........