summaryrefslogtreecommitdiff
path: root/ext/threads/threads.xs
blob: cce263a7c4705bab0af1f5ee7b65a06092870c3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "threads.h"

/*
 *	Starts executing the thread. Needs to clean up memory a tad better.
 */

#ifdef WIN32
THREAD_RET_TYPE Perl_thread_run(LPVOID arg) {
#else
void* Perl_thread_run(void * arg) {
#endif
	ithread* thread = (ithread*) arg;
	SV* thread_tid_ptr;
	SV* thread_ptr;
	dTHXa(thread->interp);
	PERL_SET_CONTEXT(thread->interp);

#ifdef WIN32
	thread->thr = GetCurrentThreadId();
#else
	thread->thr = pthread_self();
#endif

	SHAREDSvLOCK(threads);
	SHAREDSvEDIT(threads);
	PERL_THREAD_ALLOC_SPECIFIC(self_key);
	PERL_THREAD_SETSPECIFIC(self_key,INT2PTR(void*,thread->tid));
	thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, thread->tid);	
	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);
	SHAREDSvRELEASE(threads);
	SHAREDSvUNLOCK(threads);
	PL_perl_destruct_level = 2;

	{

		AV* params;
		I32 len;
		int i;
		dSP;
		params = (AV*) SvRV(thread->params);
		len = av_len(params);
		ENTER;
		SAVETMPS;
		PUSHMARK(SP);
		if(len > -1) {
			for(i = 0; i < len + 1; i++) {
				XPUSHs(av_shift(params));
			}	
		}
		PUTBACK;
	 	call_sv(thread->init_function, G_DISCARD);
		FREETMPS;
		LEAVE;


	}

	MUTEX_LOCK(&thread->mutex);
	PerlIO_flush((PerlIO*)NULL);
 	perl_destruct(thread->interp);	
	perl_free(thread->interp);
	if(thread->detached == 1) {
		MUTEX_UNLOCK(&thread->mutex);
		Perl_thread_destruct(thread);
	} else {
	  	MUTEX_UNLOCK(&thread->mutex);
   	}
#ifdef WIN32
	return (DWORD)0;
#else
	return 0;
#endif

}

/*
 * iThread->create();
 */

SV* Perl_thread_create(char* class, SV* init_function, SV* params) {
	ithread* thread = malloc(sizeof(ithread));
  	SV*      obj_ref;
  	SV*      obj;
	SV*		temp_store;
	PerlInterpreter *current_perl;

	MUTEX_LOCK(&create_mutex);  
	obj_ref = newSViv(0);
	obj = newSVrv(obj_ref, class);
	sv_setiv(obj, (IV)thread);
	SvREADONLY_on(obj);

	current_perl = PERL_GET_CONTEXT;	

	/*
	 * 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.
	 */

	temp_store = Perl_get_sv(current_perl, "threads::paramtempstore",
				 TRUE | GV_ADDMULTI);
	Perl_sv_setsv(current_perl, temp_store,params);
	params = NULL;
	temp_store = NULL;

	temp_store = Perl_get_sv(current_perl, "threads::calltempstore",
				 TRUE | GV_ADDMULTI);
	Perl_sv_setsv(current_perl,temp_store, init_function);
	init_function = NULL;
	temp_store = NULL;

#ifdef WIN32
	thread->interp = perl_clone(current_perl, 4);
#else
	thread->interp = perl_clone(current_perl, 0);
#endif

	thread->init_function = newSVsv(Perl_get_sv(thread->interp,
						    "threads::calltempstore",FALSE));
	thread->params = newSVsv(Perl_get_sv(thread->interp,
					     "threads::paramtempstore",FALSE));

	/*
	 * 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 */

	temp_store = Perl_get_sv(thread->interp, "threads::paramtempstore",FALSE);
	Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);

	temp_store = Perl_get_sv(thread->interp,"threads::calltempstore",FALSE);
	Perl_sv_setsv(thread->interp,temp_store, &PL_sv_undef);

	PERL_SET_CONTEXT(current_perl);

	temp_store = Perl_get_sv(current_perl,"threads::paramtempstore",FALSE);
	Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);

	temp_store = Perl_get_sv(current_perl,"threads::calltempstore",FALSE);
	Perl_sv_setsv(current_perl, temp_store, &PL_sv_undef);

	/* let's init the thread */

	MUTEX_INIT(&thread->mutex);
	thread->tid = tid_counter++;
	thread->detached = 0;
	thread->count = 1;

