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.131 by root, Thu Jan 4 00:53:54 2007 UTC vs.
Revision 1.135 by root, Thu Jan 4 20:29:46 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;
180sub to_json($) { 179sub to_json($) {
181 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs 180 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
182 JSON::Syck::Dump $_[0] 181 JSON::Syck::Dump $_[0]
183} 182}
184 183
185=item my $guard = cf::guard { BLOCK }
186
187Run the given callback when the guard object gets destroyed (useful for
188coroutine cancellations).
189
190You can call C<< ->cancel >> on the guard object to stop the block from
191being executed.
192
193=cut
194
195sub guard(&) {
196 bless \(my $cb = $_[0]), cf::guard::;
197}
198
199sub cf::guard::cancel {
200 ${$_[0]} = sub { };
201}
202
203sub cf::guard::DESTROY {
204 ${$_[0]}->();
205}
206
207=item cf::lock_wait $string 184=item cf::lock_wait $string
208 185
209Wait until the given lock is available. See cf::lock_acquire. 186Wait until the given lock is available. See cf::lock_acquire.
210 187
211=item my $lock = cf::lock_acquire $string 188=item my $lock = cf::lock_acquire $string
212 189
213Wait until the given lock is available and then acquires it and returns 190Wait 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, 191a Coro::guard object. If the guard object gets destroyed (goes out of scope,
215for example when the coroutine gets canceled), the lock is automatically 192for example when the coroutine gets canceled), the lock is automatically
216returned. 193returned.
217 194
218Lock names should begin with a unique identifier (for example, find_map 195Lock names should begin with a unique identifier (for example, cf::map::find
219uses map_find and load_map uses map_load). 196uses map_find and cf::map::load uses map_load).
220 197
221=cut 198=cut
222 199
223our %LOCK; 200our %LOCK;
224 201
238 # wait, to be sure we are not locked 215 # wait, to be sure we are not locked
239 lock_wait $key; 216 lock_wait $key;
240 217
241 $LOCK{$key} = []; 218 $LOCK{$key} = [];
242 219
243 cf::guard { 220 Coro::guard {
244 # wake up all waiters, to be on the safe side 221 # wake up all waiters, to be on the safe side
245 $_->ready for @{ delete $LOCK{$key} }; 222 $_->ready for @{ delete $LOCK{$key} };
246 } 223 }
247} 224}
248 225
258 235
259 Coro::async { 236 Coro::async {
260 eval { $cb->() }; 237 eval { $cb->() };
261 warn $@ if $@; 238 warn $@ if $@;
262 } 239 }
240}
241
242sub freeze_mainloop {
243 return unless $TICK_WATCHER->is_active;
244
245 my $guard = Coro::guard { $TICK_WATCHER->start };
246 $TICK_WATCHER->stop;
247 $guard
263} 248}
264 249
265=item cf::sync_job { BLOCK } 250=item cf::sync_job { BLOCK }
266 251
267The design of crossfire+ requires that the main coro ($Coro::main) is 252The design of crossfire+ requires that the main coro ($Coro::main) is
282 # this is the main coro, too bad, we have to block 267 # this is the main coro, too bad, we have to block
283 # till the operation succeeds, freezing the server :/ 268 # till the operation succeeds, freezing the server :/
284 269
285 # TODO: use suspend/resume instead 270 # TODO: use suspend/resume instead
286 # (but this is cancel-safe) 271 # (but this is cancel-safe)
287 local $FREEZE = 1; 272 my $freeze_guard = freeze_mainloop;
288 273
289 my $busy = 1; 274 my $busy = 1;
290 my @res; 275 my @res;
291 276
292 (Coro::async { 277 (Coro::async {
1136package cf::map; 1121package cf::map;
1137 1122
1138use Fcntl; 1123use Fcntl;
1139use Coro::AIO; 1124use Coro::AIO;
1140 1125
1141our $MAX_RESET = 7200; 1126our $MAX_RESET = 3600;
1142our $DEFAULT_RESET = 3600; 1127our $DEFAULT_RESET = 3000;
1143 1128
1144sub generate_random_map { 1129sub generate_random_map {
1145 my ($path, $rmp) = @_; 1130 my ($path, $rmp) = @_;
1146 1131
1147 # mit "rum" bekleckern, nicht 1132 # mit "rum" bekleckern, nicht
1177 or return; 1162 or return;
1178 1163
1179 my $map = cf::map::new 1164 my $map = cf::map::new
1180 or return; 1165 or return;
1181 1166
1167 # for better error messages only, will be overwritten
1168 $map->path ($path);
1169
1182 $map->load_header ($path) 1170 $map->load_header ($path)
1183 or return; 1171 or return;
1184 1172
1185 $map->{load_path} = $path; 1173 $map->{load_path} = $path;
1186 1174
1187 $map 1175 $map
1188} 1176}
1189 1177
1190sub find_map; 1178sub find;
1191sub find_map { 1179sub find {
1192 my ($path, $origin) = @_; 1180 my ($path, $origin) = @_;
1193 1181
1194 #warn "find_map<$path,$origin>\n";#d# 1182 #warn "find<$path,$origin>\n";#d#
1195 1183
1196 $path = new cf::path $path, $origin && $origin->path; 1184 $path = new cf::path $path, $origin && $origin->path;
1197 my $key = $path->as_string; 1185 my $key = $path->as_string;
1198 1186
1199 cf::lock_wait "map_find:$key"; 1187 cf::lock_wait "map_find:$key";
1202 my $guard = cf::lock_acquire "map_find:$key"; 1190 my $guard = cf::lock_acquire "map_find:$key";
1203 1191
1204 # do it the slow way 1192 # do it the slow way
1205 my $map = try_load_header $path->save_path; 1193 my $map = try_load_header $path->save_path;
1206 1194
1195 Coro::cede;
1196
1207 if ($map) { 1197 if ($map) {
1198 $map->last_access ((delete $map->{last_access})
1199 || $cf::RUNTIME); #d#
1208 # safety 1200 # safety
1209 $map->{instantiate_time} = $cf::RUNTIME 1201 $map->{instantiate_time} = $cf::RUNTIME
1210 if $map->{instantiate_time} > $cf::RUNTIME; 1202 if $map->{instantiate_time} > $cf::RUNTIME;
1211 } else { 1203 } else {
1212 if (my $rmp = $path->random_map_params) { 1204 if (my $rmp = $path->random_map_params) {
1217 1209
1218 $map or return; 1210 $map or return;
1219 1211
1220 $map->{load_original} = 1; 1212 $map->{load_original} = 1;
1221 $map->{instantiate_time} = $cf::RUNTIME; 1213 $map->{instantiate_time} = $cf::RUNTIME;
1214 $map->last_access ($cf::RUNTIME);
1222 $map->instantiate; 1215 $map->instantiate;
1223 1216
1224 # per-player maps become, after loading, normal maps 1217 # per-player maps become, after loading, normal maps
1225 $map->per_player (0) if $path->{user_rel}; 1218 $map->per_player (0) if $path->{user_rel};
1226 } 1219 }
1227 1220
1228 $map->path ($key); 1221 $map->path ($key);
1229 $map->{path} = $path; 1222 $map->{path} = $path;
1230 $map->{last_save} = $cf::RUNTIME; 1223 $map->{last_save} = $cf::RUNTIME;
1231 $map->last_access ($cf::RUNTIME); 1224
1225 Coro::cede;
1232 1226
1233 if ($map->should_reset) { 1227 if ($map->should_reset) {
1234 $map->reset; 1228 $map->reset;
1235 undef $guard; 1229 undef $guard;
1236 $map = find_map $path 1230 $map = find $path
1237 or return; 1231 or return;
1238 } 1232 }
1239 1233
1240 $cf::MAP{$key} = $map 1234 $cf::MAP{$key} = $map
1241 } 1235 }
1263 if (aio_open $uniq, O_RDONLY, 0) { 1257 if (aio_open $uniq, O_RDONLY, 0) {
1264 $self->clear_unique_items; 1258 $self->clear_unique_items;
1265 $self->load_objects ($uniq, 0); 1259 $self->load_objects ($uniq, 0);
1266 } 1260 }
1267 } 1261 }
1262
1263 Coro::cede;
1268 1264
1269 # now do the right thing for maps 1265 # now do the right thing for maps
1270 $self->link_multipart_objects; 1266 $self->link_multipart_objects;
1271 1267
1272 if ($self->{path}->is_style_map) { 1268 if ($self->{path}->is_style_map) {
1280 $self->difficulty ($self->estimate_difficulty) 1276 $self->difficulty ($self->estimate_difficulty)
1281 unless $self->difficulty; 1277 unless $self->difficulty;
1282 $self->activate; 1278 $self->activate;
1283 } 1279 }
1284 1280
1281 Coro::cede;
1282
1285 $self->in_memory (cf::MAP_IN_MEMORY); 1283 $self->in_memory (cf::MAP_IN_MEMORY);
1286} 1284}
1287 1285
1288sub load_map_sync { 1286sub find_sync {
1289 my ($path, $origin) = @_; 1287 my ($path, $origin) = @_;
1290 1288
1291 #warn "load_map_sync<$path, $origin>\n";#d# 1289 cf::sync_job { cf::map::find $path, $origin }
1290}
1292 1291
1293 cf::sync_job { 1292sub do_load_sync {
1294 my $map = cf::map::find_map $path, $origin 1293 my ($map) = @_;
1295 or return; 1294
1296 $map->load; 1295 cf::sync_job { $map->load };
1297 $map
1298 }
1299} 1296}
1300 1297
1301sub save { 1298sub save {
1302 my ($self) = @_; 1299 my ($self) = @_;
1303 1300
1309 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; 1306 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
1310 1307
1311 $self->{load_path} = $save; 1308 $self->{load_path} = $save;
1312 1309
1313 return if $self->{deny_save}; 1310 return if $self->{deny_save};
1311
1312 local $self->{last_access} = $self->last_access;#d#
1314 1313
1315 if ($uniq) { 1314 if ($uniq) {
1316 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); 1315 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
1317 $self->save_objects ($uniq, cf::IO_UNIQUES); 1316 $self->save_objects ($uniq, cf::IO_UNIQUES);
1318 } else { 1317 } else {
1403 1402
1404sub customise_for { 1403sub customise_for {
1405 my ($map, $ob) = @_; 1404 my ($map, $ob) = @_;
1406 1405
1407 if ($map->per_player) { 1406 if ($map->per_player) {
1408 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path}; 1407 return cf::map::find "~" . $ob->name . "/" . $map->{path}{path};
1409 } 1408 }
1410 1409
1411 $map 1410 $map
1412} 1411}
1413 1412
1414sub emergency_save { 1413sub emergency_save {
1415 local $cf::FREEZE = 1; 1414 my $freeze_guard = cf::freeze_mainloop;
1416 1415
1417 warn "enter emergency map save\n"; 1416 warn "enter emergency map save\n";
1418 1417
1419 cf::sync_job { 1418 cf::sync_job {
1420 warn "begin emergency map save\n"; 1419 warn "begin emergency map save\n";
1510 my $link_pos = delete $self->{_link_pos}; 1509 my $link_pos = delete $self->{_link_pos};
1511 1510
1512 unless ($map) { 1511 unless ($map) {
1513 # restore original map position 1512 # restore original map position
1514 ($map, $x, $y) = @{ $link_pos || [] }; 1513 ($map, $x, $y) = @{ $link_pos || [] };
1515 $map = cf::map::find_map $map; 1514 $map = cf::map::find $map;
1516 1515
1517 unless ($map) { 1516 unless ($map) {
1518 ($map, $x, $y) = @$EMERGENCY_POSITION; 1517 ($map, $x, $y) = @$EMERGENCY_POSITION;
1519 $map = cf::map::find_map $map 1518 $map = cf::map::find $map
1520 or die "FATAL: cannot load emergency map\n"; 1519 or die "FATAL: cannot load emergency map\n";
1521 } 1520 }
1522 } 1521 }
1523 1522
1524 ($x, $y) = (-1, -1) 1523 ($x, $y) = (-1, -1)
1572 $self->enter_link; 1571 $self->enter_link;
1573 1572
1574 (cf::async { 1573 (cf::async {
1575 $path = new cf::path $path; 1574 $path = new cf::path $path;
1576 1575
1577 my $map = cf::map::find_map $path->as_string; 1576 my $map = cf::map::find $path->as_string;
1578 $map = $map->customise_for ($self) if $map; 1577 $map = $map->customise_for ($self) if $map;
1579 1578
1580# warn "entering ", $map->path, " at ($x, $y)\n" 1579# warn "entering ", $map->path, " at ($x, $y)\n"
1581# if $map; 1580# if $map;
1582 1581
1642 return unless $self->type == cf::PLAYER; 1641 return unless $self->type == cf::PLAYER;
1643 1642
1644 $self->enter_link; 1643 $self->enter_link;
1645 1644
1646 (cf::async { 1645 (cf::async {
1646 $self->deactivate_recursive; # just to be sure
1647 unless (eval { 1647 unless (eval {
1648 prepare_random_map $exit 1648 prepare_random_map $exit
1649 if $exit->slaying eq "/!"; 1649 if $exit->slaying eq "/!";
1650 1650
1651 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 1651 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
2026 return; 2026 return;
2027 } 2027 }
2028 2028
2029 warn "reloading..."; 2029 warn "reloading...";
2030 2030
2031 local $FREEZE = 1; 2031 my $guard = freeze_mainloop;
2032 cf::emergency_save; 2032 cf::emergency_save;
2033 2033
2034 eval { 2034 eval {
2035 # if anything goes wrong in here, we should simply crash as we already saved 2035 # if anything goes wrong in here, we should simply crash as we already saved
2036 2036
2172 reentrant => 0, 2172 reentrant => 0,
2173 prio => 0, 2173 prio => 0,
2174 at => $NEXT_TICK || $TICK, 2174 at => $NEXT_TICK || $TICK,
2175 data => WF_AUTOCANCEL, 2175 data => WF_AUTOCANCEL,
2176 cb => sub { 2176 cb => sub {
2177 unless ($FREEZE) {
2178 cf::server_tick; # one server iteration 2177 cf::server_tick; # one server iteration
2179 $RUNTIME += $TICK; 2178 $RUNTIME += $TICK;
2180 }
2181
2182 $NEXT_TICK += $TICK; 2179 $NEXT_TICK += $TICK;
2183 2180
2184 # if we are delayed by four ticks or more, skip them all 2181 # if we are delayed by four ticks or more, skip them all
2185 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2182 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2186 2183

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines