From: Artur Bergman Date: Tue, 18 Sep 2001 14:13:26 +0000 (+0000) Subject: Adds the thread 0.05 module. It is now moved to the core from CPAN. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=47ba87809fbf35eecb6fc2ac6e979d62b1d62fd5;p=p5sagit%2Fp5-mst-13.2.git Adds the thread 0.05 module. It is now moved to the core from CPAN. The test will fail because of Test.pm not knowing about threads. basic needs to be manually tested compile time warnings on IV2PTR conversions need to be fixed p4raw-id: //depot/perl@12068 --- diff --git a/MANIFEST b/MANIFEST index 809536e..cdd65fa 100644 --- a/MANIFEST +++ b/MANIFEST @@ -569,6 +569,13 @@ ext/Thread/unsync.tx Test thread implicit synchronisation ext/Thread/unsync2.tx Test thread implicit synchronisation ext/Thread/unsync3.tx Test thread implicit synchronisation ext/Thread/unsync4.tx Test thread implicit synchronisation +ext/threads/Changes ithreads +ext/threads/Makefile.PL ithreads +ext/threads/README ithreads +ext/threads/threads.h ithreads +ext/threads/threads.pm ithreads +ext/threads/threads.xs ithreads +ext/threads/t/basic.t ithreads ext/Time/HiRes/Changes Time::HiRes extension ext/Time/HiRes/hints/dynixptx.pl Hint for Time::HiRes for named architecture ext/Time/HiRes/hints/sco.pl Hints for Time::HiRes for named architecture diff --git a/ext/threads/Changes b/ext/threads/Changes new file mode 100755 index 0000000..d832435 --- /dev/null +++ b/ext/threads/Changes @@ -0,0 +1,11 @@ +Revision history for Perl extension threads. + +0.03 Mon Jul 2 12:00:50 CEST 2001 + Fixed bug with threads->self() in main thread, thanks Hackworth! + +0.02 Sat Jun 30 09:41:00 GMT 2001 + Fixed bug in threads->self() reported by Hackworth + +0.01 Tue Apr 24 19:04:12 2001 + Cleaned up documentation + diff --git a/ext/threads/README b/ext/threads/README new file mode 100755 index 0000000..dd3f8fe --- /dev/null +++ b/ext/threads/README @@ -0,0 +1,24 @@ +threads version 0.03 +==================== + +This module needs perl 5.7.2 or later compiled with USEITHREADS, +it exposes interpreter threads to the perl level. + +INSTALLATION + +To install this module type the following: + + perl Makefile.PL + make + make test + make install + +DEPENDENCIES + +This module requires these other modules and libraries: + + +COPYRIGHT AND LICENCE + +Copyright (C) 2001 Artur Bergman artur at contiller.se +Same licence as perl. diff --git a/ext/threads/t/basic.t b/ext/threads/t/basic.t new file mode 100755 index 0000000..880497f --- /dev/null +++ b/ext/threads/t/basic.t @@ -0,0 +1,95 @@ +# 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()); + + + + + + + + + + diff --git a/ext/threads/threads.h b/ext/threads/threads.h new file mode 100755 index 0000000..869b270 --- /dev/null +++ b/ext/threads/threads.h @@ -0,0 +1,68 @@ + +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" +#include +#include + +#ifdef WIN32 +#include +#include +#else +#include +#include +#endif + +typedef struct { + PerlInterpreter *interp; /* The threads interpreter */ + I32 tid; /* Our thread */ + perl_mutex mutex; /* our mutex */ + I32 count; /* how many threads have a reference to us */ + signed char detached; /* are we detached ? */ + SV* init_function; + SV* params; +#ifdef WIN32 + DWORD thr; + HANDLE handle; +#else + pthread_t thr; +#endif +} ithread; + + + +static perl_mutex create_mutex; /* protects the creation of threads ??? */ + + + +I32 tid_counter = 1; +shared_sv* threads; + + + + + + +/* internal functions */ +#ifdef WIN32 +THREAD_RET_TYPE thread_run(LPVOID arg); +#else +void thread_run(ithread* thread); +#endif +void thread_destruct(ithread* thread); + +/* Perl mapped functions to iThread:: */ +SV* thread_create(char* class, SV* function_to_call, SV* params); +I32 thread_tid (SV* obj); +void thread_join(SV* obj); +void thread_detach(SV* obj); +SV* thread_self (char* class); + + + + + + + + + diff --git a/ext/threads/threads.pm b/ext/threads/threads.pm new file mode 100755 index 0000000..ae7eb99 --- /dev/null +++ b/ext/threads/threads.pm @@ -0,0 +1,164 @@ + + +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 artur at contiller.se + +threads is released under the same license as Perl + +Thanks to + +Richard Soderberg rs at crystalflame.net +Helping me out tons, trying to find reasons for races and other wierd bugs! + +Simon Cozens simon at brecon.co.uk +Being there to answer zillions of annoying questions + +Rocco Caputo troc at netrus.net + +Vipul Ved Prakash mail at vipul.net +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, L, L, L + +=cut + + + + diff --git a/ext/threads/threads.xs b/ext/threads/threads.xs new file mode 100755 index 0000000..d74a198 --- /dev/null +++ b/ext/threads/threads.xs @@ -0,0 +1,419 @@ + +#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 */ + + +