10 Starts executing the thread. Needs to clean up memory a tad better.
14 THREAD_RET_TYPE thread_run(LPVOID arg) {
15 ithread* thread = (ithread*) arg;
17 void thread_run(ithread* thread) {
21 dTHXa(thread->interp);
24 PERL_SET_CONTEXT(thread->interp);
27 thread->thr = GetCurrentThreadId();
29 thread->thr = pthread_self();
32 SHAREDSvEDIT(threads);
33 thread_tid_ptr = Perl_newSViv(sharedsv_space, (IV) thread->thr);
34 thread_ptr = Perl_newSViv(sharedsv_space, (IV) thread);
35 hv_store_ent((HV*)SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
36 SvREFCNT_dec(thread_tid_ptr);
37 SHAREDSvRELEASE(threads);
40 PL_perl_destruct_level = 2;
47 params = (AV*) SvRV(thread->params);
53 for(i = 0; i < len + 1; i++) {
54 XPUSHs(av_shift(params));
58 call_sv(thread->init_function, G_DISCARD);
67 MUTEX_LOCK(&thread->mutex);
68 perl_destruct(thread->interp);
69 perl_free(thread->interp);
70 if(thread->detached == 1) {
71 MUTEX_UNLOCK(&thread->mutex);
72 thread_destruct(thread);
74 MUTEX_UNLOCK(&thread->mutex);
88 SV* thread_create(char* class, SV* init_function, SV* params) {
89 ithread* thread = malloc(sizeof(ithread));
94 PerlInterpreter *current_perl;
96 MUTEX_LOCK(&create_mutex);
98 obj = newSVrv(obj_ref, class);
99 sv_setiv(obj, (IV)thread);
103 current_perl = PERL_GET_CONTEXT;
106 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.
109 /*if(SvTYPE(SvRV(init_function)) == SVt_PVCV) {
110 CvCLONED_on(SvRV(init_function));
114 temp_store = Perl_get_sv(current_perl, "threads::paramtempstore", TRUE | GV_ADDMULTI);
115 Perl_sv_setsv(current_perl, temp_store,params);
119 temp_store = Perl_get_sv(current_perl, "threads::calltempstore", TRUE | GV_ADDMULTI);
120 Perl_sv_setsv(current_perl,temp_store, init_function);
125 thread->interp = perl_clone(current_perl,4);
127 thread->interp = perl_clone(current_perl,0);
130 PL_perl_destruct_level = 2;
132 // sv_dump(SvRV(Perl_get_sv(current_perl, "threads::calltempstore",FALSE)));
133 // sv_dump(SvRV(Perl_get_sv(thread->interp, "threads::calltempstore",FALSE)));
135 thread->init_function = newSVsv(Perl_get_sv(thread->interp, "threads::calltempstore",FALSE));
136 thread->params = newSVsv(Perl_get_sv(thread->interp, "threads::paramtempstore",FALSE));
138 init_function = NULL;
143 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
148 temp_store = Perl_get_sv(thread->interp,"threads::paramtempstore",FALSE);
149 Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
151 temp_store = Perl_get_sv(thread->interp,"threads::calltempstore",FALSE);
152 Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
154 PERL_SET_CONTEXT(current_perl);
156 temp_store = Perl_get_sv(current_perl,"threads::paramtempstore",FALSE);
157 Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);
159 temp_store = Perl_get_sv(current_perl,"threads::calltempstore",FALSE);
160 Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);
162 /* lets init the thread */
168 MUTEX_INIT(&thread->mutex);
169 thread->tid = tid_counter++;
170 thread->detached = 0;
175 thread->handle = CreateThread(NULL, 0, thread_run,
176 (LPVOID)thread, 0, &thread->thr);
179 pthread_create( &thread->thr, NULL, (void *) thread_run, thread);
181 MUTEX_UNLOCK(&create_mutex);
184 if(!SvRV(obj_ref)) printf("FUCK\n");
189 returns the id of the thread
191 I32 thread_tid (SV* obj) {
194 obj = thread_self(SvPV_nolen(obj));
195 thread = (ithread*)SvIV(SvRV(obj));
198 thread = (ithread*)SvIV(SvRV(obj));
203 SV* thread_self (char* class) {
211 PerlInterpreter *old_context = PERL_GET_CONTEXT;
215 SHAREDSvEDIT(threads);
217 thread_tid_ptr = Perl_newSViv(sharedsv_space, (IV) GetCurrentThreadId());
219 thread_tid_ptr = Perl_newSViv(sharedsv_space, (IV) pthread_self());
221 thread_entry = Perl_hv_fetch_ent(sharedsv_space,(HV*) SHAREDSvGET(threads), thread_tid_ptr, 0,0);
222 thread_ptr = HeVAL(thread_entry);
223 SvREFCNT_dec(thread_tid_ptr);
224 pointer = SvIV(thread_ptr);
225 SHAREDSvRELEASE(threads);
230 obj_ref = newSViv(0);
231 obj = newSVrv(obj_ref, class);
232 sv_setiv(obj, pointer);
239 this code needs to take the returnvalue from the call_sv and send it back
242 void thread_join(SV* obj) {
243 ithread* thread = (ithread*)SvIV(SvRV(obj));
246 waitcode = WaitForSingleObject(thread->handle, INFINITE);
249 pthread_join(thread->thr,&retval);
256 needs to better clean up memory
259 void thread_detach(SV* obj) {
260 ithread* thread = (ithread*)SvIV(SvRV(obj));
261 MUTEX_LOCK(&thread->mutex);
262 thread->detached = 1;
264 pthread_detach(thread->thr);
266 MUTEX_UNLOCK(&thread->mutex);
271 void thread_DESTROY (SV* obj) {
272 ithread* thread = (ithread*)SvIV(SvRV(obj));
274 MUTEX_LOCK(&thread->mutex);
276 MUTEX_UNLOCK(&thread->mutex);
277 thread_destruct(thread);
281 void thread_destruct (ithread* thread) {
283 MUTEX_LOCK(&thread->mutex);
284 if(thread->count != 0) {
285 MUTEX_UNLOCK(&thread->mutex);
288 MUTEX_UNLOCK(&thread->mutex);
289 /* it is safe noone is holding a ref to this */
290 /*printf("proper destruction!\n");*/
294 MODULE = threads PACKAGE = threads
296 Perl_sharedsv_init(aTHX);
297 PL_perl_destruct_level = 2;
298 threads = Perl_sharedsv_new(aTHX);
299 SHAREDSvEDIT(threads);
300 ((HV*) SHAREDSvGET(threads)) = newHV();
301 SHAREDSvRELEASE(threads);
305 SV* temp = get_sv("threads::sharedsv_space", TRUE | GV_ADDMULTI);
306 SV* temp2 = newSViv((IV)sharedsv_space );
307 sv_setsv( temp , temp2 );
310 ithread* thread = malloc(sizeof(ithread));
313 MUTEX_INIT(&thread->mutex);
316 thread->thr = GetCurrentThreadId();
318 thread->thr = pthread_self();
320 SHAREDSvEDIT(threads);
321 thread_tid_ptr = Perl_newSViv(sharedsv_space, (IV) thread->thr);
322 thread_ptr = Perl_newSViv(sharedsv_space, (IV) thread);
323 hv_store_ent((HV*) SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
324 SvREFCNT_dec(thread_tid_ptr);
325 SHAREDSvRELEASE(threads);
328 MUTEX_INIT(&create_mutex);
335 create (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 = thread_create(class, function_to_call, newRV_noinc((SV*) params));
354 RETVAL = thread_self(class);
362 RETVAL = thread_tid(obj);
372 temp = PL_markstack_ptr++;
374 if (PL_markstack_ptr != temp) {
375 /* truly void, because dXSARGS not invoked */
376 PL_markstack_ptr = temp;
377 XSRETURN_EMPTY; /* return empty stack */
379 /* must have used dXSARGS; list context implied */
380 return; /* assume stack size is correct */
388 temp = PL_markstack_ptr++;
390 if (PL_markstack_ptr != temp) {
391 /* truly void, because dXSARGS not invoked */
392 PL_markstack_ptr = temp;
393 XSRETURN_EMPTY; /* return empty stack */
395 /* must have used dXSARGS; list context implied */
396 return; /* assume stack size is correct */
408 temp = PL_markstack_ptr++;
410 if (PL_markstack_ptr != temp) {
411 /* truly void, because dXSARGS not invoked */
412 PL_markstack_ptr = temp;
413 XSRETURN_EMPTY; /* return empty stack */
415 /* must have used dXSARGS; list context implied */
416 return; /* assume stack size is correct */