1 |
=head1 NAME |
2 |
|
3 |
Coro::Storable - offer a more fine-grained Storable interface |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro::Storable; |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
This module implements a few functions from the Storable module in a way |
12 |
so that it cede's more often. Some applications (such as the Crossfire |
13 |
game server) sometimes need to load large Storable objects without |
14 |
blocking the server for a long time. |
15 |
|
16 |
=head1 FUNCTIONS |
17 |
|
18 |
=over 4 |
19 |
|
20 |
=item $ref = thaw $pst |
21 |
|
22 |
Retrieve an object from the given $pst, which must have been created with |
23 |
C<Coro::Storable::freeze> or C<Storable::store_fd>/C<Storable::store> |
24 |
(sorry, but Storable uses incompatible formats for disk/mem objects). |
25 |
|
26 |
This works by calling C<Coro::cede> for every 512 bytes read in. |
27 |
|
28 |
=item $pst = freeze $ref |
29 |
|
30 |
Freeze the given scalar into a Storable object. It uses the same format as |
31 |
C<Storable::nstore_fd> (note the C<n>). |
32 |
|
33 |
This works by calling C<Coro::cede> for every write that Storable |
34 |
issues. Unfortunately, Storable often makes many very small writes, so it |
35 |
is rather inefficient. But it does keep the latency low. |
36 |
|
37 |
=back |
38 |
|
39 |
=cut |
40 |
|
41 |
package Coro::Storable; |
42 |
|
43 |
use strict; |
44 |
|
45 |
use Coro (); |
46 |
|
47 |
use Storable; |
48 |
use base "Exporter"; |
49 |
|
50 |
our $VERSION = '0.1'; |
51 |
our @EXPORT = qw(freeze thaw); |
52 |
|
53 |
sub freeze($) { |
54 |
open my $fh, ">:via(CoroCede)", \my $buf |
55 |
or die "cannot open pst via CoroCede: $!"; |
56 |
Storable::nstore_fd $_[0], $fh; |
57 |
$buf |
58 |
} |
59 |
|
60 |
sub thaw($) { |
61 |
open my $fh, "<:via(CoroCede)", \$_[0] |
62 |
or die "cannot open pst via CoroCede: $!"; |
63 |
Storable::fd_retrieve $fh |
64 |
} |
65 |
|
66 |
package PerlIO::via::CoroCede; |
67 |
|
68 |
# generic cede-on-read/write filtering layer |
69 |
|
70 |
sub PUSHED { |
71 |
__PACKAGE__ |
72 |
} |
73 |
|
74 |
sub FILL { |
75 |
Coro::cede; |
76 |
read $_[1], my $buf, 512; |
77 |
$buf |
78 |
} |
79 |
|
80 |
sub WRITE { |
81 |
Coro::cede; |
82 |
(print {$_[2]} $_[1]) ? length $_[1] : -1 |
83 |
} |
84 |
|
85 |
1; |
86 |
|
87 |
=head1 AUTHOR |
88 |
|
89 |
Marc Lehmann <schmorp@schmorp.de> |
90 |
http://home.schmorp.de/ |
91 |
|
92 |
=cut |
93 |
|
94 |
|