Created
March 22, 2013 20:40
-
-
Save malachheb/5224558 to your computer and use it in GitHub Desktop.
Ruby C extension to use pthread c library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mkmf' | |
have_header("pthread.h") | |
create_makefile("pthread") | |
# to use the pthread just run | |
# ruby extconf.rb | |
# make | |
# make install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ruby.h" | |
#include <pthread.h> | |
#include <string.h> | |
static VALUE | |
c_init(VALUE self, VALUE obj, VALUE func) | |
{ | |
Check_Type(func, T_STRING); | |
pthread_t thread; | |
rb_iv_set(self, "@func", func); | |
rb_iv_set(self, "@obj", obj); | |
rb_iv_set(self, "@thread", thread); | |
return self; | |
} | |
void *exec_function(void * self) | |
{ | |
VALUE func = rb_iv_get(self, "@func"); | |
VALUE obj = rb_iv_get(self, "@obj"); | |
char *c_func = StringValuePtr(func); | |
printf("\n Calling function : %s\n", c_func); | |
if (! rb_respond_to(obj, rb_intern(c_func))) | |
rb_raise(rb_eRuntimeError, "target must respond to function"); | |
rb_funcall(obj, rb_intern(c_func), 0); | |
return (void *) 42; | |
} | |
int c_pthread_create(VALUE self) | |
{ | |
int result; | |
VALUE thread = rb_iv_get(self, "@thread"); | |
printf("\n Thread launch \n"); | |
result = pthread_create (&thread, NULL, exec_function, (void *) self); | |
return INT2NUM(result); | |
} | |
int c_pthread_cancel(VALUE self) | |
{ | |
int result; | |
VALUE thread = rb_iv_get(self, "@thread"); | |
printf("\n Cancelling thread \n"); | |
result = pthread_cancel(&thread); | |
return INT2NUM(result); | |
} | |
int c_pthread_join(VALUE self) | |
{ | |
int result; | |
VALUE thread = rb_iv_get(self, "@thread"); | |
printf("\n Waiting for thread terminate \n"); | |
result = pthread_join(&thread, NULL); | |
return INT2NUM(result); | |
} | |
VALUE cPthread; | |
void Init_pthread() { | |
cPthread = rb_define_class("Pthread", rb_cObject); | |
rb_define_method(cPthread, "initialize", c_init, 2); | |
rb_define_method(cPthread, "pthread_create", c_pthread_create, 0); | |
rb_define_method(cPthread, "pthread_cancel", c_pthread_cancel, 0); | |
rb_define_method(cPthread, "pthread_join", c_pthread_join, 0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'pthread' | |
class Threadclass | |
def run() | |
puts "I am runing in thread" | |
end | |
end | |
th1 = Threadrclass.new | |
p1 = Pthread.new(th1,'run') | |
p1.pthread_create |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment