Fortran THREADLOCAL common blocks are implemented using the thread-specific data facilities that are defined by the POSIX pthreads library. For additional information about thread-specific data areas, please refer to AIX documentation on threads programming.
Internally, the storage for the thread-specific common block is allocated dynamically by the Fortran run-time library. The Fortran run-time library maintains a control structure that holds information about the common block. This control area is an external structure whose name is the name of the common block.
For example, if you declare a common block in Fortran as the following:
common /myblock/ i !ibm* threadlocal /myblock/
the Fortran compiler creates an external structure (or common area) that is named myblock, which contains control information about the thread-specific common block. The control structure has the following layout, and would be coded as such in C:
typedef struct { pthread_key_t key; int flags; void *unused_1; int unused_2; } FORT_LOCAL_COMMON; extern FORT_LOCAL_COMMON myblock;
The "key" field is a unique identifier that describes a threadlocal data area. Every threadlocal common block has its own key. The "flags" field indicates whether a key has been obtained for the common block. Within a C function, you should use the "key" in the control block in a call to pthread_getspecific to obtain the thread-specific address of the threadlocal common area.
! Example 1: "fort_sub" is invoked by multiple threads. This is an invalid example ! because "fort_sub" and "another_sub" both declare /block/ to be THREADLOCAL, ! they intend to share the common block, but they are executed by different threads. SUBROUTINE fort_sub() COMMON /block/ j INTEGER :: j !IBM* THREADLOCAL /block/ ! Each thread executing fort_sub ! obtains its own copy of /block/ INTEGER a(10) ... !IBM* INDEPENDENT DO index = 1,10 CALL another_sub(a(i)) END DO ... END SUBROUTINE fort_sub SUBROUTINE another_sub(aa) ! Multiple threads are used to execute another_sub. INTEGER aa COMMON /block/ j ! Each thread obtains a new copy of the INTEGER :: j ! common block: /block/. !IBM* THREADLOCAL /block/ ... aa = j ! The value of 'j' is undefined. END SUBROUTINE another_sub
For more information on the THREADLOCAL directive, see "Directives" in the XL Fortran for AIX Language Reference.