#ifdef WIN32

	thread->handle = CreateThread(NULL, 0, Perl_thread_run,
			(LPVOID)thread, 0, &thread->thr);


#else
	{
	  static pthread_attr_t attr;
	  static int attr_inited = 0;
	  sigset_t fullmask, oldmask;
	  static int attr_joinable = PTHREAD_CREATE_JOINABLE;
	  if (!attr_inited) {
	    attr_inited = 1;
	    pthread_attr_init(&attr);
	  }
#  ifdef PTHREAD_ATTR_SETDETACHSTATE
            PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
#  endif
#  ifdef THREAD_CREATE_NEEDS_STACK
	    if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
	      croak("panic: pthread_attr_setstacksize failed");
#  endif

#ifdef OLD_PTHREADS_API
	  pthread_create( &thread->thr, attr, Perl_thread_run, (void *)thread);
#else
	  pthread_create( &thread->thr, &attr, Perl_thread_run, (void *)thread);
#endif
	}
#endif
	MUTEX_UNLOCK(&create_mutex);	

	return obj_ref;
}

/*
 * returns the id of the thread
 */
I32 Perl_thread_tid (SV* obj) {
	ithread* thread;
	if(!SvROK(obj)) {
		obj = Perl_thread_self(SvPV_nolen(obj));
		thread = (ithread*)SvIV(SvRV(obj));	
		SvREFCNT_dec(obj);
	} else {
		thread = (ithread*)SvIV(SvRV(obj));	
	}
	return thread->tid;
}

SV* Perl_thread_self (char* class) {
	dTHX;
	SV*      obj_ref;
	SV*      obj;
	SV*	thread_tid_ptr;
	SV*	thread_ptr;
	HE*	thread_entry;
	void*   id;
	PERL_THREAD_GETSPECIFIC(self_key,id);
	SHAREDSvLOCK(threads);
	SHAREDSvEDIT(threads);
	
	thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, PTR2UV(id));	

	thread_entry = Perl_hv_fetch_ent(PL_sharedsv_space,
					 (HV*) SHAREDSvGET(threads),
					 thread_tid_ptr, 0,0);
	thread_ptr = HeVAL(thread_entry);
	SvREFCNT_dec(thread_tid_ptr);	
	SHAREDSvRELEASE(threads);
	SHAREDSvUNLOCK(threads);

	obj_ref = newSViv(0);
	obj = newSVrv(obj_ref, class);
   	sv_setsv(obj, thread_ptr);
   	SvREADONLY_on(obj);
	return obj_ref;
}

/*
 * joins the thread this code needs to take the returnvalue from the
 * call_sv and send it back */

void Perl_thread_join(SV* obj) {
	ithread* thread = (ithread*)SvIV(SvRV(obj));
#ifdef WIN32
	DWORD waitcode;
	waitcode = WaitForSingleObject(thread->handle, INFINITE);
#else
	void *retval;
	pthread_join(thread->thr,&retval);
#endif
}

/* detaches a thread
 * needs to better clean up memory */

void Perl_thread_detach(SV* obj) {
	ithread* thread = (ithread*)SvIV(SvRV(obj));
	MUTEX_LOCK(&thread->mutex);
	thread->detached = 1;
	PERL_THREAD_DETACH(thread->thr);
	MUTEX_UNLOCK(&thread->mutex);
}

void Perl_thread_DESTROY (SV* obj) {
	ithread* thread = (ithread*)SvIV(SvRV(obj));
	
	MUTEX_LOCK(&thread->mutex);
	thread->count--;
	MUTEX_UNLOCK(&thread->mutex);
	Perl_thread_destruct(thread);
}

