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.133 by root, Thu Jan 4 16:19:32 2007 UTC vs.
Revision 1.137 by root, Fri Jan 5 10:23:05 2007 UTC

179sub to_json($) { 179sub to_json($) {
180 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs 180 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
181 JSON::Syck::Dump $_[0] 181 JSON::Syck::Dump $_[0]
182} 182}
183 183
184=item my $guard = cf::guard { BLOCK }
185
186Run the given callback when the guard object gets destroyed (useful for
187coroutine cancellations).
188
189You can call C<< ->cancel >> on the guard object to stop the block from
190being executed.
191
192=cut
193
194sub guard(&) {
195 bless \(my $cb = $_[0]), cf::guard::;
196}
197
198sub cf::guard::cancel {
199 ${$_[0]} = sub { };
200}
201
202sub cf::guard::DESTROY {
203 ${$_[0]}->();
204}
205
206=item cf::lock_wait $string 184=item cf::lock_wait $string
207 185
208Wait until the given lock is available. See cf::lock_acquire. 186Wait until the given lock is available. See cf::lock_acquire.
209 187
210=item my $lock = cf::lock_acquire $string 188=item my $lock = cf::lock_acquire $string
211 189
212Wait 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
213a 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,
214for example when the coroutine gets canceled), the lock is automatically 192for example when the coroutine gets canceled), the lock is automatically
215returned. 193returned.
216 194
217Lock names should begin with a unique identifier (for example, cf::map::find 195Lock names should begin with a unique identifier (for example, cf::map::find
218uses map_find and cf::map::load uses map_load). 196uses map_find and cf::map::load uses map_load).
237 # wait, to be sure we are not locked 215 # wait, to be sure we are not locked
238 lock_wait $key; 216 lock_wait $key;
239 217
240 $LOCK{$key} = []; 218 $LOCK{$key} = [];
241 219
242 cf::guard { 220 Coro::guard {
243 # wake up all waiters, to be on the safe side 221 # wake up all waiters, to be on the safe side
244 $_->ready for @{ delete $LOCK{$key} }; 222 $_->ready for @{ delete $LOCK{$key} };
245 } 223 }
246} 224}
247 225
262} 240}
263 241
264sub freeze_mainloop { 242sub freeze_mainloop {
265 return unless $TICK_WATCHER->is_active; 243 return unless $TICK_WATCHER->is_active;
266 244
267 my $guard = guard { $TICK_WATCHER->start }; 245 my $guard = Coro::guard { $TICK_WATCHER->start };
268 $TICK_WATCHER->stop; 246 $TICK_WATCHER->stop;
269 $guard 247 $guard
270} 248}
271 249
272=item cf::sync_job { BLOCK } 250=item cf::sync_job { BLOCK }
1184 or return; 1162 or return;
1185 1163
1186 my $map = cf::map::new 1164 my $map = cf::map::new
1187 or return; 1165 or return;
1188 1166
1167 # for better error messages only, will be overwritten
1168 $map->path ($path);
1169
1189 $map->load_header ($path) 1170 $map->load_header ($path)
1190 or return; 1171 or return;
1191 1172
1192 $map->{load_path} = $path; 1173 $map->{load_path} = $path;
1193 1174
1208 $cf::MAP{$key} || do { 1189 $cf::MAP{$key} || do {
1209 my $guard = cf::lock_acquire "map_find:$key"; 1190 my $guard = cf::lock_acquire "map_find:$key";
1210 1191
1211 # do it the slow way 1192 # do it the slow way
1212 my $map = try_load_header $path->save_path; 1193 my $map = try_load_header $path->save_path;
1194
1195 Coro::cede;
1213 1196
1214 if ($map) { 1197 if ($map) {
1215 $map->last_access ((delete $map->{last_access}) 1198 $map->last_access ((delete $map->{last_access})
1216 || $cf::RUNTIME); #d# 1199 || $cf::RUNTIME); #d#
1217 # safety 1200 # safety
1237 1220
1238 $map->path ($key); 1221 $map->path ($key);
1239 $map->{path} = $path; 1222 $map->{path} = $path;
1240 $map->{last_save} = $cf::RUNTIME; 1223 $map->{last_save} = $cf::RUNTIME;
1241 1224
1225 Coro::cede;
1226
1242 if ($map->should_reset) { 1227 if ($map->should_reset) {
1243 $map->reset; 1228 $map->reset;
1244 undef $guard; 1229 undef $guard;
1245 $map = find $path 1230 $map = find $path
1246 or return; 1231 or return;
1272 if (aio_open $uniq, O_RDONLY, 0) { 1257 if (aio_open $uniq, O_RDONLY, 0) {
1273 $self->clear_unique_items; 1258 $self->clear_unique_items;
1274 $self->load_objects ($uniq, 0); 1259 $self->load_objects ($uniq, 0);
1275 } 1260 }
1276 } 1261 }
1262
1263 Coro::cede;
1277 1264
1278 # now do the right thing for maps 1265 # now do the right thing for maps
1279 $self->link_multipart_objects; 1266 $self->link_multipart_objects;
1280 1267
1281 if ($self->{path}->is_style_map) { 1268 if ($self->{path}->is_style_map) {
1289 $self->difficulty ($self->estimate_difficulty) 1276 $self->difficulty ($self->estimate_difficulty)
1290 unless $self->difficulty; 1277 unless $self->difficulty;
1291 $self->activate; 1278 $self->activate;
1292 } 1279 }
1293 1280
1281 Coro::cede;
1282
1294 $self->in_memory (cf::MAP_IN_MEMORY); 1283 $self->in_memory (cf::MAP_IN_MEMORY);
1295} 1284}
1296 1285
1297sub find_sync { 1286sub find_sync {
1298 my ($path, $origin) = @_; 1287 my ($path, $origin) = @_;
1306 cf::sync_job { $map->load }; 1295 cf::sync_job { $map->load };
1307} 1296}
1308 1297
1309sub save { 1298sub save {
1310 my ($self) = @_; 1299 my ($self) = @_;
1300
1301 my $lock = cf::lock_acquire "map_data:" . $self->path;
1311 1302
1312 $self->{last_save} = $cf::RUNTIME; 1303 $self->{last_save} = $cf::RUNTIME;
1313 1304
1314 return unless $self->dirty; 1305 return unless $self->dirty;
1315 1306
1334 my ($self) = @_; 1325 my ($self) = @_;
1335 1326
1336 # save first because save cedes 1327 # save first because save cedes
1337 $self->save; 1328 $self->save;
1338 1329
1330 my $lock = cf::lock_acquire "map_data:" . $self->path;
1331
1339 return if $self->players; 1332 return if $self->players;
1340 return if $self->in_memory != cf::MAP_IN_MEMORY; 1333 return if $self->in_memory != cf::MAP_IN_MEMORY;
1341 return if $self->{deny_save}; 1334 return if $self->{deny_save};
1342 1335
1343 $self->clear; 1336 $self->clear;
1384 $self->save; 1377 $self->save;
1385} 1378}
1386 1379
1387sub reset { 1380sub reset {
1388 my ($self) = @_; 1381 my ($self) = @_;
1382
1383 my $lock = cf::lock_acquire "map_data:" . $self->path;
1389 1384
1390 return if $self->players; 1385 return if $self->players;
1391 return if $self->{path}{user_rel};#d# 1386 return if $self->{path}{user_rel};#d#
1392 1387
1393 warn "resetting map ", $self->path;#d# 1388 warn "resetting map ", $self->path;#d#
1570 }; 1565 };
1571 } 1566 }
1572 }, 1567 },
1573); 1568);
1574 1569
1575=item $player_object->goto_map ($path, $x, $y) 1570=item $player_object->goto ($path, $x, $y)
1576 1571
1577=cut 1572=cut
1578 1573
1579sub cf::object::player::goto_map { 1574sub cf::object::player::goto {
1580 my ($self, $path, $x, $y) = @_; 1575 my ($self, $path, $x, $y) = @_;
1581 1576
1582 $self->enter_link; 1577 $self->enter_link;
1583 1578
1584 (cf::async { 1579 (cf::async {
1658 unless (eval { 1653 unless (eval {
1659 prepare_random_map $exit 1654 prepare_random_map $exit
1660 if $exit->slaying eq "/!"; 1655 if $exit->slaying eq "/!";
1661 1656
1662 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 1657 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
1663 $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); 1658 $self->goto ($path, $exit->stats->hp, $exit->stats->sp);
1664 1659
1665 1; 1660 1;
1666 }) { 1661 }) {
1667 $self->message ("Something went wrong deep within the crossfire server. " 1662 $self->message ("Something went wrong deep within the crossfire server. "
1668 . "I'll try to bring you back to the map you were before. " 1663 . "I'll try to bring you back to the map you were before. "

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines