X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.16.109 Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!nospam.fr.eu.org!talisker.lacave.net!lacave.net!not-for-mail From: -= Ko =- Black Ninja Newsgroups: comp.lang.ruby Subject: How to call Ruby from non-interpreter native thread? Date: Fri, 1 Apr 2011 03:32:23 -0500 Organization: Service de news de lacave.net Lines: 67 Message-ID: <3d387397bafae9b859872a3f17cb679c@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1301647025 87477 65.111.164.187 (1 Apr 2011 08:37:05 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Fri, 1 Apr 2011 08:37:05 +0000 (UTC) X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 380742 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <3d387397bafae9b859872a3f17cb679c@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2098 Hi everybody, i start using Ruby interpreter and wrote some Ruby extension on C++ on Win32 platform. I embedded Ruby extension with SWIG. So my extension provide some communication capabilities, i.e. create server endpoint and wait for messages. If in extension i make WaitForSingleObject() or another blocking operation that will block interpreter thread, if i call method from Ruby, which implement block operation. Besides, i run some parallel Ruby threads, which are blocked too. So, i can't receive messages synchronous and have to use asynchronous mechanism. Thus, i create new native by CreateThread() (non-interpreter native thread), when i call Run() method of my extension class. And no blocks occur, control return to Ruby interpreter thread. Below is a part of my Ruby code, that starts new thread, which starts new server and listen for messages. RubyCallback is my extension class, that implement callback interface from C++ into Ruby. RubyCallback class has call() method, which call rb_funcall() function. If no messages arrived then receiver_thread will suspend until new message arrives and callback resume the thread, that start Receive() method. receiver_thread = Thread.new do cb = RubyCallback::RubyCallback.new(Thread.current, "wakeup") ccinit = InterComm::CC_INIT.new ccinit.pAlertObject = cb #register callback cc = InterComm::Communication.new ret = cc.Init(ccinit) if (ret) puts "InterComm INIT OK" end ret = cc.Run() # here new native listener thread starts if (ret) puts "InterComm RUN OK" end loop do mess = cc.Receive() if (mess == nil) puts "not Receive, fall a sleep...\n" Thread.stop # thread self suspended if # no new messages arrived else puts mess.MessageData end end end When new message arrived extension call callback function from Ruby to say, that extension have new message. I implement C-Ruby callback interface and call rb_funcall(...), but application starts to work very strange: very seldom it works well just for one time, but often it may crashed or deadlock in rb_funcall() call. I know that Ruby interpreter isn't thread safe. Advice me how to properly call Ruby function from C in interpreter thread context from non-interpreter native thread? Or how can i schedule or queued call Ruby function in properly way? Thanx in advance! -- Posted via http://www.ruby-forum.com/.