1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
root |
1.34 |
Coro::Specific - manage coroutine-specific variables. |
4 |
root |
1.1 |
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use Coro::Specific; |
8 |
|
|
|
9 |
|
|
my $ref = new Coro::Specific; |
10 |
|
|
|
11 |
|
|
$$ref = 5; |
12 |
|
|
print $$ref; |
13 |
|
|
|
14 |
|
|
=head1 DESCRIPTION |
15 |
|
|
|
16 |
|
|
This module can be used to create variables (or better: references to |
17 |
|
|
them) that are specific to the currently executing coroutine. This module |
18 |
|
|
does not automatically load the Coro module (so the overhead will be small |
19 |
|
|
when no coroutines are used). |
20 |
|
|
|
21 |
root |
1.84 |
A much faster method is to store extra keys into C<%$Coro::current> |
22 |
|
|
- all you have to do is to make sure that the key is unique (e.g. by |
23 |
|
|
prefixing it with your module name). You can even store data there before |
24 |
|
|
loading the L<Coro> module - when Coro is loaded, the keys stored in |
25 |
|
|
C<%$Coro::current> are automatically attached to the coro thread executing |
26 |
|
|
the main program. |
27 |
|
|
|
28 |
|
|
You don't have to load C<Coro::Specific> manually, it will be loaded |
29 |
|
|
automatically when you C<use Coro> and call the C<new> constructor. |
30 |
|
|
|
31 |
root |
1.1 |
=over 4 |
32 |
|
|
|
33 |
|
|
=cut |
34 |
|
|
|
35 |
|
|
package Coro::Specific; |
36 |
|
|
|
37 |
root |
1.77 |
use common::sense; |
38 |
root |
1.1 |
|
39 |
root |
1.109 |
our $VERSION = 6.41; |
40 |
root |
1.1 |
|
41 |
|
|
=item new |
42 |
|
|
|
43 |
root |
1.8 |
Create a new coroutine-specific scalar and return a reference to it. The |
44 |
|
|
scalar is guarenteed to be "undef". Once such a scalar has been allocated |
45 |
|
|
you cannot deallocate it (yet), so allocate only when you must. |
46 |
root |
1.1 |
|
47 |
|
|
=cut |
48 |
|
|
|
49 |
|
|
my $idx; |
50 |
|
|
|
51 |
|
|
sub new { |
52 |
|
|
my $var; |
53 |
|
|
tie $var, Coro::Specific::; |
54 |
|
|
\$var; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
sub TIESCALAR { |
58 |
root |
1.8 |
my $idx = $idx++; |
59 |
root |
1.1 |
bless \$idx, $_[0]; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
sub FETCH { |
63 |
root |
1.43 |
$Coro::current->{_specific}[${$_[0]}]; |
64 |
root |
1.1 |
} |
65 |
|
|
|
66 |
|
|
sub STORE { |
67 |
root |
1.43 |
$Coro::current->{_specific}[${$_[0]}] = $_[1]; |
68 |
root |
1.1 |
} |
69 |
|
|
|
70 |
root |
1.8 |
#sub DESTROY { |
71 |
|
|
# push @idx, $$_[0]; |
72 |
|
|
#} |
73 |
root |
1.1 |
|
74 |
|
|
1; |
75 |
|
|
|
76 |
|
|
=back |
77 |
|
|
|
78 |
|
|
=head1 BUGS |
79 |
|
|
|
80 |
|
|
The actual coroutine specific values do not automatically get destroyed |
81 |
|
|
when the Coro::Specific object gets destroyed. |
82 |
|
|
|
83 |
|
|
=head1 AUTHOR |
84 |
|
|
|
85 |
root |
1.33 |
Marc Lehmann <schmorp@schmorp.de> |
86 |
root |
1.31 |
http://home.schmorp.de/ |
87 |
root |
1.1 |
|
88 |
|
|
=cut |
89 |
|
|
|