void Perl_thread_destruct (ithread* thread) {
	return;
	MUTEX_LOCK(&thread->mutex);
	if(thread->count != 0) {
		MUTEX_UNLOCK(&thread->mutex);
		return;	
	}
	MUTEX_UNLOCK(&thread->mutex);
	/* it is safe noone is holding a ref to this */
	/*printf("proper destruction!\n");*/
}

MODULE = threads		PACKAGE = threads		
BOOT:
	Perl_sharedsv_init(aTHX);
	PL_perl_destruct_level = 2;
	threads = Perl_sharedsv_new(aTHX);
	SHAREDSvEDIT(threads);
	SHAREDSvGET(threads) = (SV *)newHV();
	SHAREDSvRELEASE(threads);
	{
	    
	
	    SV* temp = get_sv("threads::sharedsv_space", TRUE | GV_ADDMULTI);
	    SV* temp2 = newSViv((IV)PL_sharedsv_space );
	    sv_setsv( temp , temp2 );
	}
	{
		ithread* thread = malloc(sizeof(ithread));
		SV* thread_tid_ptr;
		SV* thread_ptr;
		MUTEX_INIT(&thread->mutex);
		thread->tid = 0;
#ifdef WIN32
		thread->thr = GetCurrentThreadId();
#else
		thread->thr = pthread_self();
#endif
		SHAREDSvEDIT(threads);
		PERL_THREAD_ALLOC_SPECIFIC(self_key);
		PERL_THREAD_SETSPECIFIC(self_key,0);
		thread_tid_ptr = Perl_newSVuv(PL_sharedsv_space, 0);
		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);
		SHAREDSvRELEASE(threads);
	}
	MUTEX_INIT(&create_mutex);

PROTOTYPES: DISABLE

SV *
create (class, function_to_call, ...)
        char *  class
        SV *    function_to_call
		CODE:
			AV* params = newAV();
			if(items > 2) {
				int i;
				for(i = 2; i < items ; i++) {
					av_push(params, ST(i));
				}
			}
			RETVAL = Perl_thread_create(class, function_to_call, newRV_noinc((SV*) params));
			OUTPUT:
			RETVAL

SV *
self (class)
		char* class
	CODE:
		RETVAL = Perl_thread_self(class);
	OUTPUT:
		RETVAL

int
tid (obj)	
		SV *	obj;
	CODE:
		RETVAL = Perl_thread_tid(obj);
	OUTPUT:
	RETVAL

void
join (obj)
        SV *    obj
        PREINIT:
        I32* temp;
        PPCODE:
        temp = PL_markstack_ptr++;
        Perl_thread_join(obj);
        if (PL_markstack_ptr != temp) {
          /* truly void, because dXSARGS not invoked */
          PL_markstack_ptr = temp;
          XSRETURN_EMPTY; /* return empty stack */
        }
        /* must have used dXSARGS; list context implied */
        return; /* assume stack size is correct */

void
detach (obj)
        SV *    obj
        PREINIT:
        I32* temp;
        PPCODE:
        temp = PL_markstack_ptr++;
        Perl_thread_detach(obj);
        if (PL_markstack_ptr != temp) {
          /* truly void, because dXSARGS not invoked */
          PL_markstack_ptr = temp;
          XSRETURN_EMPTY; /* return empty stack */
        }
        /* must have used dXSARGS; list context implied */
        return; /* assume stack size is correct */

void
DESTROY (obj)
        SV *    obj
        PREINIT:
        I32* temp;
        PPCODE:
        temp = PL_markstack_ptr++;
        Perl_thread_DESTROY(obj);
        if (PL_markstack_ptr != temp) {
          /* truly void, because dXSARGS not invoked */
          PL_markstack_ptr = temp;
          XSRETURN_EMPTY; /* return empty stack */
        }
        /* must have used dXSARGS; list context implied */
        return; /* assume stack size is correct */