[#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 03:54:09 UTC
List: ruby-core #15762
Yukihiro Matsumoto wrote:
> In message "Re: embedding Ruby 1.9.0 inside pthread"
>     on Tue, 4 Mar 2008 09:30:34 +0900, Nobuyoshi Nakada <nobu@ruby-lang.org> writes:
> 
> |At Tue, 4 Mar 2008 07:50:00 +0900,
> |Suraj Kurapati wrote in [ruby-core:15756]:
> |> I'm having trouble embedding Ruby inside a pthread because calling
> |> ruby_sysinit() is segfaulting.
> |
> |You can't call ruby_init() in child threads.
> 
> Because GC needs to know system stack address, that is only taken from
> main thread.

Ah!  Thank you, both, for the explanation.

I moved the ruby initialization calls to the main thread as you
suggested and the example worked!  The revised main.c is now:

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

  pthread_t gRubyThread;
  pthread_mutex_t gCProgLock;

  void* gRubyThread_body(void* dummy)
  {
      char* file = "hello.rb"; // the file to run

      printf("Ruby interpreter is loading file: %s\n", file);
      void* node = rb_load_file(file);

      printf("Ruby thread is starting interpreter\n");
      ruby_run_node(node);

      printf("Ruby thread is done, waking up C program...\n");
      pthread_mutex_unlock(&gCProgLock);
      return NULL;
  }

  RUBY_GLOBAL_SETUP

  int main(int argc, char** argv)
  {
      int fake_argc = 0;
      char* fake_argv[1];

      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 putting Ruby thread in control...\n");

      pthread_mutex_init(&gCProgLock, NULL);
      pthread_mutex_lock(&gCProgLock);

      pthread_create(&gRubyThread, NULL, gRubyThread_body, NULL);
      pthread_mutex_lock(&gCProgLock); // C program blocks here

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

Running this new main.c program gives the expected output:

  $ ./main.so
  C program is calling ruby_sysinit()
  C program is calling RUBY_INIT_STACK()
  C program is calling ruby_init()
  C program is putting Ruby thread in control...
  Ruby interpreter is loading file: hello.rb
  Ruby thread is starting interpreter
  Hello World!
  Ruby thread is done, waking up C program...
  C program is back in control, exiting...

However, 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?

Thanks for your consideration.

In This Thread