Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:47:26

0001 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
0002 /*
0003  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
0004  *                         University Research and Technology
0005  *                         Corporation.  All rights reserved.
0006  * Copyright (c) 2004-2006 The University of Tennessee and The University
0007  *                         of Tennessee Research Foundation.  All rights
0008  *                         reserved.
0009  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
0010  *                         University of Stuttgart.  All rights reserved.
0011  * Copyright (c) 2004-2005 The Regents of the University of California.
0012  *                         All rights reserved.
0013  * Copyright (c) 2007-2015 Los Alamos National Security, LLC.  All rights
0014  *                         reserved.
0015  * Copyright (c) 2015-2016 Research Organization for Information Science
0016  *                         and Technology (RIST). All rights reserved.
0017  * Copyright (c) 2017-2020 Intel, Inc.  All rights reserved.
0018  * Copyright (c) 2021-2022 Nanook Consulting  All rights reserved.
0019  * $COPYRIGHT$
0020  *
0021  * Additional copyrights may follow
0022  *
0023  * $HEADER$
0024  */
0025 
0026 #ifndef PMIX_MUTEX_UNIX_H
0027 #define PMIX_MUTEX_UNIX_H 1
0028 
0029 /**
0030  * @file:
0031  *
0032  * Mutual exclusion functions: Unix implementation.
0033  *
0034  * Functions for locking of critical sections.
0035  *
0036  * On unix, use pthreads or our own atomic operations as
0037  * available.
0038  */
0039 
0040 #include "src/include/pmix_config.h"
0041 
0042 #include <errno.h>
0043 #include <pthread.h>
0044 #include <stdio.h>
0045 
0046 #include "src/class/pmix_object.h"
0047 
0048 BEGIN_C_DECLS
0049 
0050 struct pmix_mutex_t {
0051     pmix_object_t super;
0052 
0053     pthread_mutex_t m_lock_pthread;
0054 
0055 #if PMIX_ENABLE_DEBUG
0056     int m_lock_debug;
0057     const char *m_lock_file;
0058     int m_lock_line;
0059 #endif
0060 };
0061 PMIX_EXPORT PMIX_CLASS_DECLARATION(pmix_mutex_t);
0062 PMIX_EXPORT PMIX_CLASS_DECLARATION(pmix_recursive_mutex_t);
0063 
0064 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
0065 #    define PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
0066 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
0067 #    define PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER
0068 #endif
0069 
0070 #if PMIX_ENABLE_DEBUG
0071 #    define PMIX_MUTEX_STATIC_INIT                                                               \
0072         {                                                                                        \
0073             .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t),                                         \
0074             .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER, .m_lock_debug = 0, .m_lock_file = NULL, \
0075             .m_lock_line = 0,                                                                    \
0076         }
0077 #else
0078 #    define PMIX_MUTEX_STATIC_INIT                                                               \
0079         {                                                                                        \
0080             .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t),                                         \
0081             .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER,                                         \
0082         }
0083 #endif
0084 
0085 #if defined(PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
0086 
0087 #    if PMIX_ENABLE_DEBUG
0088 #        define PMIX_RECURSIVE_MUTEX_STATIC_INIT                                               \
0089             {                                                                                  \
0090                 .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t),                                   \
0091                 .m_lock_pthread = PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER, .m_lock_debug = 0, \
0092                 .m_lock_file = NULL, .m_lock_line = 0,                                         \
0093             }
0094 #    else
0095 #        define PMIX_RECURSIVE_MUTEX_STATIC_INIT                            \
0096             {                                                               \
0097                 .super = PMIX_OBJ_STATIC_INIT(pmix_mutex_t),                \
0098                 .m_lock_pthread = PMIX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER, \
0099             }
0100 #    endif
0101 
0102 #endif
0103 
0104 /************************************************************************
0105  *
0106  * mutex operations (non-atomic versions)
0107  *
0108  ************************************************************************/
0109 
0110 static inline int pmix_mutex_trylock(pmix_mutex_t *m)
0111 {
0112 #if PMIX_ENABLE_DEBUG
0113     int ret = pthread_mutex_trylock(&m->m_lock_pthread);
0114     if (ret == EDEADLK) {
0115         errno = ret;
0116         perror("pmix_mutex_trylock()");
0117         abort();
0118     }
0119     return ret;
0120 #else
0121     return pthread_mutex_trylock(&m->m_lock_pthread);
0122 #endif
0123 }
0124 
0125 static inline void pmix_mutex_lock(pmix_mutex_t *m)
0126 {
0127 #if PMIX_ENABLE_DEBUG
0128     int ret = pthread_mutex_lock(&m->m_lock_pthread);
0129     if (ret == EDEADLK) {
0130         errno = ret;
0131         perror("pmix_mutex_lock()");
0132         abort();
0133     }
0134 #else
0135     pthread_mutex_lock(&m->m_lock_pthread);
0136 #endif
0137 }
0138 
0139 static inline void pmix_mutex_unlock(pmix_mutex_t *m)
0140 {
0141 #if PMIX_ENABLE_DEBUG
0142     int ret = pthread_mutex_unlock(&m->m_lock_pthread);
0143     if (ret == EPERM) {
0144         errno = ret;
0145         perror("pmix_mutex_unlock");
0146         abort();
0147     }
0148 #else
0149     pthread_mutex_unlock(&m->m_lock_pthread);
0150 #endif
0151 }
0152 
0153 
0154 /************************************************************************
0155  * Standard locking
0156  ************************************************************************/
0157 
0158 static inline int pmix_mutex_atomic_trylock(pmix_mutex_t *m)
0159 {
0160     return pmix_mutex_trylock(m);
0161 }
0162 
0163 static inline void pmix_mutex_atomic_lock(pmix_mutex_t *m)
0164 {
0165     pmix_mutex_lock(m);
0166 }
0167 
0168 static inline void pmix_mutex_atomic_unlock(pmix_mutex_t *m)
0169 {
0170     pmix_mutex_unlock(m);
0171 }
0172 
0173 END_C_DECLS
0174 
0175 #endif /* PMIX_MUTEX_UNIX_H */