Skip to content

Commit f08d215

Browse files
committed
[Win32] Ensure and document thread-safety of SWTFontProvider
The SWTFontProvider is Win32-internal for accessing font registries that manage the fonts for a device at different zoom levels. Currently, this class does not consider it's accesses' thread-safety. This change makes the SWTFontProvider thread-safe under current usage contexts: - Make the internal map thread-safe as it may be modified from different threads (used for the different devices/displays) concurrently. Using a ConcurrentHashMap ensures that read access stays fast. Write access only happens once per device creation. - Document non-thread-safety of the provider's operations and the necessity to always call it from the same thread for the same device. All current callers fulfill this requirement, thus no race conditions may occur.
1 parent 5f54080 commit f08d215

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.swt.internal;
1515

1616
import java.util.*;
17+
import java.util.concurrent.*;
1718

1819
import org.eclipse.swt.graphics.*;
1920
import org.eclipse.swt.widgets.*;
@@ -26,20 +27,46 @@
2627
* take the provided values for the zoom into consideration and return scaled variant of a font if necessary.
2728
*/
2829
public class SWTFontProvider {
29-
private static Map<Device, SWTFontRegistry> fontRegistries = new HashMap<>();
30+
private static final Map<Device, SWTFontRegistry> fontRegistries = new ConcurrentHashMap<>();
3031

3132
private static SWTFontRegistry getFontRegistry(Device device) {
3233
return fontRegistries.computeIfAbsent(device, SWTFontProvider::newFontRegistry);
3334
}
3435

36+
/**
37+
* Returns the system font for the given device at the specified zoom.
38+
*
39+
* <b>Note:</b> This operation is not thread-safe. It must thus always be called
40+
* from the same thread for the same device, such as the display's UI thread.
41+
*
42+
* @param device the device to retrieve the font for, must not be {@code null}
43+
* @param zoom the zoom for which the font shall be scaled
44+
*/
3545
public static Font getSystemFont(Device device, int zoom) {
3646
return getFontRegistry(device).getSystemFont(zoom);
3747
}
3848

49+
/**
50+
* Returns the font with the given font data for the given device at the
51+
* specified zoom.
52+
*
53+
* <b>Note:</b> This operation is not thread-safe. It must thus always be called
54+
* from the same thread for the same device, such as the display's UI thread.
55+
*
56+
* @param device the device to retrieve the font for, must not be {@code null}
57+
* @param fontData the data for the font to retrieve, must not be {@code null}
58+
* @param zoom the zoom for which the font shall be scaled
59+
*/
3960
public static Font getFont(Device device, FontData fontData, int zoom) {
4061
return getFontRegistry(device).getFont(fontData, zoom);
4162
}
4263

64+
/**
65+
* Disposes the font registry for the given device, if one exists.
66+
*
67+
* @param device the device to dispose the font registry for, must not be
68+
* {@code null}
69+
*/
4370
public static void disposeFontRegistry(Device device) {
4471
SWTFontRegistry fontRegistry = fontRegistries.remove(device);
4572
if (fontRegistry != null) {

0 commit comments

Comments
 (0)