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.211 by root, Tue Feb 13 16:23:32 2007 UTC vs.
Revision 1.215 by root, Thu Feb 15 03:19:02 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 }
356 354
357 $coro 355 $coro
358} 356}
359 357
360sub write_runtime { 358sub write_runtime {
361 $TICK_WATCHER->is_active or cf::cleanup "mainloop frozen but runtime active", 1;
362
363 my $runtime = cf::localdir . "/runtime"; 359 my $runtime = cf::localdir . "/runtime";
364 360
365 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 361 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644
366 or return; 362 or return;
367 363
1822 $map->load; 1818 $map->load;
1823 $map->load_diag; 1819 $map->load_diag;
1824 1820
1825 return unless $self->contr->active; 1821 return unless $self->contr->active;
1826 $self->activate_recursive; 1822 $self->activate_recursive;
1823
1824 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
1827 $self->enter_map ($map, $x, $y); 1825 $self->enter_map ($map, $x, $y);
1828} 1826}
1829 1827
1830cf::player->attach ( 1828cf::player->attach (
1831 on_logout => sub { 1829 on_logout => sub {
1844 # try to abort aborted map switching on player login :) 1842 # try to abort aborted map switching on player login :)
1845 # should happen only on crashes 1843 # should happen only on crashes
1846 if ($pl->ob->{_link_pos}) { 1844 if ($pl->ob->{_link_pos}) {
1847 $pl->ob->enter_link; 1845 $pl->ob->enter_link;
1848 (async { 1846 (async {
1849 # we need this sleep as the login has a concurrent enter_exit running
1850 # and this sleep increases chances of the player not ending up in scorn
1851 $pl->ob->reply (undef, 1847 $pl->ob->reply (undef,
1852 "There was an internal problem at your last logout, " 1848 "There was an internal problem at your last logout, "
1853 . "the server will try to bring you to your intended destination in a second.", 1849 . "the server will try to bring you to your intended destination in a second.",
1854 cf::NDI_RED); 1850 cf::NDI_RED);
1851 # we need this sleep as the login has a concurrent enter_exit running
1852 # and this sleep increases chances of the player not ending up in scorn
1855 Coro::Timer::sleep 1; 1853 Coro::Timer::sleep 1;
1856 $pl->ob->leave_link; 1854 $pl->ob->leave_link;
1857 })->prio (2); 1855 })->prio (2);
1858 } 1856 }
1859 }, 1857 },
2372 return; 2370 return;
2373 } 2371 }
2374 2372
2375 warn "reloading..."; 2373 warn "reloading...";
2376 2374
2377 warn "cancelling server ticker"; 2375 warn "entering sync_job";
2378 $TICK_WATCHER->cancel;
2379 2376
2377 cf::sync_job {
2378 cf::write_runtime; # external watchdog should not bark
2380 cf::emergency_save; 2379 cf::emergency_save;
2380 cf::write_runtime; # external watchdog should not bark
2381 2381
2382 eval { 2382 warn "syncing database to disk";
2383 BDB::db_env_txn_checkpoint $DB_ENV;
2384
2383 # if anything goes wrong in here, we should simply crash as we already saved 2385 # if anything goes wrong in here, we should simply crash as we already saved
2384 2386
2385 warn "cancelling all WF_AUTOCANCEL watchers"; 2387 warn "cancelling all WF_AUTOCANCEL watchers";
2386 for (Event::all_watchers) { 2388 for (Event::all_watchers) {
2387 $_->cancel if $_->data & WF_AUTOCANCEL; 2389 $_->cancel if $_->data & WF_AUTOCANCEL;
2388 } 2390 }
2389
2390 warn "syncing database to disk";
2391 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
2392 2391
2393 warn "flushing outstanding aio requests"; 2392 warn "flushing outstanding aio requests";
2394 for (;;) { 2393 for (;;) {
2395 BDB::flush; 2394 BDB::flush;
2396 IO::AIO::flush; 2395 IO::AIO::flush;
2470 reattach $_ for values %MAP; 2469 reattach $_ for values %MAP;
2471 2470
2472 warn "loading reloadable resources"; 2471 warn "loading reloadable resources";
2473 load_resources; 2472 load_resources;
2474 2473
2475 warn "restarting server ticker"; 2474 warn "leaving sync_job";
2476 2475
2477 $TICK_WATCHER->start; 2476 1
2478 }; 2477 } or do {
2479
2480 if ($@) {
2481 warn $@; 2478 warn $@;
2482 warn "error while reloading, exiting."; 2479 warn "error while reloading, exiting.";
2483 exit 1; 2480 exit 1;
2484 } 2481 };
2485 2482
2486 warn "reloaded"; 2483 warn "reloaded";
2487}; 2484};
2488 2485
2489our $RELOAD_WATCHER; # used only during reload 2486our $RELOAD_WATCHER; # used only during reload
2530 2527
2531 cf::server_tick; # one server iteration 2528 cf::server_tick; # one server iteration
2532 $RUNTIME += $TICK; 2529 $RUNTIME += $TICK;
2533 $NEXT_TICK += $TICK; 2530 $NEXT_TICK += $TICK;
2534 2531
2532 if ($NOW >= $NEXT_RUNTIME_WRITE) {
2533 $NEXT_RUNTIME_WRITE = $NOW + 10;
2534 Coro::async_pool {
2535 write_runtime
2536 or warn "ERROR: unable to write runtime file: $!";
2537 };
2538 }
2539
2540
2535 $WAIT_FOR_TICK->broadcast; 2541 $WAIT_FOR_TICK->broadcast;
2536 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; 2542 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited;
2537 2543
2538 Event::sweep;
2539 Coro::cede_notself;
2540
2541# my $AFTER = Event::time; 2544# my $AFTER = Event::time;
2542# warn $AFTER - $NOW;#d# 2545# warn $AFTER - $NOW;#d#
2543 2546
2544 # if we are delayed by four ticks or more, skip them all 2547 # if we are delayed by four ticks or more, skip them all
2545 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2548 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2604 2607
2605 undef $Coro::AIO::WATCHER; 2608 undef $Coro::AIO::WATCHER;
2606 IO::AIO::max_poll_time $TICK * 0.1; 2609 IO::AIO::max_poll_time $TICK * 0.1;
2607 $AIO_POLL_WATCHER = Event->io ( 2610 $AIO_POLL_WATCHER = Event->io (
2608 reentrant => 0, 2611 reentrant => 0,
2612 data => WF_AUTOCANCEL,
2609 fd => IO::AIO::poll_fileno, 2613 fd => IO::AIO::poll_fileno,
2610 poll => 'r', 2614 poll => 'r',
2611 prio => 6, 2615 prio => 6,
2612 data => WF_AUTOCANCEL,
2613 cb => \&IO::AIO::poll_cb, 2616 cb => \&IO::AIO::poll_cb,
2614 ); 2617 );
2615} 2618}
2616 2619
2617$WRITE_RUNTIME_WATCHER = Event->timer (
2618 reentrant => 0,
2619 data => WF_AUTOCANCEL,
2620 after => 1,
2621 interval => 10,
2622 prio => 6, # keep it lowest so it acts like a watchdog
2623 cb => Coro::unblock_sub {
2624 write_runtime
2625 or warn "ERROR: unable to write runtime file: $!";
2626 },
2627);
2628
2629END { cf::emergency_save } 2620END { cf::emergency_save }
2630 2621
26311 26221
2632 2623

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines