… | |
… | |
13 | use Coro 3.3; |
13 | use Coro 3.3; |
14 | use Coro::Event; |
14 | use Coro::Event; |
15 | use Coro::Timer; |
15 | use Coro::Timer; |
16 | use Coro::Signal; |
16 | use Coro::Signal; |
17 | use Coro::Semaphore; |
17 | use Coro::Semaphore; |
|
|
18 | use Coro::AIO; |
18 | |
19 | |
|
|
20 | use Fcntl; |
19 | use IO::AIO 2.3; |
21 | use IO::AIO 2.31 (); |
20 | use YAML::Syck (); |
22 | use YAML::Syck (); |
21 | use Time::HiRes; |
23 | use Time::HiRes; |
22 | |
24 | |
23 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
25 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
24 | |
26 | |
… | |
… | |
172 | sub to_json($) { |
174 | sub to_json($) { |
173 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
175 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
174 | JSON::Syck::Dump $_[0] |
176 | JSON::Syck::Dump $_[0] |
175 | } |
177 | } |
176 | |
178 | |
|
|
179 | =item cf::sync_job { BLOCK } |
|
|
180 | |
|
|
181 | The design of crossfire+ requires that the main coro ($Coro::main) is |
|
|
182 | always able to handle events or runnable, as crossfire+ is only partly |
|
|
183 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
|
|
184 | |
|
|
185 | If it must be done, put the blocking parts into C<sync_job>. This will run |
|
|
186 | the given BLOCK in another coroutine while waiting for the result. The |
|
|
187 | server will be frozen during this time, so the block should either finish |
|
|
188 | fast or be very important. |
|
|
189 | |
|
|
190 | =cut |
|
|
191 | |
|
|
192 | sub sync_job(&) { |
|
|
193 | my ($job) = @_; |
|
|
194 | |
|
|
195 | my $busy = 1; |
|
|
196 | my @res; |
|
|
197 | |
|
|
198 | # TODO: use suspend/resume instead |
|
|
199 | local $FREEZE = 1; |
|
|
200 | |
|
|
201 | my $coro = Coro::async { |
|
|
202 | @res = eval { $job->() }; |
|
|
203 | warn $@ if $@; |
|
|
204 | undef $busy; |
|
|
205 | }; |
|
|
206 | |
|
|
207 | if ($Coro::current == $Coro::main) { |
|
|
208 | $coro->prio (Coro::PRIO_MAX); |
|
|
209 | while ($busy) { |
|
|
210 | Coro::cede_notself; |
|
|
211 | Event::one_event unless Coro::nready; |
|
|
212 | } |
|
|
213 | } else { |
|
|
214 | $coro->join; |
|
|
215 | } |
|
|
216 | |
|
|
217 | wantarray ? @res : $res[0] |
|
|
218 | } |
|
|
219 | |
177 | =item $coro = cf::coro { BLOCK } |
220 | =item $coro = cf::coro { BLOCK } |
178 | |
221 | |
179 | Creates and returns a new coro. This coro is automcatially being canceled |
222 | Creates and returns a new coro. This coro is automcatially being canceled |
180 | when the extension calling this is being unloaded. |
223 | when the extension calling this is being unloaded. |
181 | |
224 | |
… | |
… | |
563 | ); |
606 | ); |
564 | |
607 | |
565 | sub object_freezer_save { |
608 | sub object_freezer_save { |
566 | my ($filename, $rdata, $objs) = @_; |
609 | my ($filename, $rdata, $objs) = @_; |
567 | |
610 | |
|
|
611 | sync_job { |
568 | if (length $$rdata) { |
612 | if (length $$rdata) { |
569 | warn sprintf "saving %s (%d,%d)\n", |
613 | warn sprintf "saving %s (%d,%d)\n", |
570 | $filename, length $$rdata, scalar @$objs; |
614 | $filename, length $$rdata, scalar @$objs; |
571 | |
615 | |
572 | if (open my $fh, ">:raw", "$filename~") { |
616 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
573 | chmod SAVE_MODE, $fh; |
|
|
574 | syswrite $fh, $$rdata; |
|
|
575 | close $fh; |
|
|
576 | |
|
|
577 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
578 | chmod SAVE_MODE, $fh; |
617 | chmod SAVE_MODE, $fh; |
579 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
618 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
|
|
619 | aio_fsync $fh; |
580 | close $fh; |
620 | close $fh; |
|
|
621 | |
|
|
622 | if (@$objs) { |
|
|
623 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
|
|
624 | chmod SAVE_MODE, $fh; |
|
|
625 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
|
|
626 | aio_write $fh, 0, (length $data), $data, 0; |
|
|
627 | aio_fsync $fh; |
|
|
628 | close $fh; |
581 | rename "$filename.pst~", "$filename.pst"; |
629 | aio_rename "$filename.pst~", "$filename.pst"; |
|
|
630 | } |
|
|
631 | } else { |
|
|
632 | aio_unlink "$filename.pst"; |
|
|
633 | } |
|
|
634 | |
|
|
635 | aio_rename "$filename~", $filename; |
582 | } else { |
636 | } else { |
583 | unlink "$filename.pst"; |
637 | warn "FATAL: $filename~: $!\n"; |
584 | } |
638 | } |
585 | |
|
|
586 | rename "$filename~", $filename; |
|
|
587 | } else { |
639 | } else { |
588 | warn "FATAL: $filename~: $!\n"; |
|
|
589 | } |
|
|
590 | } else { |
|
|
591 | unlink $filename; |
640 | aio_unlink $filename; |
592 | unlink "$filename.pst"; |
641 | aio_unlink "$filename.pst"; |
|
|
642 | } |
593 | } |
643 | } |
594 | } |
644 | } |
595 | |
645 | |
596 | sub object_freezer_as_string { |
646 | sub object_freezer_as_string { |
597 | my ($rdata, $objs) = @_; |
647 | my ($rdata, $objs) = @_; |
… | |
… | |
602 | } |
652 | } |
603 | |
653 | |
604 | sub object_thawer_load { |
654 | sub object_thawer_load { |
605 | my ($filename) = @_; |
655 | my ($filename) = @_; |
606 | |
656 | |
607 | local $/; |
657 | my ($data, $av); |
608 | |
658 | |
609 | my $av; |
659 | (aio_load $filename, $data) >= 0 |
|
|
660 | or return; |
610 | |
661 | |
611 | #TODO: use sysread etc. |
662 | unless (aio_stat "$filename.pst") { |
612 | if (open my $data, "<:raw:perlio", $filename) { |
663 | (aio_load "$filename.pst", $av) >= 0 |
613 | $data = <$data>; |
664 | or return; |
614 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
|
|
615 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
665 | $av = eval { (Storable::thaw <$av>)->{objs} }; |
616 | } |
666 | } |
|
|
667 | |
617 | return ($data, $av); |
668 | return ($data, $av); |
618 | } |
|
|
619 | |
|
|
620 | () |
|
|
621 | } |
669 | } |
622 | |
670 | |
623 | ############################################################################# |
671 | ############################################################################# |
624 | # command handling &c |
672 | # command handling &c |
625 | |
673 | |
… | |
… | |
1234 | |
1282 | |
1235 | ############################################################################# |
1283 | ############################################################################# |
1236 | # initialisation |
1284 | # initialisation |
1237 | |
1285 | |
1238 | sub _perl_reload() { |
1286 | sub _perl_reload() { |
|
|
1287 | # can/must only be called in main |
|
|
1288 | if ($Coro::current != $Coro::main) { |
|
|
1289 | warn "can only reload from main coroutine\n"; |
|
|
1290 | return; |
|
|
1291 | } |
|
|
1292 | |
1239 | warn "reloading..."; |
1293 | warn "reloading..."; |
1240 | |
1294 | |
|
|
1295 | local $FREEZE = 1; |
|
|
1296 | cf::emergency_save; |
|
|
1297 | |
1241 | eval { |
1298 | eval { |
1242 | local $FREEZE = 1; |
1299 | # if anything goes wrong in here, we should simply crash as we already saved |
1243 | |
|
|
1244 | cf::emergency_save; |
|
|
1245 | |
1300 | |
1246 | # cancel all watchers |
1301 | # cancel all watchers |
1247 | for (Event::all_watchers) { |
1302 | for (Event::all_watchers) { |
1248 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1303 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1249 | } |
1304 | } |
… | |
… | |
1308 | |
1363 | |
1309 | # reattach attachments to objects |
1364 | # reattach attachments to objects |
1310 | warn "reattach"; |
1365 | warn "reattach"; |
1311 | _global_reattach; |
1366 | _global_reattach; |
1312 | }; |
1367 | }; |
1313 | warn $@ if $@; |
|
|
1314 | |
1368 | |
1315 | warn "reloaded"; |
1369 | if ($@) { |
|
|
1370 | warn $@; |
|
|
1371 | warn "error while reloading, exiting."; |
|
|
1372 | exit 1; |
|
|
1373 | } |
|
|
1374 | |
|
|
1375 | warn "reloaded successfully"; |
1316 | }; |
1376 | }; |
1317 | |
1377 | |
1318 | sub perl_reload() { |
1378 | sub perl_reload() { |
1319 | _perl_reload; |
1379 | _perl_reload; |
1320 | } |
1380 | } |