ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Storable.pm
(Generate patch)

Comparing Coro/Coro/Storable.pm (file contents):
Revision 1.7 by root, Wed Oct 3 18:37:48 2007 UTC vs.
Revision 1.19 by root, Wed Jul 23 22:15:25 2008 UTC

57 57
58=item $pst = blocking_nfreeze $ref 58=item $pst = blocking_nfreeze $ref
59 59
60Same as C<blocking_freeze> but uses C<nfreeze> internally. 60Same as C<blocking_freeze> but uses C<nfreeze> internally.
61 61
62=item $guard = guard;
63
64Acquire the Storable lock, for when you want to call Storable yourself.
65
62=back 66=back
63 67
64=cut 68=cut
65 69
66package Coro::Storable; 70package Coro::Storable;
67 71
68use strict; 72use strict;
73no warnings;
69 74
70use Coro (); 75use Coro ();
71use Coro::Semaphore (); 76use Coro::Semaphore ();
72 77
78BEGIN {
79 # suppress warnings
80 local $^W = 0;
81 require Storable;
82}
83
73use Storable; 84use Storable;
74use base "Exporter"; 85use base "Exporter";
75 86
76our $VERSION = '0.2'; 87our $VERSION = 4.745;
77our @EXPORT = qw(thaw freeze nfreeze blocking_freeze blocking_nfreeze); 88our @EXPORT = qw(thaw freeze nfreeze blocking_thaw blocking_freeze blocking_nfreeze);
78 89
79my $lock = new Coro::Semaphore; 90my $lock = new Coro::Semaphore;
91
92sub guard {
93 $lock->guard
94}
80 95
81sub thaw($) { 96sub 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
122sub 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
107sub blocking_freeze { 130sub 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
116sub blocking_nfreeze { 141sub 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
139sub FILL { 166sub 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines