]>
git.ipfire.org Git - people/ms/strongswan.git/blob - src/libstrongswan/utils/utils/atomics.c
2 * Copyright (C) 2008-2014 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include <utils/utils.h>
19 #if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
21 #include <threading/spinlock.h>
24 * Spinlock for ref_get/put
26 static spinlock_t
*ref_lock
;
31 refcount_t
ref_get(refcount_t
*ref
)
35 ref_lock
->lock(ref_lock
);
37 ref_lock
->unlock(ref_lock
);
45 bool ref_put(refcount_t
*ref
)
49 ref_lock
->lock(ref_lock
);
50 more_refs
= --(*ref
) > 0;
51 ref_lock
->unlock(ref_lock
);
58 refcount_t
ref_cur(refcount_t
*ref
)
62 ref_lock
->lock(ref_lock
);
64 ref_lock
->unlock(ref_lock
);
70 * Spinlock for all compare and swap operations.
72 static spinlock_t
*cas_lock
;
75 * Compare and swap if equal to old value
77 #define _cas_impl(name, type) \
78 bool cas_##name(type *ptr, type oldval, type newval) \
81 cas_lock->lock(cas_lock); \
82 if ((swapped = (*ptr == oldval))) { *ptr = newval; } \
83 cas_lock->unlock(cas_lock); \
90 #endif /* !HAVE_GCC_ATOMIC_OPERATIONS && !HAVE_GCC_SYNC_OPERATIONS */
97 #if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
98 ref_lock
= spinlock_create();
99 cas_lock
= spinlock_create();
106 void atomics_deinit()
108 #if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
109 ref_lock
->destroy(ref_lock
);
110 cas_lock
->destroy(cas_lock
);