GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
libgfortran

Avoid libatomic calls for unit file ID

Fixes libgfortran to avoid unnecessary libatomic calls for unit file IDs on 32-bit targets lacking lock-free 64-bit atomics.

This change addresses a bug in libgfortran where accesses to 64-bit file_dev and file_ino fields of gfc_unit incorrectly used __atomic builtins, leading to calls into libatomic on 32-bit targets without lock-free 64-bit atomics. This caused crashes as libgfortran doesn’t link libatomic. The fix reverts a previous change that tested for 64-bit types, and instead uses atomic accesses for the file ID only when 64-bit atomics are lock-free, preventing incorrect linking issues and ensuring thread safety.

In Details

PR118935's fix incorrectly relied on HAVE_ATOMIC_FETCH_ADD to guard accesses to gfc_unit's 64-bit file_dev and file_ino fields. This configure check only tests for int atomics. On 32-bit targets without lock-free 64-bit atomics, this resulted in calls to libatomic, which libgfortran does not link. The fix reverts r17-6152's attempt to test 64-bit types, which also broke atomicity for the unit waiting count. Instead, it reintroduces a check specifically for lock-free 64-bit atomics for the file ID access, ensuring correctness on affected architectures.

For Context
libgfortran
The runtime library for the GNU Fortran compiler (gfortran). It provides functions for input/output, intrinsic procedures, and other runtime services.
libatomic
A library that provides atomic memory access operations, useful for multi-threaded programming. Compilers may use it when built-in atomic operations are not available or suitable.
gfc_unit
A data structure within libgfortran that represents an opened Fortran file unit, managing its state and associated metadata.
HAVE_ATOMIC_FETCH_ADD
A configuration macro that indicates whether the target system supports the atomic fetch-and-add operation, typically used for thread-safe incrementing.
lock-free atomics
Atomic operations that can be performed without requiring locks or synchronization primitives, typically implemented directly using special CPU instructions for better performance in concurrent environments.
Filed Under: libgfortranbugfixatomics32-bit