root/opal/mca/pmix/pmix4x/pmix/src/threads/mutex_unix.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. pmix_mutex_trylock
  2. pmix_mutex_lock
  3. pmix_mutex_unlock
  4. pmix_mutex_atomic_trylock
  5. pmix_mutex_atomic_lock
  6. pmix_mutex_atomic_unlock
  7. pmix_mutex_atomic_trylock
  8. pmix_mutex_atomic_lock
  9. pmix_mutex_atomic_unlock

   1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
   2 /*
   3  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   4  *                         University Research and Technology
   5  *                         Corporation.  All rights reserved.
   6  * Copyright (c) 2004-2006 The University of Tennessee and The University
   7  *                         of Tennessee Research Foundation.  All rights
   8  *                         reserved.
   9  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
  10  *                         University of Stuttgart.  All rights reserved.
  11  * Copyright (c) 2004-2005 The Regents of the University of California.
  12  *                         All rights reserved.
  13  * Copyright (c) 2007-2015 Los Alamos National Security, LLC.  All rights
  14  *                         reserved.
  15  * Copyright (c) 2015-2016 Research Organization for Information Science
  16  *                         and Technology (RIST). All rights reserved.
  17  * Copyright (c) 2017-2018 Intel, Inc. All rights reserved.
  18  * $COPYRIGHT$
  19  *
  20  * Additional copyrights may follow
  21  *
  22  * $HEADER$
  23  */
  24 
  25 #ifndef  PMIX_MUTEX_UNIX_H
  26 #define  PMIX_MUTEX_UNIX_H 1
  27 
  28 /**
  29  * @file:
  30  *
  31  * Mutual exclusion functions: Unix implementation.
  32  *
  33  * Functions for locking of critical sections.
  34  *
  35  * On unix, use pthreads or our own atomic operations as
  36  * available.
  37  */
  38 
  39 #include "pmix_config.h"
  40 
  41 #include <pthread.h>
  42 #include <errno.h>
  43 #include <stdio.h>
  44 
  45 #include "src/class/pmix_object.h"
  46 #include "src/atomics/sys/atomic.h"
  47 
  48 BEGIN_C_DECLS
  49 
  50 struct pmix_mutex_t {
  51     pmix_object_t super;
  52 
  53     pthread_mutex_t m_lock_pthread;
  54 
  55 #if PMIX_ENABLE_DEBUG
  56     int m_lock_debug;
  57     const char *m_lock_file;
  58     int m_lock_line;
  59 #endif
  60 
  61     pmix_atomic_lock_t m_lock_atomic;
  62 };
  63 PMIX_EXPORT PMIX_CLASS_DECLARATION(pmix_mutex_t);
  64 PMIX_EXPORT PMIX_CLASS_DECLARATION(pmix_recursive_mutex_t);
  65 
  66 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
  67 #define PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  68 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
  69 #define PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  70 #endif
  71 
  72 #if PMIX_ENABLE_DEBUG
  73 #define PMIX_MUTEX_STATIC_INIT                                          \
  74     {                                                                   \
  75         .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t),                    \
  76         .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER,                    \
  77         .m_lock_debug = 0,                                              \
  78         .m_lock_file = NULL,                                            \
  79         .m_lock_line = 0,                                               \
  80         .m_lock_atomic = {PMIX_ATOMIC_LOCK_UNLOCKED},                   \
  81     }
  82 #else
  83 #define PMIX_MUTEX_STATIC_INIT                                          \
  84     {                                                                   \
  85         .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t),                    \
  86         .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER,                    \
  87         .m_lock_atomic = {PMIX_ATOMIC_LOCK_UNLOCKED},                   \
  88     }
  89 #endif
  90 
  91 #if defined(PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
  92 
  93 #if PMIX_ENABLE_DEBUG
  94 #define PMIX_RECURSIVE_MUTEX_STATIC_INIT                                \
  95     {                                                                   \
  96         .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t),                    \
  97         .m_lock_pthread = PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER,     \
  98         .m_lock_debug = 0,                                              \
  99         .m_lock_file = NULL,                                            \
 100         .m_lock_line = 0,                                               \
 101         .m_lock_atomic = {PMIX_ATOMIC_LOCK_UNLOCKED},                   \
 102     }
 103 #else
 104 #define PMIX_RECURSIVE_MUTEX_STATIC_INIT                                \
 105     {                                                                   \
 106         .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t),                    \
 107         .m_lock_pthread = PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER,     \
 108         .m_lock_atomic = {PMIX_ATOMIC_LOCK_UNLOCKED},                   \
 109     }
 110 #endif
 111 
 112 #endif
 113 
 114 /************************************************************************
 115  *
 116  * mutex operations (non-atomic versions)
 117  *
 118  ************************************************************************/
 119 
 120 static inline int pmix_mutex_trylock(pmix_mutex_t *m)
 121 {
 122 #if PMIX_ENABLE_DEBUG
 123     int ret = pthread_mutex_trylock(&m->m_lock_pthread);
 124     if (ret == EDEADLK) {
 125         errno = ret;
 126         perror("pmix_mutex_trylock()");
 127         abort();
 128     }
 129     return ret;
 130 #else
 131     return pthread_mutex_trylock(&m->m_lock_pthread);
 132 #endif
 133 }
 134 
 135 static inline void pmix_mutex_lock(pmix_mutex_t *m)
 136 {
 137 #if PMIX_ENABLE_DEBUG
 138     int ret = pthread_mutex_lock(&m->m_lock_pthread);
 139     if (ret == EDEADLK) {
 140         errno = ret;
 141         perror("pmix_mutex_lock()");
 142         abort();
 143     }
 144 #else
 145     pthread_mutex_lock(&m->m_lock_pthread);
 146 #endif
 147 }
 148 
 149 static inline void pmix_mutex_unlock(pmix_mutex_t *m)
 150 {
 151 #if PMIX_ENABLE_DEBUG
 152     int ret = pthread_mutex_unlock(&m->m_lock_pthread);
 153     if (ret == EPERM) {
 154         errno = ret;
 155         perror("pmix_mutex_unlock");
 156         abort();
 157     }
 158 #else
 159     pthread_mutex_unlock(&m->m_lock_pthread);
 160 #endif
 161 }
 162 
 163 /************************************************************************
 164  *
 165  * mutex operations (atomic versions)
 166  *
 167  ************************************************************************/
 168 
 169 #if PMIX_HAVE_ATOMIC_SPINLOCKS
 170 
 171 /************************************************************************
 172  * Spin Locks
 173  ************************************************************************/
 174 
 175 static inline int pmix_mutex_atomic_trylock(pmix_mutex_t *m)
 176 {
 177     return pmix_atomic_trylock(&m->m_lock_atomic);
 178 }
 179 
 180 static inline void pmix_mutex_atomic_lock(pmix_mutex_t *m)
 181 {
 182     pmix_atomic_lock(&m->m_lock_atomic);
 183 }
 184 
 185 static inline void pmix_mutex_atomic_unlock(pmix_mutex_t *m)
 186 {
 187     pmix_atomic_unlock(&m->m_lock_atomic);
 188 }
 189 
 190 #else
 191 
 192 /************************************************************************
 193  * Standard locking
 194  ************************************************************************/
 195 
 196 static inline int pmix_mutex_atomic_trylock(pmix_mutex_t *m)
 197 {
 198     return pmix_mutex_trylock(m);
 199 }
 200 
 201 static inline void pmix_mutex_atomic_lock(pmix_mutex_t *m)
 202 {
 203     pmix_mutex_lock(m);
 204 }
 205 
 206 static inline void pmix_mutex_atomic_unlock(pmix_mutex_t *m)
 207 {
 208     pmix_mutex_unlock(m);
 209 }
 210 
 211 #endif
 212 
 213 END_C_DECLS
 214 
 215 #endif                          /* PMIX_MUTEX_UNIX_H */

/* [<][>][^][v][top][bottom][index][help] */