Keil Logo

Reentrant Functions

A reentrant function may be shared by several processes at the same time. When a reentrant function is executing, another process can interrupt the execution and then begin to execute that same reentrant function.

By default, the C251 compiler generates static code, which means that functions cannot normally be called recursively or in a fashion which causes reentrancy. The advantage of static code is that function arguments and local variables are stored in fixed memory locations which allows the 251 MCU fast variable access.

The reentrant function attribute allows you to declare functions that may be reentrant and, therefore, may be called recursively. For example:

int calc (char i, int b) reentrant  {
  int  x;
  x = table [i];
  return (x * b);

#pragma FUNCTIONS (reentrant)         /* C251 generates reentrant CODE */
int func (unsigned char index)  {
  return (table[index);

#pragma FUNCTIONS (static)            /* select static CODE again       */

Reentrant functions may be called recursively and may be called simultaneously by two or more processes. Reentrant functions are often required in real-time applications or in situations where interrupt code and non-interrupt code must share a function.

As in the above example, you may selectively define (using the reentrant attribute or the FUNCTIONS directive) functions as reentrant. Reentrant code uses the 251 hardware stack area for non register parameters and automatic variables. Refer to Hardware Stack for a detailed explanation about the stack layout in reentrant code. For reentrant functions a few restrictions exist due to the 251 hardware architecture:

  • bit type function arguments may not be used. Local bit scalars are also not available. The reentrant capability does not support bit-addressable variables.
  • Reentrant functions must not be called from alien functions. Reentrant code cannot use the alien attribute since the PL/M-51 argument passing is static only.


  • Variable access using the 251 hardware stack is slower than accessing static memory. For this reason, use reentrant functions only when required.
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.