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.132 by root, Thu Jan 4 01:35:56 2007 UTC vs.
Revision 1.134 by root, Thu Jan 4 17:28:49 2007 UTC

49our $UPTIME; $UPTIME ||= time; 49our $UPTIME; $UPTIME ||= time;
50our $RUNTIME; 50our $RUNTIME;
51 51
52our %MAP; # all maps 52our %MAP; # all maps
53our $LINK_MAP; # the special {link} map 53our $LINK_MAP; # the special {link} map
54our $FREEZE;
55our $RANDOM_MAPS = cf::localdir . "/random"; 54our $RANDOM_MAPS = cf::localdir . "/random";
56our %EXT_CORO; 55our %EXT_CORO;
57 56
58binmode STDOUT; 57binmode STDOUT;
59binmode STDERR; 58binmode STDERR;
213Wait until the given lock is available and then acquires it and returns 212Wait until the given lock is available and then acquires it and returns
214a guard object. If the guard object gets destroyed (goes out of scope, 213a guard object. If the guard object gets destroyed (goes out of scope,
215for example when the coroutine gets canceled), the lock is automatically 214for example when the coroutine gets canceled), the lock is automatically
216returned. 215returned.
217 216
218Lock names should begin with a unique identifier (for example, find_map 217Lock names should begin with a unique identifier (for example, cf::map::find
219uses map_find and load_map uses map_load). 218uses map_find and cf::map::load uses map_load).
220 219
221=cut 220=cut
222 221
223our %LOCK; 222our %LOCK;
224 223
258 257
259 Coro::async { 258 Coro::async {
260 eval { $cb->() }; 259 eval { $cb->() };
261 warn $@ if $@; 260 warn $@ if $@;
262 } 261 }
262}
263
264sub freeze_mainloop {
265 return unless $TICK_WATCHER->is_active;
266
267 my $guard = guard { $TICK_WATCHER->start };
268 $TICK_WATCHER->stop;
269 $guard
263} 270}
264 271
265=item cf::sync_job { BLOCK } 272=item cf::sync_job { BLOCK }
266 273
267The design of crossfire+ requires that the main coro ($Coro::main) is 274The design of crossfire+ requires that the main coro ($Coro::main) is
282 # this is the main coro, too bad, we have to block 289 # this is the main coro, too bad, we have to block
283 # till the operation succeeds, freezing the server :/ 290 # till the operation succeeds, freezing the server :/
284 291
285 # TODO: use suspend/resume instead 292 # TODO: use suspend/resume instead
286 # (but this is cancel-safe) 293 # (but this is cancel-safe)
287 local $FREEZE = 1; 294 my $freeze_guard = freeze_mainloop;
288 295
289 my $busy = 1; 296 my $busy = 1;
290 my @res; 297 my @res;
291 298
292 (Coro::async { 299 (Coro::async {
1136package cf::map; 1143package cf::map;
1137 1144
1138use Fcntl; 1145use Fcntl;
1139use Coro::AIO; 1146use Coro::AIO;
1140 1147
1141our $MAX_RESET = 7200; 1148our $MAX_RESET = 3600;
1142our $DEFAULT_RESET = 3600; 1149our $DEFAULT_RESET = 3000;
1143 1150
1144sub generate_random_map { 1151sub generate_random_map {
1145 my ($path, $rmp) = @_; 1152 my ($path, $rmp) = @_;
1146 1153
1147 # mit "rum" bekleckern, nicht 1154 # mit "rum" bekleckern, nicht
1185 $map->{load_path} = $path; 1192 $map->{load_path} = $path;
1186 1193
1187 $map 1194 $map
1188} 1195}
1189 1196
1190sub find_map; 1197sub find;
1191sub find_map { 1198sub find {
1192 my ($path, $origin) = @_; 1199 my ($path, $origin) = @_;
1193 1200
1194 #warn "find_map<$path,$origin>\n";#d# 1201 #warn "find<$path,$origin>\n";#d#
1195 1202
1196 $path = new cf::path $path, $origin && $origin->path; 1203 $path = new cf::path $path, $origin && $origin->path;
1197 my $key = $path->as_string; 1204 my $key = $path->as_string;
1198 1205
1199 cf::lock_wait "map_find:$key"; 1206 cf::lock_wait "map_find:$key";
1201 $cf::MAP{$key} || do { 1208 $cf::MAP{$key} || do {
1202 my $guard = cf::lock_acquire "map_find:$key"; 1209 my $guard = cf::lock_acquire "map_find:$key";
1203 1210
1204 # do it the slow way 1211 # do it the slow way
1205 my $map = try_load_header $path->save_path; 1212 my $map = try_load_header $path->save_path;
1213
1214 Coro::cede;
1206 1215
1207 if ($map) { 1216 if ($map) {
1208 $map->last_access ((delete $map->{last_access}) 1217 $map->last_access ((delete $map->{last_access})
1209 || $cf::RUNTIME); #d# 1218 || $cf::RUNTIME); #d#
1210 # safety 1219 # safety
1230 1239
1231 $map->path ($key); 1240 $map->path ($key);
1232 $map->{path} = $path; 1241 $map->{path} = $path;
1233 $map->{last_save} = $cf::RUNTIME; 1242 $map->{last_save} = $cf::RUNTIME;
1234 1243
1244 Coro::cede;
1245
1235 if ($map->should_reset) { 1246 if ($map->should_reset) {
1236 $map->reset; 1247 $map->reset;
1237 undef $guard; 1248 undef $guard;
1238 $map = find_map $path 1249 $map = find $path
1239 or return; 1250 or return;
1240 } 1251 }
1241 1252
1242 $cf::MAP{$key} = $map 1253 $cf::MAP{$key} = $map
1243 } 1254 }
1265 if (aio_open $uniq, O_RDONLY, 0) { 1276 if (aio_open $uniq, O_RDONLY, 0) {
1266 $self->clear_unique_items; 1277 $self->clear_unique_items;
1267 $self->load_objects ($uniq, 0); 1278 $self->load_objects ($uniq, 0);
1268 } 1279 }
1269 } 1280 }
1281
1282 Coro::cede;
1270 1283
1271 # now do the right thing for maps 1284 # now do the right thing for maps
1272 $self->link_multipart_objects; 1285 $self->link_multipart_objects;
1273 1286
1274 if ($self->{path}->is_style_map) { 1287 if ($self->{path}->is_style_map) {
1282 $self->difficulty ($self->estimate_difficulty) 1295 $self->difficulty ($self->estimate_difficulty)
1283 unless $self->difficulty; 1296 unless $self->difficulty;
1284 $self->activate; 1297 $self->activate;
1285 } 1298 }
1286 1299
1300 Coro::cede;
1301
1287 $self->in_memory (cf::MAP_IN_MEMORY); 1302 $self->in_memory (cf::MAP_IN_MEMORY);
1288} 1303}
1289 1304
1290sub load_map_sync { 1305sub find_sync {
1291 my ($path, $origin) = @_; 1306 my ($path, $origin) = @_;
1292 1307
1293 #warn "load_map_sync<$path, $origin>\n";#d# 1308 cf::sync_job { cf::map::find $path, $origin }
1309}
1294 1310
1295 cf::sync_job { 1311sub do_load_sync {
1296 my $map = cf::map::find_map $path, $origin 1312 my ($map) = @_;
1297 or return; 1313
1298 $map->load; 1314 cf::sync_job { $map->load };
1299 $map
1300 }
1301} 1315}
1302 1316
1303sub save { 1317sub save {
1304 my ($self) = @_; 1318 my ($self) = @_;
1305 1319
1407 1421
1408sub customise_for { 1422sub customise_for {
1409 my ($map, $ob) = @_; 1423 my ($map, $ob) = @_;
1410 1424
1411 if ($map->per_player) { 1425 if ($map->per_player) {
1412 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path}; 1426 return cf::map::find "~" . $ob->name . "/" . $map->{path}{path};
1413 } 1427 }
1414 1428
1415 $map 1429 $map
1416} 1430}
1417 1431
1418sub emergency_save { 1432sub emergency_save {
1419 local $cf::FREEZE = 1; 1433 my $freeze_guard = cf::freeze_mainloop;
1420 1434
1421 warn "enter emergency map save\n"; 1435 warn "enter emergency map save\n";
1422 1436
1423 cf::sync_job { 1437 cf::sync_job {
1424 warn "begin emergency map save\n"; 1438 warn "begin emergency map save\n";
1514 my $link_pos = delete $self->{_link_pos}; 1528 my $link_pos = delete $self->{_link_pos};
1515 1529
1516 unless ($map) { 1530 unless ($map) {
1517 # restore original map position 1531 # restore original map position
1518 ($map, $x, $y) = @{ $link_pos || [] }; 1532 ($map, $x, $y) = @{ $link_pos || [] };
1519 $map = cf::map::find_map $map; 1533 $map = cf::map::find $map;
1520 1534
1521 unless ($map) { 1535 unless ($map) {
1522 ($map, $x, $y) = @$EMERGENCY_POSITION; 1536 ($map, $x, $y) = @$EMERGENCY_POSITION;
1523 $map = cf::map::find_map $map 1537 $map = cf::map::find $map
1524 or die "FATAL: cannot load emergency map\n"; 1538 or die "FATAL: cannot load emergency map\n";
1525 } 1539 }
1526 } 1540 }
1527 1541
1528 ($x, $y) = (-1, -1) 1542 ($x, $y) = (-1, -1)
1576 $self->enter_link; 1590 $self->enter_link;
1577 1591
1578 (cf::async { 1592 (cf::async {
1579 $path = new cf::path $path; 1593 $path = new cf::path $path;
1580 1594
1581 my $map = cf::map::find_map $path->as_string; 1595 my $map = cf::map::find $path->as_string;
1582 $map = $map->customise_for ($self) if $map; 1596 $map = $map->customise_for ($self) if $map;
1583 1597
1584# warn "entering ", $map->path, " at ($x, $y)\n" 1598# warn "entering ", $map->path, " at ($x, $y)\n"
1585# if $map; 1599# if $map;
1586 1600
1646 return unless $self->type == cf::PLAYER; 1660 return unless $self->type == cf::PLAYER;
1647 1661
1648 $self->enter_link; 1662 $self->enter_link;
1649 1663
1650 (cf::async { 1664 (cf::async {
1665 $self->deactivate_recursive; # just to be sure
1651 unless (eval { 1666 unless (eval {
1652 prepare_random_map $exit 1667 prepare_random_map $exit
1653 if $exit->slaying eq "/!"; 1668 if $exit->slaying eq "/!";
1654 1669
1655 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 1670 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
2030 return; 2045 return;
2031 } 2046 }
2032 2047
2033 warn "reloading..."; 2048 warn "reloading...";
2034 2049
2035 local $FREEZE = 1; 2050 my $guard = freeze_mainloop;
2036 cf::emergency_save; 2051 cf::emergency_save;
2037 2052
2038 eval { 2053 eval {
2039 # if anything goes wrong in here, we should simply crash as we already saved 2054 # if anything goes wrong in here, we should simply crash as we already saved
2040 2055
2176 reentrant => 0, 2191 reentrant => 0,
2177 prio => 0, 2192 prio => 0,
2178 at => $NEXT_TICK || $TICK, 2193 at => $NEXT_TICK || $TICK,
2179 data => WF_AUTOCANCEL, 2194 data => WF_AUTOCANCEL,
2180 cb => sub { 2195 cb => sub {
2181 unless ($FREEZE) {
2182 cf::server_tick; # one server iteration 2196 cf::server_tick; # one server iteration
2183 $RUNTIME += $TICK; 2197 $RUNTIME += $TICK;
2184 }
2185
2186 $NEXT_TICK += $TICK; 2198 $NEXT_TICK += $TICK;
2187 2199
2188 # if we are delayed by four ticks or more, skip them all 2200 # if we are delayed by four ticks or more, skip them all
2189 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2201 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2190 2202

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines