ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.210 by root, Mon Feb 12 01:32:49 2007 UTC vs.
Revision 1.214 by root, Wed Feb 14 00:40:05 2007 UTC

49our $LIBDIR = datadir . "/ext"; 49our $LIBDIR = datadir . "/ext";
50 50
51our $TICK = MAX_TIME * 1e-6; 51our $TICK = MAX_TIME * 1e-6;
52our $TICK_WATCHER; 52our $TICK_WATCHER;
53our $AIO_POLL_WATCHER; 53our $AIO_POLL_WATCHER;
54our $WRITE_RUNTIME_WATCHER; 54our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
55our $NEXT_TICK; 55our $NEXT_TICK;
56our $NOW; 56our $NOW;
57our $USE_FSYNC = 1; # use fsync to write maps - default off 57our $USE_FSYNC = 1; # use fsync to write maps - default off
58 58
59our $BDB_POLL_WATCHER; 59our $BDB_POLL_WATCHER;
273sub freeze_mainloop { 273sub freeze_mainloop {
274 return unless $TICK_WATCHER->is_active; 274 return unless $TICK_WATCHER->is_active;
275 275
276 my $guard = Coro::guard { 276 my $guard = Coro::guard {
277 $TICK_WATCHER->start; 277 $TICK_WATCHER->start;
278 $WRITE_RUNTIME_WATCHER->start;
279 }; 278 };
280 $WRITE_RUNTIME_WATCHER->stop;
281 $TICK_WATCHER->stop; 279 $TICK_WATCHER->stop;
282 $guard 280 $guard
283} 281}
284 282
285=item cf::async { BLOCK } 283=item cf::async { BLOCK }
2354 }; 2352 };
2355 2353
2356 warn "leave emergency perl save\n"; 2354 warn "leave emergency perl save\n";
2357} 2355}
2358 2356
2357sub post_cleanup {
2358 my ($make_core) = @_;
2359
2360 warn Carp::longmess "post_cleanup backtrace"
2361 if $make_core;
2362}
2363
2359sub reload() { 2364sub reload() {
2360 # can/must only be called in main 2365 # can/must only be called in main
2361 if ($Coro::current != $Coro::main) { 2366 if ($Coro::current != $Coro::main) {
2362 warn "can only reload from main coroutine"; 2367 warn "can only reload from main coroutine";
2363 return; 2368 return;
2364 } 2369 }
2365 2370
2366 warn "reloading..."; 2371 warn "reloading...";
2367 2372
2368 warn "cancelling server ticker"; 2373 warn "entering sync_job";
2369 $TICK_WATCHER->cancel;
2370 2374
2375 cf::sync_job {
2376 cf::write_runtime; # external watchdog should not bark
2371 cf::emergency_save; 2377 cf::emergency_save;
2378 cf::write_runtime; # external watchdog should not bark
2372 2379
2373 eval { 2380 warn "syncing database to disk";
2381 BDB::db_env_txn_checkpoint $DB_ENV;
2382
2374 # if anything goes wrong in here, we should simply crash as we already saved 2383 # if anything goes wrong in here, we should simply crash as we already saved
2375 2384
2376 warn "cancelling all WF_AUTOCANCEL watchers"; 2385 warn "cancelling all WF_AUTOCANCEL watchers";
2377 for (Event::all_watchers) { 2386 for (Event::all_watchers) {
2378 $_->cancel if $_->data & WF_AUTOCANCEL; 2387 $_->cancel if $_->data & WF_AUTOCANCEL;
2379 } 2388 }
2380
2381 warn "syncing database to disk";
2382 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
2383 2389
2384 warn "flushing outstanding aio requests"; 2390 warn "flushing outstanding aio requests";
2385 for (;;) { 2391 for (;;) {
2386 BDB::flush; 2392 BDB::flush;
2387 IO::AIO::flush; 2393 IO::AIO::flush;
2461 reattach $_ for values %MAP; 2467 reattach $_ for values %MAP;
2462 2468
2463 warn "loading reloadable resources"; 2469 warn "loading reloadable resources";
2464 load_resources; 2470 load_resources;
2465 2471
2466 warn "restarting server ticker"; 2472 warn "leaving sync_job";
2467 2473
2468 $TICK_WATCHER->start; 2474 1
2469 }; 2475 } or do {
2470
2471 if ($@) {
2472 warn $@; 2476 warn $@;
2473 warn "error while reloading, exiting."; 2477 warn "error while reloading, exiting.";
2474 exit 1; 2478 exit 1;
2475 } 2479 };
2476 2480
2477 warn "reloaded"; 2481 warn "reloaded";
2478}; 2482};
2479 2483
2480our $RELOAD_WATCHER; # used only during reload 2484our $RELOAD_WATCHER; # used only during reload
2521 2525
2522 cf::server_tick; # one server iteration 2526 cf::server_tick; # one server iteration
2523 $RUNTIME += $TICK; 2527 $RUNTIME += $TICK;
2524 $NEXT_TICK += $TICK; 2528 $NEXT_TICK += $TICK;
2525 2529
2530 if ($NOW >= $NEXT_RUNTIME_WRITE) {
2531 $NEXT_RUNTIME_WRITE = $NOW + 10;
2532 Coro::async_pool {
2533 write_runtime
2534 or warn "ERROR: unable to write runtime file: $!";
2535 };
2536 }
2537
2538
2526 $WAIT_FOR_TICK->broadcast; 2539 $WAIT_FOR_TICK->broadcast;
2527 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; 2540 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited;
2528 2541
2529 Event::sweep;
2530 Coro::cede_notself;
2531
2532# my $AFTER = Event::time; 2542# my $AFTER = Event::time;
2533# warn $AFTER - $NOW;#d# 2543# warn $AFTER - $NOW;#d#
2534 2544
2535 # if we are delayed by four ticks or more, skip them all 2545 # if we are delayed by four ticks or more, skip them all
2536 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2546 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2595 2605
2596 undef $Coro::AIO::WATCHER; 2606 undef $Coro::AIO::WATCHER;
2597 IO::AIO::max_poll_time $TICK * 0.1; 2607 IO::AIO::max_poll_time $TICK * 0.1;
2598 $AIO_POLL_WATCHER = Event->io ( 2608 $AIO_POLL_WATCHER = Event->io (
2599 reentrant => 0, 2609 reentrant => 0,
2610 data => WF_AUTOCANCEL,
2600 fd => IO::AIO::poll_fileno, 2611 fd => IO::AIO::poll_fileno,
2601 poll => 'r', 2612 poll => 'r',
2602 prio => 6, 2613 prio => 6,
2603 data => WF_AUTOCANCEL,
2604 cb => \&IO::AIO::poll_cb, 2614 cb => \&IO::AIO::poll_cb,
2605 ); 2615 );
2606} 2616}
2607 2617
2608$WRITE_RUNTIME_WATCHER = Event->timer (
2609 reentrant => 0,
2610 data => WF_AUTOCANCEL,
2611 after => 1,
2612 interval => 10,
2613 prio => 6, # keep it lowest so it acts like a watchdog
2614 cb => Coro::unblock_sub {
2615 write_runtime
2616 or warn "ERROR: unable to write runtime file: $!";
2617 },
2618);
2619
2620END { cf::emergency_save } 2618END { cf::emergency_save }
2621 2619
26221 26201
2623 2621

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines