This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Note to self, doing *src_ary++ in a macro that evaluates
[perl5.git] / ext / threads / shared / shared.pm
CommitLineData
b050c948
AB
1package threads::shared;
2
3use strict;
4use warnings;
5use Config;
6use Scalar::Util qw(weaken);
7use attributes qw(reftype);
8
9BEGIN {
10 if($Config{'useithreads'} && $Config::threads) {
11 *share = \&share_enabled;
12 *cond_wait = \&cond_wait_disabled;
13 *cond_signal = \&cond_signal_disabled;
14 *cond_broadcast = \&cond_broadcast_disabled;
15 *unlock = \&unlock_disabled;
16 *lock = \&lock_disabled;
17 } else {
18 *share = \&share_enabled;
19 }
20}
21
22require Exporter;
23require DynaLoader;
24our @ISA = qw(Exporter DynaLoader);
25
26our @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock lock);
27our $VERSION = '0.01';
28
29our %shared;
30
b050c948
AB
31sub cond_wait_disabled { return @_ };
32sub cond_signal_disabled { return @_};
33sub cond_broadcast_disabled { return @_};
34sub unlock_disabled { 1 };
35sub lock_disabled { 1 }
36sub share_disabled { return @_}
37
38sub share_enabled (\[$@%]) { # \]
39 my $value = $_[0];
40 my $ref = reftype($value);
41 if($ref eq 'SCALAR') {
aaf3876d
AB
42 my $obj = \threads::shared::sv->new($$value);
43 bless $obj, 'threads::shared::sv';
44 $shared{$$obj} = $value;
45 weaken($shared{$$obj});
46 } elsif($ref eq "ARRAY") {
47 tie @$value, 'threads::shared::av', $value;
8669ce85
AB
48 } elsif($ref eq "HASH") {
49 tie %$value, "threads::shared::hv", $value;
b050c948
AB
50 } else {
51 die "You cannot share ref of type $_[0]\n";
52 }
53}
54
55sub CLONE {
56 return unless($_[0] eq "threads::shared");
57 foreach my $ptr (keys %shared) {
58 if($ptr) {
59 thrcnt_inc($shared{$ptr});
60 }
61 }
62}
63
aaf3876d
AB
64sub DESTROY {
65 my $self = shift;
66 delete($shared{$$self});
67}
68
b050c948
AB
69package threads::shared::sv;
70use base 'threads::shared';
71
aaf3876d
AB
72sub DESTROY {}
73
b050c948
AB
74package threads::shared::av;
75use base 'threads::shared';
aaf3876d
AB
76use Scalar::Util qw(weaken);
77sub TIEARRAY {
78 my $class = shift;
79 my $value = shift;
80 my $self = bless \threads::shared::av->new($value),'threads::shared::av';
81 $shared{$self->ptr} = $value;
82 weaken($shared{$self->ptr});
83 return $self;
84}
b050c948
AB
85
86package threads::shared::hv;
87use base 'threads::shared';
8669ce85
AB
88use Scalar::Util qw(weaken);
89sub TIEHASH {
90 my $class = shift;
91 my $value = shift;
92 my $self = bless \threads::shared::hv->new($value),'threads::shared::hv';
93 $shared{$self->ptr} = $value;
94 weaken($shared{$self->ptr});
95 return $self;
96}
b050c948 97
8669ce85 98package threads::shared;
b050c948
AB
99bootstrap threads::shared $VERSION;
100
101__END__
102
103=head1 NAME
104
105threads::shared - Perl extension for sharing data structures between threads
106
107=head1 SYNOPSIS
108
109 use threads::shared;
110
111 my($foo, @foo, %foo);
aaf3876d
AB
112 share($foo);
113 share(@foo);
114 share(%hash);
b050c948
AB
115 my $bar = share([]);
116 $hash{bar} = share({});
117
118 lock(\%hash);
119 unlock(\%hash);
120 cond_wait($scalar);
121 cond_broadcast(\@array);
122 cond_signal($scalar);
123
124=head1 DESCRIPTION
125
ad91d581
JH
126This modules allows you to share() variables. These variables will
127then be shared across different threads (and pseudoforks on
128win32). They are used together with the threads module.
b050c948
AB
129
130=head2 EXPORT
131
132share(), lock(), unlock(), cond_wait, cond_signal, cond_broadcast
133
134=head1 BUGS
135
136Not stress tested!
b050c948 137Does not support splice on arrays!
b050c948
AB
138
139=head1 AUTHOR
140
aaf3876d 141Arthur Bergman E<lt>arthur at contiller.seE<gt>
b050c948 142
aaf3876d 143threads::shared is released under the same license as Perl
b050c948
AB
144
145=head1 SEE ALSO
146
147L<perl> L<threads>
148
149=cut