1 /*
2  * Copyright © 2017 Gražvydas Ignotas
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  */
23 
24 #if defined(MISSING_64BIT_ATOMICS) && defined(HAVE_PTHREAD)
25 
26 #include <stdint.h>
27 #include <pthread.h>
28 
29 #if defined(HAVE_FUNC_ATTRIBUTE_WEAK) && !defined(__CYGWIN__)
30 #define WEAK __attribute__((weak))
31 #else
32 #define WEAK
33 #endif
34 
35 static pthread_mutex_t sync_mutex = PTHREAD_MUTEX_INITIALIZER;
36 
37 #ifdef __clang__
38 #pragma clang diagnostic ignored "-Wmissing-prototypes"
39 #pragma redefine_extname __sync_add_and_fetch_8_c __sync_add_and_fetch_8
40 #pragma redefine_extname __sync_sub_and_fetch_8_c __sync_sub_and_fetch_8
41 #pragma redefine_extname __sync_fetch_and_add_8_c __sync_fetch_and_add_8
42 #pragma redefine_extname __sync_fetch_and_sub_8_c __sync_fetch_and_sub_8
43 #pragma redefine_extname __sync_val_compare_and_swap_8_c \
44     __sync_val_compare_and_swap_8
45 #define __sync_add_and_fetch_8 __sync_add_and_fetch_8_c
46 #define __sync_sub_and_fetch_8 __sync_sub_and_fetch_8_c
47 #define __sync_fetch_and_add_8 __sync_fetch_and_add_8_c
48 #define __sync_fetch_and_sub_8 __sync_fetch_and_sub_8_c
49 #define __sync_val_compare_and_swap_8 __sync_val_compare_and_swap_8_c
50 #endif
51 
52 WEAK uint64_t
__sync_add_and_fetch_8(uint64_t * ptr,uint64_t val)53 __sync_add_and_fetch_8(uint64_t *ptr, uint64_t val)
54 {
55    uint64_t r;
56 
57    pthread_mutex_lock(&sync_mutex);
58    *ptr += val;
59    r = *ptr;
60    pthread_mutex_unlock(&sync_mutex);
61 
62    return r;
63 }
64 
65 WEAK uint64_t
__sync_sub_and_fetch_8(uint64_t * ptr,uint64_t val)66 __sync_sub_and_fetch_8(uint64_t *ptr, uint64_t val)
67 {
68    uint64_t r;
69 
70    pthread_mutex_lock(&sync_mutex);
71    *ptr -= val;
72    r = *ptr;
73    pthread_mutex_unlock(&sync_mutex);
74 
75    return r;
76 }
77 
78 WEAK uint64_t
__sync_fetch_and_add_8(uint64_t * ptr,uint64_t val)79 __sync_fetch_and_add_8(uint64_t *ptr, uint64_t val)
80 {
81    uint64_t r;
82 
83    pthread_mutex_lock(&sync_mutex);
84    r = *ptr;
85    *ptr += val;
86    pthread_mutex_unlock(&sync_mutex);
87 
88    return r;
89 }
90 
91 WEAK uint64_t
__sync_fetch_and_sub_8(uint64_t * ptr,uint64_t val)92 __sync_fetch_and_sub_8(uint64_t *ptr, uint64_t val)
93 {
94    uint64_t r;
95 
96    pthread_mutex_lock(&sync_mutex);
97    r = *ptr;
98    *ptr -= val;
99    pthread_mutex_unlock(&sync_mutex);
100 
101    return r;
102 }
103 
104 WEAK uint64_t
__sync_val_compare_and_swap_8(uint64_t * ptr,uint64_t oldval,uint64_t newval)105 __sync_val_compare_and_swap_8(uint64_t *ptr, uint64_t oldval, uint64_t newval)
106 {
107    uint64_t r;
108 
109    pthread_mutex_lock(&sync_mutex);
110    r = *ptr;
111    if (*ptr == oldval)
112       *ptr = newval;
113    pthread_mutex_unlock(&sync_mutex);
114 
115    return r;
116 }
117 
118 #endif
119