# Ruby 1.9 - embedding inside a pthread

**URL:** <https://www.ruby-forum.com/t/ruby-1-9-embedding-inside-a-pthread/130765>\
**Category:** Ruby\
**Created:** [March 3, 2008, 6:22am UTC](https://www.ruby-forum.com/t/ruby-1-9-embedding-inside-a-pthread/130765 "2008-03-03T06:22:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [March 3, 2008, 6:22am UTC](https://www.ruby-forum.com/t/ruby-1-9-embedding-inside-a-pthread/130765/1 "2008-03-03T06:22:46Z")

</div>

Hello,

I’m having trouble embedding Ruby 1.9 inside a pthread because calling  
ruby\_sysinit() is segfaulting. I created a small example to reproduce  
the problem and illustrate my humble goal. (The reason for this weird  
way of embedding Ruby is: I need to embed Ruby inside a Verilog HDL  
simulator and this is the best way I could accomplish it!).

There are three files in this example:

$ ls  
extconf.rb hello.rb main.c

First, we have a simple Ruby script:

$ cat hello.rb  
puts “Hello World!”

Second, we have a Makefile generator:

$ cat extconf.rb  
require ‘mkmf’

have\_library(‘pthread’, ‘pthread\_create’)

have\_library(‘ruby’, ‘ruby\_init’) ||  
have\_library(‘ruby-static’, ‘ruby\_init’)

create\_makefile(‘main’)

Finally, we have a C program:

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

RUBY\_GLOBAL\_SETUP

pthread\_t gRubyThread;  
pthread\_mutex\_t gCProgLock;

void\* gRubyThread\_body(void\* dummy)  
{  
int argc = 0;  
char\* argv[1];

```
  printf("Ruby thread is calling ruby_sysinit()\n");
  ruby_sysinit(&argc, &argv);

  {
      printf("Ruby thread is calling RUBY_INIT_STACK()\n");
      RUBY_INIT_STACK;

      printf("Ruby thread is calling ruby_init()\n");
      ruby_init();

      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;

```

}

int main(int argc, char\*\* argv)  
{  
pthread\_mutex\_init(&gCProgLock, NULL);  
pthread\_mutex\_lock(&gCProgLock);

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

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

  printf("C program got control back from Ruby thread,

```

exiting…\n");  
return 0;  
}

This C program is very simple: it creates a new pthread and locks  
itself. (At this point, only the pthread in running.) The pthread then  
proceeds to embed a Ruby 1.9 interpreter inside itself and have the  
interpreter run the “hello.rb” file. Once this is complete, the pthread  
unlocks the main C program and dies. Finally, the main C program exits.

Now let’s try running this example.

Here’s the version of Ruby 1.9 I am using (the official release):

$ ruby -v  
ruby 1.9.0 (2007-12-25 revision 14709) [i686-linux]

Generate the makefile:

$ ruby extconf.rb  
checking for pthread\_create() in -lpthread… yes  
checking for ruby\_init() in -lruby… no  
checking for ruby\_init() in -lruby-static… yes  
creating Makefile

Build the C extension:

$ make  
gcc -I. -I/home/sun/app/ruby19/include/ruby-1.9.0/i686-linux  
-I/home/sun/app/ruby19/include/ruby-1.9.0 -I. -D\_FILE\_OFFSET\_BITS=64  
-fPIC -g -O2 -o main.o -c main.c  
main.c: In function â€˜gRubyThread\_bodyâ€™:  
main.c:16: warning: passing argument 2 of â€˜ruby\_sysinitâ€™ from  
incompatible pointer type  
gcc -shared -o main.so main.o -L. -L/home/sun/app/ruby19/lib  
-Wl,-R/home/sun/app/ruby19/lib -L. -rdynamic -Wl,-export-dynamic  
-lruby-static -lpthread -lpthread -lrt -ldl -lcrypt -lm -lc

Copy and paste the last gcc command (from the output above) and remove  
the “-shared” option, and then run the new command:

$ gcc -o main.so main.o -L. -L/home/sun/app/ruby19/lib  
-Wl,-R/home/sun/app/ruby19/lib -L. -rdynamic -Wl,-export-dynamic  
-lruby-static -lpthread -lpthread -lrt -ldl -lcrypt -lm -lc

Now run the C program:

$ ./main.so  
C program is putting Ruby thread in control…  
Ruby thread is calling ruby\_sysinit()  
Segmentation fault (core dumped)

As you can see, ruby\_sysinit() core dumped on us. Why?

Thanks for your consideration.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [March 3, 2008, 6:31am UTC](https://www.ruby-forum.com/t/ruby-1-9-embedding-inside-a-pthread/130765/2 "2008-03-03T06:31:55Z")

</div>

Suraj K. wrote:

> I’m having trouble embedding Ruby 1.9 inside a pthread because calling  
> ruby\_sysinit() is segfaulting. I created a small example to reproduce  
> the problem and illustrate my humble goal. (The reason for this weird  
> way of embedding Ruby is: I need to embed Ruby inside a Verilog HDL  
> simulator and this is the best way I could accomplish it!).

I should clarify that although the main.c in this example has argc and  
argv, when I deploy my C extension as a .so file, there is no main()  
method as far as my code is concerned. The Verilog simulator owns the  
main() method and my C extension has _no_ way of accessing the argc and  
argv passed to that main() method.

For this reason, I did not pass the argc and argv from main() to the  
body of the gRubyThread (because that is the real-life scenario of this  
simple example). And since ruby\_sysinit() requires an argc and argv, I  
am forced to fabricate them inside the body of gRubyThread.
