4 * Starts executing the thread. Needs to clean up memory a tad better.
8 THREAD_RET_TYPE Perl_thread_run(LPVOID arg) {
10 void* Perl_thread_run(void * arg) {
12 ithread* thread = (ithread*) arg;
15 dTHXa(thread->interp);
16 PERL_SET_CONTEXT(thread->interp);
19 thread->thr = GetCurrentThreadId();
21 thread->thr = pthread_self();
24 SHAREDSvLOCK(threads);
25 SHAREDSvEDIT(threads);
26 PERL_THREAD_SETSPECIFIC(self_key,INT2PTR(void*,thread->tid));
27 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, thread->tid);
28 thread_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread));
29 hv_store_ent((HV*)SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
30 SvREFCNT_dec(thread_tid_ptr);
31 SHAREDSvRELEASE(threads);
32 SHAREDSvUNLOCK(threads);
33 PL_perl_destruct_level = 2;
41 params = (AV*) SvRV(thread->params);
47 for(i = 0; i < len + 1; i++) {
48 XPUSHs(av_shift(params));
52 call_sv(thread->init_function, G_DISCARD);
59 MUTEX_LOCK(&thread->mutex);
60 PerlIO_flush((PerlIO*)NULL);
61 perl_destruct(thread->interp);
62 perl_free(thread->interp);
63 if(thread->detached == 1) {
64 MUTEX_UNLOCK(&thread->mutex);
65 Perl_thread_destruct(thread);
67 MUTEX_UNLOCK(&thread->mutex);
81 SV* Perl_thread_create(char* class, SV* init_function, SV* params) {
82 ithread* thread = malloc(sizeof(ithread));
86 PerlInterpreter *current_perl;
88 MUTEX_LOCK(&create_mutex);
90 obj = newSVrv(obj_ref, class);
91 sv_setiv(obj, (IV)thread);
93 PerlIO_flush((PerlIO*)NULL);
94 current_perl = PERL_GET_CONTEXT;
97 * here we put the values of params and function to call onto
98 * namespace, this is so perl will properly clone them when we
102 temp_store = Perl_get_sv(current_perl, "threads::paramtempstore",
104 Perl_sv_setsv_flags(current_perl, temp_store,params, SV_GMAGIC);
108 temp_store = Perl_get_sv(current_perl, "threads::calltempstore",
110 Perl_sv_setsv_flags(current_perl,temp_store, init_function, SV_GMAGIC);
111 init_function = NULL;
115 thread->interp = perl_clone(current_perl, 4);
117 thread->interp = perl_clone(current_perl, 0);
120 thread->init_function = newSVsv(Perl_get_sv(thread->interp,
121 "threads::calltempstore",FALSE));
122 thread->params = newSVsv(Perl_get_sv(thread->interp,
123 "threads::paramtempstore",FALSE));
126 * And here we make sure we clean up the data we put in the
127 * namespace of iThread, both in the new and the calling
130 temp_store = Perl_get_sv(thread->interp, "threads::paramtempstore",FALSE);
131 Perl_sv_setsv_flags(thread->interp,temp_store, &PL_sv_undef, SV_GMAGIC);
133 temp_store = Perl_get_sv(thread->interp,"threads::calltempstore",FALSE);
134 Perl_sv_setsv_flags(thread->interp,temp_store, &PL_sv_undef, SV_GMAGIC);
136 PERL_SET_CONTEXT(current_perl);
138 temp_store = Perl_get_sv(current_perl,"threads::paramtempstore",FALSE);
139 Perl_sv_setsv_flags(current_perl, temp_store, &PL_sv_undef, SV_GMAGIC);
141 temp_store = Perl_get_sv(current_perl,"threads::calltempstore",FALSE);
142 Perl_sv_setsv_flags(current_perl, temp_store, &PL_sv_undef, SV_GMAGIC);
144 /* let's init the thread */
146 MUTEX_INIT(&thread->mutex);
147 thread->tid = tid_counter++;
148 thread->detached = 0;
153 thread->handle = CreateThread(NULL, 0, Perl_thread_run,
154 (LPVOID)thread, 0, &thread->thr);
159 static pthread_attr_t attr;
160 static int attr_inited = 0;
161 sigset_t fullmask, oldmask;
162 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
165 pthread_attr_init(&attr);
167 # ifdef PTHREAD_ATTR_SETDETACHSTATE
168 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
170 # ifdef THREAD_CREATE_NEEDS_STACK
171 if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
172 croak("panic: pthread_attr_setstacksize failed");
175 #ifdef OLD_PTHREADS_API
176 pthread_create( &thread->thr, attr, Perl_thread_run, (void *)thread);
178 pthread_create( &thread->thr, &attr, Perl_thread_run, (void *)thread);
182 MUTEX_UNLOCK(&create_mutex);
188 * returns the id of the thread
190 I32 Perl_thread_tid (SV* obj) {
193 obj = Perl_thread_self(SvPV_nolen(obj));
194 thread = (ithread*)SvIV(SvRV(obj));
197 thread = (ithread*)SvIV(SvRV(obj));
202 SV* Perl_thread_self (char* class) {
210 PERL_THREAD_GETSPECIFIC(self_key,id);
211 SHAREDSvLOCK(threads);
212 SHAREDSvEDIT(threads);
214 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(id));
216 thread_entry = Perl_hv_fetch_ent(PL_sharedsv_space,
217 (HV*) SHAREDSvGET(threads),
218 thread_tid_ptr, 0,0);
219 thread_ptr = HeVAL(thread_entry);
220 SvREFCNT_dec(thread_tid_ptr);
221 SHAREDSvRELEASE(threads);
222 SHAREDSvUNLOCK(threads);
224 obj_ref = newSViv(0);
225 obj = newSVrv(obj_ref, class);
226 sv_setsv(obj, thread_ptr);
232 * joins the thread this code needs to take the returnvalue from the
233 * call_sv and send it back */
235 void Perl_thread_join(SV* obj) {
236 ithread* thread = (ithread*)SvIV(SvRV(obj));
239 waitcode = WaitForSingleObject(thread->handle, INFINITE);
242 pthread_join(thread->thr,&retval);
247 * needs to better clean up memory */
249 void Perl_thread_detach(SV* obj) {
250 ithread* thread = (ithread*)SvIV(SvRV(obj));
251 MUTEX_LOCK(&thread->mutex);
252 thread->detached = 1;
253 PERL_THREAD_DETACH(thread->thr);
254 MUTEX_UNLOCK(&thread->mutex);
257 void Perl_thread_DESTROY (SV* obj) {
258 ithread* thread = (ithread*)SvIV(SvRV(obj));
260 MUTEX_LOCK(&thread->mutex);
262 MUTEX_UNLOCK(&thread->mutex);
263 Perl_thread_destruct(thread);
266 void Perl_thread_destruct (ithread* thread) {
268 MUTEX_LOCK(&thread->mutex);
269 if(thread->count != 0) {
270 MUTEX_UNLOCK(&thread->mutex);
273 MUTEX_UNLOCK(&thread->mutex);
274 /* it is safe noone is holding a ref to this */
275 /*printf("proper destruction!\n");*/
278 MODULE = threads PACKAGE = threads
280 Perl_sharedsv_init(aTHX);
281 PERL_THREAD_ALLOC_SPECIFIC(self_key);
282 PL_perl_destruct_level = 2;
283 threads = Perl_sharedsv_new(aTHX);
284 SHAREDSvEDIT(threads);
285 SHAREDSvGET(threads) = (SV *)newHV();
286 SHAREDSvRELEASE(threads);
290 SV* temp = get_sv("threads::sharedsv_space", TRUE | GV_ADDMULTI);
291 SV* temp2 = newSViv((IV)PL_sharedsv_space );
292 sv_setsv( temp , temp2 );
295 ithread* thread = malloc(sizeof(ithread));
298 MUTEX_INIT(&thread->mutex);
301 thread->thr = GetCurrentThreadId();
303 thread->thr = pthread_self();
305 SHAREDSvEDIT(threads);
306 PERL_THREAD_ALLOC_SPECIFIC(self_key);
307 PERL_THREAD_SETSPECIFIC(self_key,0);
308 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, 0);
309 thread_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread));
310 hv_store_ent((HV*) SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
311 SvREFCNT_dec(thread_tid_ptr);
312 SHAREDSvRELEASE(threads);
314 MUTEX_INIT(&create_mutex);
319 create (class, function_to_call, ...)
321 SV * function_to_call
323 AV* params = newAV();
326 for(i = 2; i < items ; i++) {
327 av_push(params, ST(i));
330 RETVAL = Perl_thread_create(class, function_to_call, newRV_noinc((SV*) params));
335 new (class, function_to_call, ...)
337 SV * function_to_call
339 AV* params = newAV();
342 for(i = 2; i < items ; i++) {
343 av_push(params, ST(i));
346 RETVAL = Perl_thread_create(class, function_to_call, newRV_noinc((SV*) params));
356 RETVAL = Perl_thread_self(class);
364 RETVAL = Perl_thread_tid(obj);
374 temp = PL_markstack_ptr++;
375 Perl_thread_join(obj);
376 if (PL_markstack_ptr != temp) {
377 /* truly void, because dXSARGS not invoked */
378 PL_markstack_ptr = temp;
379 XSRETURN_EMPTY; /* return empty stack */
381 /* must have used dXSARGS; list context implied */
382 return; /* assume stack size is correct */
390 temp = PL_markstack_ptr++;
391 Perl_thread_detach(obj);
392 if (PL_markstack_ptr != temp) {
393 /* truly void, because dXSARGS not invoked */
394 PL_markstack_ptr = temp;
395 XSRETURN_EMPTY; /* return empty stack */
397 /* must have used dXSARGS; list context implied */
398 return; /* assume stack size is correct */
406 temp = PL_markstack_ptr++;
407 Perl_thread_DESTROY(obj);
408 if (PL_markstack_ptr != temp) {
409 /* truly void, because dXSARGS not invoked */
410 PL_markstack_ptr = temp;
411 XSRETURN_EMPTY; /* return empty stack */
413 /* must have used dXSARGS; list context implied */
414 return; /* assume stack size is correct */