b9f5e0e2eea6712904d4dffe8acf1c30e8d8bf6e
[alexxy/gromacs.git] / src / gromacs / gmxlib / thread_mpi / atomic.c
1 /*
2    This source code file is part of thread_mpi.
3    Written by Sander Pronk, Erik Lindahl, and possibly others.
4
5    Copyright (c) 2009, Sander Pronk, Erik Lindahl.
6    All rights reserved.
7
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.
18
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.
29
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
35    files.
36  */
37
38 #include "impl.h"
39
40 /* This file is only needed when no intrinsic atomic operations are present. */
41 #ifdef TMPI_NO_ATOMICS
42
43 /** System mutex used for locking to guarantee atomicity */
44 static tMPI_Thread_mutex_t tMPI_Atomic_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
45
46 struct tMPI_Spinlock
47 {
48     tMPI_Thread_mutex_t *lock;
49 };
50
51 int tMPI_Atomic_get(const tMPI_Atomic_t *a)
52 {
53     int ret;
54     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
55     ret = a->value;
56     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
57     return ret;
58 }
59
60 void tMPI_Atomic_set(tMPI_Atomic_t *a, int value)
61 {
62     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
63     a->value = value;
64     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
65 }
66
67
68 void* tMPI_Atomic_ptr_get(const tMPI_Atomic_ptr_t *a)
69 {
70     void* ret;
71     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
72     ret = a->value;
73     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
74     return ret;
75 }
76
77 void tMPI_Atomic_ptr_set(tMPI_Atomic_ptr_t *a, void *value)
78 {
79     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
80     a->value = value;
81     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
82 }
83
84 int tMPI_Atomic_add_return(tMPI_Atomic_t *a, int i)
85 {
86     int t;
87     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
88     t        = a->value + i;
89     a->value = t;
90     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
91     return t;
92 }
93
94 int tMPI_Atomic_fetch_add(tMPI_Atomic_t *a, int i)
95 {
96     int old_value;
97
98     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
99     old_value = a->value;
100     a->value  = old_value + i;
101     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
102     return old_value;
103 }
104
105 int tMPI_Atomic_cas(tMPI_Atomic_t *a, int old_val, int new_val)
106 {
107     int t = 0;
108
109     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
110     if (a->value == old_val)
111     {
112         a->value = new_val;
113         t        = 1;
114     }
115     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
116     return t;
117 }
118
119
120 int tMPI_Atomic_ptr_cas(tMPI_Atomic_ptr_t * a, void *old_val, void *new_val)
121 {
122     int t = 0;
123
124     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
125     if (a->value == old_val)
126     {
127         a->value = new_val;
128         t        = 1;
129     }
130     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
131     return t;
132 }
133
134 int tMPI_Atomic_swap(tMPI_Atomic_t *a, int b)
135 {
136     int ret;
137     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
138     ret      = a->value;
139     a->value = b;
140     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
141
142     return ret;
143 }
144
145 void *tMPI_Atomic_ptr_swap(tMPI_Atomic_ptr_t *a, void *b)
146 {
147     void *ret;
148
149     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
150     ret      = a->value;
151     a->value = b;
152     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
153
154     return ret;
155 }
156
157
158 void tMPI_Spinlock_init( tMPI_Spinlock_t *x)
159 {
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);
165 }
166
167 /* NOTE: assumes atomic mutex is locked */
168 static void tMPI_Spinlock_init_once(tMPI_Spinlock_t *x)
169 {
170     tMPI_Thread_mutex_lock(&tMPI_Atomic_mutex);
171     if (!*x)
172     {
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);
176     }
177     tMPI_Thread_mutex_unlock(&tMPI_Atomic_mutex);
178 }
179
180
181 void tMPI_Spinlock_lock( tMPI_Spinlock_t *x)
182 {
183     tMPI_Spinlock_init_once(x);
184     tMPI_Thread_mutex_lock((*x)->lock);
185 }
186
187 void tMPI_Spinlock_unlock( tMPI_Spinlock_t *x)
188 {
189     tMPI_Spinlock_init_once(x);
190     tMPI_Thread_mutex_unlock((*x)->lock);
191 }
192
193 int tMPI_Spinlock_trylock( tMPI_Spinlock_t *x)
194 {
195     int ret;
196     tMPI_Spinlock_init_once(x);
197     ret = tMPI_Thread_mutex_trylock((*x)->lock);
198     return ret;
199 }
200
201 int tMPI_Spinlock_islocked(tMPI_Spinlock_t *x)
202 {
203     int ret;
204     tMPI_Spinlock_init_once(x);
205     ret = tMPI_Thread_mutex_trylock((*x)->lock);
206     if (ret == 0)
207     {
208         tMPI_Thread_mutex_unlock((*x)->lock);
209         ret = 0;
210     }
211     else
212     {
213         ret = 1;
214     }
215
216     return ret;
217 }
218
219
220 void tMPI_Spinlock_wait(tMPI_Spinlock_t *x)
221 {
222     tMPI_Spinlock_init_once(x);
223
224     tMPI_Spinlock_lock(x);
225     /* Got the lock now, so the waiting is over */
226     tMPI_Spinlock_unlock(x);
227 }
228
229 #else
230
231 /* just to have some symbols */
232 int _tMPI_Atomics = 1;
233
234 #endif