2 This source code file is part of thread_mpi.
3 Written by Sander Pronk, Erik Lindahl, and possibly others.
5 Copyright (c) 2009, Sander Pronk, Erik Lindahl.
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 1) Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3) Neither the name of the copyright holders nor the
16 names of its contributors may be used to endorse or promote products
17 derived from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY US ''AS IS'' AND ANY
20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 If you want to redistribute modifications, please consider that
31 scientific software is very special. Version control is crucial -
32 bugs must be traceable. We will be happy to consider code for
33 inclusion in the official distribution, but derived work should not
34 be called official thread_mpi. Details are found in the README & COPYING
40 /* This file is only needed when no intrinsic atomic operations are present. */
41 #ifdef TMPI_NO_ATOMICS
43 /** System mutex used for locking to guarantee atomicity */
44 static tMPI_Thread_mutex_t tMPI_Atomic_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
48 tMPI_Thread_mutex_t *lock;
51 int tMPI_Atomic_get(const tMPI_Atomic_t *a)
54 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
56 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
60 void tMPI_Atomic_set(tMPI_Atomic_t *a, int value)
62 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
64 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
68 void* tMPI_Atomic_ptr_get(const tMPI_Atomic_ptr_t *a)
71 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
73 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
77 void tMPI_Atomic_ptr_set(tMPI_Atomic_ptr_t *a, void *value)
79 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
81 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
84 int tMPI_Atomic_add_return(tMPI_Atomic_t *a, int i)
87 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
90 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
94 int tMPI_Atomic_fetch_add(tMPI_Atomic_t *a, int i)
98 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
100 a->value = old_value + i;
101 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
105 int tMPI_Atomic_cas(tMPI_Atomic_t *a, int old_val, int new_val)
109 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
110 if (a->value == old_val)
115 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
120 int tMPI_Atomic_ptr_cas(tMPI_Atomic_ptr_t * a, void *old_val, void *new_val)
124 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
125 if (a->value == old_val)
130 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
134 int tMPI_Atomic_swap(tMPI_Atomic_t *a, int b)
137 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
140 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
145 void *tMPI_Atomic_ptr_swap(tMPI_Atomic_ptr_t *a, void *b)
149 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
152 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
158 void tMPI_Spinlock_init( tMPI_Spinlock_t *x)
160 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
161 *x = (tMPI_Spinlock_t)malloc(sizeof(tMPI_Spinlock_t));
162 (*x)->lock = (tMPI_Thread_mutex_t*)malloc(sizeof(tMPI_Thread_mutex_t));
163 tMPI_Thread_mutex_init((*x)->lock);
164 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
167 /* NOTE: assumes atomic mutex is locked */
168 static void tMPI_Spinlock_init_once(tMPI_Spinlock_t *x)
170 tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
173 *x = (tMPI_Spinlock_t)malloc(sizeof(tMPI_Spinlock_t));
174 (*x)->lock = (tMPI_Thread_mutex_t*)malloc(sizeof(tMPI_Thread_mutex_t));
175 tMPI_Thread_mutex_init((*x)->lock);
177 tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
181 void tMPI_Spinlock_lock( tMPI_Spinlock_t *x)
183 tMPI_Spinlock_init_once(x);
184 tMPI_Thread_mutex_lock((*x)->lock);
187 void tMPI_Spinlock_unlock( tMPI_Spinlock_t *x)
189 tMPI_Spinlock_init_once(x);
190 tMPI_Thread_mutex_unlock((*x)->lock);
193 int tMPI_Spinlock_trylock( tMPI_Spinlock_t *x)
196 tMPI_Spinlock_init_once(x);
197 ret = tMPI_Thread_mutex_trylock((*x)->lock);
201 int tMPI_Spinlock_islocked(tMPI_Spinlock_t *x)
204 tMPI_Spinlock_init_once(x);
205 ret = tMPI_Thread_mutex_trylock((*x)->lock);
208 tMPI_Thread_mutex_unlock((*x)->lock);
220 void tMPI_Spinlock_wait(tMPI_Spinlock_t *x)
222 tMPI_Spinlock_init_once(x);
224 tMPI_Spinlock_lock(x);
225 /* Got the lock now, so the waiting is over */
226 tMPI_Spinlock_unlock(x);
231 /* just to have some symbols */
232 int _tMPI_Atomics = 1;