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