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 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread->thr));
27 thread_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread));
28 hv_store_ent((HV*)SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
29 SvREFCNT_dec(thread_tid_ptr);
30 SHAREDSvRELEASE(threads);
31 SHAREDSvUNLOCK(threads);
32 PL_perl_destruct_level = 2;
40 params = (AV*) SvRV(thread->params);
46 for(i = 0; i < len + 1; i++) {
47 XPUSHs(av_shift(params));
51 call_sv(thread->init_function, G_DISCARD);
58 MUTEX_LOCK(&thread->mutex);
59 perl_destruct(thread->interp);
60 perl_free(thread->interp);
61 if(thread->detached == 1) {
62 MUTEX_UNLOCK(&thread->mutex);
63 Perl_thread_destruct(thread);
65 MUTEX_UNLOCK(&thread->mutex);
79 SV* Perl_thread_create(char* class, SV* init_function, SV* params) {
80 ithread* thread = malloc(sizeof(ithread));
84 PerlInterpreter *current_perl;
86 MUTEX_LOCK(&create_mutex);
88 obj = newSVrv(obj_ref, class);
89 sv_setiv(obj, (IV)thread);
92 current_perl = PERL_GET_CONTEXT;
95 * here we put the values of params and function to call onto
96 * namespace, this is so perl will properly clone them when we
100 temp_store = Perl_get_sv(current_perl, "threads::paramtempstore",
102 Perl_sv_setsv(current_perl, temp_store,params);
106 temp_store = Perl_get_sv(current_perl, "threads::calltempstore",
108 Perl_sv_setsv(current_perl,temp_store, init_function);
109 init_function = NULL;
113 thread->interp = perl_clone(current_perl, 4);
115 thread->interp = perl_clone(current_perl, 0);
118 thread->init_function = newSVsv(Perl_get_sv(thread->interp,
119 "threads::calltempstore",FALSE));
120 thread->params = newSVsv(Perl_get_sv(thread->interp,
121 "threads::paramtempstore",FALSE));
124 * And here we make sure we clean up the data we put in the
125 * namespace of iThread, both in the new and the calling
128 temp_store = Perl_get_sv(thread->interp, "threads::paramtempstore",FALSE);
129 Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
131 temp_store = Perl_get_sv(thread->interp,"threads::calltempstore",FALSE);
132 Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
134 PERL_SET_CONTEXT(current_perl);
136 temp_store = Perl_get_sv(current_perl,"threads::paramtempstore",FALSE);
137 Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);
139 temp_store = Perl_get_sv(current_perl,"threads::calltempstore",FALSE);
140 Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);
142 /* let's init the thread */
144 MUTEX_INIT(&thread->mutex);
145 thread->tid = tid_counter++;
146 thread->detached = 0;
151 thread->handle = CreateThread(NULL, 0, Perl_thread_run,
152 (LPVOID)thread, 0, &thread->thr);
155 pthread_create( &thread->thr, (pthread_attr_t*)NULL, Perl_thread_run, thread);
157 MUTEX_UNLOCK(&create_mutex);
163 * returns the id of the thread
165 I32 Perl_thread_tid (SV* obj) {
168 obj = Perl_thread_self(SvPV_nolen(obj));
169 thread = (ithread*)SvIV(SvRV(obj));
172 thread = (ithread*)SvIV(SvRV(obj));
177 SV* Perl_thread_self (char* class) {
185 SHAREDSvLOCK(threads);
186 SHAREDSvEDIT(threads);
188 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space,
189 (UV) GetCurrentThreadId());
191 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space,
192 PTR2UV(pthread_self()));
194 thread_entry = Perl_hv_fetch_ent(PL_sharedsv_space,
195 (HV*) SHAREDSvGET(threads),
196 thread_tid_ptr, 0,0);
197 thread_ptr = HeVAL(thread_entry);
198 SvREFCNT_dec(thread_tid_ptr);
199 SHAREDSvRELEASE(threads);
200 SHAREDSvUNLOCK(threads);
202 obj_ref = newSViv(0);
203 obj = newSVrv(obj_ref, class);
204 sv_setsv(obj, thread_ptr);
210 * joins the thread this code needs to take the returnvalue from the
211 * call_sv and send it back */
213 void Perl_thread_join(SV* obj) {
214 ithread* thread = (ithread*)SvIV(SvRV(obj));
217 waitcode = WaitForSingleObject(thread->handle, INFINITE);
220 pthread_join(thread->thr,&retval);
225 * needs to better clean up memory */
227 void Perl_thread_detach(SV* obj) {
228 ithread* thread = (ithread*)SvIV(SvRV(obj));
229 MUTEX_LOCK(&thread->mutex);
230 thread->detached = 1;
232 pthread_detach(thread->thr);
234 MUTEX_UNLOCK(&thread->mutex);
237 void Perl_thread_DESTROY (SV* obj) {
238 ithread* thread = (ithread*)SvIV(SvRV(obj));
240 MUTEX_LOCK(&thread->mutex);
242 MUTEX_UNLOCK(&thread->mutex);
243 Perl_thread_destruct(thread);
246 void Perl_thread_destruct (ithread* thread) {
248 MUTEX_LOCK(&thread->mutex);
249 if(thread->count != 0) {
250 MUTEX_UNLOCK(&thread->mutex);
253 MUTEX_UNLOCK(&thread->mutex);
254 /* it is safe noone is holding a ref to this */
255 /*printf("proper destruction!\n");*/
258 MODULE = threads PACKAGE = threads
260 Perl_sharedsv_init(aTHX);
261 PL_perl_destruct_level = 2;
262 threads = Perl_sharedsv_new(aTHX);
263 SHAREDSvEDIT(threads);
264 SHAREDSvGET(threads) = (SV *)newHV();
265 SHAREDSvRELEASE(threads);
269 SV* temp = get_sv("threads::sharedsv_space", TRUE | GV_ADDMULTI);
270 SV* temp2 = newSViv((IV)PL_sharedsv_space );
271 sv_setsv( temp , temp2 );
274 ithread* thread = malloc(sizeof(ithread));
277 MUTEX_INIT(&thread->mutex);
280 thread->thr = GetCurrentThreadId();
282 thread->thr = pthread_self();
284 SHAREDSvEDIT(threads);
285 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread->thr));
286 thread_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread));
287 hv_store_ent((HV*) SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
288 SvREFCNT_dec(thread_tid_ptr);
289 SHAREDSvRELEASE(threads);
291 MUTEX_INIT(&create_mutex);
296 create (class, function_to_call, ...)
298 SV * function_to_call
300 AV* params = newAV();
303 for(i = 2; i < items ; i++) {
304 av_push(params, ST(i));
307 RETVAL = Perl_thread_create(class, function_to_call, newRV_noinc((SV*) params));
315 RETVAL = Perl_thread_self(class);
323 RETVAL = Perl_thread_tid(obj);
333 temp = PL_markstack_ptr++;
334 Perl_thread_join(obj);
335 if (PL_markstack_ptr != temp) {
336 /* truly void, because dXSARGS not invoked */
337 PL_markstack_ptr = temp;
338 XSRETURN_EMPTY; /* return empty stack */
340 /* must have used dXSARGS; list context implied */
341 return; /* assume stack size is correct */
349 temp = PL_markstack_ptr++;
350 Perl_thread_detach(obj);
351 if (PL_markstack_ptr != temp) {
352 /* truly void, because dXSARGS not invoked */
353 PL_markstack_ptr = temp;
354 XSRETURN_EMPTY; /* return empty stack */
356 /* must have used dXSARGS; list context implied */
357 return; /* assume stack size is correct */
365 temp = PL_markstack_ptr++;
366 Perl_thread_DESTROY(obj);
367 if (PL_markstack_ptr != temp) {
368 /* truly void, because dXSARGS not invoked */
369 PL_markstack_ptr = temp;
370 XSRETURN_EMPTY; /* return empty stack */
372 /* must have used dXSARGS; list context implied */
373 return; /* assume stack size is correct */