This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Dump Unicode better for re 'debug'. The regprop()
[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') {
42 my $obj = \threads::shared::sv->new($$value);
43 bless $obj, 'threads::shared::sv';
44 $shared{$$obj} = $value;
45 weaken($shared{$$obj});
46 } else {
47 die "You cannot share ref of type $_[0]\n";
48 }
49}
50
51sub CLONE {
52 return unless($_[0] eq "threads::shared");
53 foreach my $ptr (keys %shared) {
54 if($ptr) {
55 thrcnt_inc($shared{$ptr});
56 }
57 }
58}
59
b050c948
AB
60package threads::shared::sv;
61use base 'threads::shared';
62
63package threads::shared::av;
64use base 'threads::shared';
65
66package threads::shared::hv;
67use base 'threads::shared';
68
b050c948
AB
69bootstrap threads::shared $VERSION;
70
71__END__
72
73=head1 NAME
74
75threads::shared - Perl extension for sharing data structures between threads
76
77=head1 SYNOPSIS
78
79 use threads::shared;
80
81 my($foo, @foo, %foo);
82 share(\$foo);
83 share(\@foo);
84 share(\%hash);
85 my $bar = share([]);
86 $hash{bar} = share({});
87
88 lock(\%hash);
89 unlock(\%hash);
90 cond_wait($scalar);
91 cond_broadcast(\@array);
92 cond_signal($scalar);
93
94=head1 DESCRIPTION
95
ad91d581
JH
96This modules allows you to share() variables. These variables will
97then be shared across different threads (and pseudoforks on
98win32). They are used together with the threads module.
b050c948
AB
99
100=head2 EXPORT
101
102share(), lock(), unlock(), cond_wait, cond_signal, cond_broadcast
103
104=head1 BUGS
105
106Not stress tested!
107Does not support references
108Does not support splice on arrays!
109The exported functions need a reference due to unsufficent prototyping!
110
111=head1 AUTHOR
112
ad91d581 113Artur Bergman E<lt>artur at contiller.seE<gt>
b050c948
AB
114
115threads is released under the same license as Perl
116
117=head1 SEE ALSO
118
119L<perl> L<threads>
120
121=cut