Thread Safety


Up: Special Considerations Next: Cancelling a Message Previous: Freed Requests

Most of this design is intrinsically thread safe. A possible exception is the test/wait routines, and this can be fixed by being precise about their definition. In particular, it is important that an MPID_Wait routine not assume that, since it was called, the message must still be waited on. In a multithreaded environment, some other thread may have completed the message between the time the MPI code decided to call MPID_Wait and when MPID_Wait actually got started. In particular, this situation forces MPID_DeviceCheck to be a no-op (at least nonblocking) in multithreaded versions. Note that the implementation examples in this document are not necessarily thread safe.

Because the ADI must consistently participate in any thread locks used in the MPI implementation, the ADI is responsible for defining what the thread lock interface looks like. The following macros are used to define the interface; they are shown with a sample implementation with a version of pthreads.

#define MPID_THREAD_DS_LOCK_DECLARE pthread_mutex_t mutex; 
#define MPID_THREAD_DS_LOCK_INIT(p) pthread_mutex_init( &(p)->mutex, \ 
                                    pthread_mutexattr_default ); 
#define MPID_THREAD_DS_LOCK(p)      pthread_mutex_lock( &(p)->mutex ); 
#define MPID_THREAD_DS_UNLOCK(p)    pthread_mutex_unlock( &(p)->mutex ); 
#define MPID_THREAD_DS_LOCK_FREE(p) pthread_mutex_destroy( &(p)->mutex ); 
A typical use is
typedef struct { 
      MPID_THREAD_DS_LOCK_DECLARE 
      other stuff 
      } foo; 
   ... 
   foo *p = (foo*)malloc(sizeof(foo)); 
   MPID_THREAD_DS_INIT(p) 
   ... 
   MPID_THREAD_DS_LOCK(p) 
   ... 
   MPID_THREAD_DS_UNLOCK(p) 
   ... 
   MPID_THREAD_DS_FREE(p) 
There are no global locks; each data-structure has its own lock. If the definitions are empty, the ADI provides no thread support (this is why the macro definitions include the statement-terminating semi-colon). Currently, the ADI does not have any direct support for the creation or scheduling of threads.



Up: Special Considerations Next: Cancelling a Message Previous: Freed Requests