LOCK Statement

Sets an execution lock, so another program cannot set the same lock until the program that set the lock unlocks it or exits.

Syntax

LOCK lock-val {THEN statement(s)} {ELSE statement(s)}

Syntax Elements

lock-val specifies which execution lock is to be set.

The DataBasic and Proc processors use the same 256 execution locks, numbered 0 to 255.

statement(s) is either a THEN or ELSE clause (or both). The THEN and ELSE clauses are optional.

Operation

If the specified lock has already been set by another concurrently running program, and an ELSE clause is not provided, execution halts temporarily, until the lock is reset by the other program.

If an ELSE clause has been provided, the statements in the ELSE clause are executed and the program continues.

Another DataBasic program cannot set the same lock, until it is reset with the UNLOCK statement, by the program that originally issued the LOCK, or until that program exits.

To lock single items in a file, use the READU, READVU and MATREADU statements.

Example 1

LOCK 15 ELSE STOP

Sets execution lock 15. If lock 15 is already set, the program terminates.

Example 2

LOCK 2

Sets execution lock 2. If lock 2 is already set, program execution is halted until the lock is unlocked by the same program that locked it.

Example 3

LOCK 10 ELSE PRINT X; GOTO 5

Sets execution lock 10. If it is already set, X is printed and control transfers to statement 5.

Example 4

Program A

Program B

LOCK 54
OPEN 'INVENTORY'
READ ... etc.
(process)
WRITE ... etc.
UNLOCK 54
(process)
   WRITE... (file x)
50 LOCK 54 ELSE GO 70
   OPEN 'INVENTORY' ...
   (process)
   WRITE...
70 (other code)
   .
   .
   GOTO 50

Program A sets execution lock 54. The program then opens the 'INVENTORY' file and performs various processing tasks with READ and WRITE operations to the 'INVENTORY' file. When finished with the 'INVENTORY' file, Program A unlocks execution lock 54.

Program B also sets execution lock 54 before opening the 'INVENTORY' file. If this happens while Program A has lock 54 set, Program B executes the ELSE clause and performs some other programming tasks. Later, Program B returns to statement label 50 and attempts to set execution lock 54. If by this time, Program A has executed the UNLOCK statement, Program B sets execution lock 54 and proceeds to open the 'INVENTORY' file.

The efficiency of the system could be enhanced if both programs used READU statements to lock only the item within the file to be referenced and updated rather than the entire file.

This example illustrates how an execution lock does not, in itself, lock a particular entity such as a file, group, item, process, or whatever. Only DataBasic and Proc program segments that reference the same execution lock number are actually locked.