root/opal/threads/mutex_unix.h

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

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. opal_mutex_trylock
  2. opal_mutex_lock
  3. opal_mutex_unlock
  4. opal_mutex_atomic_trylock
  5. opal_mutex_atomic_lock
  6. opal_mutex_atomic_unlock
  7. opal_mutex_atomic_trylock
  8. opal_mutex_atomic_lock
  9. opal_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-2018 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$
  18  *
  19  * Additional copyrights may follow
  20  *
  21  * $HEADER$
  22  */
  23 
  24 #ifndef  OPAL_MUTEX_UNIX_H
  25 #define  OPAL_MUTEX_UNIX_H 1
  26 
  27 /**
  28  * @file:
  29  *
  30  * Mutual exclusion functions: Unix implementation.
  31  *
  32  * Functions for locking of critical sections.
  33  *
  34  * On unix, use pthreads or our own atomic operations as
  35  * available.
  36  */
  37 
  38 #include "opal_config.h"
  39 
  40 #include <pthread.h>
  41 #include <errno.h>
  42 #include <stdio.h>
  43 
  44 #include "opal/class/opal_object.h"
  45 #include "opal/sys/atomic.h"
  46 
  47 BEGIN_C_DECLS
  48 
  49 struct opal_mutex_t {
  50     opal_object_t super;
  51 
  52     pthread_mutex_t m_lock_pthread;
  53 
  54 #if OPAL_ENABLE_DEBUG
  55     int m_lock_debug;
  56     const char *m_lock_file;
  57     int m_lock_line;
  58 #endif
  59 
  60     opal_atomic_lock_t m_lock_atomic;
  61 };
  62 OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_mutex_t);
  63 OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_recursive_mutex_t);
  64 
  65 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
  66 #define OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  67 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
  68 #define OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  69 #endif
  70 
  71 #if OPAL_ENABLE_DEBUG
  72 #define OPAL_MUTEX_STATIC_INIT                                          \
  73     {                                                                   \
  74         .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t),                    \
  75         .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER,                    \
  76         .m_lock_debug = 0,                                              \
  77         .m_lock_file = NULL,                                            \
  78         .m_lock_line = 0,                                               \
  79         .m_lock_atomic = OPAL_ATOMIC_LOCK_INIT,                         \
  80     }
  81 #else
  82 #define OPAL_MUTEX_STATIC_INIT                                          \
  83     {                                                                   \
  84         .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t),                    \
  85         .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER,                    \
  86         .m_lock_atomic = OPAL_ATOMIC_LOCK_INIT,                         \
  87     }
  88 #endif
  89 
  90 #if defined(OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
  91 
  92 #if OPAL_ENABLE_DEBUG
  93 #define OPAL_RECURSIVE_MUTEX_STATIC_INIT                                \
  94     {                                                                   \
  95         .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t),                    \
  96         .m_lock_pthread = OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER,     \
  97         .m_lock_debug = 0,                                              \
  98         .m_lock_file = NULL,                                            \
  99         .m_lock_line = 0,                                               \
 100         .m_lock_atomic = OPAL_ATOMIC_LOCK_INIT,                         \
 101     }
 102 #else
 103 #define OPAL_RECURSIVE_MUTEX_STATIC_INIT                                \
 104     {                                                                   \
 105         .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t),                    \
 106         .m_lock_pthread = OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER,     \
 107         .m_lock_atomic = OPAL_ATOMIC_LOCK_INIT,                         \
 108     }
 109 #endif
 110 
 111 #endif
 112 
 113 /************************************************************************
 114  *
 115  * mutex operations (non-atomic versions)
 116  *
 117  ************************************************************************/
 118 
 119 static inline int opal_mutex_trylock(opal_mutex_t *m)
 120 {
 121 #if OPAL_ENABLE_DEBUG
 122     int ret = pthread_mutex_trylock(&m->m_lock_pthread);
 123     if (ret == EDEADLK) {
 124         errno = ret;
 125         perror("opal_mutex_trylock()");
 126         abort();
 127     }
 128     return ret;
 129 #else
 130     return pthread_mutex_trylock(&m->m_lock_pthread);
 131 #endif
 132 }
 133 
 134 static inline void opal_mutex_lock(opal_mutex_t *m)
 135 {
 136 #if OPAL_ENABLE_DEBUG
 137     int ret = pthread_mutex_lock(&m->m_lock_pthread);
 138     if (ret == EDEADLK) {
 139         errno = ret;
 140         perror("opal_mutex_lock()");
 141         abort();
 142     }
 143 #else
 144     pthread_mutex_lock(&m->m_lock_pthread);
 145 #endif
 146 }
 147 
 148 static inline void opal_mutex_unlock(opal_mutex_t *m)
 149 {
 150 #if OPAL_ENABLE_DEBUG
 151     int ret = pthread_mutex_unlock(&m->m_lock_pthread);
 152     if (ret == EPERM) {
 153         errno = ret;
 154         perror("opal_mutex_unlock");
 155         abort();
 156     }
 157 #else
 158     pthread_mutex_unlock(&m->m_lock_pthread);
 159 #endif
 160 }
 161 
 162 /************************************************************************
 163  *
 164  * mutex operations (atomic versions)
 165  *
 166  ************************************************************************/
 167 
 168 #if OPAL_HAVE_ATOMIC_SPINLOCKS
 169 
 170 /************************************************************************
 171  * Spin Locks
 172  ************************************************************************/
 173 
 174 static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
 175 {
 176     return opal_atomic_trylock(&m->m_lock_atomic);
 177 }
 178 
 179 static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
 180 {
 181     opal_atomic_lock(&m->m_lock_atomic);
 182 }
 183 
 184 static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
 185 {
 186     opal_atomic_unlock(&m->m_lock_atomic);
 187 }
 188 
 189 #else
 190 
 191 /************************************************************************
 192  * Standard locking
 193  ************************************************************************/
 194 
 195 static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
 196 {
 197     return opal_mutex_trylock(m);
 198 }
 199 
 200 static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
 201 {
 202     opal_mutex_lock(m);
 203 }
 204 
 205 static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
 206 {
 207     opal_mutex_unlock(m);
 208 }
 209 
 210 #endif
 211 
 212 END_C_DECLS
 213 
 214 #endif                          /* OPAL_MUTEX_UNIX_H */

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