… | |
… | |
285 | Return true if the lock is currently active, i.e. somebody has locked it. |
285 | Return true if the lock is currently active, i.e. somebody has locked it. |
286 | |
286 | |
287 | =cut |
287 | =cut |
288 | |
288 | |
289 | our %LOCK; |
289 | our %LOCK; |
|
|
290 | our %LOCKER;#d# |
290 | |
291 | |
291 | sub lock_wait($) { |
292 | sub lock_wait($) { |
292 | my ($key) = @_; |
293 | my ($key) = @_; |
|
|
294 | |
|
|
295 | if ($LOCKER{$key} == $Coro::current) {#d# |
|
|
296 | Carp::cluck "lock_wait($key) for already-acquired lock";#d# |
|
|
297 | return;#d# |
|
|
298 | }#d# |
293 | |
299 | |
294 | # wait for lock, if any |
300 | # wait for lock, if any |
295 | while ($LOCK{$key}) { |
301 | while ($LOCK{$key}) { |
296 | push @{ $LOCK{$key} }, $Coro::current; |
302 | push @{ $LOCK{$key} }, $Coro::current; |
297 | Coro::schedule; |
303 | Coro::schedule; |
… | |
… | |
303 | |
309 | |
304 | # wait, to be sure we are not locked |
310 | # wait, to be sure we are not locked |
305 | lock_wait $key; |
311 | lock_wait $key; |
306 | |
312 | |
307 | $LOCK{$key} = []; |
313 | $LOCK{$key} = []; |
|
|
314 | $LOCKER{$key} = $Coro::current;#d# |
308 | |
315 | |
309 | Coro::guard { |
316 | Coro::guard { |
|
|
317 | delete $LOCKER{$key};#d# |
310 | # wake up all waiters, to be on the safe side |
318 | # wake up all waiters, to be on the safe side |
311 | $_->ready for @{ delete $LOCK{$key} }; |
319 | $_->ready for @{ delete $LOCK{$key} }; |
312 | } |
320 | } |
313 | } |
321 | } |
314 | |
322 | |
… | |
… | |
1841 | sub find { |
1849 | sub find { |
1842 | my ($path, $origin) = @_; |
1850 | my ($path, $origin) = @_; |
1843 | |
1851 | |
1844 | $path = normalise $path, $origin && $origin->path; |
1852 | $path = normalise $path, $origin && $origin->path; |
1845 | |
1853 | |
|
|
1854 | cf::lock_wait "map_data:$path";#d#remove |
1846 | cf::lock_wait "map_find:$path"; |
1855 | cf::lock_wait "map_find:$path"; |
1847 | |
1856 | |
1848 | $cf::MAP{$path} || do { |
1857 | $cf::MAP{$path} || do { |
1849 | my $guard = cf::lock_acquire "map_find:$path"; |
1858 | my $guard1 = cf::lock_acquire "map_find:$path"; |
|
|
1859 | my $guard2 = cf::lock_acquire "map_data:$path"; # just for the fun of it |
|
|
1860 | |
1850 | my $map = new_from_path cf::map $path |
1861 | my $map = new_from_path cf::map $path |
1851 | or return; |
1862 | or return; |
1852 | |
1863 | |
1853 | $map->{last_save} = $cf::RUNTIME; |
1864 | $map->{last_save} = $cf::RUNTIME; |
1854 | |
1865 | |
… | |
… | |
1856 | or return; |
1867 | or return; |
1857 | |
1868 | |
1858 | if ($map->should_reset) {#d#TODO# disabled, crashy (locking issue?) |
1869 | if ($map->should_reset) {#d#TODO# disabled, crashy (locking issue?) |
1859 | # doing this can freeze the server in a sync job, obviously |
1870 | # doing this can freeze the server in a sync job, obviously |
1860 | #$cf::WAIT_FOR_TICK->wait; |
1871 | #$cf::WAIT_FOR_TICK->wait; |
|
|
1872 | undef $guard1; |
|
|
1873 | undef $guard2; |
1861 | $map->reset; |
1874 | $map->reset; |
1862 | undef $guard; |
|
|
1863 | return find $path; |
1875 | return find $path; |
1864 | } |
1876 | } |
1865 | |
1877 | |
1866 | $cf::MAP{$path} = $map |
1878 | $cf::MAP{$path} = $map |
1867 | } |
1879 | } |
… | |
… | |
1876 | local $self->{deny_reset} = 1; # loading can take a long time |
1888 | local $self->{deny_reset} = 1; # loading can take a long time |
1877 | |
1889 | |
1878 | my $path = $self->{path}; |
1890 | my $path = $self->{path}; |
1879 | |
1891 | |
1880 | { |
1892 | { |
1881 | my $guard1 = cf::lock_acquire "map_data:$path"; |
1893 | my $guard = cf::lock_acquire "map_data:$path"; |
1882 | my $guard2 = cf::lock_acquire "map_load:$path"; |
|
|
1883 | |
1894 | |
|
|
1895 | return unless $self->valid; |
1884 | return if $self->in_memory != cf::MAP_SWAPPED; |
1896 | return unless $self->in_memory == cf::MAP_SWAPPED; |
1885 | |
1897 | |
1886 | $self->in_memory (cf::MAP_LOADING); |
1898 | $self->in_memory (cf::MAP_LOADING); |
1887 | |
1899 | |
1888 | $self->alloc; |
1900 | $self->alloc; |
1889 | |
1901 | |
… | |
… | |
2061 | |
2073 | |
2062 | return if $self->players; |
2074 | return if $self->players; |
2063 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
2075 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
2064 | return if $self->{deny_save}; |
2076 | return if $self->{deny_save}; |
2065 | |
2077 | |
|
|
2078 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2079 | |
|
|
2080 | $self->deactivate; |
|
|
2081 | $_->clear_links_to ($self) for values %cf::MAP; |
2066 | $self->clear; |
2082 | $self->clear; |
2067 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2068 | } |
2083 | } |
2069 | |
2084 | |
2070 | sub reset_at { |
2085 | sub reset_at { |
2071 | my ($self) = @_; |
2086 | my ($self) = @_; |
2072 | |
2087 | |
… | |
… | |
2104 | if $uniq; |
2119 | if $uniq; |
2105 | } |
2120 | } |
2106 | |
2121 | |
2107 | delete $cf::MAP{$self->path}; |
2122 | delete $cf::MAP{$self->path}; |
2108 | |
2123 | |
|
|
2124 | $self->deactivate; |
|
|
2125 | $_->clear_links_to ($self) for values %cf::MAP; |
2109 | $self->clear; |
2126 | $self->clear; |
2110 | |
|
|
2111 | $_->clear_links_to ($self) for values %cf::MAP; |
|
|
2112 | |
2127 | |
2113 | $self->unlink_save; |
2128 | $self->unlink_save; |
2114 | $self->destroy; |
2129 | $self->destroy; |
2115 | } |
2130 | } |
2116 | |
2131 | |