Commit | Line | Data |
47ba8780 |
1 | |
a54396a0 |
2 | |
3 | # |
4 | # The reason this does not use a Test module is that |
5 | # they mess up test numbers between threads |
6 | # |
7 | # And even when that will be fixed, this is a basic |
8 | # test and should not rely on shared variables |
56a2bab7 |
9 | # |
5da2326b |
10 | # This will test the basic API, it will not use any coderefs |
11 | # as they are more advanced |
a54396a0 |
12 | # |
47ba8780 |
13 | ######################### |
14 | |
a54396a0 |
15 | |
83ebf7e2 |
16 | BEGIN { |
17 | chdir 't' if -d 't'; |
18 | @INC = '../lib'; |
19 | require Config; import Config; |
20 | unless ($Config{'useithreads'}) { |
21 | print "1..0 # Skip: no useithreads\n"; |
22 | exit 0; |
23 | } |
24 | } |
25 | |
47ba8780 |
26 | use ExtUtils::testlib; |
47ba8780 |
27 | use strict; |
56a2bab7 |
28 | BEGIN { $| = 1; print "1..15\n" }; |
47ba8780 |
29 | use threads; |
30 | |
31 | |
a54396a0 |
32 | |
33 | print "ok 1\n"; |
34 | |
47ba8780 |
35 | |
36 | ######################### |
37 | |
5da2326b |
38 | |
39 | |
40 | |
a54396a0 |
41 | sub ok { |
42 | my ($id, $ok, $name) = @_; |
56a2bab7 |
43 | |
a54396a0 |
44 | # You have to do it this way or VMS will get confused. |
45 | print $ok ? "ok $id - $name\n" : "not ok $id - $name\n"; |
46 | |
47 | printf "# Failed test at line %d\n", (caller)[2] unless $ok; |
56a2bab7 |
48 | |
a54396a0 |
49 | return $ok; |
50 | } |
51 | |
52 | |
47ba8780 |
53 | |
5da2326b |
54 | sub test1 { |
55 | ok(2,'bar' eq $_[0],"Test that argument passing works"); |
56 | } |
57 | threads->create('test1','bar')->join(); |
47ba8780 |
58 | |
5da2326b |
59 | sub test2 { |
60 | ok(3,'bar' eq $_[0]->[0]->{foo},"Test that passing arguments as references work"); |
61 | } |
47ba8780 |
62 | |
5da2326b |
63 | threads->create('test2',[{foo => 'bar'}])->join(); |
a54396a0 |
64 | |
47ba8780 |
65 | |
66 | #test execuion of normal sub |
5da2326b |
67 | sub test3 { ok(4,shift() == 1,"Test a normal sub") } |
68 | threads->create('test3',1)->join(); |
a54396a0 |
69 | |
47ba8780 |
70 | |
71 | #check Config |
9ece3ee6 |
72 | ok(5, 1 == $threads::threads,"Check that threads::threads is true"); |
47ba8780 |
73 | |
74 | #test trying to detach thread |
75 | |
f1f3224a |
76 | sub test4 { ok(6,1,"Detach test"); rmdir "thrsem" } |
77 | |
78 | # Just a sleep() would not guarantee that we sleep and will not |
79 | # wake up before the just created thread finishes. Instead, let's |
80 | # use the filesystem as a semaphore. Creating a directory and removing |
81 | # it should be a reasonably atomic operation even over NFS. |
82 | # Also, we do not want to depend here on shared variables. |
83 | |
84 | mkdir "thrsem", 0700; |
5da2326b |
85 | |
86 | my $thread1 = threads->create('test4'); |
47ba8780 |
87 | |
88 | $thread1->detach(); |
f1f3224a |
89 | sleep 1 while -d "thrsem"; |
5da2326b |
90 | ok(7,1,"Detach test"); |
91 | |
92 | |
93 | |
94 | sub test5 { |
95 | threads->create('test6')->join(); |
96 | ok(9,1,"Nested thread test"); |
47ba8780 |
97 | } |
a54396a0 |
98 | |
5da2326b |
99 | sub test6 { |
100 | ok(8,1,"Nested thread test"); |
101 | } |
47ba8780 |
102 | |
5da2326b |
103 | threads->create('test5')->join(); |
104 | |
105 | sub test7 { |
106 | my $self = threads->self(); |
eb75a40f |
107 | ok(10, $self->tid == 7, "Wanted 7, got ".$self->tid); |
108 | ok(11, threads->tid() == 7, "Wanted 7, got ".threads->tid()); |
47ba8780 |
109 | } |
47ba8780 |
110 | |
5da2326b |
111 | threads->create('test7')->join; |
112 | |
113 | sub test8 { |
114 | my $self = threads->self(); |
eb75a40f |
115 | ok(12, $self->tid == 8, "Wanted 8, got ".$self->tid); |
116 | ok(13, threads->tid() == 8, "Wanted 8, got ".threads->tid()); |
5da2326b |
117 | } |
47ba8780 |
118 | |
5da2326b |
119 | threads->create('test8')->join; |
47ba8780 |
120 | |
121 | |
5da2326b |
122 | #check support for threads->self() in main thread |
eb75a40f |
123 | ok(14, 0 == threads->self->tid(),"Check so that tid for threads work for main thread"); |
124 | ok(15, 0 == threads->tid(),"Check so that tid for threads work for main thread"); |
5da2326b |
125 | |
f1f3224a |
126 | END { |
127 | 1 while rmdir "thrsem"; |
128 | } |
47ba8780 |
129 | |
f1f3224a |
130 | 1; |