… | |
… | |
7 | use Coro::Storable; |
7 | use Coro::Storable; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
11 | This module implements a few functions from the Storable module in a way |
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 |
12 | so that it cede's more often. Some applications (such as the Deliantra |
13 | game server) sometimes need to load large Storable objects without |
13 | game server) sometimes need to load large Storable objects without |
14 | blocking the server for a long time. |
14 | blocking the server for a long time. |
15 | |
15 | |
16 | This is being implemented by using a perlio layer that feeds only small |
16 | This is being implemented by using a perlio layer that feeds only small |
17 | amounts of data (512 bytes per call) into Storable, and C<Coro::cede>'ing |
17 | amounts of data (4096 bytes per call) into Storable, and C<Coro::cede>'ing |
18 | regularly (at most 1000 times per second by default, though). |
18 | regularly (at most 100 times per second by default, though). |
19 | |
19 | |
20 | As it seems that Storable is not reentrant, this module also serialises |
20 | As Storable is not reentrant, this module also wraps most functions of the |
21 | calls to freeze and thaw between coroutines as necessary (for this to work |
21 | Storable module so that only one freeze or thaw is done at any one moment |
22 | reliably you always have to use this module, however). |
22 | (and recursive invocations are not currently supported). |
23 | |
23 | |
24 | =head1 FUNCTIONS |
24 | =head1 FUNCTIONS |
25 | |
25 | |
26 | =over 4 |
26 | =over 4 |
27 | |
27 | |
… | |
… | |
29 | |
29 | |
30 | Retrieve an object from the given $pst, which must have been created with |
30 | Retrieve an object from the given $pst, which must have been created with |
31 | C<Coro::Storable::freeze> or C<Storable::store_fd>/C<Storable::store> |
31 | C<Coro::Storable::freeze> or C<Storable::store_fd>/C<Storable::store> |
32 | (sorry, but Storable uses incompatible formats for disk/mem objects). |
32 | (sorry, but Storable uses incompatible formats for disk/mem objects). |
33 | |
33 | |
34 | This works by calling C<Coro::cede> for every 4096 bytes read in. |
34 | This function will cede regularly. |
35 | |
35 | |
36 | =item $pst = freeze $ref |
36 | =item $pst = freeze $ref |
37 | |
37 | |
38 | Freeze the given scalar into a Storable object. It uses the same format as |
38 | Freeze the given scalar into a Storable object. It uses the same format as |
39 | C<Storable::store_fd>. |
39 | C<Storable::store_fd>. |
40 | |
40 | |
41 | This works by calling C<Coro::cede> for every write that Storable |
41 | This functino will cede regularly. |
42 | issues. Unfortunately, Storable often makes many very small writes, so it |
|
|
43 | is rather inefficient. But it does keep the latency low. |
|
|
44 | |
42 | |
45 | =item $pst = nfreeze $ref |
43 | =item $pst = nfreeze $ref |
46 | |
44 | |
47 | Same as C<freeze> but is compatible to C<Storable::nstore_fd> (note the |
45 | Same as C<freeze> but is compatible to C<Storable::nstore_fd> (note the |
48 | C<n>). |
46 | C<n>). |
… | |
… | |
51 | |
49 | |
52 | Same as C<freeze> but is guaranteed to block. This is useful e.g. in |
50 | Same as C<freeze> but is guaranteed to block. This is useful e.g. in |
53 | C<Coro::Util::fork_eval> when you want to serialise a data structure |
51 | C<Coro::Util::fork_eval> when you want to serialise a data structure |
54 | for use with the C<thaw> function for this module. You cannot use |
52 | for use with the C<thaw> function for this module. You cannot use |
55 | C<Storable::freeze> for this as Storable uses incompatible formats for |
53 | C<Storable::freeze> for this as Storable uses incompatible formats for |
56 | memory and file images. |
54 | memory and file images, and this module uses file images. |
57 | |
55 | |
58 | =item $pst = blocking_nfreeze $ref |
56 | =item $pst = blocking_nfreeze $ref |
59 | |
57 | |
60 | Same as C<blocking_freeze> but uses C<nfreeze> internally. |
58 | Same as C<blocking_freeze> but uses C<nfreeze> internally. |
61 | |
59 | |
62 | =item $guard = guard; |
60 | =item $guard = guard |
63 | |
61 | |
64 | Acquire the Storable lock, for when you want to call Storable yourself. |
62 | Acquire the Storable lock, for when you want to call Storable yourself. |
|
|
63 | |
|
|
64 | Note that this module already wraps all Storable functions, so there is |
|
|
65 | rarely the need to do this yourself. |
65 | |
66 | |
66 | =back |
67 | =back |
67 | |
68 | |
68 | =cut |
69 | =cut |
69 | |
70 | |
70 | package Coro::Storable; |
71 | package Coro::Storable; |
71 | |
72 | |
72 | use strict; |
73 | use common::sense; |
73 | no warnings; |
|
|
74 | |
74 | |
75 | use Coro (); |
75 | use Coro (); |
76 | use Coro::Semaphore (); |
76 | use Coro::Semaphore (); |
77 | |
77 | |
78 | BEGIN { |
78 | BEGIN { |
… | |
… | |
82 | } |
82 | } |
83 | |
83 | |
84 | use Storable; |
84 | use Storable; |
85 | use base "Exporter"; |
85 | use base "Exporter"; |
86 | |
86 | |
87 | our $VERSION = 4.74; |
87 | our $VERSION = 6.07; |
88 | our @EXPORT = qw(thaw freeze nfreeze blocking_thaw blocking_freeze blocking_nfreeze); |
88 | our @EXPORT = qw(thaw freeze nfreeze blocking_thaw blocking_freeze blocking_nfreeze); |
|
|
89 | |
|
|
90 | our $GRANULARITY = 0.01; |
89 | |
91 | |
90 | my $lock = new Coro::Semaphore; |
92 | my $lock = new Coro::Semaphore; |
91 | |
93 | |
92 | sub guard { |
94 | sub guard { |
93 | $lock->guard |
95 | $lock->guard |
94 | } |
96 | } |
95 | |
97 | |
|
|
98 | # wrap xs functions |
|
|
99 | for (qw(net_pstore pstore net_mstore mstore pretrieve mretrieve dclone)) { |
|
|
100 | my $orig = \&{"Storable::$_"}; |
|
|
101 | *{"Storable::$_"} = eval 'sub (' . (prototype $orig) . ') { |
|
|
102 | my $guard = $lock->guard; |
|
|
103 | &$orig |
|
|
104 | }'; |
|
|
105 | die if $@; |
|
|
106 | } |
|
|
107 | |
96 | sub thaw($) { |
108 | sub thaw($) { |
97 | my $guard = $lock->guard; |
109 | open my $fh, "<:cede($GRANULARITY)", \$_[0] |
98 | |
|
|
99 | open my $fh, "<:via(CoroCede)", \$_[0] |
|
|
100 | or die "cannot open pst via CoroCede: $!"; |
110 | or die "cannot open pst via PerlIO::cede: $!"; |
101 | Storable::fd_retrieve $fh |
111 | Storable::fd_retrieve $fh |
102 | } |
112 | } |
103 | |
113 | |
104 | sub freeze($) { |
114 | sub freeze($) { |
105 | my $guard = $lock->guard; |
115 | open my $fh, ">:cede($GRANULARITY)", \my $buf |
|
|
116 | or die "cannot open pst via PerlIO::cede: $!"; |
|
|
117 | Storable::store_fd $_[0], $fh; |
|
|
118 | close $fh; |
106 | |
119 | |
107 | open my $fh, ">:via(CoroCede)", \my $buf |
|
|
108 | or die "cannot open pst via CoroCede: $!"; |
|
|
109 | Storable::store_fd $_[0], $fh; |
|
|
110 | $buf |
120 | $buf |
111 | } |
121 | } |
112 | |
122 | |
113 | sub nfreeze($) { |
123 | sub nfreeze($) { |
114 | my $guard = $lock->guard; |
124 | open my $fh, ">:cede($GRANULARITY)", \my $buf |
|
|
125 | or die "cannot open pst via PerlIO::cede: $!"; |
|
|
126 | Storable::nstore_fd $_[0], $fh; |
|
|
127 | close $fh; |
115 | |
128 | |
116 | open my $fh, ">:via(CoroCede)", \my $buf |
|
|
117 | or die "cannot open pst via CoroCede: $!"; |
|
|
118 | Storable::nstore_fd $_[0], $fh; |
|
|
119 | $buf |
129 | $buf |
120 | } |
130 | } |
121 | |
131 | |
122 | sub blocking_thaw($) { |
132 | sub blocking_thaw($) { |
123 | my $guard = $lock->guard; |
|
|
124 | |
|
|
125 | open my $fh, "<", \$_[0] |
133 | open my $fh, "<", \$_[0] |
126 | or die "cannot open pst: $!"; |
134 | or die "cannot open pst: $!"; |
127 | Storable::fd_retrieve $fh |
135 | Storable::fd_retrieve $fh |
128 | } |
136 | } |
129 | |
137 | |
130 | sub blocking_freeze($) { |
138 | sub blocking_freeze($) { |
131 | my $guard = $lock->guard; |
|
|
132 | |
|
|
133 | open my $fh, ">", \my $buf |
139 | open my $fh, ">", \my $buf |
134 | or die "cannot open pst: $!"; |
140 | or die "cannot open pst: $!"; |
135 | Storable::store_fd $_[0], $fh; |
141 | Storable::store_fd $_[0], $fh; |
136 | close $fh; |
142 | close $fh; |
137 | |
143 | |
138 | $buf |
144 | $buf |
139 | } |
145 | } |
140 | |
146 | |
141 | sub blocking_nfreeze($) { |
147 | sub blocking_nfreeze($) { |
142 | my $guard = $lock->guard; |
|
|
143 | |
|
|
144 | open my $fh, ">", \my $buf |
148 | open my $fh, ">", \my $buf |
145 | or die "cannot open pst: $!"; |
149 | or die "cannot open pst: $!"; |
146 | Storable::nstore_fd $_[0], $fh; |
150 | Storable::nstore_fd $_[0], $fh; |
147 | close $fh; |
151 | close $fh; |
148 | |
152 | |
149 | $buf |
153 | $buf |
150 | } |
|
|
151 | |
|
|
152 | package PerlIO::via::CoroCede; |
|
|
153 | |
|
|
154 | # generic cede-on-read/write filtering layer |
|
|
155 | |
|
|
156 | use Time::HiRes ("time"); |
|
|
157 | |
|
|
158 | our $GRANULARITY = 0.001; |
|
|
159 | |
|
|
160 | my $next_cede; |
|
|
161 | |
|
|
162 | sub PUSHED { |
|
|
163 | __PACKAGE__ |
|
|
164 | } |
|
|
165 | |
|
|
166 | sub FILL { |
|
|
167 | if ($next_cede <= time) { |
|
|
168 | $next_cede = time + $GRANULARITY; # calling time() twice usually is a net win |
|
|
169 | Coro::cede; |
|
|
170 | } |
|
|
171 | |
|
|
172 | read $_[1], my $buf, 512 |
|
|
173 | or return undef; |
|
|
174 | |
|
|
175 | $buf |
|
|
176 | } |
|
|
177 | |
|
|
178 | sub WRITE { |
|
|
179 | if ($next_cede <= (my $now = time)) { |
|
|
180 | Coro::cede; |
|
|
181 | $next_cede = $now + $GRANULARITY; |
|
|
182 | } |
|
|
183 | |
|
|
184 | (print {$_[2]} $_[1]) ? length $_[1] : -1 |
|
|
185 | } |
154 | } |
186 | |
155 | |
187 | 1; |
156 | 1; |
188 | |
157 | |
189 | =head1 AUTHOR |
158 | =head1 AUTHOR |