Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP 9000 Computer Systems: HP Pascal/HP-UX Release Notes Version A.10.06 > Chapter 1 New and Changed Features

Making Thread-Safe HP Pascal/HP-UX Routines

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

There are four major areas of concern when using HP Pascal/HP-UX multithreaded applications. These concerns are:

  • Outer block limitations

  • Input/output

  • Heap management

  • Other libraries (Trap and Unwind libraries)

Not all Pascal routines and constructs that use these features are thread-safe. For example, Pascal I/O procedures such as APPEND, CLOSE, READLN, and WRITELN are not thread-safe. Additionally, some string manipulation code uses the heap for temporary storage.

Outer Block Limitations

HP Pascal/HP-UX multithreaded applications require a non-Pascal (such as a C or C++) outer block.

To convert an existing HP Pascal/HP-UX outer block to C, see Chapter 2 of HP Pascal/HP-UX Programmer's Guide. In particular, the $SUBPROGRAM$ compiler option must be changed to $EXTERNAL$, and one module must have the $SUBPROGRAM; GLOBAL$ compiler options. Otherwise, Pascal modules must be used.

After the outer block is converted, the C outer block must call the routine documented in the example in Chapter 9, "How To Do Pascal I/O with a Non-Pascal Outer Block" in HP Pascal/HP-UX.

NOTE: Failure to initialize the Pascal Runtime Library with the routine in the example will probably cause runtime aborts with a NIL pointer.

Input/Output

Because of the language definition of various Pascal I/O statements, it is impossible to make them completely thread-safe. For example,



   WRITELN(f,a,b,c);

is replaced by



   WRITE(f,a); WRITE(f,b); WRITE(f,c); WRITELN(f);

and



   READDIR(f,k,x);

is replaced by



   SEEK(f,k); READ(f,x);

In a threaded application, the input or output could be interspersed.

Because of the language design limitation, you must do your own locking for each file or use a separate file for each thread.

NOTE: The Pascal Runtime Library assumes that you are coordinating your I/O to files. It does NO locking whatsoever. If you fail to do this, the result could be interspersed output, or worse.

There is another class of routines that are inherently unsafe, since the values that they return may be invalid as soon as they are returned. This class includes EOF, EOLN, LASTPOS, LINEPOS, and MAXPOS. These routines and the GET and PUT routines may require locking around multiple I/O statements or where the buffer variable f is referenced.

File Control Block List

The only limited locking that the library does is to protect the global linked-list of Pascal file control blocks. Opening or closing a file adds to or deletes from this list. The Pascal I/O module maintains this list so that files can be closed on routine exit, heap deallocation, or nonlocal GOTO and ESCAPE. Because the compiler does this implicitly, these operations were made thread-safe.

File Control Blocks

The file control blocks themselves are not protected.

There is a control block for each opened file. Reading, writing, and other operations on a file search the list and update the file's control block or return information from the control block. Each control block contains its own buffer for file reading and writing.

The file control blocks must reside in a shared data area. Because file control blocks are accessed when the file list is traversed, the control blocks must be accessible to all threads in the task.

Without synchronization, a control block can become corrupt. You must synchronize threaded applications by using mutexes and condition variables to protect files that are shared among threads. Otherwise, a file must be accessed by only one thread. This includes the built-in files INPUT and OUTPUT.

Refer to HP-UX Linker and Libraries Online User Guide for guidelines.

Heap Management

The heap routines NEW, DISPOSE, MARK, RELEASE, p_getheap, and p_rtnheap (described in Chapter 6 of HP Pascal/HP-UX Programmer's Guide) are all thread-safe. The only important consideration is that the effects of MARK and RELEASE are shared by all threads. If one thread does a MARK and RELEASE, it affects all threads.

Other Libraries (Trap and Unwind Libraries)

Certain Pascal constructs depend on libraries other than the Pascal Runtime Library, in particular, the routines documented in Chapter 11 of HP Pascal/HP-UX Programmer's Guide. These include ESCAPE, ESCAPECODE, XARITRAP, ARITRAP, HPENBLTRAP, and XLIBTRAP.

Support for a per-thread ESCAPECODE is provided.

The four trap routines only provide the various masks and plabels on a global basis. It is expected that these routines are called before any user threads are created.

The only other construct that is important to note is that certain string manipulation operators and functions use the heap for temporary storage. This is thread-safe but may cause a performance problem. You can use the $STRINGTEMPLIMIT$ compiler option to allocate space in the stack.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.