module PThreads
For documentation - A C extension that leverages PThreads instead of ruby threads in order to maximize performance
Public Instance Methods
pthreads()
click to toggle source
VALUE pthread_test(VALUE self) {
clock_t begin, end;
double time_spent;
// starts clock timer
begin = clock();
// setup time information
time_t rawtime;
struct tm *timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
// get number of available cores
int num_threads = get_cpu_count();
// set number of available cores
pthread_t threads[num_threads];
// place to store thread attributes
pthread_attr_t attr;
// set actual attributes to joinable
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// setup thread ids
int rc;
long t;
void *status;
// ruby VALUE that becomes our Ruby block
VALUE p;
if(rb_block_given_p())
{
p = rb_block_proc();
printf("Block was provided.\n");
}
else
{
rb_raise(rb_eArgError, "a block is required");
return Qnil;
}
// Acceptable to be outside if/else because we raise an error and return if there is no block given.
rb_funcall(p, rb_intern("call"), 0);
for(t = 0; t < num_threads; t++){
printf("In main: creating thread %ld\n", t);
// assemble thread arguments
struct arg_struct thread_args;
thread_args.threadid = t;
// create threads and pass function pointer and pointer to arguments for the thread to execute
rc = pthread_create(&threads[t], &attr, run_block, (void *)&thread_args);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
// Free attribute and wait for the other threads to finish
pthread_attr_destroy(&attr);
for(t=0; t<num_threads; t++) {
rc = pthread_join(threads[t], &status);
if (rc) {
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
} else {
printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status);
}
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
VALUE run_time = rb_float_new(time_spent);
return run_time;
}