https://kotlinlang.org logo
#announcements
Title
# announcements
s

Sam

08/23/2020, 3:09 PM
Ran into this, is there any reason why kotlin bytecode ignores synchronization primitive for get()
Copy code
var data = false
    @Synchronized get
    @Synchronized set(value) { field = !value }

fun main() {
    println("Value $data")
}
The only way to get bytecode to call getData() is by having this redundant get (which is prompted as redundant getter in IDE)
Copy code
@Synchronized get() = field
n

nanodeath

08/23/2020, 3:20 PM
can you share more? bytecode looks synchronized to me
Copy code
public final static synchronized getData()Z
   L0
    LINENUMBER 1 L0
    GETSTATIC SyncKt.data : Z
    IRETURN
   L1
    MAXSTACK = 1
    MAXLOCALS = 0
s

Sam

08/23/2020, 3:21 PM
getData() is defined as synchronized but the call site doesn't invoke getData(), instead it uses data directly
n

nanodeath

08/23/2020, 3:22 PM
Copy code
GETSTATIC SyncKt.data : Z
this? .....yeah, that seems to corroborate that
s

Sam

08/23/2020, 3:22 PM
yup, if you decompile that,
Copy code
private static boolean data;

   public static final synchronized boolean getData() {
      return data;
   }

   public static final synchronized void setData(boolean value) {
      data = !value;
   }

   public static final void main() {
      String var0 = "Value " + data;
      System.out.println(var0);
   }
n

nanodeath

08/23/2020, 3:23 PM
well, I've been told before that the decompiler is approximate, but in this case they do seem to be in agreement
(for example, the bytecode creates a StringBuilder)
which version of Kt is this?
s

Sam

08/23/2020, 3:25 PM
1.4.0
n

nanodeath

08/23/2020, 3:27 PM
repros for me on 1.3.72 also. sounds like a bug worth reporting.
s

Sam

08/23/2020, 3:27 PM
Sound good
n

nanodeath

08/23/2020, 3:31 PM
I think the YouTrack search might be having a bad time right now...some of my search queries are taking >30 seconds
s

Sam

08/23/2020, 3:31 PM
yup likewise
n

nanodeath

08/23/2020, 3:33 PM
oh well. good luck 😛 link back for us when you find something or report a new issue 😄
s

Sam

08/23/2020, 3:33 PM
Sure
a

araqnid

08/23/2020, 3:59 PM
field = !value
? That sounds like a recipe for confusion.
n

nanodeath

08/23/2020, 4:07 PM
I'm assuming it's just an example
s

Sam

08/23/2020, 5:41 PM
@araqnid yeah foobar, just to get around lint warning
a

araqnid

08/23/2020, 7:29 PM
If you really just want to wrap synchronization around that field access, you could do this of course:
Copy code
private var privateData = false
    get() = synchronized(this) { field }
    set(value) { synchronized(this) { field = value } }
With some added fun if you want this for a top-level property (maybe lock some class object similar to what
static synchronized
in Java does) Or even just create a delegate for applying synchronization a la
private var privateData by synchronizeOn(this) { false }
But yes, the current situation where the
@Synchronized
annotation is ignored here seems wrong
s

Sam

08/23/2020, 7:30 PM
yeah, Delegates works better. This was just to confirm and report the issue.
👍🏻 1
k

Kroppeb

08/24/2020, 6:09 AM
What is the
@Syncronised
supposed to do here
Standard getters get converted into standard field accessed by the JVM. Idk if this prevents that but I don't see why reading has to be synchronised?
n

nanodeath

08/24/2020, 4:29 PM
in case something is setting it at the same time
k

Kroppeb

08/24/2020, 4:45 PM
I don't think this will do anything about that? I thought this just prevents two threads from reading the field at the same time, which should be just fine.
n

nanodeath

08/24/2020, 4:46 PM
if you synchronized on the getters and setters in Java it would though
k

Kroppeb

08/24/2020, 4:51 PM
Oh, my bad, I thought synchronised was on a method basis instead of working for the full object. But shouldn't you make the variable volatile then too?
n

nanodeath

08/24/2020, 4:57 PM
@Synchronized adds "synchronization barriers" that cause caches to be read/flushed upon entering/exiting them, IIRC, so adding volatile would be redundant.
though tbh this isn't my strong suit and I'm not looking in the right places for documentation apparently
3 Views