OpenMPI  0.1.1
mutex_unix.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3  * University Research and Technology
4  * Corporation. All rights reserved.
5  * Copyright (c) 2004-2006 The University of Tennessee and The University
6  * of Tennessee Research Foundation. All rights
7  * reserved.
8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9  * University of Stuttgart. All rights reserved.
10  * Copyright (c) 2004-2005 The Regents of the University of California.
11  * All rights reserved.
12  * Copyright (c) 2007 Los Alamos National Security, LLC. All rights
13  * reserved.
14  * $COPYRIGHT$
15  *
16  * Additional copyrights may follow
17  *
18  * $HEADER$
19  */
20 
21 #ifndef OPAL_MUTEX_UNIX_H
22 #define OPAL_MUTEX_UNIX_H 1
23 
24 /**
25  * @file:
26  *
27  * Mutual exclusion functions: Unix implementation.
28  *
29  * Functions for locking of critical sections.
30  *
31  * On unix, use pthreads or our own atomic operations as
32  * available.
33  */
34 
35 #include "opal_config.h"
36 
37 #if OPAL_HAVE_POSIX_THREADS
38 #ifdef HAVE_PTHREAD_H
39 #include <pthread.h>
40 #endif
41 #include <errno.h>
42 #include <stdio.h>
43 #elif OPAL_HAVE_SOLARIS_THREADS
44 #include <thread.h>
45 #include <synch.h>
46 #endif
47 
48 #include "opal/class/opal_object.h"
49 #include "opal/sys/atomic.h"
50 
51 BEGIN_C_DECLS
52 
53 struct opal_mutex_t {
54  opal_object_t super;
55 
56 #if OPAL_HAVE_POSIX_THREADS
57  pthread_mutex_t m_lock_pthread;
58 #elif OPAL_HAVE_SOLARIS_THREADS
59  mutex_t m_lock_solaris;
60 #endif
61 
62 #if !OPAL_ENABLE_MULTI_THREADS && OPAL_ENABLE_DEBUG
63  int m_lock_debug;
64  const char *m_lock_file;
65  int m_lock_line;
66 #endif
67 
68  opal_atomic_lock_t m_lock_atomic;
69 };
70 OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_mutex_t);
71 
72 /************************************************************************
73  *
74  * mutex operations (non-atomic versions)
75  *
76  ************************************************************************/
77 
78 #if OPAL_HAVE_POSIX_THREADS
79 
80 /************************************************************************
81  * POSIX threads
82  ************************************************************************/
83 
84 static inline int opal_mutex_trylock(opal_mutex_t *m)
85 {
86 #if OPAL_ENABLE_DEBUG
87  int ret = pthread_mutex_trylock(&m->m_lock_pthread);
88  if (ret == EDEADLK) {
89  errno = ret;
90  perror("opal_mutex_trylock()");
91  abort();
92  }
93  return ret;
94 #else
95  return pthread_mutex_trylock(&m->m_lock_pthread);
96 #endif
97 }
98 
99 static inline void opal_mutex_lock(opal_mutex_t *m)
100 {
101 #if OPAL_ENABLE_DEBUG
102  int ret = pthread_mutex_lock(&m->m_lock_pthread);
103  if (ret == EDEADLK) {
104  errno = ret;
105  perror("opal_mutex_lock()");
106  abort();
107  }
108 #else
109  pthread_mutex_lock(&m->m_lock_pthread);
110 #endif
111 }
112 
113 static inline void opal_mutex_unlock(opal_mutex_t *m)
114 {
115 #if OPAL_ENABLE_DEBUG
116  int ret = pthread_mutex_unlock(&m->m_lock_pthread);
117  if (ret == EPERM) {
118  errno = ret;
119  perror("opal_mutex_unlock");
120  abort();
121  }
122 #else
123  pthread_mutex_unlock(&m->m_lock_pthread);
124 #endif
125 }
126 
127 #elif OPAL_HAVE_SOLARIS_THREADS
128 
129 /************************************************************************
130  * Solaris threads
131  ************************************************************************/
132 
133 
134 static inline int opal_mutex_trylock(opal_mutex_t *m)
135 {
136  return mutex_trylock(&m->m_lock_solaris);
137 }
138 
139 static inline void opal_mutex_lock(opal_mutex_t *m)
140 {
141  mutex_lock(&m->m_lock_solaris);
142 }
143 
144 static inline void opal_mutex_unlock(opal_mutex_t *m)
145 {
146  mutex_unlock(&m->m_lock_solaris);
147 }
148 
149 #elif OPAL_HAVE_ATOMIC_SPINLOCKS
150 
151 /************************************************************************
152  * Spin Locks
153  ************************************************************************/
154 
155 static inline int opal_mutex_trylock(opal_mutex_t *m)
156 {
157  return opal_atomic_trylock(&m->m_lock_atomic);
158 }
159 
160 static inline void opal_mutex_lock(opal_mutex_t *m)
161 {
162  opal_atomic_lock(&m->m_lock_atomic);
163 }
164 
165 static inline void opal_mutex_unlock(opal_mutex_t *m)
166 {
167  opal_atomic_unlock(&m->m_lock_atomic);
168 }
169 
170 #else
171 
172 #error No mutex definition
173 
174 #endif
175 
176 
177 /************************************************************************
178  *
179  * mutex operations (atomic versions)
180  *
181  ************************************************************************/
182 
183 #if OPAL_HAVE_ATOMIC_SPINLOCKS
184 
185 /************************************************************************
186  * Spin Locks
187  ************************************************************************/
188 
189 static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
190 {
191  return opal_atomic_trylock(&m->m_lock_atomic);
192 }
193 
194 static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
195 {
196  opal_atomic_lock(&m->m_lock_atomic);
197 }
198 
199 static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
200 {
201  opal_atomic_unlock(&m->m_lock_atomic);
202 }
203 
204 #else
205 
206 /************************************************************************
207  * Standard locking
208  ************************************************************************/
209 
210 static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
211 {
212  return opal_mutex_trylock(m);
213 }
214 
215 static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
216 {
217  opal_mutex_lock(m);
218 }
219 
220 static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
221 {
223 }
224 
225 #endif
226 
227 END_C_DECLS
228 
229 #endif /* OPAL_MUTEX_UNIX_H */
static void opal_mutex_lock(opal_mutex_t *mutex)
Acquire a mutex.
static int opal_mutex_atomic_trylock(opal_mutex_t *mutex)
Try to acquire a mutex using atomic operations.
static void opal_mutex_atomic_unlock(opal_mutex_t *mutex)
Release a mutex using atomic operations.
Definition: mutex_unix.h:53
static int opal_mutex_trylock(opal_mutex_t *mutex)
Try to acquire a mutex.
Volatile lock object (with optional padding).
Definition: atomic.h:102
Functions for multi-threaded applications using Libevent.
static void opal_atomic_unlock(opal_atomic_lock_t *lock)
Release a lock.
static void opal_mutex_unlock(opal_mutex_t *mutex)
Release a mutex.
Base object.
Definition: opal_object.h:182
static void opal_atomic_lock(opal_atomic_lock_t *lock)
Acquire a lock by spinning.
static int opal_atomic_trylock(opal_atomic_lock_t *lock)
Try to acquire a lock.
Atomic operations.
A simple C-language object-oriented system with single inheritance and ownership-based memory managem...
static void opal_mutex_atomic_lock(opal_mutex_t *mutex)
Acquire a mutex using atomic operations.
#define OBJ_CLASS_DECLARATION(NAME)
Declaration for class descriptor.
Definition: opal_object.h:236