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.183 by root, Fri Jan 19 19:42:37 2007 UTC vs.
Revision 1.190 by root, Mon Jan 22 03:44:41 2007 UTC

349 my $runtime = cf::localdir . "/runtime"; 349 my $runtime = cf::localdir . "/runtime";
350 350
351 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 351 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644
352 or return; 352 or return;
353 353
354 my $value = $cf::RUNTIME + 90 + 10;
354 my $value = $cf::RUNTIME + 1 + 10; # 10 is the runtime save interval, for a monotonic clock 355 # 10 is the runtime save interval, for a monotonic clock
356 # 60 allows for the watchdog to kill the server.
357
355 (aio_write $fh, 0, (length $value), $value, 0) <= 0 358 (aio_write $fh, 0, (length $value), $value, 0) <= 0
356 and return; 359 and return;
357 360
358 aio_fsync $fh 361 aio_fsync $fh
359 and return; 362 and return;
1137 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, 1140 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
1138 (cf::region::find $rmp->{region}), $rmp->{custom} 1141 (cf::region::find $rmp->{region}), $rmp->{custom}
1139 ) 1142 )
1140} 1143}
1141 1144
1145=item cf::map->register ($regex, $prio)
1146
1147Register a handler for the map path matching the given regex at the
1148givne priority (higher is better, built-in handlers have priority 0, the
1149default).
1150
1151=cut
1152
1142sub register { 1153sub register {
1143 my (undef, $regex) = @_; 1154 my (undef, $regex, $prio) = @_;
1144 my $pkg = caller; 1155 my $pkg = caller;
1145 1156
1146 no strict; 1157 no strict;
1147 push @{"$pkg\::ISA"}, __PACKAGE__; 1158 push @{"$pkg\::ISA"}, __PACKAGE__;
1148 1159
1149 $EXT_MAP{$pkg} = qr<$regex>; 1160 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1150} 1161}
1151 1162
1152# also paths starting with '/' 1163# also paths starting with '/'
1153$EXT_MAP{"cf::map"} = qr{^(?=/)}; 1164$EXT_MAP{"cf::map"} = [0, qr{^(?=/)}];
1154 1165
1155sub thawer_merge { 1166sub thawer_merge {
1156 my ($self, $merge) = @_; 1167 my ($self, $merge) = @_;
1157 1168
1158 # we have to keep some variables in memory intact 1169 # we have to keep some variables in memory intact
1201 1212
1202 return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object 1213 return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object
1203 1214
1204 $path = normalise $path, $base; 1215 $path = normalise $path, $base;
1205 1216
1206 for my $pkg (keys %EXT_MAP) { 1217 for my $pkg (sort { $EXT_MAP{$b}[0] <=> $EXT_MAP{$a}[0] } keys %EXT_MAP) {
1207 if ($path =~ $EXT_MAP{$pkg}) { 1218 if ($path =~ $EXT_MAP{$pkg}[1]) {
1208 my $self = bless cf::map::new, $pkg; 1219 my $self = bless cf::map::new, $pkg;
1209 $self->{path} = $path; $self->path ($path); 1220 $self->{path} = $path; $self->path ($path);
1210 $self->init; # pass $1 etc. 1221 $self->init; # pass $1 etc.
1211 return $self; 1222 return $self;
1212 } 1223 }
1286 $self->{load_path} = $path; 1297 $self->{load_path} = $path;
1287 1298
1288 1 1299 1
1289} 1300}
1290 1301
1291sub load_orig { 1302sub load_header_orig {
1292 my ($self) = @_; 1303 my ($self) = @_;
1293 1304
1294 $self->load_header_from ($self->load_path) 1305 $self->load_header_from ($self->load_path)
1295} 1306}
1296 1307
1297sub load_temp { 1308sub load_header_temp {
1298 my ($self) = @_; 1309 my ($self) = @_;
1299 1310
1300 $self->load_header_from ($self->save_path) 1311 $self->load_header_from ($self->save_path)
1312}
1313
1314sub prepare_temp {
1315 my ($self) = @_;
1316
1317 $self->last_access ((delete $self->{last_access})
1318 || $cf::RUNTIME); #d#
1319 # safety
1320 $self->{instantiate_time} = $cf::RUNTIME
1321 if $self->{instantiate_time} > $cf::RUNTIME;
1322}
1323
1324sub prepare_orig {
1325 my ($self) = @_;
1326
1327 $self->{load_original} = 1;
1328 $self->{instantiate_time} = $cf::RUNTIME;
1329 $self->last_access ($cf::RUNTIME);
1330 $self->instantiate;
1301} 1331}
1302 1332
1303sub load_header { 1333sub load_header {
1304 my ($self) = @_; 1334 my ($self) = @_;
1305 1335
1306 if ($self->load_temp) { 1336 if ($self->load_header_temp) {
1307 $self->last_access ((delete $self->{last_access}) 1337 $self->prepare_temp;
1308 || $cf::RUNTIME); #d#
1309 # safety
1310 $self->{instantiate_time} = $cf::RUNTIME
1311 if $self->{instantiate_time} > $cf::RUNTIME;
1312 } else { 1338 } else {
1313 $self->load_orig 1339 $self->load_header_orig
1314 or return; 1340 or return;
1315 1341 $self->prepare_orig;
1316 $self->{load_original} = 1;
1317 $self->{instantiate_time} = $cf::RUNTIME;
1318 $self->last_access ($cf::RUNTIME);
1319 $self->instantiate;
1320 } 1342 }
1321 1343
1322 1 1344 1
1323} 1345}
1324 1346
1339 1361
1340 $map->load_header 1362 $map->load_header
1341 or return; 1363 or return;
1342 1364
1343 if ($map->should_reset) { 1365 if ($map->should_reset) {
1366 # doing this can freeze the server in a sync job, obviously
1344 $cf::WAIT_FOR_TICK->wait; 1367 #$cf::WAIT_FOR_TICK->wait;
1345 $map->reset; 1368 $map->reset;
1346 undef $guard; 1369 undef $guard;
1347 $map = find $path 1370 $map = find $path
1348 or return; 1371 or return;
1349 } 1372 }
1350 1373
1351 $cf::MAP{$path} = $map 1374 $cf::MAP{$path} = $map
1352 } 1375 }
1353} 1376}
1354 1377
1378sub pre_load { }
1379sub post_load { }
1380
1355sub load { 1381sub load {
1356 my ($self) = @_; 1382 my ($self) = @_;
1357 1383
1358 my $path = $self->{path}; 1384 my $path = $self->{path};
1359 my $guard = cf::lock_acquire "map_load:$path"; 1385 my $guard = cf::lock_acquire "map_load:$path";
1361 return if $self->in_memory != cf::MAP_SWAPPED; 1387 return if $self->in_memory != cf::MAP_SWAPPED;
1362 1388
1363 $self->in_memory (cf::MAP_LOADING); 1389 $self->in_memory (cf::MAP_LOADING);
1364 1390
1365 $self->alloc; 1391 $self->alloc;
1392
1393 $self->pre_load;
1394
1366 $self->_load_objects ($self->{load_path}, 1) 1395 $self->_load_objects ($self->{load_path}, 1)
1367 or return; 1396 or return;
1368 1397
1369 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) 1398 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1)
1370 if delete $self->{load_original}; 1399 if delete $self->{load_original};
1392 unless $self->difficulty; 1421 unless $self->difficulty;
1393 Coro::cede; 1422 Coro::cede;
1394 $self->activate; 1423 $self->activate;
1395 } 1424 }
1396 1425
1426 $self->post_load;
1427
1397 $self->in_memory (cf::MAP_IN_MEMORY); 1428 $self->in_memory (cf::MAP_IN_MEMORY);
1398} 1429}
1399 1430
1400sub customise_for { 1431sub customise_for {
1401 my ($self, $ob) = @_; 1432 my ($self, $ob) = @_;
1456 1487
1457 undef $MAP_PREFETCH{$path}; 1488 undef $MAP_PREFETCH{$path};
1458 $MAP_PREFETCHER ||= cf::async { 1489 $MAP_PREFETCHER ||= cf::async {
1459 while (%MAP_PREFETCH) { 1490 while (%MAP_PREFETCH) {
1460 for my $path (keys %MAP_PREFETCH) { 1491 for my $path (keys %MAP_PREFETCH) {
1461 warn "prefetching $path...\n";#d#
1462 my $map = find $path 1492 my $map = find $path
1463 or next; 1493 or next;
1464 $map->load; 1494 $map->load;
1465 1495
1466 delete $MAP_PREFETCH{$path}; 1496 delete $MAP_PREFETCH{$path};
1467 } 1497 }
1468 } 1498 }
1469 undef $MAP_PREFETCHER; 1499 undef $MAP_PREFETCHER;
1470 }; 1500 };
1501 $MAP_PREFETCHER->prio (6);
1471 1502
1472 () 1503 ()
1473} 1504}
1474 1505
1475sub save { 1506sub save {
2175 sub db_sync() { 2206 sub db_sync() {
2176 db_save if $dirty; 2207 db_save if $dirty;
2177 undef $dirty; 2208 undef $dirty;
2178 } 2209 }
2179 2210
2180 my $idle = Event->idle (min => 10, max => 20, repeat => 0, data => WF_AUTOCANCEL, cb => sub { 2211 my $idle = Event->idle (
2181 db_sync; 2212 reentrant => 0,
2213 min => 10,
2214 max => 20,
2215 repeat => 0,
2216 data => WF_AUTOCANCEL,
2217 cb => \&db_sync,
2182 }); 2218 );
2183 2219
2184 sub db_dirty() { 2220 sub db_dirty() {
2185 $dirty = 1; 2221 $dirty = 1;
2186 $idle->start; 2222 $idle->start;
2187 } 2223 }
2255 2291
2256# install some emergency cleanup handlers 2292# install some emergency cleanup handlers
2257BEGIN { 2293BEGIN {
2258 for my $signal (qw(INT HUP TERM)) { 2294 for my $signal (qw(INT HUP TERM)) {
2259 Event->signal ( 2295 Event->signal (
2296 reentrant => 0,
2260 data => WF_AUTOCANCEL, 2297 data => WF_AUTOCANCEL,
2261 signal => $signal, 2298 signal => $signal,
2262 cb => sub { 2299 cb => sub {
2263 cf::cleanup "SIG$signal"; 2300 cf::cleanup "SIG$signal";
2264 }, 2301 },
2265 ); 2302 );
2266 } 2303 }
2267} 2304}
2422 $who->message ("reloading server."); 2459 $who->message ("reloading server.");
2423 2460
2424 # doing reload synchronously and two reloads happen back-to-back, 2461 # doing reload synchronously and two reloads happen back-to-back,
2425 # coro crashes during coro_state_free->destroy here. 2462 # coro crashes during coro_state_free->destroy here.
2426 2463
2427 $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { 2464 $RELOAD_WATCHER ||= Event->timer (
2465 reentrant => 0,
2466 after => 0,
2467 data => WF_AUTOCANCEL,
2468 cb => sub {
2428 reload; 2469 reload;
2429 undef $RELOAD_WATCHER; 2470 undef $RELOAD_WATCHER;
2471 },
2430 }); 2472 );
2431 } 2473 }
2432}; 2474};
2433 2475
2434unshift @INC, $LIBDIR; 2476unshift @INC, $LIBDIR;
2435 2477
2436my $bug_warning = 0; 2478my $bug_warning = 0;
2437 2479
2438$TICK_WATCHER = Event->timer ( 2480$TICK_WATCHER = Event->timer (
2439 reentrant => 0, 2481 reentrant => 0,
2440 parked => 1, 2482 parked => 1,
2441 prio => 0, 2483 prio => -1,
2442 at => $NEXT_TICK || $TICK, 2484 at => $NEXT_TICK || $TICK,
2443 data => WF_AUTOCANCEL, 2485 data => WF_AUTOCANCEL,
2444 cb => sub { 2486 cb => sub {
2445 if ($Coro::current != $Coro::main) { 2487 if ($Coro::current != $Coro::main) {
2446 Carp::cluck "major BUG: server tick called outside of main coro, skipping it" 2488 Carp::cluck "major BUG: server tick called outside of main coro, skipping it"
2455 $NEXT_TICK += $TICK; 2497 $NEXT_TICK += $TICK;
2456 2498
2457 $WAIT_FOR_TICK->broadcast; 2499 $WAIT_FOR_TICK->broadcast;
2458 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; 2500 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited;
2459 2501
2502 my $AFTER = Event::time;
2503 warn $AFTER - $NOW;#d#
2504
2460 # if we are delayed by four ticks or more, skip them all 2505 # if we are delayed by four ticks or more, skip them all
2461 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2506 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2462 2507
2463 $TICK_WATCHER->at ($NEXT_TICK); 2508 $TICK_WATCHER->at ($NEXT_TICK);
2464 $TICK_WATCHER->start; 2509 $TICK_WATCHER->start;
2465 }, 2510 },
2466); 2511);
2467 2512
2468IO::AIO::max_poll_time $TICK * 0.2; 2513IO::AIO::max_poll_time $TICK * 0.2;
2469 2514
2515undef $Coro::AIO::WATCHER;
2470$AIO_POLL_WATCHER = Event->io ( 2516$AIO_POLL_WATCHER = Event->io (
2517 reentrant => 0,
2471 fd => IO::AIO::poll_fileno, 2518 fd => IO::AIO::poll_fileno,
2472 poll => 'r', 2519 poll => 'r',
2473 prio => 5, 2520 prio => 6,
2474 data => WF_AUTOCANCEL, 2521 data => WF_AUTOCANCEL,
2475 cb => \&IO::AIO::poll_cb, 2522 cb => \&IO::AIO::poll_cb,
2476); 2523);
2477 2524
2478$WRITE_RUNTIME_WATCHER = Event->timer ( 2525$WRITE_RUNTIME_WATCHER = Event->timer (
2526 reentrant => 0,
2479 data => WF_AUTOCANCEL, 2527 data => WF_AUTOCANCEL,
2480 after => 1, 2528 after => 1,
2481 interval => 10, 2529 interval => 10,
2530 prio => 6, # keep it lowest so it acts like a watchdog
2482 cb => Coro::unblock_sub { 2531 cb => Coro::unblock_sub {
2483 write_runtime 2532 write_runtime
2484 or warn "ERROR: unable to write runtime file: $!"; 2533 or warn "ERROR: unable to write runtime file: $!";
2485 }, 2534 },
2486); 2535);
2487 2536

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines