简体   繁体   中英

How to find if a function is reentrant

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.

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 (eg there is no way to simultaneously read and modify a global). This usually means avoiding using globals, or guaranteeing synchronised access to them - eg 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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM