[MacRuby] #477: Need GCD wrapper for dispatch_once
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: MacRuby 0.5 Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- We need an API for dispatch_once: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPag... http://developer.apple.com/mac/library/documentation/Performance/Reference/G... http://www.opensource.apple.com/source/libdispatch/libdispatch-84.5.1/src/on... http://www.opensource.apple.com/source/libdispatch/libdispatch-84.5.1/src/on... Ideally something as simple as: Dispatch::Once.new { run_this_once } The tricky part is that the "Once" would have to allocate (the equivalent of) a global or static dispatch_once_t (i.e., a long). Could that be done with class variables? -- Ticket URL: <http://www.macruby.org/trac/ticket/477> MacRuby <http://macruby.org/>
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: MacRuby 0.5 Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by joshua.ballanco@…): What about using dispatch once as a method decorator like "private". Something like: {{{ class Foo def initialize(db_file) @db_file = db_file end def connect DB.connect(@db_file) end dispatch_once :connect ... end }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/477#comment:1> MacRuby <http://macruby.org/>
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: minor | Milestone: MacRuby 0.5 Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Changes (by joshua.ballanco@…): * type: defect => enhancement -- Ticket URL: <http://www.macruby.org/trac/ticket/477#comment:2> MacRuby <http://macruby.org/>
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: minor | Milestone: MacRuby 0.5 Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by ernest.prabhakar@…): Ooh, I like it. I wonder if that would allows us to also create an instance variable to use as the predicate. -- Ticket URL: <http://www.macruby.org/trac/ticket/477#comment:3> MacRuby <http://macruby.org/>
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: minor | Milestone: MacRuby 0.5 Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by ernest.prabhakar@…): Needs a (pseudo-static) variable managed by RoxorCore Dispatch.once { DB.connect(@db_file) } -- Ticket URL: <http://www.macruby.org/trac/ticket/477#comment:4> MacRuby <http://macruby.org/>
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: minor | Milestone: MacRuby 0.5 Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by lsansonetti@…): {{{ @io ||= File.open('hey') }}} would become {{{ Dispatch.once { @io = File.open('hey') } }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/477#comment:5> MacRuby <http://macruby.org/>
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: minor | Milestone: MacRuby 0.5 Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by lsansonetti@…): The only way I see to implement this (without hacking the compiler) is to keep a static map of dispatch_once_t structures dynamically allocated on the fly and using the position of the caller within the stack frame as the unique id (key). However, accessing the table in a thread-safe manner will imply to keep it protected with a lock. I therefore wonder if all of this is really worth doing, since a call to Dispatch.once will be costlier than expected, and very similar to the following pure-Ruby code (which should even run faster): {{{ @lock = Mutex.new .. @lock.synchronize do @called ||= false unless @called do_stuff @called = true end end }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/477#comment:6> MacRuby <http://macruby.org/>
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: minor | Milestone: MacRuby 0.5 Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by ernest.prabhakar@…): Is there any way we could call LLVM directly to generate the static variable dynamically (as it were)? Or is that what you mean by "hacking the compiler"? Conversely, could we emulate Dispatch.once using some variant of the above, so that we provide API compatibility? -- Ticket URL: <http://www.macruby.org/trac/ticket/477#comment:7> MacRuby <http://macruby.org/>
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: minor | Milestone: MacRuby 0.5 Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by lsansonetti@…): That's what I meant by hacking the compiler. The static variable would need to be emitted at compilation time. Of course the compiler would need to know when the variable should be emitted (which requires an heuristic). I'm not sure if dispatch_once() is worth all of that. As for emulation, sure, we could potentially provide this in the future pure-ruby gcd.rb we discussed earlier today. -- Ticket URL: <http://www.macruby.org/trac/ticket/477#comment:8> MacRuby <http://macruby.org/>
#477: Need GCD wrapper for dispatch_once ----------------------------------------+----------------------------------- Reporter: ernest.prabhakar@… | Owner: lsansonetti@… Type: enhancement | Status: closed Priority: minor | Milestone: Component: MacRuby | Resolution: wontfix Keywords: | ----------------------------------------+----------------------------------- Changes (by lsansonetti@…): * status: new => closed * resolution: => wontfix * milestone: MacRuby 0.5 => Comment: Looks like we won't do that. -- Ticket URL: <http://www.macruby.org/trac/ticket/477#comment:9> MacRuby <http://macruby.org/>
participants (1)
-
MacRuby