lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/******************************************************************************/
#ifdef JEMALLOC_H_TYPES
 
#endif /* JEMALLOC_H_TYPES */
/******************************************************************************/
#ifdef JEMALLOC_H_STRUCTS
 
#endif /* JEMALLOC_H_STRUCTS */
/******************************************************************************/
#ifdef JEMALLOC_H_EXTERNS
 
#endif /* JEMALLOC_H_EXTERNS */
/******************************************************************************/
#ifdef JEMALLOC_H_INLINES
 
#ifndef JEMALLOC_ENABLE_INLINE
void    mb_write(void);
#endif
 
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MB_C_))
#ifdef __i386__
/*
 * According to the Intel Architecture Software Developer's Manual, current
 * processors execute instructions in order from the perspective of other
 * processors in a multiprocessor system, but 1) Intel reserves the right to
 * change that, and 2) the compiler's optimizer could re-order instructions if
 * there weren't some form of barrier.  Therefore, even if running on an
 * architecture that does not need memory barriers (everything through at least
 * i686), an "optimizer barrier" is necessary.
 */
JEMALLOC_INLINE void
mb_write(void)
{
 
#  if 0
   /* This is a true memory barrier. */
   asm volatile ("pusha;"
       "xor  %%eax,%%eax;"
       "cpuid;"
       "popa;"
       : /* Outputs. */
       : /* Inputs. */
       : "memory" /* Clobbers. */
       );
#  else
   /*
    * This is hopefully enough to keep the compiler from reordering
    * instructions around this one.
    */
   asm volatile ("nop;"
       : /* Outputs. */
       : /* Inputs. */
       : "memory" /* Clobbers. */
       );
#  endif
}
#elif (defined(__amd64__) || defined(__x86_64__))
JEMALLOC_INLINE void
mb_write(void)
{
 
   asm volatile ("sfence"
       : /* Outputs. */
       : /* Inputs. */
       : "memory" /* Clobbers. */
       );
}
#elif defined(__powerpc__)
JEMALLOC_INLINE void
mb_write(void)
{
 
   asm volatile ("eieio"
       : /* Outputs. */
       : /* Inputs. */
       : "memory" /* Clobbers. */
       );
}
#elif defined(__sparc64__)
JEMALLOC_INLINE void
mb_write(void)
{
 
   asm volatile ("membar #StoreStore"
       : /* Outputs. */
       : /* Inputs. */
       : "memory" /* Clobbers. */
       );
}
#elif defined(__tile__)
JEMALLOC_INLINE void
mb_write(void)
{
 
   __sync_synchronize();
}
#else
/*
 * This is much slower than a simple memory barrier, but the semantics of mutex
 * unlock make this work.
 */
JEMALLOC_INLINE void
mb_write(void)
{
   malloc_mutex_t mtx;
 
   malloc_mutex_init(&mtx, "mb", WITNESS_RANK_OMIT);
   malloc_mutex_lock(TSDN_NULL, &mtx);
   malloc_mutex_unlock(TSDN_NULL, &mtx);
}
#endif
#endif
 
#endif /* JEMALLOC_H_INLINES */
/******************************************************************************/