Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Knot projects
Knot DNS
Commits
7759c4ed
Commit
7759c4ed
authored
Nov 25, 2022
by
David Vasek
Browse files
WIP: contrib: universal atomic operations
parent
49a8bb72
Pipeline
#106766
passed with stages
in 6 minutes and 5 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/contrib/atomic.h
0 → 100644
View file @
7759c4ed
/* Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*!
* \brief Multiplatform atomic operations.
*/
#pragma once
#ifdef HAVE_ATOMIC
/* C11 */
#define ATOMIC_SET(dst, val) __atomic_store_n(&(dst), (val), __ATOMIC_RELAXED)
#define ATOMIC_GET(src) __atomic_load_n(&(src), __ATOMIC_RELAXED)
#define ATOMIC_ADD(dst, val) __atomic_add_fetch(&(dst), (val), __ATOMIC_RELAXED)
#define ATOMIC_SUB(dst, val) __atomic_sub_fetch(&(dst), (val), __ATOMIC_RELAXED)
#elif HAVE_SYNC_ATOMIC
/* GCC, partial support only. */
#define ATOMIC_SET(dst, val) ((dst) = (val))
#define ATOMIC_GET(src) __sync_fetch_and_or(&(src), 0)
#define ATOMIC_ADD(dst, val) __sync_add_and_fetch(&(dst), (val))
#define ATOMIC_SUB(dst, val) __sync_sub_and_fetch(&(dst), (val))
#else
/* Fallback, non-atomic. */
#define ATOMIC_SET(dst, val) ((dst) = (val))
#define ATOMIC_GET(src) (src)
#define ATOMIC_ADD(dst, val) ((dst) += (val))
#define ATOMIC_SUB(dst, val) ((dst) -= (val))
#endif
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment