This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
threads::shared 1.22
[perl5.git] / ext / threads / shared / t / object.t
CommitLineData
794f4697
JH
1use strict;
2use warnings;
3
4BEGIN {
5 if ($ENV{'PERL_CORE'}){
6 chdir 't';
7 unshift @INC, '../lib';
8 }
9 use Config;
10 if (! $Config{'useithreads'}) {
6c791b15 11 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
794f4697
JH
12 exit(0);
13 }
14 if ($] < 5.010) {
6c791b15 15 print("1..0 # SKIP Needs Perl 5.10.0 or later\n");
794f4697
JH
16 exit(0);
17 }
18}
19
20use ExtUtils::testlib;
21
22BEGIN {
23 $| = 1;
ea6a7c58 24 print("1..28\n"); ### Number of tests that will be run ###
794f4697
JH
25};
26
27use threads;
28use threads::shared;
29
30my $TEST;
31BEGIN {
32 share($TEST);
33 $TEST = 1;
34}
35
36sub ok {
37 my ($ok, $name) = @_;
38
39 lock($TEST);
40 my $id = $TEST++;
41
42 # You have to do it this way or VMS will get confused.
43 if ($ok) {
44 print("ok $id - $name\n");
45 } else {
46 print("not ok $id - $name\n");
47 printf("# Failed test at line %d\n", (caller)[2]);
48 }
49
50 return ($ok);
51}
52
53ok(1, 'Loaded');
54
55### Start of Testing ###
56
57{ package Jar;
58 my @jar :shared;
59
60 sub new
61 {
62 bless(&threads::shared::share({}), shift);
63 }
64
65 sub store
66 {
67 my ($self, $cookie) = @_;
68 push(@jar, $cookie);
69 return $jar[-1]; # Results in destruction of proxy object
70 }
71
72 sub peek
73 {
74 return $jar[-1];
75 }
76
77 sub fetch
78 {
79 pop(@jar);
80 }
81}
82
83{ package Cookie;
84
85 sub new
86 {
87 my $self = bless(&threads::shared::share({}), shift);
88 $self->{'type'} = shift;
89 return $self;
90 }
91
92 sub DESTROY
93 {
94 delete(shift->{'type'});
95 }
96}
97
98my $C1 = 'chocolate chip';
99my $C2 = 'oatmeal raisin';
100my $C3 = 'vanilla wafer';
101
102my $cookie = Cookie->new($C1);
103ok($cookie->{'type'} eq $C1, 'Have cookie');
104
105my $jar = Jar->new();
106$jar->store($cookie);
107
108ok($cookie->{'type'} eq $C1, 'Still have cookie');
109ok($jar->peek()->{'type'} eq $C1, 'Still have cookie');
110ok($cookie->{'type'} eq $C1, 'Still have cookie');
111
112threads->create(sub {
113 ok($cookie->{'type'} eq $C1, 'Have cookie in thread');
114 ok($jar->peek()->{'type'} eq $C1, 'Still have cookie in thread');
115 ok($cookie->{'type'} eq $C1, 'Still have cookie in thread');
116
117 $jar->store(Cookie->new($C2));
118 ok($jar->peek()->{'type'} eq $C2, 'Added cookie in thread');
119})->join();
120
121ok($cookie->{'type'} eq $C1, 'Still have original cookie after thread');
122ok($jar->peek()->{'type'} eq $C2, 'Still have added cookie after thread');
123
124$cookie = $jar->fetch();
125ok($cookie->{'type'} eq $C2, 'Fetched cookie from jar');
126ok($jar->peek()->{'type'} eq $C1, 'Cookie still in jar');
127
128$cookie = $jar->fetch();
129ok($cookie->{'type'} eq $C1, 'Fetched cookie from jar');
130undef($cookie);
131
132share($cookie);
133$cookie = $jar->store(Cookie->new($C3));
134ok($jar->peek()->{'type'} eq $C3, 'New cookie in jar');
135ok($cookie->{'type'} eq $C3, 'Have cookie');
136
137threads->create(sub {
138 ok($cookie->{'type'} eq $C3, 'Have cookie in thread');
139 $cookie = Cookie->new($C1);
140 ok($cookie->{'type'} eq $C1, 'Change cookie in thread');
141 ok($jar->peek()->{'type'} eq $C3, 'Still have cookie in jar');
142})->join();
143
144ok($cookie->{'type'} eq $C1, 'Have changed cookie after thread');
145ok($jar->peek()->{'type'} eq $C3, 'Still have cookie in jar');
146undef($cookie);
147ok($jar->peek()->{'type'} eq $C3, 'Still have cookie in jar');
148$cookie = $jar->fetch();
149ok($cookie->{'type'} eq $C3, 'Fetched cookie from jar');
150
ea6a7c58
JH
151{ package Foo;
152
153 my $ID = 1;
154 threads::shared::share($ID);
155
156 sub new
157 {
158 # Anonymous scalar with an internal ID
159 my $obj = \do{ my $scalar = $ID++; };
160 threads::shared::share($obj); # Make it shared
161 return (bless($obj, 'Foo')); # Make it an object
162 }
163}
164
165my $obj :shared;
166$obj = Foo->new();
167ok($$obj == 1, "Main: Object ID $$obj");
168
169threads->create( sub {
170 ok($$obj == 1, "Thread: Object ID $$obj");
171
172 $$obj = 10;
173 ok($$obj == 10, "Thread: Changed object ID $$obj");
174
175 $obj = Foo->new();
176 ok($$obj == 2, "Thread: New object ID $$obj");
177 } )->join();
178
179ok($$obj == 2, "Main: New object ID $$obj # TODO - should be 2");
180
6c791b15
JH
181exit(0);
182
794f4697 183# EOF