xref: /openbsd-src/gnu/usr.bin/perl/dist/Thread-Queue/t/07_lock.t (revision f3efcd0145415b7d44d9da97e0ad5c21b186ac61)
1b39c5158Smillertuse strict;
2b39c5158Smillertuse warnings;
3b39c5158Smillert
4b39c5158SmillertBEGIN {
5b39c5158Smillert    use Config;
6b39c5158Smillert    if (! $Config{'useithreads'}) {
7b39c5158Smillert        print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
8b39c5158Smillert        exit(0);
9b39c5158Smillert    }
10b39c5158Smillert}
11b39c5158Smillert
12b39c5158Smillertuse threads;
13b39c5158Smillertuse Thread::Queue;
14b39c5158Smillertuse Thread::Semaphore;
15b39c5158Smillert
16*f3efcd01Safresh1BEGIN { # perl RT 133382
17b39c5158Smillertif ($] == 5.008) {
18b39c5158Smillert    require 't/test.pl';   # Test::More work-alike for Perl 5.8.0
19b39c5158Smillert} else {
20b39c5158Smillert    require Test::More;
21b39c5158Smillert}
22b39c5158SmillertTest::More->import();
23*f3efcd01Safresh1} # end BEGIN
24b39c5158Smillertplan('tests' => 3);
25b39c5158Smillert
26b39c5158Smillert# The following tests locking a queue
27b39c5158Smillert
28b39c5158Smillertmy $q = Thread::Queue->new(1..10);
29b39c5158Smillertok($q, 'New queue');
30b39c5158Smillert
31b39c5158Smillertmy $sm = Thread::Semaphore->new(0);
32b39c5158Smillertmy $st = Thread::Semaphore->new(0);
33b39c5158Smillert
345759b3d2Safresh1my $thr = threads->create(sub {
35b39c5158Smillert    {
36b39c5158Smillert        lock($q);
37b39c5158Smillert        $sm->up();
38b39c5158Smillert        $st->down();
39b39c5158Smillert        threads::yield();
40b39c5158Smillert        select(undef, undef, undef, 0.1);
41b39c5158Smillert        my @x = $q->extract(5,2);
42b39c5158Smillert        is_deeply(\@x, [6,7], 'Thread dequeues under lock');
43b39c5158Smillert    }
445759b3d2Safresh1});
45b39c5158Smillert
46b39c5158Smillert$sm->down();
47b39c5158Smillert$st->up();
48b39c5158Smillertmy @x = $q->dequeue_nb(100);
49b39c5158Smillertis_deeply(\@x, [1..5,8..10], 'Main dequeues');
505759b3d2Safresh1
515759b3d2Safresh1$thr->join();
52b39c5158Smillert
53b39c5158Smillertexit(0);
54b39c5158Smillert
55b39c5158Smillert# EOF
56