More Config::threads to threads::threads changes
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.xs
index 0d00c81..a4e22ec 100755 (executable)
@@ -23,9 +23,8 @@ void* Perl_thread_run(void * arg) {
 
        SHAREDSvLOCK(threads);
        SHAREDSvEDIT(threads);
-       PERL_THREAD_ALLOC_SPECIFIC(self_key);
-       PERL_THREAD_SET_SPECIFIC(self_key,INT2PTR(void*,thread->tid));
-       thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(PERL_THREAD_GET_SPECIFIC(self_key)));   
+       PERL_THREAD_SETSPECIFIC(self_key,INT2PTR(void*,thread->tid));
+       thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, thread->tid);  
        thread_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread));
        hv_store_ent((HV*)SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
        SvREFCNT_dec(thread_tid_ptr);
@@ -91,7 +90,7 @@ SV* Perl_thread_create(char* class, SV* init_function, SV* params) {
        obj = newSVrv(obj_ref, class);
        sv_setiv(obj, (IV)thread);
        SvREADONLY_on(obj);
-
+       PerlIO_flush((PerlIO*)NULL);
        current_perl = PERL_GET_CONTEXT;        
 
        /*
@@ -102,13 +101,13 @@ SV* Perl_thread_create(char* class, SV* init_function, SV* params) {
 
        temp_store = Perl_get_sv(current_perl, "threads::paramtempstore",
                                 TRUE | GV_ADDMULTI);
-       Perl_sv_setsv(current_perl, temp_store,params);
+       Perl_sv_setsv_flags(current_perl, temp_store,params, SV_GMAGIC);
        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);
+       Perl_sv_setsv_flags(current_perl,temp_store, init_function, SV_GMAGIC);
        init_function = NULL;
        temp_store = NULL;
 
@@ -129,18 +128,18 @@ SV* Perl_thread_create(char* class, SV* init_function, SV* params) {
         * inteprreter */
 
        temp_store = Perl_get_sv(thread->interp, "threads::paramtempstore",FALSE);
-       Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
+       Perl_sv_setsv_flags(thread->interp,temp_store, &PL_sv_undef, SV_GMAGIC);
 
        temp_store = Perl_get_sv(thread->interp,"threads::calltempstore",FALSE);
-       Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
+       Perl_sv_setsv_flags(thread->interp,temp_store, &PL_sv_undef, SV_GMAGIC);
 
        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);
+       Perl_sv_setsv_flags(current_perl, temp_store, &PL_sv_undef, SV_GMAGIC);
 
        temp_store = Perl_get_sv(current_perl,"threads::calltempstore",FALSE);
-       Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);
+       Perl_sv_setsv_flags(current_perl, temp_store, &PL_sv_undef, SV_GMAGIC);
 
        /* let's init the thread */
 
@@ -156,11 +155,29 @@ SV* Perl_thread_create(char* class, SV* init_function, SV* params) {
 
 
 #else
+       {
+         static pthread_attr_t attr;
+         static int attr_inited = 0;
+         sigset_t fullmask, oldmask;
+         static int attr_joinable = PTHREAD_CREATE_JOINABLE;
+         if (!attr_inited) {
+           attr_inited = 1;
+           pthread_attr_init(&attr);
+         }
+#  ifdef PTHREAD_ATTR_SETDETACHSTATE
+            PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
+#  endif
+#  ifdef THREAD_CREATE_NEEDS_STACK
+           if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
+             croak("panic: pthread_attr_setstacksize failed");
+#  endif
+
 #ifdef OLD_PTHREADS_API
-       pthread_create( &thread->thr, (pthread_attr_t)NULL, Perl_thread_run, (void *)thread);
+         pthread_create( &thread->thr, attr, Perl_thread_run, (void *)thread);
 #else
-       pthread_create( &thread->thr, (pthread_attr_t*)NULL, Perl_thread_run, (void *)thread);
+         pthread_create( &thread->thr, &attr, Perl_thread_run, (void *)thread);
 #endif
+       }
 #endif
        MUTEX_UNLOCK(&create_mutex);    
 
@@ -189,11 +206,12 @@ SV* Perl_thread_self (char* class) {
        SV*     thread_tid_ptr;
        SV*     thread_ptr;
        HE*     thread_entry;
-       
+       void*   id;
+       PERL_THREAD_GETSPECIFIC(self_key,id);
        SHAREDSvLOCK(threads);
        SHAREDSvEDIT(threads);
-
-       thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(PERL_THREAD_GET_SPECIFIC(self_key)));   
+       
+       thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(id));   
 
        thread_entry = Perl_hv_fetch_ent(PL_sharedsv_space,
                                         (HV*) SHAREDSvGET(threads),
@@ -260,6 +278,7 @@ void Perl_thread_destruct (ithread* thread) {
 MODULE = threads               PACKAGE = threads               
 BOOT:
        Perl_sharedsv_init(aTHX);
+       PERL_THREAD_ALLOC_SPECIFIC(self_key);
        PL_perl_destruct_level = 2;
        threads = Perl_sharedsv_new(aTHX);
        SHAREDSvEDIT(threads);
@@ -285,8 +304,8 @@ BOOT:
 #endif
                SHAREDSvEDIT(threads);
                PERL_THREAD_ALLOC_SPECIFIC(self_key);
-               PERL_THREAD_SET_SPECIFIC(self_key,0);
-               thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(PERL_THREAD_GET_SPECIFIC(self_key)));
+               PERL_THREAD_SETSPECIFIC(self_key,0);
+               thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, 0);
                thread_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread));
                hv_store_ent((HV*) SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
                SvREFCNT_dec(thread_tid_ptr);
@@ -313,6 +332,24 @@ create (class, function_to_call, ...)
                        RETVAL
 
 SV *
+new (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 = Perl_thread_create(class, function_to_call, newRV_noinc((SV*) params));
+                       OUTPUT:
+                       RETVAL
+
+
+
+SV *
 self (class)
                char* class
        CODE: