Replacing Readlocks with Interlocks

In the past, many programmers have avoided the processor overhead of PROCURE and LIBERATE statements through techniques that make use of the READLOCK intrinsic instead. The READLOCK intrinsic is available in ALGOL and NEWP. The use of READLOCK should be eliminated. Other alternatives are available. A swap statement replaces the use of the READLOCK intrinsic for swapping values where locking is not required. If locking is required, the READLOCK intrinsic is replaced by an interlock function. The interlock functions are a suitable replacement with low processor overhead. The following example shows how interlocks can replace readlocks in one locking situation.

Using readlocks:

  REAL
      THELOCK,
      THEOWNER;
  EVENT
      THEEVENT;
  DEFINE
      ACQUIRETHELOCK =
      BEGIN
          IF READLOCK(PROCESSID,THELOCK) NEQ 0 THEN
              DO
                  PROCURE(THEEVENT) % or WAITANDRESET(THEEVENT)
              UNTIL READLOCK(-1,THELOCK) EQL 0;
          THEOWNER := PROCESSID;
      END#,
      RELINQUISHTHELOCK =
      BEGIN
          THEOWNER := 0;
          IF READLOCK(0,THELOCK) NEQ PROCESSID THEN
              LIBERATE(THEEVENT); % or CAUSE(THEEVENT);
      END#;

Using interlocks:

INTERLOCK
   THELOCK;
DEFINE
   ACQUIRETHELOCK = LOCK(THELOCK)#,
   RELINQUISHTHELOCK = UNLOCK(THELOCK)#;