I am trying to use a function from an opensource library. This function is not exposed outside by default (so I am assuming it is not utility function provided directly by opensource library), but I need this function to solve some problem in my code, so somehow I have made some changes in makefile to expose the funtion from library.
Now the question is how to make sure this function is re-entrant.
I am not able to find out with my naked eye, because it is calling a number of functions internally.
To be precise I am curious to know if there is any tool available or any option in GDB to check if my function is only using local variables and it is not changing global variables.
A re-entrant function is one that can be interrupted (typically during thread context-switching), and re-entered by another thread without any ill-effect. Functions that rely on a local variable are considered re-entrant due to the fact that their variables are safely encapsulated between threads.
A reentrant function must not call non-reentrant functions. A non-reentrant function can often, but not always, be identified by its external interface and its usage. For example, the strtok subroutine is not reentrant, because it holds the string to be broken into tokens.
Non-reentrant functions are functions that cannot safely be called, interrupted, and then recalled before the first call has finished without resulting in memory corruption.
The way to determine if a function is reentrant is to analyse what it does.
1) It does not access globals unless the operations on those globals are atomic (e.g. there is no way to simultaneously read and modify a global). This usually means avoiding using globals, or guaranteeing synchronised access to them - e.g. all code that modifies and reads a global holds a mutex until done, so the operations are serialised. Or code th
2) The code is not self-modifying (fortunately, self-modifying code is relatively rare in standard C or C++).
3) It does not call other functions that are not reentrant functions (which includes a fair number of functions in the standard library) or programs (e.g. multiprocessing usually complicates control needed to ensure reentrancy).
I'm not aware of any specific tool to analyse a function to determine reentrancy. Usually a function is designed to be reentrant (or not). In practice, if there is no documentation saying a function has been designed to be reentrant, it is a fair bet it is not reentrant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With