Proper synchronization on rootProject.extensions.extraProperties.{get,set}?


(Thipor Kong) #1

Hi,

I’d like to cache/share some data across the project-level instances of the same task. My idea is to use an extra property on the rootProject for this.

What’s the proper way to synchronize read/write access to rootProject.extensions.extraProperties.{get,set}() to facilitate parallel execution of my task instances?

Should I synchronize on rootProject, rootProject.extensions, or rootProject.extensions.extraProperties? Or should I use a completely different synchronization mechanism? What’s the synchronization strategy employed by Gradle internally?

Thanks and regards,

Thipor


(Stefan Oehme) #2

Hi Thipor,

out of curiosity: Why do the tasks need to share data?

I think it would be best to have your own thread safe data container and put that as an extension on the root project.

Cheers,
Stefan


(Thipor Kong) #3

I’m implementing something similar to Maven dependency:analyze.

For this I’m indexing all the files in my dependency configurations. The plugin is being applied on the project level, but the index data shall be shared across the whole multi-project to avoid unnecessary re-indexing of the same dependency file (my multi-project uses 670 dependency files arbitrarily across >100 projects, so the savings are significant).

My assumption was, the rootProject.extensions.extraProperties would be the proper place to keep global data.

But as the ExtraPropertiesExtension class is being defined currently, explicit synchronization would be required to do this propely (can’t just call set() because it replaces existing values, but would have to query has() beforehand by using a synchronized block).

I have found places in the Gradle source code, where plugins do use global data in rootProject.extensions.extraProperties (e.g. AbstractScalaCompile), but couldn’t figure out, how multiple instances of the same task do synchronize on changing the extraProperties.


(Thipor Kong) #4

It’s my understanding now, that extraProperties are meant to hold build configuration, but not build runtime data (what my cache is). So I just have moved the cache to some singleton in my /buildSrc and am doing the synchronization by myself.