2 * Copyright © 2017 Gražvydas Ignotas
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #if defined(MISSING_64BIT_ATOMICS) && defined(HAVE_PTHREAD)
29 #if defined(HAVE_FUNC_ATTRIBUTE_WEAK) && !defined(__CYGWIN__)
30 #define WEAK __attribute__((weak))
35 static pthread_mutex_t sync_mutex
= PTHREAD_MUTEX_INITIALIZER
;
38 __sync_add_and_fetch_8(uint64_t *ptr
, uint64_t val
)
42 pthread_mutex_lock(&sync_mutex
);
45 pthread_mutex_unlock(&sync_mutex
);
51 __sync_sub_and_fetch_8(uint64_t *ptr
, uint64_t val
)
55 pthread_mutex_lock(&sync_mutex
);
58 pthread_mutex_unlock(&sync_mutex
);
64 __atomic_fetch_add_8(uint64_t *ptr
, uint64_t val
, int memorder
)
66 return __sync_add_and_fetch(ptr
, val
);
70 __atomic_fetch_sub_8(uint64_t *ptr
, uint64_t val
, int memorder
)
72 return __sync_sub_and_fetch(ptr
, val
);