java-native-access / jna

Java Native Access
Other
8.49k stars 1.67k forks source link

Unnecessary synchronisation? #1572

Open sebbASF opened 10 months ago

sebbASF commented 10 months ago

The synch. here appears to be unnecessary: https://github.com/java-native-access/jna/blob/365629530635440381f63939f7891456992fe91b/src/com/sun/jna/NativeLibrary.java#L186-L188

searchPaths is a ConcurrentHashMap, so does it need synch? It also does not make sense to me to synch. on the value being added, rather than the target being updated.

matthiasblaesing commented 10 months ago

That guard looks unnecessary, but this might be problematic:

https://github.com/java-native-access/jna/blob/365629530635440381f63939f7891456992fe91b/src/com/sun/jna/NativeLibrary.java#L521-L529

Here is a real race and that could now (JDK8 as baseline) be fixed using ConcurrentHashMap#computeIfAbsent.