what is a re-entrant code? again what is re-entrant kernel?
thank you ece tech
what is a re-entrant code?
Code (for example a function) that can be called safely while the same function is already being run by another task/program/function.
Reentrancy is needed, for example, for recursive function calls, or for functions that are called from multiple tasks without any kind of mutex mechanism.
Now here is another question: how do you test for reentrancy of such code?
You don't test for reentrancy. You specifically develop with that in mind. Reentrancy is a factor of what resources the function makes use of, and how it uses them.
An example is that if a function needs to change two global variables (and these variables must always be in sync, then the change must be atomic. An example is allocating/releasing memory.
Another example is that a function that reads a global resource may have to protect the read access, to make sure that two instances of the same function reads the same value and then does an updated based on the same value. For example a function that reads a counter, performs some work based on the counter and then increment the counter. If two instances of the same function checks that there are data available in a read queue, they may both process the same character and insert two results based on the same input.
Note that a reentrant function may only call other reentrant functions.
Another example is that a function that reads a global resource may have to protect ........
Pers post abbreviated: stay away from reentrancy.
Erik
stay away from reentrancy.
To be more precise - on a '51, stay away from situations where you may need reentrant functions (e.g. recursive functions, multithreading, calling the same function in ISRs and the regular program, etc)
Erik is a bit rough, but when specifically talking about a C51 processor (it doesn't have a real generic stack), reentrancy costs a lot.
For a C51, most parameters and function-local variables are converted to global variables by the compiler and linker. A reentrant function can not have it's parameters or local variables converted to global variables since each active instance of the function must have a separate set of variables. That's the rule I mentioned in my earlier post about reentrant functions often needing syncronized read and/or write access to global resources.
In short, making a function reentrant on a C51 processor costs memory, code size and speed. Few functions really need to be reentrant. It is better to create several copies of the same function. One copy to call from the main loop, and one copy to call from an interrupt handler. Especially if the interrupt handler are already using a different register bank.
when specifically talking about a C51 processor
I am posting based on that. I see no reason to mention that this is specifically about the C51 when the architecture window states C51.
It doesn't matter if a thread is about a specific architecture. If the answer sounds general, the OP may assume that the answer really is valid for other architectures too.
View all questions in Keil forum