```diff diff --git a/core/src/main/java/org/jruby/ext/jruby/JRubyUtilLibrary.java b/core/src/main/java/org/jruby/ext/jruby/JRubyUtilLibrary.java index 327f29f353..ecf979d564 100644 --- a/core/src/main/java/org/jruby/ext/jruby/JRubyUtilLibrary.java +++ b/core/src/main/java/org/jruby/ext/jruby/JRubyUtilLibrary.java @@ -39,6 +39,7 @@ import org.jruby.anno.JRubyMethod; import org.jruby.ast.util.ArgsUtil; import org.jruby.exceptions.RaiseException; import org.jruby.javasupport.Java; +import org.jruby.runtime.Block; import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.runtime.load.BasicLibraryService; @@ -248,6 +249,22 @@ public class JRubyUtilLibrary implements Library { } } + /** + * Invoke the given block under synchronized lock, using standard Java synchronization. + * + * @param context the current context + * @param recv the JRuby module + * @param arg the object against which to synchronize + * @param block the block to execute + * @return the return value of the block + */ + @JRubyMethod(name = "synchronized") + public static IRubyObject rbSynchronized(ThreadContext context, IRubyObject recv, IRubyObject arg, Block block) { + synchronized (arg) { + return block.call(context); + } + } + @Deprecated // since 9.2 only loaded with require 'core_ext/string.rb' public static class StringUtils { public static IRubyObject unseeded_hash(ThreadContext context, IRubyObject recv) { ```