C18 Reentrancy

nobody wrote on Tuesday, January 11, 2005:

Since C18 isn’t reentrant, won’t the rtos cause problems if the same function is called in two different tasks?

Do I have to take care of this myself?

I’m particularly thinking about things like sprintf and the math library.  I don’t think the C standard guarantees reentrancy.

nobody wrote on Tuesday, January 11, 2005:

I have been investigating C18 re-entrancy, and I’m getting more confused by the minute :slight_smile:

From the manual:
"MPLAB C18 supports parameters and local variables allocated either on the software
stack or directly from global memory. The static keyword places a local variable or
a function parameter in global memory instead of on the software stack.1 In general,
stack-based local variables and function parameters require more code to access than
static local variables and function parameters (see Section 2.3.2 static
Function Arguments). Functions that use stack-based variables are more flexible in
that they can be reentrant and/or recursive."

Still searching…

rtel wrote on Tuesday, January 11, 2005:

The areas of global memory used by the compiler are saved as part of the task context so you need not worry about these directly - only to ensure that the correct number of bytes are stored as per the WEB documentation.

There are some library functions that are very rarely reentrant.  malloc(), free(), strtok() for example.  Also anything that uses errno if that is of concern to you.  You always need to take care when using these.  If in doubt you can suspend and resume the scheduler around a function call - this will allow interrupts to be processed but guarantee that a context switch does not occur.

sprintf may struggle on a PIC as it normally uses a fair amount of stack.

The section from the manual you quote seems to refer to your own function parameters and the C standard use of the static modifier.  The problem with the C18 is it’s use of memory when performing simple routines like adding two integers.

I am going to get the latest C18 version and see how things are different.


rtel wrote on Tuesday, January 11, 2005:

Just for completion - from the other thread:

"With the new version, the MATH_DATA and .tmpdata sections are the same size, suggesting they are used in the same way. Having just compiled and run the first and third FreeRTOS.org demo applications they appear to execute correctly with V2.4."