LCOV - code coverage report
Current view: top level - lib/eal/x86/include - rte_atomic.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 3 3 100.0 %
Date: 2026-11-01 19:11:22 Functions: 0 0 -
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 48 326 14.7 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #ifndef _RTE_ATOMIC_X86_H_
       6                 :            : #define _RTE_ATOMIC_X86_H_
       7                 :            : 
       8                 :            : #include <stdint.h>
       9                 :            : #include <rte_common.h>
      10                 :            : #include <rte_config.h>
      11                 :            : #include <emmintrin.h>
      12                 :            : #include "generic/rte_atomic.h"
      13                 :            : 
      14                 :            : #if RTE_MAX_LCORE == 1
      15                 :            : #define MPLOCKED                        /**< No need to insert MP lock prefix. */
      16                 :            : #else
      17                 :            : #define MPLOCKED        "lock ; "       /**< Insert MP lock prefix. */
      18                 :            : #endif
      19                 :            : 
      20                 :            : #define rte_mb() _mm_mfence()
      21                 :            : 
      22                 :            : #define rte_wmb() _mm_sfence()
      23                 :            : 
      24                 :            : #define rte_rmb() _mm_lfence()
      25                 :            : 
      26                 :            : #ifdef __cplusplus
      27                 :            : extern "C" {
      28                 :            : #endif
      29                 :            : 
      30                 :            : /*
      31                 :            :  * From Intel Software Development Manual; Vol 3;
      32                 :            :  * 8.2.2 Memory Ordering in P6 and More Recent Processor Families:
      33                 :            :  * ...
      34                 :            :  * . Reads are not reordered with other reads.
      35                 :            :  * . Writes are not reordered with older reads.
      36                 :            :  * . Writes to memory are not reordered with other writes,
      37                 :            :  *   with the following exceptions:
      38                 :            :  *   . streaming stores (writes) executed with the non-temporal move
      39                 :            :  *     instructions (MOVNTI, MOVNTQ, MOVNTDQ, MOVNTPS, and MOVNTPD); and
      40                 :            :  *   . string operations (see Section 8.2.4.1).
      41                 :            :  *  ...
      42                 :            :  * . Reads may be reordered with older writes to different locations but not
      43                 :            :  * with older writes to the same location.
      44                 :            :  * . Reads or writes cannot be reordered with I/O instructions,
      45                 :            :  * locked instructions, or serializing instructions.
      46                 :            :  * . Reads cannot pass earlier LFENCE and MFENCE instructions.
      47                 :            :  * . Writes ... cannot pass earlier LFENCE, SFENCE, and MFENCE instructions.
      48                 :            :  * . LFENCE instructions cannot pass earlier reads.
      49                 :            :  * . SFENCE instructions cannot pass earlier writes ...
      50                 :            :  * . MFENCE instructions cannot pass earlier reads, writes ...
      51                 :            :  *
      52                 :            :  * As pointed by Java guys, that makes possible to use lock-prefixed
      53                 :            :  * instructions to get the same effect as mfence and on most modern HW
      54                 :            :  * that gives a better performance then using mfence:
      55                 :            :  * https://shipilev.net/blog/2014/on-the-fence-with-dependencies/
      56                 :            :  * Basic idea is to use lock prefixed add with some dummy memory location
      57                 :            :  * as the destination. From their experiments 128B(2 cache lines) below
      58                 :            :  * current stack pointer looks like a good candidate.
      59                 :            :  */
      60                 :            : 
      61                 :            : #define rte_io_mb() rte_mb()
      62                 :            : 
      63                 :            : #define rte_io_wmb() rte_compiler_barrier()
      64                 :            : 
      65                 :            : #define rte_io_rmb() rte_compiler_barrier()
      66                 :            : 
      67                 :            : /**
      68                 :            :  * Synchronization fence between threads based on the specified memory order.
      69                 :            :  *
      70                 :            :  * On x86 the __rte_atomic_thread_fence(rte_memory_order_seq_cst) generates
      71                 :            :  * a full 'mfence' which is quite expensive. The optimized lock add on a
      72                 :            :  * dummy stack location (see above) is used instead.
      73                 :            :  */
      74                 :            : static __rte_always_inline void
      75                 :            : rte_atomic_thread_fence(rte_memory_order memorder)
      76                 :            : {
      77                 :            :         if (memorder != rte_memory_order_seq_cst) {
      78   [ +  +  +  +  :  102733643 :                 __rte_atomic_thread_fence(memorder);
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  -  +  
          -  +  -  +  -  
          +  +  +  -  +  
          -  -  -  +  -  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  +  +  -  
          -  +  -  -  -  
          -  +  -  -  +  
          +  -  -  +  -  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  -  -  +  
          +  -  -  +  +  
          -  -  +  -  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  -  
          -  -  +  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      79                 :          8 :                 return;
      80                 :            :         }
      81                 :            : 
      82                 :            : #ifdef RTE_TOOLCHAIN_MSVC
      83                 :            :         _mm_mfence();
      84                 :            : #else
      85                 :            : #ifdef RTE_ARCH_I686
      86                 :            :         asm volatile("lock addl $0, -128(%%esp); " ::: "memory");
      87                 :            : #else
      88                 :        405 :         asm volatile("lock addl $0, -128(%%rsp); " ::: "memory");
      89                 :            : #endif
      90                 :            : #endif
      91                 :            : }
      92                 :            : 
      93                 :            : #ifdef __cplusplus
      94                 :            : }
      95                 :            : #endif
      96                 :            : 
      97                 :            : #ifndef RTE_TOOLCHAIN_MSVC
      98                 :            : 
      99                 :            : #ifndef RTE_ARCH_I686
     100                 :            : #include "rte_atomic_64.h"
     101                 :            : #endif
     102                 :            : 
     103                 :            : #endif /* !RTE_TOOLCHAIN_MSVC */
     104                 :            : 
     105                 :            : #endif /* _RTE_ATOMIC_X86_H_ */

Generated by: LCOV version 1.14