bin/contrib/memaslap: attempt to fix atomics detection
[m6w6/libmemcached] / src / bin / contrib / memaslap / ms_atomic.h
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #ifndef CLIENTS_MS_ATOMIC_H
17 #define CLIENTS_MS_ATOMIC_H
18
19 #include "mem_config.h"
20
21 #if defined(__SUNPRO_C) || defined(HAVE_ATOMIC_ADD_NV)
22 # define ATOMIC volatile
23 # define _KERNEL
24 # include <atomic.h>
25 # if SIZEOF_SIZE_T == 8
26 # define atomic_add_size(X, Y) atomic_add_64((X), (Y))
27 # else
28 # define atomic_add_size(X, Y) atomic_add_32((X), (Y))
29 # endif
30 # undef _KERNEL
31
32 #elif HAVE_C_STDATOMIC
33 # define ATOMIC _Atomic
34 # include <stdatomic.h>
35 # define atomic_dec_32(X) atomic_fetch_sub(X,1)
36 # define atomic_add_32 atomic_fetch_add
37 # define atomic_add_32_nv(X,Y) (atomic_fetch_add(X,Y)+(Y))
38 # define atomic_add_size atomic_fetch_add
39
40 #elif HAVE_BUILTIN_ATOMIC
41 # define ATOMIC
42 # define atomic_dec_32(X) BUILTIN_ATOMIC_PREFIX ## _atomic_fetch_sub(X,1)
43 # define atomic_add_32 BUILTIN_ATOMIC_PREFIX ## _atomic_fetch_add
44 # define atomic_add_32_nv BUILTIN_ATOMIC_PREFIX ## _atomic_add_fetch(X,Y)
45 # define atomic_add_size BUILTIN_ATOMIC_PREFIX ## _atomic_fetch_add
46
47 #elif HAVE_BUILTIN_SYNC
48 # define ATOMIC
49 # define atomic_dec_32(X) __sync_fetch_and_sub((X), 1)
50 # define atomic_add_32 __sync_fetch_and_add
51 # define atomic_add_32_nv __sync_add_and_fetch
52 # define atomic_add_size __sync_fetch_and_add
53
54 #else
55 # warning "Atomic operators not found so memslap will not work correctly"
56 # define ATOMIC
57 # define atomic_dec_32(X) (--(*(X)))
58 # define atomic_add_32(X,Y) ((*(X))+=(Y))
59 # define atomic_add_32_nv atomic_add_32
60 # define atomic_add_size atomic_add_32
61
62 #endif
63
64 #endif /* CLIENTS_MS_ATOMIC_H */