1

I'm trying to find a class/file with *.cpp extension which represents any Java object (java.lang.Object) inside of JVM from sources of openjdk-14.

Namely, I'm interested in structure responsible for monitor release/acquire when we synchronize on an object:

synchronized(obj) { }

As far as I understand synchronization in Java is possible on any object, so the structure I'm looking for must be declared somewhere in the header of java.lang.Object.

Sergey Tsypanov
  • 3,265
  • 3
  • 8
  • 34
  • Have you looked at JNI `jobject`? It may be in area of your interest since it cpp representation of `Object` – Steyrix Dec 10 '19 at 11:35
  • 3
    [I have a bad news for you, too.](https://stackoverflow.com/a/57318264/3448419) Like with allocations, there is no a single place in HotSpot sources for synchronization. There is no even a single structure for this, because there are multiple kinds of locks on the VM level: biased locks, thin locks, recursive on-stack locks, inflated locks... and they are handled differently. Try to be more specific - what exactly are you looking for? What problem are you going to solve? – apangin Dec 10 '19 at 12:02
  • @apangin hello Andrey, thanks for the response! Actually there's no any particular problem, I just wanted to have a link to share in my article. I think adding ability to lock on any Java object was a mistake of language designers, as vast majority of all objects are never used as locks, still hold structure used for locking i.e. waste memory. P.S. I think I've found it, looks like it is `/src/hotspot/share/oops/markWord.hpp`, there they this declaration ```cpp class BasicLock; class ObjectMonitor; ``` – Sergey Tsypanov Dec 10 '19 at 12:43
  • @apangin is it the correct place? – Sergey Tsypanov Dec 10 '19 at 13:16
  • 2
    The markword serves different purposes, so the statement that each object holds a structure for locking is not correct. Unless you consider a few bits "a structure". – Holger Dec 10 '19 at 15:04
  • 1
    The [header] structure of all heap objects is declared in [oop.hpp](http://hg.openjdk.java.net/jdk/jdk/file/44aa31d0dea3/src/hotspot/share/oops/oop.hpp#l52). And yes, markWord is the part of this header. As @Holger noticed, markWord is used not only for locking. It is also reused for e.g. identity hashCode and GC age. In this sense, a never-locked object does not actually waste memory. – apangin Dec 10 '19 at 15:56

0 Answers0