| |||
| Home > The C and C++ Libraries > Writing reentrant and thread-safe code > Use of static data in the C libraries | |||
Static data refers to persistent read/write data that is not stored on the stack or the heap. This persistent data can be external or internal in scope, and is:
at a fixed address, when compiled with --apcs
/norwpi
at a fixed address relative to the static base (sb)
register (r9), when compiled with --apcs
/rwpi.
Libraries that use static data might be reentrant, but this
depends on their use of the __user_libspace static
data area, and on the build options you choose:
When compiled with --apcs /norwpi, read/write
static data is addressed in a position-dependent fashion. This is
the compiler default. Code from these variants is single-threaded
because it uses read/write static data.
For example, library c_a__un has position-dependent
data (see Library naming conventions).
When compiled with --apcs /rwpi,
read/write static data is addressed in a position-independent fashion
using offsets from the static base register sb.
Code from these variants is reentrant, and can be multiply threaded
if each thread uses a different static base value.
For example, library c_a__ue has position-independent
data (see Library naming conventions).
The following describes how the C libraries use static data:
The default floating-point arithmetic libraries fz_* and fj_* do
not use static data and are always reentrant. However, the f_* and g_* libraries
do use static data.
All statically-initialized data in the C libraries is read-only.
All writable static data is zero initialized.
Most C library functions use no writable static
data and are reentrant whether built with default build options
(--apcs /norwpi) or reentrant build options (--apcs /rwpi).
Some functions have static data implicit in their
definitions. You must not use these in a reentrant application unless
you build it with --apcs /rwpi and the caller uses
different values in sb.
See Position independence
qualifiers for
details on the --apcs build options described here.
Exactly which functions use static data in their definitions might change in future releases.