forked from premiere/premiere-libtorrent
clean up file pool a bit
This commit is contained in:
parent
005d1a47b0
commit
1600d4dca5
|
@ -127,9 +127,8 @@ namespace libtorrent
|
||||||
|
|
||||||
struct lru_file_entry
|
struct lru_file_entry
|
||||||
{
|
{
|
||||||
lru_file_entry(): key(0), last_use(aux::time_now()), mode(0) {}
|
lru_file_entry(): last_use(aux::time_now()), mode(0) {}
|
||||||
mutable file_handle file_ptr;
|
file_handle file_ptr;
|
||||||
void* key;
|
|
||||||
time_point last_use;
|
time_point last_use;
|
||||||
int mode;
|
int mode;
|
||||||
};
|
};
|
||||||
|
|
|
@ -154,16 +154,6 @@ namespace libtorrent
|
||||||
lru_file_entry& e = i->second;
|
lru_file_entry& e = i->second;
|
||||||
e.last_use = aux::time_now();
|
e.last_use = aux::time_now();
|
||||||
|
|
||||||
if (e.key != st && ((e.mode & file::rw_mask) != file::read_only
|
|
||||||
|| (m & file::rw_mask) != file::read_only))
|
|
||||||
{
|
|
||||||
// this means that another instance of the storage
|
|
||||||
// is using the exact same file.
|
|
||||||
ec = errors::file_collision;
|
|
||||||
return file_handle();
|
|
||||||
}
|
|
||||||
|
|
||||||
e.key = st;
|
|
||||||
// if we asked for a file in write mode,
|
// if we asked for a file in write mode,
|
||||||
// and the cached file is is not opened in
|
// and the cached file is is not opened in
|
||||||
// write mode, re-open it
|
// write mode, re-open it
|
||||||
|
@ -212,13 +202,10 @@ namespace libtorrent
|
||||||
set_low_priority(e.file_ptr);
|
set_low_priority(e.file_ptr);
|
||||||
#endif
|
#endif
|
||||||
e.mode = m;
|
e.mode = m;
|
||||||
e.key = st;
|
|
||||||
m_files.insert(std::make_pair(std::make_pair(st, file_index), e));
|
|
||||||
TORRENT_ASSERT(e.file_ptr->is_open());
|
|
||||||
|
|
||||||
file_handle file_ptr = e.file_ptr;
|
file_handle file_ptr = e.file_ptr;
|
||||||
|
m_files.insert(std::make_pair(std::make_pair(st, file_index), e));
|
||||||
|
TORRENT_ASSERT(file_ptr->is_open());
|
||||||
|
|
||||||
// the file is not in our cache
|
|
||||||
if (int(m_files.size()) >= m_size)
|
if (int(m_files.size()) >= m_size)
|
||||||
{
|
{
|
||||||
// the file cache is at its maximum size, close
|
// the file cache is at its maximum size, close
|
||||||
|
@ -271,7 +258,8 @@ namespace libtorrent
|
||||||
file_handle file_ptr = i->second.file_ptr;
|
file_handle file_ptr = i->second.file_ptr;
|
||||||
m_files.erase(i);
|
m_files.erase(i);
|
||||||
|
|
||||||
// closing a file may be long running operation (mac os x)
|
// closing a file may take a long time (mac os x), so make sure
|
||||||
|
// we're not holding the mutex
|
||||||
l.unlock();
|
l.unlock();
|
||||||
file_ptr.reset();
|
file_ptr.reset();
|
||||||
}
|
}
|
||||||
|
@ -284,26 +272,22 @@ namespace libtorrent
|
||||||
|
|
||||||
if (st == 0)
|
if (st == 0)
|
||||||
{
|
{
|
||||||
file_set tmp;
|
m_files.clear();
|
||||||
tmp.swap(m_files);
|
|
||||||
l.unlock();
|
l.unlock();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file_set::iterator begin = m_files.lower_bound(std::make_pair(st, 0));
|
||||||
|
file_set::iterator end = m_files.upper_bound(std::make_pair(st, std::numeric_limits<int>::max()));
|
||||||
|
|
||||||
std::vector<file_handle> to_close;
|
std::vector<file_handle> to_close;
|
||||||
for (file_set::iterator i = m_files.begin();
|
while (begin != end)
|
||||||
i != m_files.end();)
|
|
||||||
{
|
{
|
||||||
if (i->second.key == st)
|
to_close.push_back(begin->second.file_ptr);
|
||||||
{
|
m_files.erase(begin++);
|
||||||
to_close.push_back(i->second.file_ptr);
|
|
||||||
m_files.erase(i++);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
l.unlock();
|
l.unlock();
|
||||||
// the files are closed here
|
// the files are closed here while the lock is not held
|
||||||
}
|
}
|
||||||
|
|
||||||
#if TORRENT_USE_ASSERTS
|
#if TORRENT_USE_ASSERTS
|
||||||
|
@ -323,7 +307,7 @@ namespace libtorrent
|
||||||
for (file_set::const_iterator i = m_files.begin();
|
for (file_set::const_iterator i = m_files.begin();
|
||||||
i != m_files.end(); ++i)
|
i != m_files.end(); ++i)
|
||||||
{
|
{
|
||||||
if (i->second.key == st && !i->second.file_ptr.unique())
|
if (i->first.first == st && !i->second.file_ptr.unique())
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in New Issue