Skip to content

Commit 33365a7

Browse files
tsarnOnePatchGuy
authored andcommitted
JBR-9865 Wayland: More granular locking in clipboard
This patch removes holding a clipboard-global lock when receiving data by reading from a pipe, since that has the potential to cause deadlocks under specific circumstances. To synchronize destroying the offer objects on the Java side, they are now refcounted.
1 parent daa3d0d commit 33365a7

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/java.desktop/unix/classes/sun/awt/wl/WLClipboard.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,29 @@ protected long[] getClipboardFormats() {
224224
protected byte[] getClipboardData(long format) throws IOException {
225225
final WLDataTransferer wlDataTransferer = (WLDataTransferer) DataTransferer.getInstance();
226226
final long utf8StringFormat = wlDataTransferer.getFormatForNativeAsLong(utf8String);
227-
synchronized (dataLock) {
227+
WLDataOffer offer = null;
228+
229+
try {
230+
synchronized (dataLock) {
231+
offer = clipboardDataOfferedToUs.ref();
232+
}
233+
228234
// Iterate over all mime types, since the mapping between mime types and java formats might not be 1:1
229235
// Also treat text/plain;charset=utf-8 as UTF8_STRING
230-
for (var mime : clipboardDataOfferedToUs.getMimes()) {
236+
for (var mime : offer.getMimes()) {
231237
long curFormat = wlDataTransferer.getFormatForNativeAsLong(mime);
232-
if (curFormat == format) {
233-
return clipboardDataOfferedToUs.receiveData(mime);
234-
} else if (mime.equalsIgnoreCase(plainTextUtf8) && utf8StringFormat == format) {
235-
return clipboardDataOfferedToUs.receiveData(mime);
238+
boolean isUtf8String = mime.equalsIgnoreCase(plainTextUtf8) && utf8StringFormat == format;
239+
if (curFormat == format || isUtf8String) {
240+
return offer.receiveData(mime);
236241
}
237242
}
243+
244+
throw new IOException("No appropriate mime type found for WLClipboard.getClipboardData with format = " + format);
245+
} finally {
246+
if (offer != null) {
247+
offer.unref();
248+
}
238249
}
239-
throw new IOException("No appropriate mime type found for WLClipboard.getClipboardData with format = " + format);
240250
}
241251

242252
@Override
@@ -267,7 +277,7 @@ void handleClipboardOffer(WLDataOffer offer /* nullable */) {
267277
}
268278

269279
if (clipboardDataOfferedToUs != null) {
270-
clipboardDataOfferedToUs.destroy();
280+
clipboardDataOfferedToUs.unref();
271281
}
272282
clipboardDataOfferedToUs = offer;
273283
}

src/java.desktop/unix/classes/sun/awt/wl/WLDataOffer.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public interface EventListener {
4242
private int sourceActions = 0;
4343
private int selectedAction = 0;
4444
private EventListener listener;
45+
private int refcount = 1;
4546

4647
private static native void destroyImpl(long nativePtr);
4748

@@ -60,14 +61,21 @@ private WLDataOffer(long nativePtr) {
6061
this.nativePtr = nativePtr;
6162
}
6263

63-
// after calling destroy(), this object enters an invalid state and needs to be deleted
64-
public synchronized void destroy() {
65-
if (nativePtr != 0) {
66-
destroyImpl(nativePtr);
67-
nativePtr = 0;
64+
public synchronized void unref() {
65+
if (nativePtr != 0 && refcount > 0) {
66+
--refcount;
67+
if (refcount == 0) {
68+
destroyImpl(nativePtr);
69+
nativePtr = 0;
70+
}
6871
}
6972
}
7073

74+
public synchronized WLDataOffer ref() {
75+
++refcount;
76+
return this;
77+
}
78+
7179
public synchronized byte[] receiveData(String mime) throws IOException {
7280
int fd;
7381

src/java.desktop/unix/classes/sun/awt/wl/WLDropTargetContextPeer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private synchronized void updateActions() {
137137

138138
private synchronized void reset() {
139139
if (currentOffer != null) {
140-
currentOffer.destroy();
140+
currentOffer.unref();
141141
}
142142

143143
currentOffer = null;

0 commit comments

Comments
 (0)