Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / gmxlib / thread_mpi / pthreads.c
index 612604697d03e4de3d3464a504b4a133de00ff97..62d8c52cae70df7c1950717c3ede8d5ecd983f36 100644 (file)
@@ -1,46 +1,46 @@
 /*
-This source code file is part of thread_mpi.  
-Written by Sander Pronk, Erik Lindahl, and possibly others. 
+   This source code file is part of thread_mpi.
+   Written by Sander Pronk, Erik Lindahl, and possibly others.
 
-Copyright (c) 2009, Sander Pronk, Erik Lindahl.
-All rights reserved.
+   Copyright (c) 2009, Sander Pronk, Erik Lindahl.
+   All rights reserved.
 
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are met:
-1) Redistributions of source code must retain the above copyright
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions are met:
+   1) Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
-2) Redistributions in binary form must reproduce the above copyright
+   2) Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
-3) Neither the name of the copyright holders nor the
+   3) Neither the name of the copyright holders nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.
 
-THIS SOFTWARE IS PROVIDED BY US ''AS IS'' AND ANY
-EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
-DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
-ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+   THIS SOFTWARE IS PROVIDED BY US ''AS IS'' AND ANY
+   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+   DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
+   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-If you want to redistribute modifications, please consider that
-scientific software is very special. Version control is crucial -
-bugs must be traceable. We will be happy to consider code for
-inclusion in the official distribution, but derived work should not
-be called official thread_mpi. Details are found in the README & COPYING
-files.
-*/
+   If you want to redistribute modifications, please consider that
+   scientific software is very special. Version control is crucial -
+   bugs must be traceable. We will be happy to consider code for
+   inclusion in the official distribution, but derived work should not
+   be called official thread_mpi. Details are found in the README & COPYING
+   files.
+ */
 
 
 
 /* Include the defines that determine which thread library to use.
-* We do not use HAVE_PTHREAD_H directly, since we might want to
-* turn off thread support explicity (e.g. for debugging).
-*/
+ * We do not use HAVE_PTHREAD_H directly, since we might want to
+ * turn off thread support explicity (e.g. for debugging).
+ */
 #ifdef HAVE_TMPI_CONFIG_H
 #include "tmpi_config.h"
 #endif
@@ -50,7 +50,7 @@ files.
 #endif
 
 
-#ifdef THREAD_PTHREADS 
+#ifdef THREAD_PTHREADS
 
 #ifdef HAVE_PTHREAD_SETAFFINITY
 #define _GNU_SOURCE
@@ -79,18 +79,18 @@ files.
 
 
 /* mutex for initializing mutexes */
-static pthread_mutex_t mutex_init=PTHREAD_MUTEX_INITIALIZER;   
+static pthread_mutex_t mutex_init = PTHREAD_MUTEX_INITIALIZER;
 /* mutex for initializing barriers */
-static pthread_mutex_t once_init=PTHREAD_MUTEX_INITIALIZER;    
+static pthread_mutex_t once_init = PTHREAD_MUTEX_INITIALIZER;
 /* mutex for initializing thread_conds */
-static pthread_mutex_t cond_init=PTHREAD_MUTEX_INITIALIZER;    
+static pthread_mutex_t cond_init = PTHREAD_MUTEX_INITIALIZER;
 /* mutex for initializing barriers */
-static pthread_mutex_t barrier_init=PTHREAD_MUTEX_INITIALIZER; 
+static pthread_mutex_t barrier_init = PTHREAD_MUTEX_INITIALIZER;
 
 /* mutex for managing  thread IDs */
-static pthread_mutex_t thread_id_mutex=PTHREAD_MUTEX_INITIALIZER;
-static pthread_key_t thread_id_key;
-static int thread_id_key_initialized=0;
+static pthread_mutex_t thread_id_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_key_t   thread_id_key;
+static int             thread_id_key_initialized = 0;
 
 
 
