https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112013-09-27T20:40:54ZRuby Issue Tracking SystemRuby master - Feature #8961: Synchronizable module to easily wrap methods in a mutexhttps://redmine.ruby-lang.org/issues/8961?journal_id=420472013-09-27T20:40:54Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>I would like to see this in 2.1, as a standard Module method. The fact that "def" returns the method name now makes this really easy.</p>
<p>I think this would need to be implemented natively to work, however. The prototype above has a key flaw: there's no guarantee that only one Monitor will be created, so two threads could execute the same method at the same time, synchronizing against different monitors. Putting the synchronized wrapper into C code would prevent a potential context switch when first creating the Monitor instance (or it could simply use some other mechanism, such as normal Object monitor synchronization in JRuby).</p>
<p>This feature is similar to an extension in JRuby called JRuby::Synchronized that causes all method lookups to return synchronized equivalents.</p>
<p>Combined with <a href="https://bugs.ruby-lang.org/issues/8556" class="external">https://bugs.ruby-lang.org/issues/8556</a> this could go a very long way toward giving Ruby users better tools to write thread-safe code.</p> Ruby master - Feature #8961: Synchronizable module to easily wrap methods in a mutexhttps://redmine.ruby-lang.org/issues/8961?journal_id=420492013-09-27T21:45:20Ztobiassvn (Tobias Svensson)
<ul></ul><p>Having this as a method on Module directly would of course be ideal. However, I believe the mutex/monitor used should still be exposed as a private method so it can be used without the 'synchronized' method.</p> Ruby master - Feature #8961: Synchronizable module to easily wrap methods in a mutexhttps://redmine.ruby-lang.org/issues/8961?journal_id=420572013-09-28T03:45:23Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>tobiassvn (Tobias Svensson) wrote:</p>
<blockquote>
<p>Having this as a method on Module directly would of course be ideal. However, I believe the mutex/monitor used should still be exposed as a private method so it can be used without the 'synchronized' method.</p>
</blockquote>
<p>Maybe. I don't like the idea of exposing this mutex/monitor, since it could be modified or locked and never released. I would be more in favor of a "tap" form that synchronizes against the same internal monitor, similar to Java's "synchronized" keyword.</p>
<p>obj.synchronized { thread-sensitive code here }</p>
<p>That would also open up the possibility of using a lighter-weight internal mutex/monitor rather than the rather heavy-weight Ruby-land version.</p> Ruby master - Feature #8961: Synchronizable module to easily wrap methods in a mutexhttps://redmine.ruby-lang.org/issues/8961?journal_id=420612013-09-28T09:23:18Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>headius (Charles Nutter) wrote:</p>
<blockquote>
<p>Maybe. I don't like the idea of exposing this mutex/monitor, since it could be modified or locked and never released. I would be more in favor of a "tap" form that synchronizes against the same internal monitor, similar to Java's "synchronized" keyword.</p>
<p>obj.synchronized { thread-sensitive code here }</p>
</blockquote>
<p>Use MonitorMixin.</p> Ruby master - Feature #8961: Synchronizable module to easily wrap methods in a mutexhttps://redmine.ruby-lang.org/issues/8961?journal_id=421342013-10-01T14:14:53Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/42134/diff?detail_id=30568">diff</a>)</li></ul> Ruby master - Feature #8961: Synchronizable module to easily wrap methods in a mutexhttps://redmine.ruby-lang.org/issues/8961?journal_id=421702013-10-02T02:13:13Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>headius (Charles Nutter) wrote:</p>
<blockquote>
<p>Maybe. I don't like the idea of exposing this mutex/monitor, since it could be modified or locked and never released. I would be more in favor of a "tap" form that synchronizes against the same internal monitor, similar to Java's "synchronized" keyword.</p>
<p>obj.synchronized { thread-sensitive code here }</p>
</blockquote>
<p>Use MonitorMixin.</p>
</blockquote>
<p>Yeah, that's not a bad option from a pure-Ruby perspective. We could add "synchronized" to classes that include MonitorMixin, perhaps?</p>
<ul>
<li>added to monitor.rb:</li>
</ul>
<p>module MonitorMixin<br>
module ClassMethods<br>
def synchronized(method)<br>
aliased = :"#{method}_without_synchronization"<br>
alias_method aliased, method</p>
<pre><code> define_method method do |*args, &block|
mon_enter
begin
__send__(aliased, *args, &block)
ensure
mon_exit
end
end
end
</code></pre>
<p>end</p>
<p>def self.included(base)<br>
base.extend(ClassMethods)<br>
end<br>
end</p>
<p>class Foo<br>
include MonitorMixin</p>
<p>synchronized def bar<br>
# ...<br>
end<br>
end</p>
<p>...</p>
<p>My suggestion to have it be native on Module opened up the possibility of implementing it in a faster, native way. MonitorMixin has a very large perf hit on all impls right now, but especially MRI. See my benchmarks in <a href="https://github.com/ruby/ruby/pull/405#issuecomment-25417666" class="external">https://github.com/ruby/ruby/pull/405#issuecomment-25417666</a></p> Ruby master - Feature #8961: Synchronizable module to easily wrap methods in a mutexhttps://redmine.ruby-lang.org/issues/8961?journal_id=422142013-10-02T22:15:01Ztobiassvn (Tobias Svensson)
<ul></ul><p>I suppose if this is being added to MonitorMixin it should probably be in Mutex_m as well?</p> Ruby master - Feature #8961: Synchronizable module to easily wrap methods in a mutexhttps://redmine.ruby-lang.org/issues/8961?journal_id=422332013-10-03T06:52:05Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>tobiassvn (Tobias Svensson) wrote:</p>
<blockquote>
<p>I suppose if this is being added to MonitorMixin it should probably be in Mutex_m as well?</p>
</blockquote>
<p>I don't think so, since a Mutex is not reentrant and what we want is monitor semantics for #synchronized.</p> Ruby master - Feature #8961: Synchronizable module to easily wrap methods in a mutexhttps://redmine.ruby-lang.org/issues/8961?journal_id=955772021-12-23T23:43:57Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Project</strong> changed from <i>14</i> to <i>Ruby master</i></li></ul>