… | |
… | |
57 | |
57 | |
58 | =item $pst = blocking_nfreeze $ref |
58 | =item $pst = blocking_nfreeze $ref |
59 | |
59 | |
60 | Same as C<blocking_freeze> but uses C<nfreeze> internally. |
60 | Same as C<blocking_freeze> but uses C<nfreeze> internally. |
61 | |
61 | |
|
|
62 | =item $guard = guard; |
|
|
63 | |
|
|
64 | Acquire the Storable lock, for when you want to call Storable yourself. |
|
|
65 | |
62 | =back |
66 | =back |
63 | |
67 | |
64 | =cut |
68 | =cut |
65 | |
69 | |
66 | package Coro::Storable; |
70 | package Coro::Storable; |
67 | |
71 | |
68 | use strict; |
72 | use strict; |
|
|
73 | no warnings; |
69 | |
74 | |
70 | use Coro (); |
75 | use Coro (); |
71 | use Coro::Semaphore (); |
76 | use Coro::Semaphore (); |
72 | |
77 | |
|
|
78 | BEGIN { |
|
|
79 | # suppress warnings |
|
|
80 | local $^W = 0; |
|
|
81 | require Storable; |
|
|
82 | } |
|
|
83 | |
73 | use Storable; |
84 | use Storable; |
74 | use base "Exporter"; |
85 | use base "Exporter"; |
75 | |
86 | |
76 | our $VERSION = '0.2'; |
87 | our $VERSION = 4.745; |
77 | our @EXPORT = qw(thaw freeze nfreeze blocking_freeze blocking_nfreeze); |
88 | our @EXPORT = qw(thaw freeze nfreeze blocking_thaw blocking_freeze blocking_nfreeze); |
78 | |
89 | |
79 | my $lock = new Coro::Semaphore; |
90 | my $lock = new Coro::Semaphore; |
|
|
91 | |
|
|
92 | sub guard { |
|
|
93 | $lock->guard |
|
|
94 | } |
80 | |
95 | |
81 | sub thaw($) { |
96 | sub thaw($) { |
82 | my $guard = $lock->guard; |
97 | my $guard = $lock->guard; |
83 | |
98 | |
84 | open my $fh, "<:via(CoroCede)", \$_[0] |
99 | open my $fh, "<:via(CoroCede)", \$_[0] |
… | |
… | |
102 | or die "cannot open pst via CoroCede: $!"; |
117 | or die "cannot open pst via CoroCede: $!"; |
103 | Storable::nstore_fd $_[0], $fh; |
118 | Storable::nstore_fd $_[0], $fh; |
104 | $buf |
119 | $buf |
105 | } |
120 | } |
106 | |
121 | |
|
|
122 | sub blocking_thaw($) { |
|
|
123 | my $guard = $lock->guard; |
|
|
124 | |
|
|
125 | open my $fh, "<", \$_[0] |
|
|
126 | or die "cannot open pst: $!"; |
|
|
127 | Storable::fd_retrieve $fh |
|
|
128 | } |
|
|
129 | |
107 | sub blocking_freeze { |
130 | sub blocking_freeze($) { |
|
|
131 | my $guard = $lock->guard; |
|
|
132 | |
108 | open my $fh, ">", \my $buf |
133 | open my $fh, ">", \my $buf |
109 | or die "cannot open pst: $!"; |
134 | or die "cannot open pst: $!"; |
110 | Storable::store_fd $_[0], $fh; |
135 | Storable::store_fd $_[0], $fh; |
111 | close $fh; |
136 | close $fh; |
112 | |
137 | |
113 | $buf |
138 | $buf |
114 | } |
139 | } |
115 | |
140 | |
116 | sub blocking_nfreeze { |
141 | sub blocking_nfreeze($) { |
|
|
142 | my $guard = $lock->guard; |
|
|
143 | |
117 | open my $fh, ">", \my $buf |
144 | open my $fh, ">", \my $buf |
118 | or die "cannot open pst: $!"; |
145 | or die "cannot open pst: $!"; |
119 | Storable::nstore_fd $_[0], $fh; |
146 | Storable::nstore_fd $_[0], $fh; |
120 | close $fh; |
147 | close $fh; |
121 | |
148 | |
… | |
… | |
137 | } |
164 | } |
138 | |
165 | |
139 | sub FILL { |
166 | sub FILL { |
140 | if ($next_cede <= time) { |
167 | if ($next_cede <= time) { |
141 | $next_cede = time + $GRANULARITY; # calling time() twice usually is a net win |
168 | $next_cede = time + $GRANULARITY; # calling time() twice usually is a net win |
142 | Coro::cede; |
169 | Coro::cede (); |
143 | } |
170 | } |
144 | |
171 | |
145 | read $_[1], my $buf, 512 |
172 | read $_[1], my $buf, 512 |
146 | or return undef; |
173 | or return undef; |
147 | |
174 | |