@@ -104,7 +104,7 @@ void tMPI_Fatal_error(const char *file, int line, const char *message, ...)
     va_start(ap, message);
     vfprintf(stderr, message, ap);
     va_end(ap);
-    fprintf(stderr,"\n");
+    fprintf(stderr, "\n");
     abort();
 }
 
@@ -117,16 +117,16 @@ enum tMPI_Thread_support tMPI_Thread_support(void)
 
 int tMPI_Thread_get_hw_number(void)
 {
-    int ret=0;
+    int ret = 0;
 #ifdef HAVE_SYSCONF
 #if defined(_SC_NPROCESSORS_ONLN)
-    ret=sysconf(_SC_NPROCESSORS_ONLN);
+    ret = sysconf(_SC_NPROCESSORS_ONLN);
 #elif defined(_SC_NPROC_ONLN)
-    ret=sysconf(_SC_NPROC_ONLN);
+    ret = sysconf(_SC_NPROC_ONLN);
 #elif defined(_SC_NPROCESSORS_CONF)
-    ret=sysconf(_SC_NPROCESSORS_CONF);
+    ret = sysconf(_SC_NPROCESSORS_CONF);
 #elif defined(_SC_NPROC_CONF)
-    ret=sysconf(_SC_NPROC_CONF);
+    ret = sysconf(_SC_NPROC_CONF);
 #endif
 #endif
 
@@ -136,10 +136,10 @@ int tMPI_Thread_get_hw_number(void)
 /* destructor for thread ids */
 static void tMPI_Destroy_thread_id(void* thread_id)
 {
-    struct tMPI_Thread *thread=(struct tMPI_Thread*)thread_id;
+    struct tMPI_Thread *thread = (struct tMPI_Thread*)thread_id;
     if (!thread->started_by_tmpi)
     {
-        /* if the thread is started by tMPI, it must be freed in the join() 
+        /* if the thread is started by tMPI, it must be freed in the join()
            call. */
         free(thread_id);
     }
@@ -154,11 +154,11 @@ static void tMPI_Init_thread_ids(void)
         /* initialize and set the thread id thread-specific variable */
         struct tMPI_Thread *main_thread;
 
-        thread_id_key_initialized=1;
+        thread_id_key_initialized = 1;
         pthread_key_create(&thread_id_key, tMPI_Destroy_thread_id);
-        main_thread=(struct tMPI_Thread*)malloc(sizeof(struct tMPI_Thread)*1);
-        main_thread->th=pthread_self();
-        main_thread->started_by_tmpi=0;
+        main_thread                  = (struct tMPI_Thread*)malloc(sizeof(struct tMPI_Thread)*1);
+        main_thread->th              = pthread_self();
+        main_thread->started_by_tmpi = 0;
         pthread_setspecific(thread_id_key, main_thread);
     }
     pthread_mutex_unlock( &thread_id_mutex );
@@ -168,20 +168,20 @@ static void tMPI_Init_thread_ids(void)
 struct tMPI_Thread_starter
 {
     struct tMPI_Thread *thread;
-    void *(*start_routine)(void*);
-    void *arg;
+    void               *(*start_routine)(void*);
+    void               *arg;
 };
 
 /* the thread_starter function that sets the thread id */
 static void *tMPI_Thread_starter(void *arg)
 {
-    struct tMPI_Thread_starter *starter=(struct tMPI_Thread_starter *)arg;
+    struct tMPI_Thread_starter *starter = (struct tMPI_Thread_starter *)arg;
     void *(*start_routine)(void*);
     void *parg;
 
     pthread_setspecific(thread_id_key, starter->thread);
-    start_routine=starter->start_routine;
-    parg=starter->arg;
+    start_routine = starter->start_routine;
+    parg          = starter->arg;
 
     free(starter);
     return (*start_routine)(parg);
@@ -193,30 +193,30 @@ int tMPI_Thread_create(tMPI_Thread_t *thread, void *(*start_routine)(void *),
     int ret;
     struct tMPI_Thread_starter *starter;
 
-    if(thread==NULL)
+    if (thread == NULL)
     {
-        tMPI_Fatal_error(TMPI_FARGS,"Invalid thread pointer.");
+        tMPI_Fatal_error(TMPI_FARGS, "Invalid thread pointer.");
         return EINVAL;
     }
     tMPI_Init_thread_ids();
 
-    *thread=(struct tMPI_Thread*)malloc(sizeof(struct tMPI_Thread)*1);
+    *thread                    = (struct tMPI_Thread*)malloc(sizeof(struct tMPI_Thread)*1);
     (*thread)->started_by_tmpi = 1;
-    starter=(struct tMPI_Thread_starter*)
-              malloc(sizeof(struct tMPI_Thread_starter)*1);
+    starter                    = (struct tMPI_Thread_starter*)
+        malloc(sizeof(struct tMPI_Thread_starter)*1);
     /* fill the starter structure */
-    starter->thread=*thread;
-    starter->start_routine=start_routine;
-    starter->arg=arg;
+    starter->thread        = *thread;
+    starter->start_routine = start_routine;
+    starter->arg           = arg;
 
     /*ret=pthread_create(&((*thread)->th),NULL,start_routine,arg);*/
-    ret=pthread_create(&((*thread)->th),NULL,tMPI_Thread_starter,
-                       (void*)starter);
+    ret = pthread_create(&((*thread)->th), NULL, tMPI_Thread_starter,
+                         (void*)starter);
 
-    if(ret!=0)
+    if (ret != 0)
     {
         /* Cannot use tMPI_error() since messages use threads for locking */
-        tMPI_Fatal_error(TMPI_FARGS,"Failed to create POSIX thread:%s, rc=%d",
+        tMPI_Fatal_error(TMPI_FARGS, "Failed to create POSIX thread:%s, rc=%d",
                          strerror(errno), ret);
         /* Use system memory allocation routines */
         return -1;
@@ -229,16 +229,16 @@ int tMPI_Thread_create(tMPI_Thread_t *thread, void *(*start_routine)(void *),
 
 int tMPI_Thread_join(tMPI_Thread_t thread, void **value_ptr)
 {
-    int ret;
-    pthread_t th=thread->th;
+    int       ret;
+    pthread_t th = thread->th;
+
 
-    
     ret = pthread_join( th, value_ptr );
 
     free(thread);
-    if(ret != 0 )
+    if (ret != 0)
     {
-        tMPI_Fatal_error(TMPI_FARGS,"Failed to join POSIX thread. rc=%d",ret);
+        tMPI_Fatal_error(TMPI_FARGS, "Failed to join POSIX thread. rc=%d", ret);
     }
     return ret;
 }
@@ -250,15 +250,15 @@ tMPI_Thread_t tMPI_Thread_self(void)
     /* make sure the key var is set */
     tMPI_Init_thread_ids();
 
-    th=pthread_getspecific(thread_id_key);
+    th = pthread_getspecific(thread_id_key);
 
     /* check if it is already in our list */
     if (th == NULL)
     {
         /* if not, create an ID, set it and return it */
-        th=(struct tMPI_Thread*)malloc(sizeof(struct tMPI_Thread)*1);
-        th->started_by_tmpi=0;
-        th->th=pthread_self();
+        th                  = (struct tMPI_Thread*)malloc(sizeof(struct tMPI_Thread)*1);
+        th->started_by_tmpi = 0;
+        th->th              = pthread_self();
         pthread_setspecific(thread_id_key, th);
     }
     return th;
@@ -284,7 +284,7 @@ enum tMPI_Thread_setaffinity_support tMPI_Thread_setaffinity_support(void)
 int tMPI_Thread_setaffinity_single(tMPI_Thread_t thread, unsigned int nr)
 {
 #ifdef HAVE_PTHREAD_SETAFFINITY
-    int nt=tMPI_Thread_get_hw_number();
+    int       nt = tMPI_Thread_get_hw_number();
     cpu_set_t set;
 
     if (nt < nr)
@@ -302,21 +302,21 @@ int tMPI_Thread_setaffinity_single(tMPI_Thread_t thread, unsigned int nr)
 
 
 
-int tMPI_Thread_mutex_init(tMPI_Thread_mutex_t *mtx) 
+int tMPI_Thread_mutex_init(tMPI_Thread_mutex_t *mtx)
 {
     int ret;
-  
+
     if (mtx == NULL)
     {
         return EINVAL;
     }
 
-    mtx->mutex=(struct tMPI_Mutex*)tMPI_Malloc(sizeof(struct tMPI_Mutex)*1);
-    ret = pthread_mutex_init(&(mtx->mutex->mtx),NULL);
-    
-    if(ret!=0)
+    mtx->mutex = (struct tMPI_Mutex*)tMPI_Malloc(sizeof(struct tMPI_Mutex)*1);
+    ret        = pthread_mutex_init(&(mtx->mutex->mtx), NULL);
+
+    if (ret != 0)
     {
-        tMPI_Fatal_error(TMPI_FARGS,"Error initializing POSIX mutex. rc=%d");
+        tMPI_Fatal_error(TMPI_FARGS, "Error initializing POSIX mutex. rc=%d");
         /* Use system memory allocation routines */
         return ret;
     }
@@ -327,36 +327,36 @@ int tMPI_Thread_mutex_init(tMPI_Thread_mutex_t *mtx)
 
 static int tMPI_Thread_mutex_init_once(tMPI_Thread_mutex_t *mtx)
 {
-    int ret=0;
+    int ret = 0;
 
     /* we're relying on the memory barrier semantics of mutex_lock/unlock
        for the check preceding this function call to have worked */
-    pthread_mutex_lock( &(mutex_init) );    
-    if(mtx->mutex==NULL)
+    pthread_mutex_lock( &(mutex_init) );
+    if (mtx->mutex == NULL)
     {
-        mtx->mutex=(struct tMPI_Mutex*)tMPI_Malloc(sizeof(struct tMPI_Mutex)*1);
-        ret=pthread_mutex_init( &(mtx->mutex->mtx), NULL);
+        mtx->mutex = (struct tMPI_Mutex*)tMPI_Malloc(sizeof(struct tMPI_Mutex)*1);
+        ret        = pthread_mutex_init( &(mtx->mutex->mtx), NULL);
     }
-    pthread_mutex_unlock( &(mutex_init) );    
+    pthread_mutex_unlock( &(mutex_init) );
     return ret;
 }
 
 
-int tMPI_Thread_mutex_destroy(tMPI_Thread_mutex_t *mtx) 
+int tMPI_Thread_mutex_destroy(tMPI_Thread_mutex_t *mtx)
 {
     int ret;
 
-    if(mtx == NULL)
+    if (mtx == NULL)
     {
         return EINVAL;
     }
-    
+
     ret = pthread_mutex_destroy( &(mtx->mutex->mtx) );
     free(mtx->mutex);
-    
-    if(ret!=0)
+
+    if (ret != 0)
     {
-        tMPI_Fatal_error(TMPI_FARGS,"Error destroying POSIX mutex. rc=%d",ret);
+        tMPI_Fatal_error(TMPI_FARGS, "Error destroying POSIX mutex. rc=%d", ret);
         /* Use system memory allocation routines */
     }
     return ret;
@@ -371,17 +371,19 @@ int tMPI_Thread_mutex_lock(tMPI_Thread_mutex_t *mtx)
     /* check whether the mutex is initialized */
     if (tMPI_Atomic_get( &(mtx->initialized)  ) == 0)
     {
-        ret=tMPI_Thread_mutex_init_once(mtx);
+        ret = tMPI_Thread_mutex_init_once(mtx);
         if (ret)
+        {
             return ret;
+        }
     }
-   
-    ret=pthread_mutex_lock(&(mtx->mutex->mtx));
+
+    ret = pthread_mutex_lock(&(mtx->mutex->mtx));
 
     return ret;
 }
 
+
 
 
 int tMPI_Thread_mutex_trylock(tMPI_Thread_mutex_t *mtx)
@@ -391,13 +393,15 @@ int tMPI_Thread_mutex_trylock(tMPI_Thread_mutex_t *mtx)
     /* check whether the mutex is initialized */
     if (tMPI_Atomic_get( &(mtx->initialized)  ) == 0)
     {
-        ret=tMPI_Thread_mutex_init_once(mtx);
+        ret = tMPI_Thread_mutex_init_once(mtx);
         if (ret)
+        {
             return ret;
+        }
     }
 
-    ret=pthread_mutex_trylock(&(mtx->mutex->mtx));
-    
+    ret = pthread_mutex_trylock(&(mtx->mutex->mtx));
+
     return ret;
 }
 
@@ -406,17 +410,19 @@ int tMPI_Thread_mutex_trylock(tMPI_Thread_mutex_t *mtx)
 int tMPI_Thread_mutex_unlock(tMPI_Thread_mutex_t *mtx)
 {
     int ret;
+
     /* check whether the mutex is initialized */
     if (tMPI_Atomic_get( &(mtx->initialized)  ) == 0)
     {
-        ret=tMPI_Thread_mutex_init_once(mtx);
+        ret = tMPI_Thread_mutex_init_once(mtx);
         if (ret)
+        {
             return ret;
+        }
     }
+
     ret = pthread_mutex_unlock(&(mtx->mutex->mtx));
-    
+
     return ret;
 }
 
@@ -426,19 +432,19 @@ int tMPI_Thread_key_create(tMPI_Thread_key_t *key, void (*destructor)(void *))
 {
     int ret;
 
-    if(key==NULL)
+    if (key == NULL)
     {
-        tMPI_Fatal_error(TMPI_FARGS,"Invalid key pointer.");
+        tMPI_Fatal_error(TMPI_FARGS, "Invalid key pointer.");
         return EINVAL;
     }
 
 
-    key->key=(struct tMPI_Thread_key*)tMPI_Malloc(sizeof(struct 
-                                                         tMPI_Thread_key)*1);
+    key->key = (struct tMPI_Thread_key*)tMPI_Malloc(sizeof(struct
+                                                           tMPI_Thread_key)*1);
     ret = pthread_key_create(&((key)->key->pkey), destructor);
-    if(ret!=0)
+    if (ret != 0)
     {
-        tMPI_Fatal_error(TMPI_FARGS,"Failed to create thread key, rc=%d.",ret);
+        tMPI_Fatal_error(TMPI_FARGS, "Failed to create thread key, rc=%d.", ret);
         fflush(stderr);
         return -1;
     }
@@ -452,15 +458,15 @@ int tMPI_Thread_key_delete(tMPI_Thread_key_t key)
 {
     int ret;
 
-    ret=pthread_key_delete((key.key->pkey));
+    ret = pthread_key_delete((key.key->pkey));
     free(key.key);
 
-    if(ret!=0)
+    if (ret != 0)
     {
-        tMPI_Fatal_error(TMPI_FARGS,"Failed to delete thread key, rc=%d.",ret);
+        tMPI_Fatal_error(TMPI_FARGS, "Failed to delete thread key, rc=%d.", ret);
         fflush(stderr);
     }
-    
+
     return ret;
 }
 
@@ -470,7 +476,7 @@ void * tMPI_Thread_getspecific(tMPI_Thread_key_t key)
 {
     void *p = NULL;
 
-    p=pthread_getspecific((key.key->pkey));
+    p = pthread_getspecific((key.key->pkey));
 
     return p;
 }
@@ -479,15 +485,15 @@ void * tMPI_Thread_getspecific(tMPI_Thread_key_t key)
 int tMPI_Thread_setspecific(tMPI_Thread_key_t key, void *value)
 {
     int ret;
-    
-    ret=pthread_setspecific((key.key->pkey),value);
-    
+
+    ret = pthread_setspecific((key.key->pkey), value);
+
     return ret;
 }
 
 
 int tMPI_Thread_once(tMPI_Thread_once_t *once_control,
-                     void (*init_routine)(void))
+                     void                (*init_routine)(void))
 {
     int ret;
     if (!once_control || !init_routine)
@@ -496,8 +502,10 @@ int tMPI_Thread_once(tMPI_Thread_once_t *once_control,
     }
 
     /* really ugly hack - and it's slow... */
-    if ( (ret=pthread_mutex_lock( &once_init )) )
+    if ( (ret = pthread_mutex_lock( &once_init )) )
+    {
         return ret;
+    }
     if (tMPI_Atomic_get(&(once_control->once)) == 0)
     {
         (*init_routine)();
@@ -511,61 +519,61 @@ int tMPI_Thread_once(tMPI_Thread_once_t *once_control,
 
 
 
-int tMPI_Thread_cond_init(tMPI_Thread_cond_t *cond) 
+int tMPI_Thread_cond_init(tMPI_Thread_cond_t *cond)
 {
     int ret;
-    
-    if(cond==NULL)
+
+    if (cond == NULL)
     {
         return EINVAL;
     }
-   
-    cond->condp=(struct tMPI_Thread_cond*)
-              tMPI_Malloc(sizeof(struct tMPI_Thread_cond)*1);
+
+    cond->condp = (struct tMPI_Thread_cond*)
+        tMPI_Malloc(sizeof(struct tMPI_Thread_cond)*1);
     ret = pthread_cond_init(&(cond->condp->cond), NULL);
-    
-    if(ret!=0)
+
+    if (ret != 0)
     {
-        tMPI_Fatal_error(TMPI_FARGS,"Error initializing POSIX condition variable. rc=%d",ret);
+        tMPI_Fatal_error(TMPI_FARGS, "Error initializing POSIX condition variable. rc=%d", ret);
         fflush(stderr);
     }
-    tMPI_Atomic_set(&(cond->initialized),1);
+    tMPI_Atomic_set(&(cond->initialized), 1);
     return ret;
 }
 
 
 static int tMPI_Thread_cond_init_once(tMPI_Thread_cond_t *cond)
 {
-    int ret=0;
+    int ret = 0;
 
     /* we're relying on the memory barrier semantics of mutex_lock/unlock
        for the check preceding this function call to have worked */
-    pthread_mutex_lock( &(cond_init) );    
-    if(cond->condp==NULL)
+    pthread_mutex_lock( &(cond_init) );
+    if (cond->condp == NULL)
     {
-        cond->condp=(struct tMPI_Thread_cond*)
-                  tMPI_Malloc(sizeof(struct tMPI_Thread_cond)*1);
-        ret=pthread_cond_init( &(cond->condp->cond), NULL);
+        cond->condp = (struct tMPI_Thread_cond*)
+            tMPI_Malloc(sizeof(struct tMPI_Thread_cond)*1);
+        ret = pthread_cond_init( &(cond->condp->cond), NULL);
     }
-    pthread_mutex_unlock( &(cond_init) );    
+    pthread_mutex_unlock( &(cond_init) );
     return ret;
 }
 
 
 
-int tMPI_Thread_cond_destroy(tMPI_Thread_cond_t *cond) 
+int tMPI_Thread_cond_destroy(tMPI_Thread_cond_t *cond)
 {
     int ret;
-    
-    if(cond == NULL)
+
+    if (cond == NULL)
     {
         return EINVAL;
     }
-    
+
     ret = pthread_cond_destroy(&(cond->condp->cond));
     free(cond->condp);
-   
-    if(ret!=0)
+
+    if (ret != 0)
     {
         tMPI_Fatal_error(TMPI_FARGS,
                          "Error destroying POSIX condition variable. rc=%d",
@@ -586,9 +594,9 @@ int tMPI_Thread_cond_wait(tMPI_Thread_cond_t *cond, tMPI_Thread_mutex_t *mtx)
         tMPI_Thread_cond_init_once(cond);
     }
     /* the mutex must have been initialized because it should be locked here */
-   
+
     ret = pthread_cond_wait( &(cond->condp->cond), &(mtx->mutex->mtx) );
-    
+
     return ret;
 }
 
@@ -604,9 +612,9 @@ int tMPI_Thread_cond_signal(tMPI_Thread_cond_t *cond)
     {
         tMPI_Thread_cond_init_once(cond);
     }
-    
+
     ret = pthread_cond_signal( &(cond->condp->cond) );
-    
+
     return ret;
 }
 
@@ -621,9 +629,9 @@ int tMPI_Thread_cond_broadcast(tMPI_Thread_cond_t *cond)
     {
         tMPI_Thread_cond_init_once(cond);
     }
-   
+
     ret = pthread_cond_broadcast( &(cond->condp->cond) );
-    
+
     return ret;
 }
 
@@ -648,33 +656,33 @@ int tMPI_Thread_barrier_init(tMPI_Thread_barrier_t *barrier, int n)
 {
     int ret;
     /*tMPI_Thread_pthread_barrier_t *p;*/
-    
-    if(barrier==NULL)
+
+    if (barrier == NULL)
     {
         return EINVAL;
     }
-    
-    barrier->barrierp=(struct tMPI_Thread_barrier*)
-              tMPI_Malloc(sizeof(struct tMPI_Thread_barrier)*1);
-    ret = pthread_mutex_init(&(barrier->barrierp->mutex),NULL);
-        
-    if(ret!=0)
+
+    barrier->barrierp = (struct tMPI_Thread_barrier*)
+        tMPI_Malloc(sizeof(struct tMPI_Thread_barrier)*1);
+    ret = pthread_mutex_init(&(barrier->barrierp->mutex), NULL);
+
+    if (ret != 0)
     {
-        tMPI_Fatal_error(TMPI_FARGS,"Error initializing POSIX mutex. rc=%d",
+        tMPI_Fatal_error(TMPI_FARGS, "Error initializing POSIX mutex. rc=%d",
                          ret);
         return ret;
     }
-    
-    ret = pthread_cond_init(&(barrier->barrierp->cv),NULL);
-    
-    if(ret!=0)
+
+    ret = pthread_cond_init(&(barrier->barrierp->cv), NULL);
+
+    if (ret != 0)
     {
         tMPI_Fatal_error(TMPI_FARGS,
                          "Error initializing POSIX condition variable. rc=%d",
                          ret);
         return ret;
     }
-        
+
     barrier->threshold = n;
     barrier->count     = n;
     barrier->cycle     = 0;
@@ -685,27 +693,27 @@ int tMPI_Thread_barrier_init(tMPI_Thread_barrier_t *barrier, int n)
 
 static int tMPI_Thread_barrier_init_once(tMPI_Thread_barrier_t *barrier)
 {
-    int ret=0;
+    int ret = 0;
 
     /* we're relying on the memory barrier semantics of mutex_lock/unlock
        for the check preceding this function call to have worked */
-    pthread_mutex_lock( &(barrier_init) );    
-    if(barrier->barrierp==NULL)
+    pthread_mutex_lock( &(barrier_init) );
+    if (barrier->barrierp == NULL)
     {
-        barrier->barrierp=(struct tMPI_Thread_barrier*)
-                  tMPI_Malloc(sizeof(struct tMPI_Thread_barrier)*1);
-        ret = pthread_mutex_init(&(barrier->barrierp->mutex),NULL);
+        barrier->barrierp = (struct tMPI_Thread_barrier*)
+            tMPI_Malloc(sizeof(struct tMPI_Thread_barrier)*1);
+        ret = pthread_mutex_init(&(barrier->barrierp->mutex), NULL);
 
-        if(ret!=0)
+        if (ret != 0)
         {
-            tMPI_Fatal_error(TMPI_FARGS,"Error initializing POSIX mutex. rc=%d",
+            tMPI_Fatal_error(TMPI_FARGS, "Error initializing POSIX mutex. rc=%d",
                              ret);
             return ret;
         }
 
-        ret = pthread_cond_init(&(barrier->barrierp->cv),NULL);
+        ret = pthread_cond_init(&(barrier->barrierp->cv), NULL);
 
-        if(ret!=0)
+        if (ret != 0)
         {
             tMPI_Fatal_error(TMPI_FARGS,
                              "Error initializing POSIX condition variable. rc=%d",
@@ -713,7 +721,7 @@ static int tMPI_Thread_barrier_init_once(tMPI_Thread_barrier_t *barrier)
             return ret;
         }
     }
-    pthread_mutex_unlock( &(barrier_init) );    
+    pthread_mutex_unlock( &(barrier_init) );
     return ret;
 }
 
@@ -722,7 +730,7 @@ static int tMPI_Thread_barrier_init_once(tMPI_Thread_barrier_t *barrier)
 
 int tMPI_Thread_barrier_destroy(tMPI_Thread_barrier_t *barrier)
 {
-    if(barrier==NULL)
+    if (barrier == NULL)
     {
         return EINVAL;
     }
@@ -731,16 +739,16 @@ int tMPI_Thread_barrier_destroy(tMPI_Thread_barrier_t *barrier)
     pthread_cond_destroy(&(barrier->barrierp->cv));
 
     free(barrier->barrierp);
-    
+
     return 0;
 }
+
 
 int tMPI_Thread_barrier_wait(tMPI_Thread_barrier_t *   barrier)
 {
     int    cycle;
     int    rc;
-    
+
     /* check whether the barrier is initialized */
     if (tMPI_Atomic_get( &(barrier->initialized)  ) == 0)
     {
@@ -750,34 +758,41 @@ int tMPI_Thread_barrier_wait(tMPI_Thread_barrier_t *   barrier)
 
     rc = pthread_mutex_lock(&barrier->barrierp->mutex);
 
-    
-    if(rc != 0)
+
+    if (rc != 0)
+    {
         return EBUSY;
+    }
 
     cycle = barrier->cycle;
-    
+
     /* Decrement the count atomically and check if it is zero.
-        * This will only be true for the last thread calling us.
-        */
-    if( --barrier->count <= 0 )
-    { 
+     * This will only be true for the last thread calling us.
+     */
+    if (--barrier->count <= 0)
+    {
         barrier->cycle = !barrier->cycle;
         barrier->count = barrier->threshold;
-        rc = pthread_cond_broadcast(&barrier->barrierp->cv);
-        
-        if(rc == 0)
+        rc             = pthread_cond_broadcast(&barrier->barrierp->cv);
+
+        if (rc == 0)
+        {
             rc = -1;
+        }
     }
     else
     {
-        while(cycle == barrier->cycle)
+        while (cycle == barrier->cycle)
         {
             rc = pthread_cond_wait(&barrier->barrierp->cv,
                                    &barrier->barrierp->mutex);
-            if(rc != 0) break;
+            if (rc != 0)
+            {
+                break;
+            }
         }
     }
-    
+
     pthread_mutex_unlock(&barrier->barrierp->mutex);
     return rc;
 }
@@ -785,7 +800,6 @@ int tMPI_Thread_barrier_wait(tMPI_Thread_barrier_t *   barrier)
 #else
 
 /* just to have some symbols */
-int tMPI_Thread_pthreads=0;
+int tMPI_Thread_pthreads = 0;
 
 #endif /* THREAD_PTHREADS */
-