Skip to content

Expose Java's built-in monitor synchronization as a JRuby API#6264

Merged
headius merged 3 commits intojruby:masterfrom
headius:jruby_java_sync_api
Sep 21, 2020
Merged

Expose Java's built-in monitor synchronization as a JRuby API#6264
headius merged 3 commits intojruby:masterfrom
headius:jruby_java_sync_api

Conversation

@headius
Copy link
Member

@headius headius commented Jun 4, 2020

I'm not sure why we've never done this before, but since there's
really no way to synchronize against an object's Java monitor from
Ruby this API seems like a gap in JRuby. Here I've added the
following JRuby utility functions:

  • JRuby.synchronized(arg) {} which works like Java's synchronized
    statement
  • JRuby.wait(arg [, millis [, nanos]]) like Object.wait
  • JRuby.notify(arg) and notifyAll(arg) like Object.notify[All]

This question came up in the JRuby chat, and it seems like a
useful and appropriate addition to the JRuby module.

@headius headius requested review from enebo and kares June 4, 2020 07:46
I'm not sure why we've never done this before, but since there's
really no way to synchronize against an object's Java monitor from
Ruby this API seems like a gap in JRuby. Here I've added the
following JRuby utility functions:

* JRuby.synchronized(arg) {} which works like Java's synchronized
  statement
* JRuby.wait(arg [, millis [, nanos]]) like Object.wait
* JRuby.notify(arg) and notifyAll(arg) like Object.notify[All]

This question came up in the JRuby chat, and it seems like a
useful and appropriate addition to the JRuby module.
@headius headius force-pushed the jruby_java_sync_api branch from 875927b to 0a8432c Compare June 4, 2020 07:47
@enebo
Copy link
Member

enebo commented Jun 4, 2020

@headius I thought we had it but I realize now if was the module to allow all methods to synch. Seems like a long overdue feature.

@headius
Copy link
Member Author

headius commented Jun 4, 2020

@enebo Yeah, kind of a "duh" moment for me too. The Synchronized module is still out there, but you have to include it into a class or extend it into an object, and then it synchronizes every method call.

@headius headius added this to the JRuby 9.3.0.0 milestone Jun 4, 2020
@headius headius changed the title Expose Java's built-in moniitor synchronization as a JRuby API Expose Java's built-in monitor synchronization as a JRuby API Jun 5, 2020
@kares
Copy link
Member

kares commented Jun 6, 2020

indeed very useful, wonder if these should be handling Java wrappers in a special way or not.
I think proxies have wait/notify inherited from java.lang.Object so that should do, users passing a Java proxy here just get semantics on the wrapper (a know maybe even desired "feature" for now).

@headius
Copy link
Member Author

headius commented Jun 6, 2020

@kares No I think your first instinct was right...it should synchronize against the wrapped object. If a user calls synchronize against a Java object, it must actually be locked for other consumers elsewhere in the JVM trying to synchronize against the same object. As far as anyone using JRuby is concerned, there is no wrapper.

@headius
Copy link
Member Author

headius commented Jun 9, 2020

I've fixed sync logic for wrapped Java objects. All that remains is writing some tests for this API.

@headius headius merged commit f68d741 into jruby:master Sep 21, 2020
@headius headius deleted the jruby_java_sync_api branch September 21, 2020 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants