--- /dev/null
+# Before `make install' is performed this script should be runnable with
+# `make test'. After `make install' it should work as `perl test.pl'
+
+#########################
+
+# change 'tests => 1' to 'tests => last_test_to_print';
+use ExtUtils::testlib;
+use Test;
+use strict;
+BEGIN { plan tests => 16 };
+use threads;
+
+
+ok(1); # If we made it this far, we're ok.
+
+#########################
+
+# Insert your test code below, the Test module is use()ed here so read
+# its man page ( perldoc Test ) for help writing this test script.
+#my $bar;
+
+skip('The ignores are here to keep test numbers correct','The ignores are here to keep test numbers correct');
+
+#test passing of simple argument
+my $thread = threads->create(sub { ok('bar',$_[0]) },"bar");
+$thread->join();
+skip('Ignore','Ignore');
+
+#test passing of complex argument
+
+$thread = threads->create(sub { ok('bar',$_[0]->[0]->{foo})},[{foo => 'bar'}]);
+
+$thread->join();
+skip('Ignore','Ignore');
+
+#test execuion of normal sub
+sub bar { ok(1,shift()) }
+threads->create(\&bar,1)->join();
+skip('Ignore','Ignore');
+
+#check Config
+ok("1", "$Config::threads");
+
+#test trying to detach thread
+
+my $thread1 = threads->create(sub {ok(1);});
+
+$thread1->detach();
+skip('Ignore','Ignore');
+sleep 1;
+ok(1);
+#create nested threads
+unless($^O eq 'MSWin32') {
+ my $thread3 = threads->create(sub { threads->create(sub {})})->join();
+ ok(1);
+} else {
+ skip('thread trees are unsafe under win32','thread trees are unsafe under win32');
+}
+skip('Ignore','Ignore');
+
+my @threads;
+my $i;
+unless($^O eq 'MSWin32') {
+for(1..25) {
+ push @threads, threads->create(sub { for(1..100000) { my $i } threads->create(sub { sleep 2})->join() });
+}
+foreach my $thread (@threads) {
+ $thread->join();
+}
+}
+ok(1);
+threads->create(sub {
+ my $self = threads->self();
+ ok($self->tid(),57);
+})->join();
+skip('Ignore','Ignore');
+threads->create(sub {
+ my $self = threads->self();
+ ok($self->tid(),58);
+})->join();
+skip('Ignore','Ignore');
+
+#check support for threads->self() in main thread
+ok(0,threads->self->tid());
+ok(0,threads->tid());
+
+
+
+
+
+
+
+
+
+
--- /dev/null
+
+
+package threads;
+
+use 5.7.2;
+use strict;
+use warnings;
+
+use overload
+ '==' => \&equals,
+ 'fallback' => 1;
+
+
+#use threads::Shared;
+
+require Exporter;
+require DynaLoader;
+
+use Devel::Peek;
+
+
+our @ISA = qw(Exporter DynaLoader);
+
+our %EXPORT_TAGS = ( all => [qw()]);
+
+our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
+
+our @EXPORT = qw(
+
+);
+our $VERSION = '0.05';
+
+sub new {
+ my $class = shift;
+ print (Dump($_[0]));
+ return $class->create(@_);
+}
+
+
+sub equals {
+ return 1 if($_[0]->tid() == $_[1]->tid());
+ return 0;
+}
+
+$Config::threads = 1;
+
+bootstrap threads $VERSION;
+
+
+# Preloaded methods go here.
+
+1;
+__END__
+
+=head1 NAME
+
+threads - Perl extension allowing use of interpreter based threads from perl
+
+=head1 SYNOPSIS
+
+
+use threads;
+
+sub start_thread {
+ print "Thread started\n";
+}
+
+
+my $thread = threads->new("start_thread","argument");
+
+$thread->new(sub { print "I am a thread"},"argument");
+
+$thread->join();
+
+$thread->detach();
+
+$thread = threads->self();
+
+thread->tid();
+
+
+
+=head1 DESCRIPTION
+
+Perl 5.6 has something called interpreter threads, interpreter threads are built on MULTIPLICITY and allows for several different perl interpreters to run in different threads. This has been used in win32 perl to fake forks, it has also been available to people embedding perl.
+
+=over
+
+=item new, function, LIST
+
+This will create a new thread with the entry point function and give it LIST as parameters.
+It will return the corresponding threads object.
+
+=item $threads->join
+
+This will wait for the corresponding thread to join. When it finishes join will return the return values of the root function.
+If a thread has been detached, join will return without wait.
+
+=item $threads->detach
+
+Will throw away the return value from the thread and make non joinable
+
+=item threads->self
+
+This will return the object for the current thread.
+
+=item $threads->tid
+
+This will return the id of the thread.
+threads->self->tid() is a quick way to get current thread id
+
+=back
+
+
+=head1 TODO
+
+=over
+
+=item Fix so the return value is returned when you join
+
+=item Add join_all
+
+=item Fix memory leaks!
+
+=back
+
+=head1 AUTHOR and COPYRIGHT
+
+Artur Bergman <lt>artur at contiller.se<gt>
+
+threads is released under the same license as Perl
+
+Thanks to
+
+Richard Soderberg <lt>rs at crystalflame.net<gt>
+Helping me out tons, trying to find reasons for races and other wierd bugs!
+
+Simon Cozens <lt>simon at brecon.co.uk<gt>
+Being there to answer zillions of annoying questions
+
+Rocco Caputo <lt>troc at netrus.net<gt>
+
+Vipul Ved Prakash <lt>mail at vipul.net<gt>
+Helping with debugging.
+
+please join perl-ithreads@perl.org for more information
+
+=head1 BUGS
+
+=over
+
+=item creating a thread from within a thread is unsafe under win32
+
+=back
+
+=head1 SEE ALSO
+
+L<perl>, L<perlcall>, L<perlembed>, L<perlguts>
+
+=cut
+
+
+
+
--- /dev/null
+
+#include "threads.h"
+
+
+
+
+
+
+/*
+ Starts executing the thread. Needs to clean up memory a tad better.
+*/
+
+#ifdef WIN32
+THREAD_RET_TYPE thread_run(LPVOID arg) {
+ ithread* thread = (ithread*) arg;
+#else
+void thread_run(ithread* thread) {
+#endif
+ SV* thread_tid_ptr;
+ SV* thread_ptr;
+ dTHXa(thread->interp);
+
+
+ PERL_SET_CONTEXT(thread->interp);
+
+#ifdef WIN32
+ thread->thr = GetCurrentThreadId();
+#else
+ thread->thr = pthread_self();
+#endif
+
+ SHAREDSvEDIT(threads);
+ thread_tid_ptr = Perl_newSViv(sharedsv_space, (IV) thread->thr);
+ thread_ptr = Perl_newSViv(sharedsv_space, (IV) thread);
+ hv_store_ent((HV*)SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
+ SvREFCNT_dec(thread_tid_ptr);
+ SHAREDSvRELEASE(threads);
+
+
+ PL_perl_destruct_level = 2;
+ {
+
+ AV* params;
+ I32 len;
+ int i;
+ dSP;
+ params = (AV*) SvRV(thread->params);
+ len = av_len(params);
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ if(len > -1) {
+ for(i = 0; i < len + 1; i++) {
+ XPUSHs(av_shift(params));
+ }
+ }
+ PUTBACK;
+ call_sv(thread->init_function, G_DISCARD);
+ FREETMPS;
+ LEAVE;
+
+
+ }
+
+
+
+ MUTEX_LOCK(&thread->mutex);
+ perl_destruct(thread->interp);
+ perl_free(thread->interp);
+ if(thread->detached == 1) {
+ MUTEX_UNLOCK(&thread->mutex);
+ thread_destruct(thread);
+ } else {
+ MUTEX_UNLOCK(&thread->mutex);
+ }
+#ifdef WIN32
+ return (DWORD)0;
+#endif
+
+}
+
+
+
+/*
+ iThread->create();
+*/
+
+SV* thread_create(char* class, SV* init_function, SV* params) {
+ ithread* thread = malloc(sizeof(ithread));
+ SV* obj_ref;
+ SV* obj;
+ SV* temp_store;
+ I32 result;
+ PerlInterpreter *current_perl;
+
+ MUTEX_LOCK(&create_mutex);
+ obj_ref = newSViv(0);
+ obj = newSVrv(obj_ref, class);
+ sv_setiv(obj, (IV)thread);
+ SvREADONLY_on(obj);
+
+
+ current_perl = PERL_GET_CONTEXT;
+
+ /*
+ here we put the values of params and function to call onto namespace, this is so perl will properly clone them when we call perl_clone.
+ */
+
+ /*if(SvTYPE(SvRV(init_function)) == SVt_PVCV) {
+ CvCLONED_on(SvRV(init_function));
+ }
+ */
+
+ temp_store = Perl_get_sv(current_perl, "threads::paramtempstore", TRUE | GV_ADDMULTI);
+ Perl_sv_setsv(current_perl, temp_store,params);
+ params = NULL;
+ temp_store = NULL;
+
+ temp_store = Perl_get_sv(current_perl, "threads::calltempstore", TRUE | GV_ADDMULTI);
+ Perl_sv_setsv(current_perl,temp_store, init_function);
+
+
+
+#ifdef WIN32
+ thread->interp = perl_clone(current_perl,4);
+#else
+ thread->interp = perl_clone(current_perl,0);
+#endif
+
+ PL_perl_destruct_level = 2;
+
+// sv_dump(SvRV(Perl_get_sv(current_perl, "threads::calltempstore",FALSE)));
+// sv_dump(SvRV(Perl_get_sv(thread->interp, "threads::calltempstore",FALSE)));
+
+ thread->init_function = newSVsv(Perl_get_sv(thread->interp, "threads::calltempstore",FALSE));
+ thread->params = newSVsv(Perl_get_sv(thread->interp, "threads::paramtempstore",FALSE));
+
+ init_function = NULL;
+ temp_store = NULL;
+
+
+ /*
+ And here we make sure we clean up the data we put in the namespace of iThread, both in the new and the calling inteprreter
+ */
+
+
+
+ temp_store = Perl_get_sv(thread->interp,"threads::paramtempstore",FALSE);
+ Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
+
+ temp_store = Perl_get_sv(thread->interp,"threads::calltempstore",FALSE);
+ Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
+
+ PERL_SET_CONTEXT(current_perl);
+
+ temp_store = Perl_get_sv(current_perl,"threads::paramtempstore",FALSE);
+ Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);
+
+ temp_store = Perl_get_sv(current_perl,"threads::calltempstore",FALSE);
+ Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);
+
+ /* lets init the thread */
+
+
+
+
+
+ MUTEX_INIT(&thread->mutex);
+ thread->tid = tid_counter++;
+ thread->detached = 0;
+ thread->count = 1;
+
+#ifdef WIN32
+
+ thread->handle = CreateThread(NULL, 0, thread_run,
+ (LPVOID)thread, 0, &thread->thr);
+
+#else
+ pthread_create( &thread->thr, NULL, (void *) thread_run, thread);
+#endif
+ MUTEX_UNLOCK(&create_mutex);
+
+
+ if(!SvRV(obj_ref)) printf("FUCK\n");
+ return obj_ref;
+}
+
+/*
+ returns the id of the thread
+*/
+I32 thread_tid (SV* obj) {
+ ithread* thread;
+ if(!SvROK(obj)) {
+ obj = thread_self(SvPV_nolen(obj));
+ thread = (ithread*)SvIV(SvRV(obj));
+ SvREFCNT_dec(obj);
+ } else {
+ thread = (ithread*)SvIV(SvRV(obj));
+ }
+ return thread->tid;
+}
+
+SV* thread_self (char* class) {
+ dTHX;
+ SV* obj_ref;
+ SV* obj;
+ SV* thread_tid_ptr;
+ SV* thread_ptr;
+ HE* thread_entry;
+ IV pointer;
+ PerlInterpreter *old_context = PERL_GET_CONTEXT;
+
+
+
+ SHAREDSvEDIT(threads);
+#ifdef WIN32
+ thread_tid_ptr = Perl_newSViv(sharedsv_space, (IV) GetCurrentThreadId());
+#else
+ thread_tid_ptr = Perl_newSViv(sharedsv_space, (IV) pthread_self());
+#endif
+ thread_entry = Perl_hv_fetch_ent(sharedsv_space,(HV*) SHAREDSvGET(threads), thread_tid_ptr, 0,0);
+ thread_ptr = HeVAL(thread_entry);
+ SvREFCNT_dec(thread_tid_ptr);
+ pointer = SvIV(thread_ptr);
+ SHAREDSvRELEASE(threads);
+
+
+
+
+ obj_ref = newSViv(0);
+ obj = newSVrv(obj_ref, class);
+ sv_setiv(obj, pointer);
+ SvREADONLY_on(obj);
+ return obj_ref;
+}
+
+/*
+ joins the thread
+ this code needs to take the returnvalue from the call_sv and send it back
+*/
+
+void thread_join(SV* obj) {
+ ithread* thread = (ithread*)SvIV(SvRV(obj));
+#ifdef WIN32
+ DWORD waitcode;
+ waitcode = WaitForSingleObject(thread->handle, INFINITE);
+#else
+ void *retval;
+ pthread_join(thread->thr,&retval);
+#endif
+}
+
+
+/*
+ detaches a thread
+ needs to better clean up memory
+*/
+
+void thread_detach(SV* obj) {
+ ithread* thread = (ithread*)SvIV(SvRV(obj));
+ MUTEX_LOCK(&thread->mutex);
+ thread->detached = 1;
+#if !defined(WIN32)
+ pthread_detach(thread->thr);
+#endif
+ MUTEX_UNLOCK(&thread->mutex);
+}
+
+
+
+void thread_DESTROY (SV* obj) {
+ ithread* thread = (ithread*)SvIV(SvRV(obj));
+
+ MUTEX_LOCK(&thread->mutex);
+ thread->count--;
+ MUTEX_UNLOCK(&thread->mutex);
+ thread_destruct(thread);
+
+}
+
+void thread_destruct (ithread* thread) {
+ return;
+ MUTEX_LOCK(&thread->mutex);
+ if(thread->count != 0) {
+ MUTEX_UNLOCK(&thread->mutex);
+ return;
+ }
+ MUTEX_UNLOCK(&thread->mutex);
+ /* it is safe noone is holding a ref to this */
+ /*printf("proper destruction!\n");*/
+}
+
+
+MODULE = threads PACKAGE = threads
+BOOT:
+ Perl_sharedsv_init(aTHX);
+ PL_perl_destruct_level = 2;
+ threads = Perl_sharedsv_new(aTHX);
+ SHAREDSvEDIT(threads);
+ ((HV*) SHAREDSvGET(threads)) = newHV();
+ SHAREDSvRELEASE(threads);
+ {
+
+
+ SV* temp = get_sv("threads::sharedsv_space", TRUE | GV_ADDMULTI);
+ SV* temp2 = newSViv((IV)sharedsv_space );
+ sv_setsv( temp , temp2 );
+ }
+ {
+ ithread* thread = malloc(sizeof(ithread));
+ SV* thread_tid_ptr;
+ SV* thread_ptr;
+ MUTEX_INIT(&thread->mutex);
+ thread->tid = 0;
+#ifdef WIN32
+ thread->thr = GetCurrentThreadId();
+#else
+ thread->thr = pthread_self();
+#endif
+ SHAREDSvEDIT(threads);
+ thread_tid_ptr = Perl_newSViv(sharedsv_space, (IV) thread->thr);
+ thread_ptr = Perl_newSViv(sharedsv_space, (IV) thread);
+ hv_store_ent((HV*) SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
+ SvREFCNT_dec(thread_tid_ptr);
+ SHAREDSvRELEASE(threads);
+
+ }
+ MUTEX_INIT(&create_mutex);
+
+
+
+PROTOTYPES: DISABLE
+
+SV *
+create (class, function_to_call, ...)
+ char * class
+ SV * function_to_call
+ CODE:
+ AV* params = newAV();
+ if(items > 2) {
+ int i;
+ for(i = 2; i < items ; i++) {
+ av_push(params, ST(i));
+ }
+ }
+ RETVAL = thread_create(class, function_to_call, newRV_noinc((SV*) params));
+ OUTPUT:
+ RETVAL
+
+SV *
+self (class)
+ char* class
+ CODE:
+ RETVAL = thread_self(class);
+ OUTPUT:
+ RETVAL
+
+int
+tid (obj)
+ SV * obj;
+ CODE:
+ RETVAL = thread_tid(obj);
+ OUTPUT:
+ RETVAL
+
+void
+join (obj)
+ SV * obj
+ PREINIT:
+ I32* temp;
+ PPCODE:
+ temp = PL_markstack_ptr++;
+ thread_join(obj);
+ if (PL_markstack_ptr != temp) {
+ /* truly void, because dXSARGS not invoked */
+ PL_markstack_ptr = temp;
+ XSRETURN_EMPTY; /* return empty stack */
+ }
+ /* must have used dXSARGS; list context implied */
+ return; /* assume stack size is correct */
+
+void
+detach (obj)
+ SV * obj
+ PREINIT:
+ I32* temp;
+ PPCODE:
+ temp = PL_markstack_ptr++;
+ thread_detach(obj);
+ if (PL_markstack_ptr != temp) {
+ /* truly void, because dXSARGS not invoked */
+ PL_markstack_ptr = temp;
+ XSRETURN_EMPTY; /* return empty stack */
+ }
+ /* must have used dXSARGS; list context implied */
+ return; /* assume stack size is correct */
+
+
+
+
+
+void
+DESTROY (obj)
+ SV * obj
+ PREINIT:
+ I32* temp;
+ PPCODE:
+ temp = PL_markstack_ptr++;
+ thread_DESTROY(obj);
+ if (PL_markstack_ptr != temp) {
+ /* truly void, because dXSARGS not invoked */
+ PL_markstack_ptr = temp;
+ XSRETURN_EMPTY; /* return empty stack */
+ }
+ /* must have used dXSARGS; list context implied */
+ return; /* assume stack size is correct */
+
+
+