Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tf_lock.h
1/*******************************************************************************
2 * This file is part of mdcore.
3 * Coypright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
4 * Coypright (c) 2017 Andy Somogyi (somogyie at indiana dot edu)
5 * Copyright (c) 2022-2024 T.J. Sego
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 ******************************************************************************/
21
22#ifndef _MDCORE_INCLUDE_TF_LOCK_H_
23#define _MDCORE_INCLUDE_TF_LOCK_H_
24
25#include "tf_platform.h"
26
27#if (defined(_MSC_VER) && !defined(__GNUC__))
28#include <winnt.h>
29#endif
30
31
32#if (defined(_MSC_VER) && !defined(__GNUC__))
33
34TF_ALWAYS_INLINE unsigned
35InterlockedExchangeAdd(int* Addend, int Value) {
36 return (unsigned)_InterlockedExchangeAdd((long*)Addend, (long)Value);
37}
38
39# define sync_val_compare_and_swap(x, y, z) (\
40 sizeof *(x) == sizeof(char) ? _InterlockedCompareExchange8 ((char*) (x), (char) (z), (char) (y)) : \
41 sizeof *(x) == sizeof(short) ? _InterlockedCompareExchange16((short*) (x), (short) (z), (short) (y)) : \
42 sizeof *(x) == sizeof(long) ? _InterlockedCompareExchange ((long*) (x), (long) (z), (long) (y)) : \
43 sizeof *(x) == sizeof(int64_t) ? InterlockedCompareExchange64 ((int64_t*)(x), (int64_t)(z), (int64_t)(y)) : \
44 (assert(!"Type error in sync_val_compare_and_swap"), 0))
45# define sync_fetch_and_add(a, b) _InterlockedExchangeAdd(a, b)
46# define sync_fetch_and_sub(a, b) _InterlockedExchangeAdd(a, -b)
47#else
48# define sync_val_compare_and_swap __sync_val_compare_and_swap
49# define sync_fetch_and_add(a, b) __sync_fetch_and_add(a, b)
50# define sync_fetch_and_sub(a, b) __sync_fetch_and_sub(a, b)
51#endif
52
53
54#ifdef PTHREAD_LOCK
55 #define lock_type pthread_spinlock_t
56 #define lock_init( l ) ( pthread_spin_init( l , PTHREAD_PROCESS_PRIVATE ) != 0 )
57 #define lock_destroy( l ) ( pthread_spin_destroy( l ) != 0 )
58 #define lock_lock( l ) ( pthread_spin_lock( l ) != 0 )
59 #define lock_trylock( l ) ( pthread_spin_lock( l ) != 0 )
60 #define lock_unlock( l ) ( pthread_spin_unlock( l ) != 0 )
61#else
62 #define lock_type volatile int
63 #define lock_init( l ) ( *l = 0 )
64 #define lock_destroy( l ) 0
65 TF_ALWAYS_INLINE int lock_lock ( volatile int *l ) {
66 while ( sync_val_compare_and_swap( l , 0 , 1 ) != 0 )
67 while( *l );
68 return 0;
69 }
70 #define lock_trylock( l ) ( ( *(l) ) ? 1 : sync_val_compare_and_swap( l , 0 , 1 ) )
71 #define lock_unlock( l ) ( sync_val_compare_and_swap( l , 1 , 0 ) != 1 )
72#endif
73
74#endif // _MDCORE_INCLUDE_TF_LOCK_H_