#ifdef WIN32
#include <windows.h>
#include <win32thread.h>
+#define PERL_THREAD_DETACH(t)
+#define PERL_THREAD_SET_SPECIFIC(k,v) TlsSetValue(k,v)
+#define PERL_THREAD_GET_SPECIFIC(k) TlsGetValue(k)
+#define PERL_THREAD_ALLOC_SPECIFIC(k) \
+STMT_START {\
+ if((k = TlsAlloc()) == TLS_OUT_OF_INDEXES) {\
+ PerlIO_printf(PerlIO_stderr(),"panic threads.h: TlsAlloc");\
+ exit(1);\
+ }\
+} STMT_END
#else
#include <pthread.h>
#include <thread.h>
+
+#define PERL_THREAD_SET_SPECIFIC(k,v) pthread_setspecific(k,v)
+#define PERL_THREAD_GET_SPECIFIC(k) PTHREAD_GETSPECIFIC(k)
+#define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
+ if(pthread_key_create(&(k),0)) {\
+ PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
+ exit(1);\
+ }\
+} STMT_END
+#ifdef OLD_PTHREADS_API
+#define PERL_THREAD_DETACH(t) pthread_detach(&(t))
+#else
+#define PERL_THREAD_DETACH(t) pthread_detach((t))
+#endif
#endif
typedef struct {
I32 tid_counter = 1;
shared_sv* threads;
-
+perl_key self_key;
SHAREDSvLOCK(threads);
SHAREDSvEDIT(threads);
- thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread->thr));
+ 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)));
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);
thread->handle = CreateThread(NULL, 0, Perl_thread_run,
(LPVOID)thread, 0, &thread->thr);
+
+#else
+#ifdef OLD_PTHREADS_API
+ pthread_create( &thread->thr, (pthread_attr_t)NULL, Perl_thread_run, thread);
#else
pthread_create( &thread->thr, (pthread_attr_t*)NULL, Perl_thread_run, thread);
#endif
+#endif
MUTEX_UNLOCK(&create_mutex);
return obj_ref;
SHAREDSvLOCK(threads);
SHAREDSvEDIT(threads);
-#ifdef WIN32
- thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space,
- (UV) GetCurrentThreadId());
-#else
- thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space,
- PTR2UV(pthread_self()));
-#endif
+
+ thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(PERL_THREAD_GET_SPECIFIC(self_key)));
+
thread_entry = Perl_hv_fetch_ent(PL_sharedsv_space,
(HV*) SHAREDSvGET(threads),
thread_tid_ptr, 0,0);
ithread* thread = (ithread*)SvIV(SvRV(obj));
MUTEX_LOCK(&thread->mutex);
thread->detached = 1;
-#if !defined(WIN32)
- pthread_detach(thread->thr);
-#endif
+ PERL_THREAD_DETACH(thread->thr);
MUTEX_UNLOCK(&thread->mutex);
}
thread->thr = pthread_self();
#endif
SHAREDSvEDIT(threads);
- thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread->thr));
+ 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)));
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);