MF Dinosaur
MF Dinosaur

Reputation: 93

Is a z/OS PL/I CONTROLLED variable preserved between separate invocations of a procedure?

Is a z/OS PL/I CONTROLLED variable preserved between separate invocations of a procedure? Let’s suppose that we need a counter that is internal to a subroutine and preserved across invocations. The easiest way to do it would be to use a static variable initialized to zero and incremented on each entry to the subroutine. But you can’t do that if the program has to be reentrant. So the question is whether we have access to a controlled variable that was allocated in a previous call. Would the following code work?

PROC1: PROCEDURE OPTIONS(MAIN);
...
CALL A;
...
A: PROCEDURE;
DECLARE COUNT CONTROLLED ALIGNED FIXED BIN(15);
IF (ALLOCATION(COUNT) = 0)
    THEN ALLOCATE COUNT INIT(1);
    ELSE COUNT = COUNT + 1;
...
END A;

END PROC1;

According to the PL/I Language Reference, after you ALLOCATE a variable, you do not need to FREE it (though that is generally good practice), and “All controlled storage is freed at the end of the program.” It doesn’t say that storage is freed at the end of the block. The PL/I Programming Guide provides some clues in the chapter Using PLIDUMP in the Locating Controlled Variables section, but it is not definitive. It says that the key to locating a controlled variable is to find its anchor. With NORENT WRITABLE there is an anchor in static storage. With NORENT NOWRITABLE(FWS) there is an address to an anchor automatic storage. (There is an extra level of indirection.) With NORENT NOWRITABLE(PRV) there appears to be a static table with an offset into a private table for each controlled variable. In other words, depending on the processing options, maybe the variable is accessible, and maybe it isn’t. It doesn’t say anything about using the RENT option. Any thoughts?

Upvotes: 3

Views: 913

Answers (1)

Deepa
Deepa

Reputation: 411

As per the PL/I Programming Guide compile time option "RENT", Your code is "naturally reentrant" if it does not alter any of its static variables.

The RENT option specifies that the compiler is to take code that is not naturally reentrant and make it reentrant.

Thus, you can increment STATIC variable on each entry to subroutine if the program is compiled with RENT option.

Please refer to this link => Rent Option from PL/I Programming Guide

As per "PL/I Structured Programming" by J.K. Hughes, A REENTRANT procedure may be called by other procedures asynchronously. For example task B invokes SQRT function. While this function is in process of computing square root, task A (having higher execution priority than task B) needs to gain control of the system and use SQRT function. The SQRT function is interrupted and intermediate results for task B saved; then task A uses SQRT function. When task A completes its execution, control is returned to task B at the point where it was interrupted. Then, task B completes its use of SQRT function.

Upvotes: 1

Related Questions