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_ALLOC_SPECIFIC(self_key);
27 PERL_THREAD_SETSPECIFIC(self_key,INT2PTR(void*,thread->tid));
28 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, thread->tid);
29 thread_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread));
30 hv_store_ent((HV*)SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
31 SvREFCNT_dec(thread_tid_ptr);
32 SHAREDSvRELEASE(threads);
33 SHAREDSvUNLOCK(threads);
34 PL_perl_destruct_level = 2;
42 params = (AV*) SvRV(thread->params);
48 for(i = 0; i < len + 1; i++) {
49 XPUSHs(av_shift(params));
53 call_sv(thread->init_function, G_DISCARD);
60 MUTEX_LOCK(&thread->mutex);
61 PerlIO_flush((PerlIO*)NULL);
62 perl_destruct(thread->interp);
63 perl_free(thread->interp);
64 if(thread->detached == 1) {
65 MUTEX_UNLOCK(&thread->mutex);
66 Perl_thread_destruct(thread);
68 MUTEX_UNLOCK(&thread->mutex);
82 SV* Perl_thread_create(char* class, SV* init_function, SV* params) {
83 ithread* thread = malloc(sizeof(ithread));
87 PerlInterpreter *current_perl;
89 MUTEX_LOCK(&create_mutex);
91 obj = newSVrv(obj_ref, class);
92 sv_setiv(obj, (IV)thread);
95 current_perl = PERL_GET_CONTEXT;
98 * here we put the values of params and function to call onto
99 * namespace, this is so perl will properly clone them when we
103 temp_store = Perl_get_sv(current_perl, "threads::paramtempstore",
105 Perl_sv_setsv(current_perl, temp_store,params);
109 temp_store = Perl_get_sv(current_perl, "threads::calltempstore",
111 Perl_sv_setsv(current_perl,temp_store, init_function);
112 init_function = NULL;
116 thread->interp = perl_clone(current_perl, 4);
118 thread->interp = perl_clone(current_perl, 0);
121 thread->init_function = newSVsv(Perl_get_sv(thread->interp,
122 "threads::calltempstore",FALSE));
123 thread->params = newSVsv(Perl_get_sv(thread->interp,
124 "threads::paramtempstore",FALSE));
127 * And here we make sure we clean up the data we put in the
128 * namespace of iThread, both in the new and the calling
131 temp_store = Perl_get_sv(thread->interp, "threads::paramtempstore",FALSE);
132 Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
134 temp_store = Perl_get_sv(thread->interp,"threads::calltempstore",FALSE);
135 Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);
137 PERL_SET_CONTEXT(current_perl);
139 temp_store = Perl_get_sv(current_perl,"threads::paramtempstore",FALSE);
140 Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);
142 temp_store = Perl_get_sv(current_perl,"threads::calltempstore",FALSE);
143 Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);
145 /* let's init the thread */
147 MUTEX_INIT(&thread->mutex);
148 thread->tid = tid_counter++;
149 thread->detached = 0;
154 thread->handle = CreateThread(NULL, 0, Perl_thread_run,
155 (LPVOID)thread, 0, &thread->thr);
160 static pthread_attr_t attr;
161 static int attr_inited = 0;
162 sigset_t fullmask, oldmask;
163 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
166 pthread_attr_init(&attr);
168 # ifdef PTHREAD_ATTR_SETDETACHSTATE
169 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
171 #ifdef OLD_PTHREADS_API
172 pthread_create( &thread->thr, attr, Perl_thread_run, (void *)thread);
174 pthread_create( &thread->thr, &attr, Perl_thread_run, (void *)thread);
178 MUTEX_UNLOCK(&create_mutex);
184 * returns the id of the thread
186 I32 Perl_thread_tid (SV* obj) {
189 obj = Perl_thread_self(SvPV_nolen(obj));
190 thread = (ithread*)SvIV(SvRV(obj));
193 thread = (ithread*)SvIV(SvRV(obj));
198 SV* Perl_thread_self (char* class) {
206 PERL_THREAD_GETSPECIFIC(self_key,id);
207 SHAREDSvLOCK(threads);
208 SHAREDSvEDIT(threads);
210 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(id));
212 thread_entry = Perl_hv_fetch_ent(PL_sharedsv_space,
213 (HV*) SHAREDSvGET(threads),
214 thread_tid_ptr, 0,0);
215 thread_ptr = HeVAL(thread_entry);
216 SvREFCNT_dec(thread_tid_ptr);
217 SHAREDSvRELEASE(threads);
218 SHAREDSvUNLOCK(threads);
220 obj_ref = newSViv(0);
221 obj = newSVrv(obj_ref, class);
222 sv_setsv(obj, thread_ptr);
228 * joins the thread this code needs to take the returnvalue from the
229 * call_sv and send it back */
231 void Perl_thread_join(SV* obj) {
232 ithread* thread = (ithread*)SvIV(SvRV(obj));
235 waitcode = WaitForSingleObject(thread->handle, INFINITE);
238 pthread_join(thread->thr,&retval);
243 * needs to better clean up memory */
245 void Perl_thread_detach(SV* obj) {
246 ithread* thread = (ithread*)SvIV(SvRV(obj));
247 MUTEX_LOCK(&thread->mutex);
248 thread->detached = 1;
249 PERL_THREAD_DETACH(thread->thr);
250 MUTEX_UNLOCK(&thread->mutex);
253 void Perl_thread_DESTROY (SV* obj) {
254 ithread* thread = (ithread*)SvIV(SvRV(obj));
256 MUTEX_LOCK(&thread->mutex);
258 MUTEX_UNLOCK(&thread->mutex);
259 Perl_thread_destruct(thread);
262 void Perl_thread_destruct (ithread* thread) {
264 MUTEX_LOCK(&thread->mutex);
265 if(thread->count != 0) {
266 MUTEX_UNLOCK(&thread->mutex);
269 MUTEX_UNLOCK(&thread->mutex);
270 /* it is safe noone is holding a ref to this */
271 /*printf("proper destruction!\n");*/
274 MODULE = threads PACKAGE = threads
276 Perl_sharedsv_init(aTHX);
277 PL_perl_destruct_level = 2;
278 threads = Perl_sharedsv_new(aTHX);
279 SHAREDSvEDIT(threads);
280 SHAREDSvGET(threads) = (SV *)newHV();
281 SHAREDSvRELEASE(threads);
285 SV* temp = get_sv("threads::sharedsv_space", TRUE | GV_ADDMULTI);
286 SV* temp2 = newSViv((IV)PL_sharedsv_space );
287 sv_setsv( temp , temp2 );
290 ithread* thread = malloc(sizeof(ithread));
293 MUTEX_INIT(&thread->mutex);
296 thread->thr = GetCurrentThreadId();
298 thread->thr = pthread_self();
300 SHAREDSvEDIT(threads);
301 PERL_THREAD_ALLOC_SPECIFIC(self_key);
302 PERL_THREAD_SETSPECIFIC(self_key,0);
303 thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, 0);
304 thread_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(thread));
305 hv_store_ent((HV*) SHAREDSvGET(threads), thread_tid_ptr, thread_ptr,0);
306 SvREFCNT_dec(thread_tid_ptr);
307 SHAREDSvRELEASE(threads);
309 MUTEX_INIT(&create_mutex);
314 create (class, function_to_call, ...)
316 SV * function_to_call
318 AV* params = newAV();
321 for(i = 2; i < items ; i++) {
322 av_push(params, ST(i));
325 RETVAL = Perl_thread_create(class, function_to_call, newRV_noinc((SV*) params));
333 RETVAL = Perl_thread_self(class);
341 RETVAL = Perl_thread_tid(obj);
351 temp = PL_markstack_ptr++;
352 Perl_thread_join(obj);
353 if (PL_markstack_ptr != temp) {
354 /* truly void, because dXSARGS not invoked */
355 PL_markstack_ptr = temp;
356 XSRETURN_EMPTY; /* return empty stack */
358 /* must have used dXSARGS; list context implied */
359 return; /* assume stack size is correct */
367 temp = PL_markstack_ptr++;
368 Perl_thread_detach(obj);
369 if (PL_markstack_ptr != temp) {
370 /* truly void, because dXSARGS not invoked */
371 PL_markstack_ptr = temp;
372 XSRETURN_EMPTY; /* return empty stack */
374 /* must have used dXSARGS; list context implied */
375 return; /* assume stack size is correct */
383 temp = PL_markstack_ptr++;
384 Perl_thread_DESTROY(obj);
385 if (PL_markstack_ptr != temp) {
386 /* truly void, because dXSARGS not invoked */
387 PL_markstack_ptr = temp;
388 XSRETURN_EMPTY; /* return empty stack */
390 /* must have used dXSARGS; list context implied */
391 return; /* assume stack size is correct */