1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2017 Gražvydas Ignotas 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#if defined(MISSING_64BIT_ATOMICS) && defined(HAVE_PTHREAD) 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#include <stdint.h> 27bf215546Sopenharmony_ci#include <pthread.h> 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#if defined(HAVE_FUNC_ATTRIBUTE_WEAK) && !defined(__CYGWIN__) 30bf215546Sopenharmony_ci#define WEAK __attribute__((weak)) 31bf215546Sopenharmony_ci#else 32bf215546Sopenharmony_ci#define WEAK 33bf215546Sopenharmony_ci#endif 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_cistatic pthread_mutex_t sync_mutex = PTHREAD_MUTEX_INITIALIZER; 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci#ifdef __clang__ 38bf215546Sopenharmony_ci#pragma clang diagnostic ignored "-Wmissing-prototypes" 39bf215546Sopenharmony_ci#pragma redefine_extname __sync_add_and_fetch_8_c __sync_add_and_fetch_8 40bf215546Sopenharmony_ci#pragma redefine_extname __sync_sub_and_fetch_8_c __sync_sub_and_fetch_8 41bf215546Sopenharmony_ci#pragma redefine_extname __sync_fetch_and_add_8_c __sync_fetch_and_add_8 42bf215546Sopenharmony_ci#pragma redefine_extname __sync_fetch_and_sub_8_c __sync_fetch_and_sub_8 43bf215546Sopenharmony_ci#pragma redefine_extname __sync_val_compare_and_swap_8_c \ 44bf215546Sopenharmony_ci __sync_val_compare_and_swap_8 45bf215546Sopenharmony_ci#define __sync_add_and_fetch_8 __sync_add_and_fetch_8_c 46bf215546Sopenharmony_ci#define __sync_sub_and_fetch_8 __sync_sub_and_fetch_8_c 47bf215546Sopenharmony_ci#define __sync_fetch_and_add_8 __sync_fetch_and_add_8_c 48bf215546Sopenharmony_ci#define __sync_fetch_and_sub_8 __sync_fetch_and_sub_8_c 49bf215546Sopenharmony_ci#define __sync_val_compare_and_swap_8 __sync_val_compare_and_swap_8_c 50bf215546Sopenharmony_ci#endif 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ciWEAK uint64_t 53bf215546Sopenharmony_ci__sync_add_and_fetch_8(uint64_t *ptr, uint64_t val) 54bf215546Sopenharmony_ci{ 55bf215546Sopenharmony_ci uint64_t r; 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ci pthread_mutex_lock(&sync_mutex); 58bf215546Sopenharmony_ci *ptr += val; 59bf215546Sopenharmony_ci r = *ptr; 60bf215546Sopenharmony_ci pthread_mutex_unlock(&sync_mutex); 61bf215546Sopenharmony_ci 62bf215546Sopenharmony_ci return r; 63bf215546Sopenharmony_ci} 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ciWEAK uint64_t 66bf215546Sopenharmony_ci__sync_sub_and_fetch_8(uint64_t *ptr, uint64_t val) 67bf215546Sopenharmony_ci{ 68bf215546Sopenharmony_ci uint64_t r; 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci pthread_mutex_lock(&sync_mutex); 71bf215546Sopenharmony_ci *ptr -= val; 72bf215546Sopenharmony_ci r = *ptr; 73bf215546Sopenharmony_ci pthread_mutex_unlock(&sync_mutex); 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci return r; 76bf215546Sopenharmony_ci} 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ciWEAK uint64_t 79bf215546Sopenharmony_ci__sync_fetch_and_add_8(uint64_t *ptr, uint64_t val) 80bf215546Sopenharmony_ci{ 81bf215546Sopenharmony_ci uint64_t r; 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci pthread_mutex_lock(&sync_mutex); 84bf215546Sopenharmony_ci r = *ptr; 85bf215546Sopenharmony_ci *ptr += val; 86bf215546Sopenharmony_ci pthread_mutex_unlock(&sync_mutex); 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci return r; 89bf215546Sopenharmony_ci} 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ciWEAK uint64_t 92bf215546Sopenharmony_ci__sync_fetch_and_sub_8(uint64_t *ptr, uint64_t val) 93bf215546Sopenharmony_ci{ 94bf215546Sopenharmony_ci uint64_t r; 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci pthread_mutex_lock(&sync_mutex); 97bf215546Sopenharmony_ci r = *ptr; 98bf215546Sopenharmony_ci *ptr -= val; 99bf215546Sopenharmony_ci pthread_mutex_unlock(&sync_mutex); 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci return r; 102bf215546Sopenharmony_ci} 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ciWEAK uint64_t 105bf215546Sopenharmony_ci__sync_val_compare_and_swap_8(uint64_t *ptr, uint64_t oldval, uint64_t newval) 106bf215546Sopenharmony_ci{ 107bf215546Sopenharmony_ci uint64_t r; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci pthread_mutex_lock(&sync_mutex); 110bf215546Sopenharmony_ci r = *ptr; 111bf215546Sopenharmony_ci if (*ptr == oldval) 112bf215546Sopenharmony_ci *ptr = newval; 113bf215546Sopenharmony_ci pthread_mutex_unlock(&sync_mutex); 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci return r; 116bf215546Sopenharmony_ci} 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci#endif 119