views:

704

answers:

6

Most of the times, the definition of reentrance is quoted from Wikipedia:

A computer program or routine is described as reentrant if it can be safely called again before its previous invocation has been completed (i.e it can be safely executed concurrently). To be reentrant, a computer program or routine:

  1. Must hold no static (or global) non-constant data.
  2. Must not return the address to static (or global) non-constant data.
  3. Must work only on the data provided to it by the caller.
  4. Must not rely on locks to singleton resources.
  5. Must not modify its own code (unless executing in its own unique thread storage)
  6. Must not call non-reentrant computer programs or routines.

How is safely defined?

If a program can be safely executed concurrently, does it always mean that it is reentrant?

What exactly is the common thread between the six points mentioned that I should keep in mind while checking my code for reentrant capabilities?

Also,

  1. Are all recursive functions reentrant?
  2. Are all thread-safe functions reentrant?
  3. Are all recursive and thread-safe functions reentrant?

While writing this question, one thing comes to mind: Are the terms like reentrance and thread safety absolute at all i.e. do they have fixed concrete definations? For, if they are not, this question is not very meaningful.

Thanks!

A: 

The answers your "Also" questions are "No", "No" and "No". Just because a function is recursive and/or thread safe it doesn't make it re-entrant.

Each of these type of function can fail on all the points you quote. (Though I'm not 100% certain of point 5).

ChrisF
+11  A: 

"Safely" is defined exactly as the common sense dictates - it means "doing its thing correctly without interfering with other things". The six points you cite quite clearly express the requirements to achieve that.

The answers to your 3 questions is 3× "no".


Are all recursive functions reentrant?

NO!

Two simultaneous invocations of a recursive function can easily screw up each other, if they access the same global/static data, for example.


Are all thread-safe functions reentrant?

NO!

A function is thread-safe if it doesn't malfunction if called concurrently. But this can be achieved e.g. by using a mutex to block the execution of the second invocation until the first finishes, so only one invocation works at a time. Reentrancy means executing concurrently without interfering with other invocations.


Are all recursive and thread-safe functions reentrant?

NO!

See above.

slacker
+6  A: 

The common thread:

Is the behavior well defined if the routine is called while it is interrupted?

If you have a function like this:

int add( int a , int b ) {
  return a + b;
}

Then it is not dependent upon any external state. The behavior is well defined.

If you have a function like this:

int add_to_global( int a ) {
  return gValue += a;
}

The result is not well defined on multiple threads. Information could be lost if the timing was just wrong.

The simplest form of a reentrant function is something that operates exclusively on the arguments passed and constant values. Anything else takes special handling or, often, is not reentrant. And of course the arguments must not reference mutable globals.

drawnonward
+1  A: 

The "common thread" (pun intended!?) amongst the points listed is that the function must not do anything that would affect the behaviour of any recursive or concurrent calls to the same function.

So for example static data is an issue because it is owned by all threads; if one call modifies a static variable the all threads use the modified data thus affecting their behaviour. Self modifying code (although rarely encountered, and in some cases prevented) would be a problem, because although there are multiple thread, there is only one copy of the code; the code is essential static data too.

Essentially to be re-entrant, each thread must be able to use the function as if it were the only user, and that is not the case if one thread can affect the behaviour of another in a non-deterministic manner. Primarily this involves each thread having either separate or constant data that the function works on.

All that said, point (1) is not necessarily true; for example, you might legitimately and by design use a static variable to retain a recursion count to guard against excessive recursion or to profile an algorithm.

A thread-safe function need not be reentrant; it may achieve thread safety by specifically preventing reentrancy with a lock, and point (6) says that such a function is not reentrant. Regarding point (6), a function that calls a thread-safe function that locks is not safe for use in recursion (it will dead-lock), and is therefore not said to be reentrant, though it may nonetheless safe for concurrency, and would still be re-entrant in the sense that multiple threads can have their program-counters in such a function simultaneously (just not with the locked region). May be this helps to distinguish thread-safety from reentarncy (or maybe adds to your confusion!).

Clifford
+5  A: 

1. How is safely defined?

Semantically. In this case, this is not a hard-defined term. It just mean "You can do that, without risk".

2. If a program can be safely executed concurrently, does it always mean that it is reentrant?

No.

For example, let's have a C++ function that takes both a lock, and a callback as a parameter:

typedef void (*MyCallback)() ;

void myFunction(MyCallback f)
{
   lock(mutex) ;
   f() ;
   unlock(mutex) ;
}

At first sight, this function seems Ok... But wait:

int main(int argc, char * argv[])
{
   myFunction(myFunction) ;
   return 0 ;
}

If the lock/mutex is not recursive, then here's what will happen:

  1. main will call myFunction
  2. myFunction will acquire the lock
  3. myFunction will call myFunction
  4. the 2nd myFunction will try acquire the lock and wait for it to be released
  5. Deadlock.
  6. Oops...

Ok, I cheated, using the Callback thing. But it's easy to imagine more complex pieces of code having a similar effect.

3. What exactly is the common thread between the six points mentioned that I should keep in mind while checking my code for reentrant capabilities?

You can smell a problem if your function has/gives access to a modifiable persistent resource, or has/gives access to a function that smells.

(Ok, 99% of our code should smell, then... See last section to handle that...)

So, studying your code, one of those points should alert you:

  1. The function has a state (i.e. access a global variable, or even a class member variable)
  2. This function can be called my multiple threads, or could appear twice in the stack while the process is executing (i.e. the function could call itself, directly or indirectly). Function taking callbacks as parameters smell a lot.

Note that non-reentrancy is viral : A function that could call a possible non-reentrant function cannot be considered reentrant.

Note, too, that C++ methods smell because they have access to this, so you should study the code to be sure they have no funny interaction.

4.1. Are all recursive functions reentrant?

No.

In multithreaded cases, a recursive function accessing a shared resources could be called by multiple threads at the same moment, resulting in bad/corrupted data.

In singlethreaded cases, a recursive function could use a non-reentrant function (like infamous strtok), or use global data without handling the fact the data is already in use. So you function is recursive because it calls itself directly or indirectly, but it can still be recursive-unsafe.

4.2. Are all thread-safe functions reentrant?

In the example above, I showed how an apparently threadsafe function was not reentrant. Ok I cheated because of the Callback parameter. But then, there are multiple ways to deadlock a thread by having it acquire twice a non-reccursive lock.

4.3. Are all recursive and thread-safe functions reentrant?

I would say "yes" if by "recursive" you mean "recursive-safe".

If you can guarantee that a function can be called simultaneously by multiple threads, and can call itself, directly or indirectly, without problems, then it is reentrant.

The problem is evaluating this guarantee... ^_^

5. Are the terms like reentrance and thread safety absolute at all i.e. do they have fixed concrete definations?

I believe they have, but then, evaluating a function is thread-safe or reentrant can be difficult. This is why I used the term smell above: You can find a function is not reentrant, but it could be difficult to be sure a complex piece of code is reentrant

6. An example

Let's say you have an object, with one method that need to use a resources:

struct MyStruct
{
   P * p ;

   void foo()
   {
      if(this->p == NULL)
      {
         this->p = new P() ;
      }

      // Lots of code, some using this->p

      if(this->p != NULL)
      {
         delete this->p ;
         this->p = NULL ;
      }
   }
} ;

The first problem is that if somehow this function is called recursively (i.e. this function calls itself, directly or indirectly), the code will probably crash, because this->p will be deleted at the end of the last call, and still probably be used before the end of the first call.

Thus, this code is not recursive-safe.

We could use a reference counter to correct this:

struct MyStruct
{
   size_t  c ;
   P *     p ;

   void foo()
   {
      if(c == 0)
      {
         this->p = new P() ;
      }

      ++c ;

      // Lots of code, some using this->p

      --c ;

      if(c == 0)
      {
         delete this->p ;
         this->p = NULL ;
      }
   }
} ;

This way, the code becomes recursive-safe... But it is still not reentrant because of multithreading issues: We must be sure the modifications of c and of p will be done atomically, using a recursive mutex (not all mutexes are recursive):

struct MyStruct
{
   mutex   m ; // recursive mutex
   size_t  c ;
   P *     p ;

   void foo()
   {
      lock(m) ;
      if(c == 0)
      {
         this->p = new P() ;
      }

      ++c ;
      unlock(m) ;

      // Lots of code, some using this->p

      lock(m) ;
      --c ;

      if(c == 0)
      {
         delete this->p ;
         this->p = NULL ;
      }
      unlock(m) ;
   }
} ;

And of course, this all assumes the lots of code is itself reentrant, including the use of p.

And the code above is not even remotely exception-safe, but this is another story... ^_^

7. Hey 99% of our code is not reentrant!!

It is quite true for spaghetti code. But if you partition correctly your code, you will avoid reentrancy problems.

7.1. Make sure all functions have NO state.

They must only use the parameters, their own local variables, other functions without state, and return copies of the data if they return at all.

7.2. Make sure your object is "recursive-safe".

An object method has access to this, so it shares a state with all the methods of the same instance of the object.

So, make sure the object can be used at one point in the stack (i.e. calling method A), and then, at another point (i.e. calling method B), without corrupting the whole object. Design your object to make sure that upon exiting a method, the object is stable and correct (no dangling pointers, no contradicting member variables, etc.).

7.3. Make sure all your objects are correctly encapsulated.

No one else should have access to their internal data:

   // bad
   int & MyObject::getCounter()
   {
      return this->counter ;
   }

   // good
   int MyObject::getCounter()
   {
      return this->counter ;
   }

   // good, too
   void MyObject::getCounter(int & p_counter)
   {
      p_counter = this->counter ;
   }

Even returning a const reference could be dangerous if the use retrieves the address of the data, as some other portion of the code could modify it without the code holding the const reference being told.

7.4. Make sure the user knows you object is not thread-safe

Thus, the user is responsible to use mutexes to use an object shared between threads.

The objects from the STL are designed to be not thread-safe (because of performance issues), and thus, if a user want to share a std::string between two threads, the user must protect its access with concurrency primitives;

7.5. Make sure you thread-safe code is recursive-safe

This means using recursive mutexes if you believe the same resource can be used twice by the same thread.

paercebal
To quibble a bit, I actually think in this case "safety" is defined - it means that the function will act only on variables provided - ie, it's shorthand for the definition quote below it. And point is that this might not imply other ideas of safety.
Joe Soul-bringer
Did you miss passing in the mutex in the first example?
detly
@paercebal: thanks a lot! awesome post!
Lazer
A: 

The terms "Thread-safe" and "re-entrant" mean only and exactly what their definitions say. "Safe" in this context means only what the definition you quote below it says.

"Safe" here certainly doesn't mean safe in the broader sense that calling a given function in a given context won't totally hose your application. Altogether, a function might reliably produce a desired effect in your multi-threaded application but not qualify as either re-entrant or thread-safe according to the definitions. Oppositely, you can call re-entrant functions in ways that will produce a variety of undesired, unexpected and/or unpredictable effects in your multi-threaded application.

Recursive function can be anything and Re-entrant has a stronger definition than thread-safe so the answers to your numbered questions are all no.

Reading the definition of re-entrant, one might summarize it as meaning a function which will not modify any anything beyond what you call it to modify. But you shouldn't rely on only the summary.

Multi-threaded programming is just extremely difficult in the general case. Knowing which part of one's code re-entrant is only a part of this challenge. Thread safety is not additive. Rather than trying to piece together re-entrant functions, it's better to use an overall thread-safe design pattern and use this pattern to guide your use of every thread and shared resources in the your program.

Joe Soul-bringer