File tree 1 file changed +9
-4
lines changed
src/native/libs/System.Native
1 file changed +9
-4
lines changed Original file line number Diff line number Diff line change @@ -57,13 +57,18 @@ void* SystemNative_GetDefaultSearchOrderPseudoHandle(void)
57
57
return (void * )RTLD_DEFAULT ;
58
58
}
59
59
#else
60
- static void * g_defaultSearchOrderPseudoHandle = NULL ;
60
+ static void * volatile g_defaultSearchOrderPseudoHandle = NULL ;
61
61
void * SystemNative_GetDefaultSearchOrderPseudoHandle (void )
62
62
{
63
- if (g_defaultSearchOrderPseudoHandle == NULL )
63
+ // Read the value once from the volatile static to avoid reading from memory twice.
64
+ void * defaultSearchOrderPseudoHandle = (void * )g_defaultSearchOrderPseudoHandle ;
65
+ if (defaultSearchOrderPseudoHandle == NULL )
64
66
{
65
- g_defaultSearchOrderPseudoHandle = dlopen (NULL , RTLD_LAZY );
67
+ // Assign back to the static as well as the local here.
68
+ // We don't need to check for a race between two threads as the value returned by
69
+ // dlopen here will always be the same in a given environment.
70
+ g_defaultSearchOrderPseudoHandle = defaultSearchOrderPseudoHandle = dlopen (NULL , RTLD_LAZY );
66
71
}
67
- return g_defaultSearchOrderPseudoHandle ;
72
+ return defaultSearchOrderPseudoHandle ;
68
73
}
69
74
#endif
You can’t perform that action at this time.
0 commit comments