[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]


4 Writing Thread-Safe Libraries

This chapter addresses writing thread-safe libraries. Typically, these libraries do not use threads themselves. However, they need to be thread-safe because they may be called by applications that do use threads.

DECthreads provides the tis library to help you write efficient thread-safe code. The tis library provides thread- independent synchronization services that are very efficient when your code runs in a nonthreaded program (avoiding interlocked instructions and memory barriers), and provides full DECthreads synchronization in a threaded program.

It is not difficult to create thread-safe code and typically easy to convert existing code from unsafe to thread-safe, if the source code is available. The first consideration is whether the language compiler used in translating the source code supports reentrant code.

Most Ada compilers generate inherently reentrant code because Ada supports multithreaded programming. Although the C, Pascal, and BLISS languages do not support multithreaded programming directly, compilers for those languages generally create reentrant code. However, the Fortran and COBOL languages are defined in such a way that compilers may make implicit use of static storage, and such compilers do not generate reentrant code. It is difficult to write reentrant code in a nonreentrant language.

tis is designed to be fast when used without threads. For example, locking a mutex is essentially just setting a bit, and unlocking the mutex is just clearing the bit. However, all tis operations require a call into the tis library. That's because when DECthreads is initialized most of the tis functions are revectored so that subsequent calls use the normal DECthreads SMP- safe operations.

Once revectored, a tis_mutex_lock() call will function exactly as if pthread_mutex_lock() had been called. The transition from tis stubs to full DECthreads operation is transparent to library code using tis. If DECthreads is dynamically activated while a tis mutex is locked, the mutex can be unlocked normally.

The tis interface deliberately provides no way to determine whether DECthreads is active within the process. Thread-safe code should always act as if multiple threads may be active, because to do otherwise would inevitably result in problems (especially when you consider that DECthreads can be dynamically activated into the process at any time).

On Digital UNIX systems, tis is a part of libc.so. On OpenVMS systems, tis is implemented by CMA$TIS_SHR.EXE, which is included in IMAGELIB.OLB so the Linker can automatically resolve tis symbol references.



[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]