| United States-English |
|
|
|
![]() |
HP 9000 Computer Systems: HP Pascal/HP-UX Release Notes Version A.10.06 > Chapter 1 New and Changed FeaturesMaking Thread-Safe HP Pascal/HP-UX Routines |
|
There are four major areas of concern when using HP Pascal/HP-UX multithreaded applications. These concerns are:
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. 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.
Because of the language definition of various Pascal I/O statements, it is impossible to make them completely thread-safe. For example,
is replaced by
and
is replaced by
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.
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. 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. 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. 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. 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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||