1 package threads::shared;
6 use Scalar::Util qw(weaken);
7 use attributes qw(reftype);
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;
18 *share = \&share_enabled;
24 our @ISA = qw(Exporter DynaLoader);
26 our @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock lock);
27 our $VERSION = '0.01';
31 sub cond_wait_disabled { return @_ };
32 sub cond_signal_disabled { return @_};
33 sub cond_broadcast_disabled { return @_};
34 sub unlock_disabled { 1 };
35 sub lock_disabled { 1 }
36 sub share_disabled { return @_}
38 sub share_enabled (\[$@%]) { # \]
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});
47 die "You cannot share ref of type $_[0]\n";
52 return unless($_[0] eq "threads::shared");
53 foreach my $ptr (keys %shared) {
55 thrcnt_inc($shared{$ptr});
60 package threads::shared::sv;
61 use base 'threads::shared';
63 package threads::shared::av;
64 use base 'threads::shared';
66 package threads::shared::hv;
67 use base 'threads::shared';
69 bootstrap threads::shared $VERSION;
75 threads::shared - Perl extension for sharing data structures between threads
86 $hash{bar} = share({});
91 cond_broadcast(\@array);
96 This modules allows you to share() variables. These variables will
97 then be shared across different threads (and pseudoforks on
98 win32). They are used together with the threads module.
102 share(), lock(), unlock(), cond_wait, cond_signal, cond_broadcast
107 Does not support references
108 Does not support splice on arrays!
109 The exported functions need a reference due to unsufficent prototyping!
113 Artur Bergman E<lt>artur at contiller.seE<gt>
115 threads is released under the same license as Perl