Free Electron
count.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __platform_Count_h__
8 #define __platform_Count_h__
9 
10 #define FE_COUNT_MT TRUE
11 
12 #if FE_COUNT_MT && FE_HW==FE_X86 && FE_OS!=FE_OSX
13 #define FE_COUNT_ASM_IMPL
14 #endif
15 
16 /* https://en.wikipedia.org/wiki/Fetch-and-add
17 static inline int fetch_and_add(int* variable, int value)
18 {
19  __asm__ volatile("lock; xaddl %0, %1"
20  : "+r" (value), "+m" (*variable) // input+output
21  : // No input-only
22  : "memory"
23  );
24  return value;
25 }
26 */
27 
28 #ifdef FE_COUNT_ASM_IMPL
29 #if FE_OS==FE_LINUX
30 static inline int __attribute__ ((__unused__))
31 feAsmSwapIncr(volatile int *pInt, int incr)
32 {
33 #if TRUE
34  __asm__ volatile("lock; xaddl %0, %1"
35  : "+r" (incr), "+m" (*pInt) // input+output
36  : // No input-only
37  : "memory"
38  );
39  return incr;
40 #else
41  //* 20180916 remove register keyword
42 // register int r;
43  int r;
44 
45  __asm__ __volatile__ ("lock; xaddl %0,%2"
46  : "=r" (r) : "0" (incr), "m" (*pInt) : "memory");
47  return r;
48 #endif
49 }
50 
51 static inline void __attribute__ ((__unused__))
52 feAsmIncr(volatile int* pInt, int incr)
53 {
54  feAsmSwapIncr(pInt, incr);
55 #if 0
56  __asm__ __volatile__ ("lock; addl %0,%1"
57  : : "ir" (incr), "m" (*pInt) : "memory");
58 #endif
59 }
60 #endif
61 #if FE_OS==FE_WIN32 || FE_OS==FE_WIN64
62 static inline int
63 feAsmSwapIncr(volatile int *pInt, int incr)
64 {
65  return InterlockedExchangeAdd(
66  reinterpret_cast<volatile LONG *>(pInt), incr);
67 #if 0
68  __asm
69  {
70  mov ecx, pInt
71  mov eax, incr
72  lock xadd dword ptr [ecx], eax
73  }
74 #endif
75 }
76 
77 static inline void
78 feAsmIncr(volatile int* pInt, int incr)
79 {
80  feAsmSwapIncr(pInt, incr);
81 }
82 #endif
83 #endif
84 
85 #endif /* __platform_Count_h__ */