2 package threads::shared;
7 use Scalar::Util qw(weaken);
8 use attributes qw(reftype);
11 if($Config{'useithreads'} && $Config::threads) {
12 *share = \&share_enabled;
13 *cond_wait = \&cond_wait_disabled;
14 *cond_signal = \&cond_signal_disabled;
15 *cond_broadcast = \&cond_broadcast_disabled;
16 *unlock = \&unlock_disabled;
17 *lock = \&lock_disabled;
19 *share = \&share_enabled;
25 our @ISA = qw(Exporter DynaLoader);
27 our @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock lock);
28 our $VERSION = '0.01';
33 sub cond_wait_disabled { return @_ };
34 sub cond_signal_disabled { return @_};
35 sub cond_broadcast_disabled { return @_};
36 sub unlock_disabled { 1 };
37 sub lock_disabled { 1 }
38 sub share_disabled { return @_}
40 sub share_enabled (\[$@%]) { # \]
42 my $ref = reftype($value);
43 if($ref eq 'SCALAR') {
44 my $obj = \threads::shared::sv->new($$value);
45 bless $obj, 'threads::shared::sv';
46 $shared{$$obj} = $value;
47 weaken($shared{$$obj});
49 die "You cannot share ref of type $_[0]\n";
54 return unless($_[0] eq "threads::shared");
55 foreach my $ptr (keys %shared) {
57 thrcnt_inc($shared{$ptr});
63 package threads::shared::sv;
64 use base 'threads::shared';
66 package threads::shared::av;
67 use base 'threads::shared';
69 package threads::shared::hv;
70 use base 'threads::shared';
73 bootstrap threads::shared $VERSION;
79 threads::shared - Perl extension for sharing data structures between threads
90 $hash{bar} = share({});
95 cond_broadcast(\@array);
100 This modules allows you to share() variables. These variables will then be shared across different threads (and pseudoforks on win32). They are used together with the threads module.
104 share(), lock(), unlock(), cond_wait, cond_signal, cond_broadcast
109 Does not support references
110 Does not support splice on arrays!
111 The exported functions need a reference due to unsufficent prototyping!
115 Artur Bergman <lt>artur at contiller.se<gt>
117 threads is released under the same license as Perl