Add support for basic support for AVs, references not supported yet.
[p5sagit/p5-mst-13.2.git] / ext / threads / shared / shared.pm
CommitLineData
b050c948 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 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 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;
b050c948 48 } else {
49 die "You cannot share ref of type $_[0]\n";
50 }
51}
52
53sub CLONE {
54 return unless($_[0] eq "threads::shared");
55 foreach my $ptr (keys %shared) {
56 if($ptr) {
57 thrcnt_inc($shared{$ptr});
58 }
59 }
60}
61
aaf3876d 62sub DESTROY {
63 my $self = shift;
64 delete($shared{$$self});
65}
66
b050c948 67package threads::shared::sv;
68use base 'threads::shared';
69
aaf3876d 70sub DESTROY {}
71
b050c948 72package threads::shared::av;
73use base 'threads::shared';
aaf3876d 74use Scalar::Util qw(weaken);
75sub TIEARRAY {
76 my $class = shift;
77 my $value = shift;
78 my $self = bless \threads::shared::av->new($value),'threads::shared::av';
79 $shared{$self->ptr} = $value;
80 weaken($shared{$self->ptr});
81 return $self;
82}
b050c948 83
84package threads::shared::hv;
85use base 'threads::shared';
86
b050c948 87bootstrap threads::shared $VERSION;
88
89__END__
90
91=head1 NAME
92
93threads::shared - Perl extension for sharing data structures between threads
94
95=head1 SYNOPSIS
96
97 use threads::shared;
98
99 my($foo, @foo, %foo);
aaf3876d 100 share($foo);
101 share(@foo);
102 share(%hash);
b050c948 103 my $bar = share([]);
104 $hash{bar} = share({});
105
106 lock(\%hash);
107 unlock(\%hash);
108 cond_wait($scalar);
109 cond_broadcast(\@array);
110 cond_signal($scalar);
111
112=head1 DESCRIPTION
113
ad91d581 114This modules allows you to share() variables. These variables will
115then be shared across different threads (and pseudoforks on
116win32). They are used together with the threads module.
b050c948 117
118=head2 EXPORT
119
120share(), lock(), unlock(), cond_wait, cond_signal, cond_broadcast
121
122=head1 BUGS
123
124Not stress tested!
b050c948 125Does not support splice on arrays!
b050c948 126
127=head1 AUTHOR
128
aaf3876d 129Arthur Bergman E<lt>arthur at contiller.seE<gt>
b050c948 130
aaf3876d 131threads::shared is released under the same license as Perl
b050c948 132
133=head1 SEE ALSO
134
135L<perl> L<threads>
136
137=cut