LCOV - differential code coverage report
Current view: top level - src/include/port/atomics - arch-x86.h (source / functions) Coverage Total Hit UBC CBC
Current: 77aeca80249c9e640c811e80633a2e334a9320de vs 38afc3dcb25c45b744d4025029ce0a6c90b7059f Lines: 100.0 % 23 23 23
Current Date: 2026-07-25 19:08:27 +0900 Functions: 100.0 % 6 6 6
Baseline: lcov-20260725-baseline Branches: 50.0 % 2 1 1 1
Baseline Date: 2026-07-25 19:09:19 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(360..) days: 100.0 % 23 23 23
Function coverage date bins:
(360..) days: 100.0 % 6 6 6
Branch coverage date bins:
(360..) days: 50.0 % 2 1 1 1

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * arch-x86.h
                                  4                 :                :  *    Atomic operations considerations specific to intel x86
                                  5                 :                :  *
                                  6                 :                :  * Note that we actually require a 486 upwards because the 386 doesn't have
                                  7                 :                :  * support for xadd and cmpxchg. Given that the 386 isn't supported anywhere
                                  8                 :                :  * anymore that's not much of a restriction luckily.
                                  9                 :                :  *
                                 10                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 11                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 12                 :                :  *
                                 13                 :                :  * NOTES:
                                 14                 :                :  *
                                 15                 :                :  * src/include/port/atomics/arch-x86.h
                                 16                 :                :  *
                                 17                 :                :  *-------------------------------------------------------------------------
                                 18                 :                :  */
                                 19                 :                : 
                                 20                 :                : /*
                                 21                 :                :  * Both 32 and 64 bit x86 do not allow loads to be reordered with other loads,
                                 22                 :                :  * or stores to be reordered with other stores, but a load can be performed
                                 23                 :                :  * before a subsequent store.
                                 24                 :                :  *
                                 25                 :                :  * Technically, some x86-ish chips support uncached memory access and/or
                                 26                 :                :  * special instructions that are weakly ordered.  In those cases we'd need
                                 27                 :                :  * the read and write barriers to be lfence and sfence.  But since we don't
                                 28                 :                :  * do those things, a compiler barrier should be enough.
                                 29                 :                :  *
                                 30                 :                :  * "lock; addl" has worked for longer than "mfence". It's also rumored to be
                                 31                 :                :  * faster in many scenarios.
                                 32                 :                :  */
                                 33                 :                : 
                                 34                 :                : #if defined(__GNUC__) || defined(__INTEL_COMPILER)
                                 35                 :                : #if defined(__i386__)
                                 36                 :                : #define pg_memory_barrier_impl()        \
                                 37                 :                :     __asm__ __volatile__ ("lock; addl $0,0(%%esp)" : : : "memory", "cc")
                                 38                 :                : #elif defined(__x86_64__)
                                 39                 :                : #define pg_memory_barrier_impl()        \
                                 40                 :                :     __asm__ __volatile__ ("lock; addl $0,0(%%rsp)" : : : "memory", "cc")
                                 41                 :                : #endif
                                 42                 :                : #endif /* defined(__GNUC__) || defined(__INTEL_COMPILER) */
                                 43                 :                : 
                                 44                 :                : #define pg_read_barrier_impl()      pg_compiler_barrier_impl()
                                 45                 :                : #define pg_write_barrier_impl()     pg_compiler_barrier_impl()
                                 46                 :                : 
                                 47                 :                : /*
                                 48                 :                :  * Provide implementation for atomics using inline assembly on x86 gcc. It's
                                 49                 :                :  * nice to support older gcc's and the compare/exchange implementation here is
                                 50                 :                :  * actually more efficient than the * __sync variant.
                                 51                 :                :  */
                                 52                 :                : #if defined(__GNUC__) || defined(__INTEL_COMPILER)
                                 53                 :                : 
                                 54                 :                : #define PG_HAVE_ATOMIC_FLAG_SUPPORT
                                 55                 :                : typedef struct pg_atomic_flag
                                 56                 :                : {
                                 57                 :                :     volatile char value;
                                 58                 :                : } pg_atomic_flag;
                                 59                 :                : 
                                 60                 :                : #define PG_HAVE_ATOMIC_U32_SUPPORT
                                 61                 :                : typedef struct pg_atomic_uint32
                                 62                 :                : {
                                 63                 :                :     volatile uint32 value;
                                 64                 :                : } pg_atomic_uint32;
                                 65                 :                : 
                                 66                 :                : /*
                                 67                 :                :  * It's too complicated to write inline asm for 64bit types on 32bit and the
                                 68                 :                :  * 486 can't do it anyway.
                                 69                 :                :  */
                                 70                 :                : #ifdef __x86_64__
                                 71                 :                : #define PG_HAVE_ATOMIC_U64_SUPPORT
                                 72                 :                : typedef struct pg_atomic_uint64
                                 73                 :                : {
                                 74                 :                :     /* alignment guaranteed due to being on a 64bit platform */
                                 75                 :                :     volatile uint64 value;
                                 76                 :                : } pg_atomic_uint64;
                                 77                 :                : #endif  /* __x86_64__ */
                                 78                 :                : 
                                 79                 :                : #define PG_HAVE_ATOMIC_TEST_SET_FLAG
                                 80                 :                : static inline bool
 4321 andres@anarazel.de         81                 :CBC        1592 : pg_atomic_test_set_flag_impl(volatile pg_atomic_flag *ptr)
                                 82                 :                : {
 1400                            83                 :           1592 :     char        _res = 1;
                                 84                 :                : 
 4080 bruce@momjian.us           85                 :           1592 :     __asm__ __volatile__(
                                 86                 :                :         "  lock            \n"
                                 87                 :                :         "  xchgb   %0,%1   \n"
                                 88                 :                : :       "+q"(_res), "+m"(ptr->value)
                                 89                 :                : :
                                 90                 :                : :       "memory");
 4321 andres@anarazel.de         91                 :           1592 :     return _res == 0;
                                 92                 :                : }
                                 93                 :                : 
                                 94                 :                : #define PG_HAVE_ATOMIC_CLEAR_FLAG
                                 95                 :                : static inline void
 4320                            96                 :          13424 : pg_atomic_clear_flag_impl(volatile pg_atomic_flag *ptr)
                                 97                 :                : {
                                 98                 :                :     /*
                                 99                 :                :      * On a TSO architecture like x86 it's sufficient to use a compiler
                                100                 :                :      * barrier to achieve release semantics.
                                101                 :                :      */
 4080 bruce@momjian.us          102                 :          13424 :     __asm__ __volatile__("" ::: "memory");
 4320 andres@anarazel.de        103                 :          13424 :     ptr->value = 0;
                                104                 :          13424 : }
                                105                 :                : 
                                106                 :                : #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32
                                107                 :                : static inline bool
 4321                           108                 :      223306196 : pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr,
                                109                 :                :                                     uint32 *expected, uint32 newval)
                                110                 :                : {
                                111                 :                :     char    ret;
                                112                 :                : 
                                113                 :                :     /*
                                114                 :                :      * Perform cmpxchg and use the zero flag which it implicitly sets when
                                115                 :                :      * equal to measure the success.
                                116                 :                :      */
 4080 bruce@momjian.us          117                 :      223306196 :     __asm__ __volatile__(
                                118                 :                :         "  lock                \n"
                                119                 :                :         "  cmpxchgl    %4,%5   \n"
                                120                 :                :         "   setz       %2      \n"
                                121                 :                : :       "=a" (*expected), "=m"(ptr->value), "=q" (ret)
                                122                 :      223306196 : :       "a" (*expected), "r" (newval), "m"(ptr->value)
                                123                 :                : :       "memory", "cc");
 4321 andres@anarazel.de        124                 :      223306196 :     return (bool) ret;
                                125                 :                : }
                                126                 :                : 
                                127                 :                : #define PG_HAVE_ATOMIC_FETCH_ADD_U32
                                128                 :                : static inline uint32
                                129                 :        8228038 : pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_)
                                130                 :                : {
                                131                 :                :     uint32 res;
 4080 bruce@momjian.us          132                 :        8228038 :     __asm__ __volatile__(
                                133                 :                :         "  lock                \n"
                                134                 :                :         "  xaddl   %0,%1       \n"
                                135                 :                : :       "=q"(res), "=m"(ptr->value)
                                136                 :                : :       "0" (add_), "m"(ptr->value)
                                137                 :                : :       "memory", "cc");
 4321 andres@anarazel.de        138                 :        8228038 :     return res;
                                139                 :                : }
                                140                 :                : 
                                141                 :                : #ifdef __x86_64__
                                142                 :                : 
                                143                 :                : #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64
                                144                 :                : static inline bool
                                145                 :      254855119 : pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,
                                146                 :                :                                     uint64 *expected, uint64 newval)
                                147                 :                : {
                                148                 :                :     char    ret;
                                149                 :                : 
  751 alvherre@alvh.no-ip.      150         [ -  + ]:      254855119 :     AssertPointerAlignment(expected, 8);
                                151                 :                : 
                                152                 :                :     /*
                                153                 :                :      * Perform cmpxchg and use the zero flag which it implicitly sets when
                                154                 :                :      * equal to measure the success.
                                155                 :                :      */
 4080 bruce@momjian.us          156                 :      254855119 :     __asm__ __volatile__(
                                157                 :                :         "  lock                \n"
                                158                 :                :         "  cmpxchgq    %4,%5   \n"
                                159                 :                :         "   setz       %2      \n"
                                160                 :                : :       "=a" (*expected), "=m"(ptr->value), "=q" (ret)
                                161                 :      254855119 : :       "a" (*expected), "r" (newval), "m"(ptr->value)
                                162                 :                : :       "memory", "cc");
 4321 andres@anarazel.de        163                 :      254855119 :     return (bool) ret;
                                164                 :                : }
                                165                 :                : 
                                166                 :                : #define PG_HAVE_ATOMIC_FETCH_ADD_U64
                                167                 :                : static inline uint64
                                168                 :       12154011 : pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_)
                                169                 :                : {
                                170                 :                :     uint64 res;
 4080 bruce@momjian.us          171                 :       12154011 :     __asm__ __volatile__(
                                172                 :                :         "  lock                \n"
                                173                 :                :         "  xaddq   %0,%1       \n"
                                174                 :                : :       "=q"(res), "=m"(ptr->value)
                                175                 :                : :       "0" (add_), "m"(ptr->value)
                                176                 :                : :       "memory", "cc");
 4321 andres@anarazel.de        177                 :       12154011 :     return res;
                                178                 :                : }
                                179                 :                : 
                                180                 :                : #endif /* __x86_64__ */
                                181                 :                : 
                                182                 :                : #endif /* defined(__GNUC__) || defined(__INTEL_COMPILER) */
                                183                 :                : 
                                184                 :                : /*
                                185                 :                :  * 8 byte reads / writes have single-copy atomicity on all x86-64 cpus.
                                186                 :                :  */
                                187                 :                : #if defined(__x86_64__)
                                188                 :                : #define PG_HAVE_8BYTE_SINGLE_COPY_ATOMICITY
                                189                 :                : #endif /* 8 byte single-copy atomicity */
        

Generated by: LCOV version 2.0-1