COBOL Programs in SOA – Using Multithreading
Originally COBOL didn’t support multithreading thereby heavily limiting the possibility of using COBOL subroutines as building blocks for web applications. With Enterprise COBOL, IBM started providing toleration level support of POSIX threads and asynchronous signals.
With THREAD compiler option, a COBOL programs can be run in multiples threads (i.e., it can be called in more than one thread in a single process) – under batch, TSO, IMS or UNIX (and not in CICS). A multi-threaded program needs to be recursive (RECURSIVE clause in PROGRAM-ID) and also compiled and linked with the RENT option.
It is important to note that COBOL doesn’t manage the threads and rather expect the application server or the calling program (in Java, C/C++, PL/I) to manage the same. The threaded application must run within a single Language Environment enclave.
With THREAD option, the storage and control blocks get appropriately allocated on invocation basis, rather than per program – making them thread-safe. Also additional serialization logic is automatically generated (which in-turn can degrade performance).
Recursive call is where a called program can directly or indirectly execute its caller. For example, program X calls program Y, program Y calls program Z, and program Z then calls program X (the original caller). The persistence of the data for each call depends on whether it is in local storage or working storage.
- Multiple threads that run simultaneously share a single copy of the WORKING-STORAGE data (statically allocated and initialized on first entry to a program and is available in the last-used state for the recursive invocations).
- A separate copy of LOCAL-STORAGE data is allocated and made available for each call of program (or invocation of a method) which gets released on returning from the program. If the VALUE clause is specified, the data is re-initialized to the same for every invocation.
If THREAD compiler option is used, data that is defined in the LINKAGE SECTION is not accessible on subsequent invocations of the program. The address of the record in the Linkage section must be reestablished for each execution instance. Pre-initialization (if required) should be done using LE services (CEEPIPI interface rather than COBOL specific interfaces).
In a multithreaded environment, a program cannot CANCEL a program that is active on any thread (cancel results in severity-3 LE condition). A multithreaded program can be ended by using GOBACK, EXIT PROGRAM, or STOP RUN:
- When you use GOBACK from the first program in a thread, the thread is terminated. If that thread is the initial thread in an enclave, the entire enclave is terminated.
- With EXIT program the thread is not terminated unless the program is the first (oldest) one in the thread.
- With STOP RUN the entire LE enclave is terminated, including all threads executing within the enclave.
Multi-threaded COBOL programs can have file operations on QSAM, VSAM and sequential files. Automatic serialization happens using the implicit lock on the file definition and during the input and output operations.
To avoid serialization problems when accessing a file from multiple threads, it is recommended that the data items that are associated with the file (such as file-status data items and key arguments) are defined in the LOCAL-STORAGE SECTION.
Similarly to avoid coding of own serialization logic (using POSIX APIs), IBM suggests the following usage patterns:
- For input: OPEN, READ, Process the Record, CLOSE
- For output: OPEN, Construct the output, WRITE, CLOSE
In a threaded application, the COBOL program can be interrupted by asynchronous signals, which the program should be able to tolerate. Alternatively using C/C++ functions, the interrupts can be disabled by setting the signal mask appropriately.
Other factors worth noting are:
- Though you cannot run multithreaded applications in the CICS environment, the program compiled with THREAD option can run in CICS in a single thread.
- There are quite a few restrictions and behavioral differences when THREAD option is used
- Enabling the existing COBOL programs to be multi-threaded can range from just recompiling at best to modifying program logic that require thorough testing.
Using multi-threading feature of COBOL, existing COBOL programs from legacy applications can be effectively utilized in the web applications (directly!).