Glossary · Automation software engineering and architecture
Reentrant function
Also known as: Reentrant code
German: Wiedereintrittsfähige Funktion
In programming, a reentrant function is a function that can be interrupted during execution and safely called again, for example by an interrupt or another task, before the first call has finished, because it keeps no unprotected shared or static state.
- Software engineering
In one sentence
A reentrant function can be safely called again while a previous call is still running, because it keeps no unprotected shared state.
Example
The CRC calculation used by both the fieldbus interrupt and the logging task works only on its parameters and local variables, so it is reentrant.
How it applies
- Engineering: Functions called from interrupts or several tasks must be reentrant: they use only local variables and parameters, or protect shared state. Many standard library functions are not reentrant.
- PLC software: Functions in IEC 61131-3 have no persistent internal state, while function block instances do. Using the same instance from two tasks is a common source of errors.
- Documentation: Library reference documentation should state for each function whether it is reentrant and whether it may be called from interrupts or several tasks.
Reentrant vs. thread-safe
A reentrant function can be re-entered even on the same thread, for example by an interrupt. A thread-safe function can be called from several threads at once, possibly by using locks, which may make it non-reentrant.