]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.1.0/liblfds710/src/liblfds710_internal.h
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.1.0 / liblfds710 / src / liblfds710_internal.h
1 /***** public prototypes *****/
2 #include "../inc/liblfds710.h"
3
4 /***** defines *****/
5 #define and &&
6 #define or  ||
7
8 #define NO_FLAGS 0x0
9
10 #define LFDS710_VERSION_STRING   "7.1.0"
11 #define LFDS710_VERSION_INTEGER  710
12
13 #if( defined KERNEL_MODE )
14   #define MODE_TYPE_STRING "kernel-mode"
15 #endif
16
17 #if( !defined KERNEL_MODE )
18   #define MODE_TYPE_STRING "user-mode"
19 #endif
20
21 #if( defined NDEBUG && !defined COVERAGE && !defined TSAN && !defined PROF )
22   #define BUILD_TYPE_STRING "release"
23 #endif
24
25 #if( !defined NDEBUG && !defined COVERAGE && !defined TSAN && !defined PROF )
26   #define BUILD_TYPE_STRING "debug"
27 #endif
28
29 #if( !defined NDEBUG && defined COVERAGE && !defined TSAN && !defined PROF )
30   #define BUILD_TYPE_STRING "coverage"
31 #endif
32
33 #if( !defined NDEBUG && !defined COVERAGE && defined TSAN && !defined PROF )
34   #define BUILD_TYPE_STRING "threadsanitizer"
35 #endif
36
37 #if( !defined NDEBUG && !defined COVERAGE && !defined TSAN && defined PROF )
38   #define BUILD_TYPE_STRING "profiling"
39 #endif
40
41 #define LFDS710_BACKOFF_INITIAL_VALUE  0
42 #define LFDS710_BACKOFF_LIMIT          10
43
44 #define LFDS710_BACKOFF_EXPONENTIAL_BACKOFF( backoff_state, backoff_iteration )                \
45 {                                                                                              \
46   lfds710_pal_uint_t volatile                                                                  \
47     loop;                                                                                      \
48                                                                                                \
49   lfds710_pal_uint_t                                                                           \
50     endloop;                                                                                   \
51                                                                                                \
52   if( (backoff_iteration) == LFDS710_BACKOFF_LIMIT )                                           \
53     (backoff_iteration) = LFDS710_BACKOFF_INITIAL_VALUE;                                       \
54   else                                                                                         \
55   {                                                                                            \
56     endloop = ( ((lfds710_pal_uint_t) 0x1) << (backoff_iteration) ) * (backoff_state).metric;  \
57     for( loop = 0 ; loop < endloop ; loop++ );                                                 \
58   }                                                                                            \
59                                                                                                \
60   (backoff_iteration)++;                                                                       \
61 }
62
63 #define LFDS710_BACKOFF_AUTOTUNE( bs, backoff_iteration )                                                                           \
64 {                                                                                                                                   \
65   if( (backoff_iteration) < 2 )                                                                                                     \
66     (bs).backoff_iteration_frequency_counters[(backoff_iteration)]++;                                                               \
67                                                                                                                                     \
68   if( ++(bs).total_operations >= 10000 and (bs).lock == LFDS710_MISC_FLAG_LOWERED )                                                 \
69   {                                                                                                                                 \
70     char unsigned                                                                                                                   \
71       result;                                                                                                                       \
72                                                                                                                                     \
73     lfds710_pal_uint_t LFDS710_PAL_ALIGN(LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES)                                                     \
74       compare = LFDS710_MISC_FLAG_LOWERED;                                                                                          \
75                                                                                                                                     \
76     LFDS710_PAL_ATOMIC_CAS( &(bs).lock, &compare, LFDS710_MISC_FLAG_RAISED, LFDS710_MISC_CAS_STRENGTH_WEAK, result );               \
77                                                                                                                                     \
78     if( result == 1 )                                                                                                               \
79     {                                                                                                                               \
80       /* TRD : if E[1] is less than 1/100th of E[0], decrease the metric, to increase E[1] */                                       \
81       if( (bs).backoff_iteration_frequency_counters[1] < (bs).backoff_iteration_frequency_counters[0] / 100 )                       \
82       {                                                                                                                             \
83         if( (bs).metric >= 11 )                                                                                                     \
84           (bs).metric -= 10;                                                                                                        \
85       }                                                                                                                             \
86       else                                                                                                                          \
87         (bs).metric += 10;                                                                                                          \
88                                                                                                                                     \
89       (bs).backoff_iteration_frequency_counters[0] = 0;                                                                             \
90       (bs).backoff_iteration_frequency_counters[1] = 0;                                                                             \
91       (bs).total_operations = 0;                                                                                                    \
92                                                                                                                                     \
93       LFDS710_MISC_BARRIER_STORE;                                                                                                   \
94                                                                                                                                     \
95       LFDS710_PAL_ATOMIC_SET( &(bs).lock, LFDS710_MISC_FLAG_LOWERED );                                                              \
96     }                                                                                                                               \
97   }                                                                                                                                 \
98 }
99
100 /***** library-wide prototypes *****/
101 void lfds710_misc_internal_backoff_init( struct lfds710_misc_backoff_state *bs );
102