[#15707] Schedule for the 1.8.7 release — "Akinori MUSHA" <knu@...>

Hi, developers,

21 messages 2008/03/01

[#15740] Copy-on-write friendly garbage collector — Hongli Lai <hongli@...99.net>

Hi.

31 messages 2008/03/03
[#15742] Re: Copy-on-write friendly garbage collector — Yukihiro Matsumoto <matz@...> 2008/03/03

Hi,

[#15829] Re: Copy-on-write friendly garbage collector — Daniel DeLorme <dan-ml@...42.com> 2008/03/08

Yukihiro Matsumoto wrote:

[#15756] embedding Ruby 1.9.0 inside pthread — "Suraj Kurapati" <sunaku@...>

Hello,

18 messages 2008/03/03
[#15759] Re: embedding Ruby 1.9.0 inside pthread — Nobuyoshi Nakada <nobu@...> 2008/03/04

Hi,

[#15760] Re: embedding Ruby 1.9.0 inside pthread — Yukihiro Matsumoto <matz@...> 2008/03/04

Hi,

[#15762] Re: embedding Ruby 1.9.0 inside pthread — "Suraj N. Kurapati" <sunaku@...> 2008/03/04

Yukihiro Matsumoto wrote:

[#15783] Adding startup and shutdown to Test::Unit — Daniel Berger <Daniel.Berger@...>

Hi all,

15 messages 2008/03/04

[#15835] TimeoutError in core, timeouts for ConditionVariable#wait — MenTaLguY <mental@...>

I've been reworking JRuby's stdlib to improve performance and fix

10 messages 2008/03/09

[#15990] Recent changes in Range#step behavior — "Vladimir Sizikov" <vsizikov@...>

Hi,

35 messages 2008/03/23
[#15991] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/23

[#15993] Re: Recent changes in Range#step behavior — "Vladimir Sizikov" <vsizikov@...> 2008/03/23

Hi Dave,

[#15997] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/23

[#16024] Re: Recent changes in Range#step behavior — "Vladimir Sizikov" <vsizikov@...> 2008/03/26

Hi Dave,

[#16025] Re: Recent changes in Range#step behavior — Yukihiro Matsumoto <matz@...> 2008/03/26

Hi,

[#16026] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/26

[#16027] Re: Recent changes in Range#step behavior — Yukihiro Matsumoto <matz@...> 2008/03/26

Hi,

[#16029] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/26

[#16030] Re: Recent changes in Range#step behavior — Yukihiro Matsumoto <matz@...> 2008/03/26

Hi,

[#16031] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/26

[#16032] Re: Recent changes in Range#step behavior — "Vladimir Sizikov" <vsizikov@...> 2008/03/26

On Wed, Mar 26, 2008 at 7:01 PM, Dave Thomas <dave@pragprog.com> wrote:

[#16033] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/26

[#16041] Re: Recent changes in Range#step behavior — David Flanagan <david@...> 2008/03/26

Dave Thomas wrote:

Re: embedding Ruby 1.9.0 inside pthread

From: "Suraj N. Kurapati" <sunaku@...>
Date: 2008-03-04 05:57:36 UTC
List: ruby-core #15767
Suraj N. Kurapati wrote:
> when I tried loading RubyGems inside hello.rb, Ruby gives
> an error:
> 
> hello.rb:2:in `require': no such file to load -- rubygems (LoadError)
> 	from hello.rb:2:in `<main>'
> 
>   $ cat hello.rb
>   puts "Hello World!"
>   require 'rubygems'
>   puts "rubygems OK!"
> 
> What must I do to initialize rubygems inside the C program?

The solution was to use ruby_options() to load the hello.rb file
instead of rb_load_file(), as shown below.

  $ cat main.c
  #include <stdio.h>
  #include <pthread.h>
  #include <ruby.h>

  pthread_t gRubyThread;
  pthread_mutex_t gMainLock;

  // aRubyProgram:: program node for the interpreter to run
  void* gRubyThread_body(void* aRubyProgram)
  {
      printf("Ruby thread is starting interpreter\n");
      ruby_run_node(aRubyProgram);

      printf("Ruby thread is done, waking up C program...\n");
      pthread_mutex_unlock(&gMainLock);

      return NULL;
  }

  RUBY_GLOBAL_SETUP

  int main(int argc, char** argv)
  {
      const char* const file = "hello.rb"; // the file to run
      int fake_argc = 2;
      char* fake_argv[fake_argc];
      fake_argv[0] = "main";
      fake_argv[1] = file;

      // printf("C program is calling ruby_sysinit()\n");
      // ruby_sysinit(&fake_argc, &fake_argv);

      printf("C program is calling RUBY_INIT_STACK()\n");
      RUBY_INIT_STACK;

      printf("C program is calling ruby_init()\n");
      ruby_init();

      printf("C program is calling ruby_init_loadpath()\n");
      ruby_init_loadpath();

      printf("Ruby interpreter is loading file: %s\n", file);
      void* rubyProgram = ruby_options(fake_argc, fake_argv);


      printf("C program is putting Ruby thread in control...\n");

      pthread_mutex_init(&gMainLock, NULL);
      pthread_mutex_lock(&gMainLock);
      pthread_create(&gRubyThread, NULL,
                     gRubyThread_body, rubyProgram);
      pthread_mutex_lock(&gMainLock); // C program blocks here

      printf("C program is back in control, exiting...\n");
      return 0;
  }

However, notice that the call to ruby_sysinit() is commented-out
this time.  If I uncomment the call to ruby_sysinit(), the program
crashes with a segfault.  (Maybe because fake_argc is now 2 instead
of 0?)

Could you please explain:

1. Why does ruby_sysinit() cause a segfault in this program?

2. Why does the program run OK even without ruby_sysinit()?  It
seems ruby_sysinit() is not

3. Is it safe to "compile" the hello.rb file into a NODE in the main
thread and then execute it inside a child thread?  Will there be any
cross-thread violations by doing this?

Thanks for your consideration.


In This Thread