CORBA-omniORB

 view release on metacpan or  search on metacpan

omnithreads/t/libc.t  view on Meta::CPAN

use strict;
use warnings;

BEGIN {
    if ($ENV{'PERL_CORE'}){
        chdir 't';
        unshift @INC, '../lib';
    }
    use Config;
    if (! $Config{'useithreads'}) {
        print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
        exit(0);
    }
}

use ExtUtils::testlib;

sub ok {
    my ($id, $ok, $name) = @_;

    # You have to do it this way or VMS will get confused.
    if ($ok) {
        print("ok $id - $name\n");
    } else {
        print("not ok $id - $name\n");
        printf("# Failed test at line %d\n", (caller)[2]);
    }

    return ($ok);
}

use omnithreads;

BEGIN {
    eval {
        require omnithreads::shared;
        import omnithreads::shared;
    };
    if ($@ || ! $omnithreads::shared::threads_shared) {
        print("1..0 # Skip: omnithreads::shared not available\n");
        exit(0);
    }

    $| = 1;
    print("1..12\n");   ### Number of tests that will be run ###
};

ok(1, 1, 'Loaded');

### Start of Testing ###

my $i = 10;
my $y = 20000;

my %localtime;
for (0..$i) {
    $localtime{$_} = localtime($_);
};

my $mutex = 2;
share($mutex);

sub localtime_r {
    lock($mutex);
    my $retval = localtime(shift());
    return $retval;
}

my @threads;
for (0..$i) {
    my $thread = omnithreads->create(sub {
                    my $arg = $_;
                    my $localtime = $localtime{$arg};
                    my $error = 0;
                    for (0..$y) {
                        my $lt = localtime($arg);
                        if($localtime ne $lt) {
                            $error++;
                        }
                    }
                    lock($mutex);
                    ok($mutex, ! $error, 'localtime safe');
                    $mutex++;
                  });
    push @threads, $thread;
}



( run in 1.318 second using v1.01-cache-2.11-cpan-d80b1682f3f )