#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef HAS_PPPORT_H
# define NEED_PL_signals
# define NEED_newRV_noinc
# define NEED_sv_2pv_nolen
# define NEED_SvREFCNT_inc_void
# include "ppport.h"
#endif
#ifndef DPTR2FPTR
#define DPTR2FPTR(t,p) ((t)(PTRV)(p))
#endif
#include <omnithread.h>
#if !defined(USE_ITHREADS)
#error Unsupported threading model
#endif
#if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
# include <sys/param.h>
#endif
#define PERL_ITHR_JOINABLE 0
#define PERL_ITHR_DETACHED 1
#define PERL_ITHR_JOINED 2
#define PERL_ITHR_FINISHED 4
#define PERL_ITHR_THREAD_EXIT_ONLY 8
struct
ithread {
ithread *next;
ithread *prev;
PerlInterpreter *interp;
UV tid;
omni_mutex mutex;
int
count;
int
state;
int
gimme;
SV *init_function;
SV *params;
omni_thread *thr;
IV stack_size;
static
UV tid_counter;
ithread(
int
state,
int
gimme)
: next(0),
prev(0),
interp(0),
tid(tid_counter++),
count(1),
state(state),
gimme(gimme),
init_function(0),
params(0),
thr(0),
stack_size(0) {
}
};
#define MY_CXT_KEY "omnithreads::_guts" XS_VERSION
typedef
struct
{
ithread *
thread
;
} my_cxt_t;
START_MY_CXT
UV ithread::tid_counter = 0;
static
ithread main_thread(PERL_ITHR_DETACHED, 0);
static
omni_mutex create_destruct_mutex;
static
IV joinable_threads = 0;
static
IV running_threads = 0;
static
IV detached_threads = 0;
static
IV default_stack_size = 0;
static
IV page_size = 0;
static
UV
S_dummy_unlock(pTHX)
{
return
0UL;
}
static
UV (*unlock_interpreter)(pTHX) = S_dummy_unlock;
static
void
S_dummy_relock(pTHX_ UV token)
{
}
static
void
(*relock_interpreter)(pTHX, UV token) = S_dummy_relock;
static
void
S_ithread_set(pTHX_ ithread *
thread
)
{
dMY_CXT;
MY_CXT.
thread
=
thread
;
}
static
ithread *
S_ithread_get(pTHX)
{
dMY_CXT;
return
(MY_CXT.
thread
);
}
static
void
S_ithread_clear(pTHX_ ithread *
thread
)
{
PerlInterpreter *interp;
assert
((
thread
->state & PERL_ITHR_FINISHED) &&
(
thread
->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
interp =
thread
->interp;
if
(interp) {
dTHXa(interp);
PERL_SET_CONTEXT(interp);
S_ithread_set(aTHX_
thread
);
SvREFCNT_dec(
thread
->params);
thread
->params = Nullsv;
perl_destruct(interp);
perl_free(interp);
thread
->interp = NULL;
}
PERL_SET_CONTEXT(aTHX);
}
static
void
S_ithread_destruct(pTHX_ ithread *
thread
)
{
if
(
thread
->count != 0) {
return
;
}
assert
(
thread
->tid != 0);
{
omni_mutex_lock lock(create_destruct_mutex);
thread
->next->prev =
thread
->prev;
thread
->prev->next =
thread
->next;
thread
->next = NULL;
thread
->prev = NULL;
}
thread
->mutex.lock();
S_ithread_clear(aTHX_
thread
);
thread
->mutex.unlock();
delete
thread
;
}
static
int
S_exit_warning(pTHX)
{
int
veto_cleanup;
{
omni_mutex_lock lock(create_destruct_mutex);
veto_cleanup = (running_threads || joinable_threads);
}
if
(veto_cleanup) {
if
(ckWARN_d(WARN_THREADS)) {
Perl_warn(aTHX_
"Perl exited with active threads:\n\t%"
IVdf
" running and unjoined\n\t%"
IVdf
" finished and unjoined\n\t%"
IVdf
" running and detached\n"
,
running_threads,
joinable_threads,
detached_threads);
}
}
return
(veto_cleanup);
}
int
Perl_ithread_hook(pTHX)
{
return
((aTHX == PL_curinterp) ? S_exit_warning(aTHX) : 0);
}
int
ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
{
ithread *
thread
= (ithread *)mg->mg_ptr;
SvIV_set(sv, PTR2IV(
thread
));
SvIOK_on(sv);
return
(0);
}
int
ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
{
ithread *
thread
= (ithread *)mg->mg_ptr;
int
cleanup;
{
omni_mutex_lock lock(
thread
->mutex);
cleanup = ((--
thread
->count == 0) &&
(
thread
->state & PERL_ITHR_FINISHED) &&
(
thread
->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
}
if
(cleanup) {
S_ithread_destruct(aTHX_
thread
);
}
return
(0);
}
int
ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
{
ithread *
thread
= (ithread *)mg->mg_ptr;
omni_mutex_lock lock(
thread
->mutex);
thread
->count++;
return
(0);
}
MGVTBL ithread_vtbl = {
ithread_mg_get,
0,
0,
0,
ithread_mg_free,
0,
ithread_mg_dup
};
static
void
*
S_ithread_run(
void
* arg)
{
ithread *
thread
= (ithread *)arg;
int
jmp_rc = 0;
I32 oldscope;
int
exit_app = 0;
int
exit_code = 0;
int
cleanup;
dJMPENV;
dTHXa(
thread
->interp);
thread
->mutex.lock();
thread
->mutex.unlock();
PERL_SET_CONTEXT(
thread
->interp);
S_ithread_set(aTHX_
thread
);
PL_perl_destruct_level = 2;
{
AV *params = (AV *)SvRV(
thread
->params);
int
len = (
int
)av_len(params)+1;
int
ii;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
for
(ii=0; ii < len; ii++) {
XPUSHs(av_shift(params));
}
PUTBACK;
oldscope = PL_scopestack_ix;
JMPENV_PUSH(jmp_rc);
if
(jmp_rc == 0) {
len = (
int
)call_sv(
thread
->init_function,
thread
->gimme|G_EVAL);
}
else
if
(jmp_rc == 2) {
exit_app = 1;
exit_code = STATUS_CURRENT;
while
(PL_scopestack_ix > oldscope) {
LEAVE;
}
}
JMPENV_POP;
SPAGAIN;
for
(ii=len-1; ii >= 0; ii--) {
SV *sv = POPs;
if
(jmp_rc == 0) {
av_store(params, ii, SvREFCNT_inc(sv));
}
}
FREETMPS;
LEAVE;
if
(SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
oldscope = PL_scopestack_ix;
JMPENV_PUSH(jmp_rc);
if
(jmp_rc == 0) {
Perl_warn(aTHX_
"Thread %"
UVuf
" terminated abnormally: %"
SVf,
thread
->tid, ERRSV);
}
else
if
(jmp_rc == 2) {
exit_app = 1;
exit_code = STATUS_CURRENT;
while
(PL_scopestack_ix > oldscope) {
LEAVE;
}
}
JMPENV_POP;
}
SvREFCNT_dec(
thread
->init_function);
thread
->init_function = Nullsv;
}
PerlIO_flush((PerlIO *)NULL);
create_destruct_mutex.lock();
thread
->mutex.lock();
thread
->state |= PERL_ITHR_FINISHED;
if
(
thread
->state & PERL_ITHR_THREAD_EXIT_ONLY) {
exit_app = 0;
}
cleanup = (
thread
->state & PERL_ITHR_DETACHED);
thread
->mutex.unlock();
if
(cleanup) {
detached_threads--;
}
else
{
running_threads--;
joinable_threads++;
}
create_destruct_mutex.unlock();
if
(exit_app) {
oldscope = PL_scopestack_ix;
JMPENV_PUSH(jmp_rc);
if
(jmp_rc == 0) {
S_exit_warning(aTHX);
}
else
if
(jmp_rc == 2) {
exit_code = STATUS_CURRENT;
while
(PL_scopestack_ix > oldscope) {
LEAVE;
}
}
JMPENV_POP;
my_exit(exit_code);
}
if
(cleanup) {
S_ithread_destruct(aTHX_
thread
);
}
return
(0);
}
static
SV *
ithread_to_SV(pTHX_ SV *obj, ithread *
thread
,
char
*classname,
bool
inc)
{
SV *sv;
MAGIC *mg;
if
(inc) {
omni_mutex_lock lock(
thread
->mutex);
thread
->count++;
}
if
(! obj) {
obj = newSV(0);
}
sv = newSVrv(obj, classname);
sv_setiv(sv, PTR2IV(
thread
));
mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (
char
*)
thread
, 0);
mg->mg_flags |= MGf_DUP;
SvREADONLY_on(sv);
return
(obj);
}
static
ithread *
SV_to_ithread(pTHX_ SV *sv)
{
if
(SvROK(sv)) {
return
(INT2PTR(ithread *, SvIV(SvRV(sv))));
}
return
(S_ithread_get(aTHX));
}
static
ithread *
S_ithread_create(
pTHX_ SV *init_function,
IV stack_size,
int
gimme,
int
exit_opt,
SV *params)
{
ithread *
thread
;
CLONE_PARAMS clone_param;
ithread *current_thread = S_ithread_get(aTHX);
SV **tmps_tmp = PL_tmps_stack;
IV tmps_ix = PL_tmps_ix;
thread
=
new
ithread(exit_opt, gimme);
if
(!
thread
) {
create_destruct_mutex.unlock();
PerlLIO_write(PerlIO_fileno(Perl_error_log),
PL_no_mem,
strlen
(PL_no_mem));
my_exit(1);
}
thread
->next = &main_thread;
thread
->prev = main_thread.prev;
main_thread.prev =
thread
;
thread
->prev->next =
thread
;
thread
->mutex.lock();
PerlIO_flush((PerlIO *)NULL);
S_ithread_set(aTHX_
thread
);
SAVEBOOL(PL_srand_called);
PL_srand_called = FALSE;
#ifdef WIN32
thread
->interp
= perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
#else
thread
->interp
= perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
#endif
{
dTHXa(
thread
->interp);
MY_CXT_CLONE;
SvREFCNT_dec(PL_endav);
PL_endav = newAV();
if
(SvPOK(init_function)) {
thread
->init_function = newSV(0);
sv_copypv(
thread
->init_function, init_function);
}
else
{
clone_param.flags = 0;
thread
->init_function = sv_dup(init_function, &clone_param);
if
(SvREFCNT(
thread
->init_function) == 0) {
SvREFCNT_inc_void(
thread
->init_function);
}
}
thread
->params = sv_dup(params, &clone_param);
SvREFCNT_inc_void(
thread
->params);
while
(tmps_ix > 0) {
SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
tmps_ix--;
if
(sv && SvREFCNT(sv) == 0) {
SvREFCNT_inc_void(sv);
SvREFCNT_dec(sv);
}
}
SvTEMP_off(
thread
->init_function);
ptr_table_free(PL_ptr_table);
PL_ptr_table = NULL;
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
}
S_ithread_set(aTHX_ current_thread);
PERL_SET_CONTEXT(aTHX);
thread
->thr =
new
omni_thread(S_ithread_run, (
void
*)
thread
);
if
(
thread
->thr) {
thread
->thr->start();
}
else
{
create_destruct_mutex.unlock();
sv_2mortal(params);
S_ithread_destruct(aTHX_
thread
);
return
(NULL);
}
running_threads++;
sv_2mortal(params);
return
(
thread
);
}
MODULE = omnithreads PACKAGE = omnithreads PREFIX = ithread_
PROTOTYPES: DISABLE
void
ithread_create(...)
PREINIT:
char
*classname;
ithread *
thread
;
SV *function_to_call;
AV *params;
HV *specs;
IV stack_size;
int
context;
int
exit_opt;
SV *thread_exit_only;
char
*str;
int
idx;
int
ii;
CODE:
if
((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
if
(--items < 2) {
Perl_croak(aTHX_
"Usage: threads->create(\\%specs, function, ...)"
);
}
specs = (HV*)SvRV(ST(1));
idx = 1;
}
else
{
if
(items < 2) {
Perl_croak(aTHX_
"Usage: threads->create(function, ...)"
);
}
specs = NULL;
idx = 0;
}
if
(sv_isobject(ST(0))) {
classname = HvNAME(SvSTASH(SvRV(ST(0))));
thread
= INT2PTR(ithread *, SvIV(SvRV(ST(0))));
stack_size =
thread
->stack_size;
exit_opt =
thread
->state & PERL_ITHR_THREAD_EXIT_ONLY;
}
else
{
classname = (
char
*)SvPV_nolen(ST(0));
stack_size = default_stack_size;
thread_exit_only = get_sv(
"omnithreads::thread_exit_only"
, TRUE);
exit_opt = (SvTRUE(thread_exit_only))
? PERL_ITHR_THREAD_EXIT_ONLY : 0;
}
function_to_call = ST(idx+1);
context = -1;
if
(specs) {
if
(hv_exists(specs,
"stack"
, 5)) {
stack_size = SvIV(*hv_fetch(specs,
"stack"
, 5, 0));
}
else
if
(hv_exists(specs,
"stacksize"
, 9)) {
stack_size = SvIV(*hv_fetch(specs,
"stacksize"
, 9, 0));
}
else
if
(hv_exists(specs,
"stack_size"
, 10)) {
stack_size = SvIV(*hv_fetch(specs,
"stack_size"
, 10, 0));
}
if
(hv_exists(specs,
"context"
, 7)) {
str = (
char
*)SvPV_nolen(*hv_fetch(specs,
"context"
, 7, 0));
switch
(*str) {
case
'a'
:
case
'A'
:
context = G_ARRAY;
break
;
case
's'
:
case
'S'
:
context = G_SCALAR;
break
;
case
'v'
:
case
'V'
:
context = G_VOID;
break
;
default
:
Perl_croak(aTHX_
"Invalid context: %s"
, str);
}
}
else
if
(hv_exists(specs,
"array"
, 5)) {
if
(SvTRUE(*hv_fetch(specs,
"array"
, 5, 0))) {
context = G_ARRAY;
}
}
else
if
(hv_exists(specs,
"scalar"
, 6)) {
if
(SvTRUE(*hv_fetch(specs,
"scalar"
, 6, 0))) {
context = G_SCALAR;
}
}
else
if
(hv_exists(specs,
"void"
, 4)) {
if
(SvTRUE(*hv_fetch(specs,
"void"
, 4, 0))) {
context = G_VOID;
}
}
if
(hv_exists(specs,
"exit"
, 4)) {
str = (
char
*)SvPV_nolen(*hv_fetch(specs,
"exit"
, 4, 0));
exit_opt = (*str ==
't'
|| *str ==
'T'
)
? PERL_ITHR_THREAD_EXIT_ONLY : 0;
}
}
if
(context == -1) {
context = GIMME_V;
}
else
{
context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
}
params = newAV();
if
(items > 2) {
for
(ii=2; ii < items ; ii++) {
av_push(params, SvREFCNT_inc(ST(idx+ii)));
}
}
create_destruct_mutex.lock();
thread
= S_ithread_create(aTHX_ function_to_call,
stack_size,
context,
exit_opt,
newRV_noinc((SV*)params));
if
(!
thread
) {
XSRETURN_UNDEF;
}
ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv,
thread
, classname, FALSE));
thread
->mutex.unlock();
create_destruct_mutex.unlock();
void
ithread_list(...)
PREINIT:
char
*classname;
ithread *
thread
;
int
list_context;
IV count = 0;
int
want_running;
PPCODE:
if
(SvROK(ST(0))) {
Perl_croak(aTHX_
"Usage: threads->list(...)"
);
}
classname = (
char
*)SvPV_nolen(ST(0));
list_context = (GIMME_V == G_ARRAY);
if
(items > 1) {
want_running = SvTRUE(ST(1));
}
create_destruct_mutex.lock();
for
(
thread
= main_thread.next;
thread
!= &main_thread;
thread
=
thread
->next)
{
if
(
thread
->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
continue
;
}
if
(items > 1) {
if
(want_running) {
if
(
thread
->state & PERL_ITHR_FINISHED) {
continue
;
}
}
else
{
if
(! (
thread
->state & PERL_ITHR_FINISHED)) {
continue
;
}
}
}
if
(list_context) {
XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv,
thread
, classname, TRUE)));
}
count++;
}
create_destruct_mutex.unlock();
if
(! list_context) {
XSRETURN_IV(count);
}
void
ithread_self(...)
PREINIT:
char
*classname;
ithread *
thread
;
CODE:
if
(SvROK(ST(0))) {
Perl_croak(aTHX_
"Usage: threads->self()"
);
}
classname = (
char
*)SvPV_nolen(ST(0));
thread
= S_ithread_get(aTHX);
ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv,
thread
, classname, TRUE));
void
ithread_tid(...)
PREINIT:
ithread *
thread
;
CODE:
thread
= SV_to_ithread(aTHX_ ST(0));
XST_mUV(0,
thread
->tid);
void
ithread_join(...)
PREINIT:
ithread *
thread
;
int
join_err;
AV *params;
int
len;
int
ii;
void
*retval;
PPCODE:
if
(! sv_isobject(ST(0))) {
Perl_croak(aTHX_
"Usage: $thr->join()"
);
}
thread
= SV_to_ithread(aTHX_ ST(0));
join_err = (
thread
->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
if
(join_err) {
if
(join_err & PERL_ITHR_DETACHED) {
Perl_croak(aTHX_
"Cannot join a detached thread"
);
}
else
{
Perl_croak(aTHX_
"Thread already joined"
);
}
}
PUTBACK;
{
UV token = unlock_interpreter(aTHX);
thread
->thr->join(0);
relock_interpreter(aTHX_ token);
}
SPAGAIN;
thread
->mutex.lock();
thread
->state |= PERL_ITHR_JOINED;
{
AV *params_copy;
PerlInterpreter *other_perl;
CLONE_PARAMS clone_params;
ithread *current_thread;
params_copy = (AV *)SvRV(
thread
->params);
other_perl =
thread
->interp;
clone_params.stashes = newAV();
clone_params.flags = CLONEf_JOIN_IN;
PL_ptr_table = ptr_table_new();
current_thread = S_ithread_get(aTHX);
S_ithread_set(aTHX_
thread
);
ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
params = (AV *)sv_dup((SV*)params_copy, &clone_params);
S_ithread_set(aTHX_ current_thread);
SvREFCNT_dec(clone_params.stashes);
SvREFCNT_inc_void(params);
ptr_table_free(PL_ptr_table);
PL_ptr_table = NULL;
}
S_ithread_clear(aTHX_
thread
);
thread
->mutex.unlock();
{
omni_mutex_lock lock(create_destruct_mutex);
if
(! (
thread
->state & PERL_ITHR_DETACHED)) {
joinable_threads--;
}
}
if
(! params) {
XSRETURN_UNDEF;
}
len = (
int
)AvFILL(params);
for
(ii=0; ii <= len; ii++) {
SV* param = av_shift(params);
XPUSHs(sv_2mortal(param));
}
SvREFCNT_dec(params);
void
ithread_yield(...)
CODE:
YIELD;
void
ithread_detach(...)
PREINIT:
ithread *
thread
;
int
detach_err;
int
cleanup;
CODE:
thread
= SV_to_ithread(aTHX_ ST(0));
if
((detach_err = (
thread
->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))) {
if
(detach_err & PERL_ITHR_DETACHED) {
Perl_croak(aTHX_
"Thread already detached"
);
}
else
{
Perl_croak(aTHX_
"Cannot detach a joined thread"
);
}
}
create_destruct_mutex.lock();
thread
->mutex.lock();
thread
->state |= PERL_ITHR_DETACHED;
cleanup = (
thread
->state & PERL_ITHR_FINISHED);
thread
->mutex.unlock();
if
(cleanup) {
joinable_threads--;
}
else
{
running_threads--;
detached_threads++;
}
create_destruct_mutex.unlock();
if
(cleanup) {
S_ithread_destruct(aTHX_
thread
);
}
void
ithread_kill(...)
PREINIT:
ithread *
thread
;
char
*sig_name;
IV
signal
;
CODE:
if
(PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
Perl_croak(aTHX_
"Cannot signal threads without safe signals"
);
}
if
(! sv_isobject(ST(0))) {
Perl_croak(aTHX_
"Usage: $thr->kill('SIG...')"
);
}
sig_name = SvPV_nolen(ST(1));
if
(isALPHA(*sig_name)) {
if
(*sig_name ==
'S'
&& sig_name[1] ==
'I'
&& sig_name[2] ==
'G'
) {
sig_name += 3;
}
if
((
signal
= whichsig(sig_name)) < 0) {
Perl_croak(aTHX_
"Unrecognized signal name: %s"
, sig_name);
}
}
else
{
signal
= SvIV(ST(1));
}
thread
= SV_to_ithread(aTHX_ ST(0));
{
omni_mutex_lock lock(
thread
->mutex);
if
(
thread
->interp) {
dTHXa(
thread
->interp);
PL_psig_pend[
signal
]++;
PL_sig_pending = 1;
}
}
ST(0) = ST(0);
void
ithread_DESTROY(...)
CODE:
sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
void
ithread_equal(...)
PREINIT:
int
are_equal = 0;
CODE:
if
(sv_isobject(ST(0)) && sv_isobject(ST(1))) {
ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
are_equal = (thr1->tid == thr2->tid);
}
if
(are_equal) {
XST_mYES(0);
}
else
{
XST_mIV(0, 0);
}
void
ithread_object(...)
PREINIT:
char
*classname;
UV tid;
ithread *
thread
;
int
have_obj = 0;
CODE:
if
(SvROK(ST(0))) {
Perl_croak(aTHX_
"Usage: threads->object($tid)"
);
}
classname = (
char
*)SvPV_nolen(ST(0));
if
((items < 2) || ! SvOK(ST(1))) {
XSRETURN_UNDEF;
}
tid = SvUV(ST(1));
create_destruct_mutex.lock();
for
(
thread
= main_thread.next;
thread
!= &main_thread;
thread
=
thread
->next)
{
if
(
thread
->tid == tid) {
if
(! (
thread
->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv,
thread
, classname, TRUE));
have_obj = 1;
}
break
;
}
}
create_destruct_mutex.unlock();
if
(! have_obj) {
XSRETURN_UNDEF;
}
void
ithread__handle(...);
PREINIT:
ithread *
thread
;
CODE:
thread
= SV_to_ithread(aTHX_ ST(0));
XST_mUV(0, PTR2UV(&
thread
->thr));
void
ithread_get_stack_size(...)
PREINIT:
IV stack_size;
CODE:
if
(sv_isobject(ST(0))) {
ithread *
thread
= INT2PTR(ithread *, SvIV(SvRV(ST(0))));
stack_size =
thread
->stack_size;
}
else
{
stack_size = default_stack_size;
}
XST_mIV(0, stack_size);
void
ithread_set_stack_size(...)
PREINIT:
IV old_size;
CODE:
if
(items != 2) {
Perl_croak(aTHX_
"Usage: threads->set_stack_size($size)"
);
}
if
(sv_isobject(ST(0))) {
Perl_croak(aTHX_
"Cannot change stack size of an existing thread"
);
}
old_size = default_stack_size;
default_stack_size = 0;
XST_mIV(0, old_size);
void
ithread_is_running(...)
PREINIT:
ithread *
thread
;
CODE:
if
(! sv_isobject(ST(0))) {
Perl_croak(aTHX_
"Usage: $thr->is_running()"
);
}
thread
= INT2PTR(ithread *, SvIV(SvRV(ST(0))));
ST(0) = (
thread
->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
void
ithread_is_detached(...)
PREINIT:
ithread *
thread
;
CODE:
thread
= SV_to_ithread(aTHX_ ST(0));
ST(0) = (
thread
->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
void
ithread_is_joinable(...)
PREINIT:
ithread *
thread
;
CODE:
if
(! sv_isobject(ST(0))) {
Perl_croak(aTHX_
"Usage: $thr->is_joinable()"
);
}
thread
= INT2PTR(ithread *, SvIV(SvRV(ST(0))));
{
omni_mutex_lock lock(
thread
->mutex);
ST(0) = ((
thread
->state & PERL_ITHR_FINISHED) &&
! (
thread
->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
? &PL_sv_yes : &PL_sv_no;
}
void
ithread_wantarray(...)
PREINIT:
ithread *
thread
;
CODE:
thread
= SV_to_ithread(aTHX_ ST(0));
ST(0) = (
thread
->gimme & G_ARRAY) ? &PL_sv_yes :
(
thread
->gimme & G_VOID) ? &PL_sv_undef
: &PL_sv_no;
void
ithread_set_thread_exit_only(...)
PREINIT:
ithread *
thread
;
CODE:
if
(items != 2) {
Perl_croak(aTHX_
"Usage: ->set_thread_exit_only(boolean)"
);
}
thread
= SV_to_ithread(aTHX_ ST(0));
omni_mutex_lock lock(
thread
->mutex);
if
(SvTRUE(ST(1))) {
thread
->state |= PERL_ITHR_THREAD_EXIT_ONLY;
}
else
{
thread
->state &= ~PERL_ITHR_THREAD_EXIT_ONLY;
}
BOOT:
{
int
count;
MY_CXT_INIT;
PL_perl_destruct_level = 2;
create_destruct_mutex.lock();
PL_threadhook = &Perl_ithread_hook;
main_thread.next = &main_thread;
main_thread.prev = &main_thread;
main_thread.interp = aTHX;
main_thread.stack_size = default_stack_size;
main_thread.thr = omni_thread::self();
S_ithread_set(aTHX_ &main_thread);
create_destruct_mutex.unlock();
ENTER;
SAVETMPS;
PUSHMARK(SP);
count = call_pv(
"CORBA::omniORB::_entry_lock_hooks"
, G_ARRAY | G_EVAL);
SPAGAIN;
if
(count == 2) {
relock_interpreter = DPTR2FPTR(
void
(*)(pTHX, UV token), POPu);
unlock_interpreter = DPTR2FPTR(UV (*)(pTHX), POPu);
PUTBACK;
}
else
{
warn(
"Couldn't obtain CORBA::omniORB entry lock hooks"
);
}
FREETMPS;
LEAVE;
}