use strict;
use warnings;
-our $VERSION = '1.38';
+our $VERSION = '1.41';
my $XS_VERSION = $VERSION;
$VERSION = eval $VERSION;
=head1 VERSION
-This document describes threads version 1.38
+This document describes threads version 1.41
=head1 SYNOPSIS
my @args = @_;
print('Thread started: ', join(' ', @args), "\n");
}
- my $thread = threads->create('start_thread', 'argument');
- $thread->join();
+ my $thr = threads->create('start_thread', 'argument');
+ $thr->join();
threads->create(sub { print("I am a thread\n"); })->join();
- my $thread3 = async { foreach (@files) { ... } };
- $thread3->join();
+ my $thr2 = async { foreach (@files) { ... } };
+ $thr2->join();
# Invoke thread in list context (implicit) so it can return a list
my ($thr) = threads->create(sub { return (qw/a b c/); });
sub { return (qw/a b c/); });
my @results = $thr->join();
- $thread->detach();
+ $thr->detach();
# Get a thread's object
- $thread = threads->self();
- $thread = threads->object($tid);
+ $thr = threads->self();
+ $thr = threads->object($tid);
# Get a thread's ID
$tid = threads->tid();
- $tid = threads->self->tid();
- $tid = $thread->tid();
+ $tid = $thr->tid();
# Give other threads a chance to run
threads->yield();
L<http://www.cpanforum.com/dist/threads>
Annotated POD for L<threads>:
-L<http://annocpan.org/~JDHEDDEN/threads-1.38/threads.pm>
+L<http://annocpan.org/~JDHEDDEN/threads-1.41/threads.pm>
L<threads::shared>, L<perlthrtut>
#ifdef WIN32
HANDLE handle;
#endif
- /* Thread is still in use */
+ /* Return if thread is still being used */
if (thread->count != 0) {
return;
}
MUTEX_DESTROY(&thread->mutex);
#ifdef WIN32
- if (handle)
+ if (handle) {
CloseHandle(handle);
+ }
#endif
/* Call PerlMemShared_free() in the context of the "first" interpreter
(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
MUTEX_UNLOCK(&thread->mutex);
- if (cleanup)
+ if (cleanup) {
S_ithread_destruct(aTHX_ thread);
+ }
return (0);
}
good_stack_size(pTHX_ IV stack_size)
{
/* Use default stack size if no stack size specified */
- if (! stack_size)
+ if (! stack_size) {
return (default_stack_size);
+ }
#ifdef PTHREAD_STACK_MIN
/* Can't use less than minimum */
page_size = 8192; /* A conservative default */
# endif
# endif
- if (page_size <= 0)
+ if (page_size <= 0) {
Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
+ }
#endif
}
stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
/* Mark as finished */
thread->state |= PERL_ITHR_FINISHED;
/* Clear exit flag if required */
- if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY)
+ if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY) {
exit_app = 0;
+ }
/* Cleanup if detached */
cleanup = (thread->state & PERL_ITHR_DETACHED);
MUTEX_UNLOCK(&thread->mutex);
}
/* Clean up detached thread */
- if (cleanup)
+ if (cleanup) {
S_ithread_destruct(aTHX_ thread);
+ }
#ifdef WIN32
return ((DWORD)0);
/* Type conversion helper functions */
+
static SV *
ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
{
S_ithread_destruct(aTHX_ thread);
#ifndef WIN32
if (ckWARN_d(WARN_THREADS)) {
- if (rc_stack_size)
+ if (rc_stack_size) {
Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
- else
+ } else {
Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
+ }
}
#endif
return (NULL);
int ii;
CODE:
if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
- if (--items < 2)
+ if (--items < 2) {
Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
+ }
specs = (HV*)SvRV(ST(1));
idx = 1;
} else {
- if (items < 2)
+ if (items < 2) {
Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
+ }
specs = NULL;
idx = 0;
}
int want_running;
PPCODE:
/* Class method only */
- if (SvROK(ST(0)))
+ if (SvROK(ST(0))) {
Perl_croak(aTHX_ "Usage: threads->list(...)");
+ }
classname = (char *)SvPV_nolen(ST(0));
/* Calling context */
ithread *thread;
CODE:
/* Class method only */
- if (SvROK(ST(0)))
+ if (SvROK(ST(0))) {
Perl_croak(aTHX_ "Usage: threads->self()");
+ }
classname = (char *)SvPV_nolen(ST(0));
thread = S_ithread_get(aTHX);
#endif
PPCODE:
/* Object method only */
- if (! sv_isobject(ST(0)))
+ if (! sv_isobject(ST(0))) {
Perl_croak(aTHX_ "Usage: $thr->join()");
+ }
/* Check if the thread is joinable */
thread = SV_to_ithread(aTHX_ ST(0));
IV signal;
CODE:
/* Must have safe signals */
- if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
+ if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
Perl_croak(aTHX_ "Cannot signal threads without safe signals");
+ }
/* Object method only */
- if (! sv_isobject(ST(0)))
+ if (! sv_isobject(ST(0))) {
Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
+ }
/* Get signal */
sig_name = SvPV_nolen(ST(1));
if (isALPHA(*sig_name)) {
- if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G')
+ if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G') {
sig_name += 3;
- if ((signal = whichsig(sig_name)) < 0)
+ }
+ if ((signal = whichsig(sig_name)) < 0) {
Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
- } else
+ }
+ } else {
signal = SvIV(ST(1));
+ }
/* Set the signal for the thread */
thread = SV_to_ithread(aTHX_ ST(0));
int have_obj = 0;
CODE:
/* Class method only */
- if (SvROK(ST(0)))
+ if (SvROK(ST(0))) {
Perl_croak(aTHX_ "Usage: threads->object($tid)");
+ }
classname = (char *)SvPV_nolen(ST(0));
if ((items < 2) || ! SvOK(ST(1))) {
PREINIT:
IV old_size;
CODE:
- if (items != 2)
+ if (items != 2) {
Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
- if (sv_isobject(ST(0)))
+ }
+ if (sv_isobject(ST(0))) {
Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
+ }
old_size = default_stack_size;
default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
ithread *thread;
CODE:
/* Object method only */
- if (! sv_isobject(ST(0)))
+ 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;
ithread *thread;
CODE:
/* Object method only */
- if (! sv_isobject(ST(0)))
+ if (! sv_isobject(ST(0))) {
Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
+ }
thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
MUTEX_LOCK(&thread->mutex);
PREINIT:
ithread *thread;
CODE:
- if (items != 2)
+ if (items != 2) {
Perl_croak(aTHX_ "Usage: ->set_thread_exit_only(boolean)");
+ }
thread = SV_to_ithread(aTHX_ ST(0));
MUTEX_LOCK(&thread->mutex);
if (SvTRUE(ST(1))) {