2006-11-14 14:36:10 +01:00
|
|
|
/*
|
|
|
|
|
2014-02-23 20:12:25 +01:00
|
|
|
Copyright (c) 2006-2014, Arvid Norberg
|
2006-11-14 14:36:10 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2008-07-18 01:41:46 +02:00
|
|
|
#include <boost/version.hpp>
|
2009-05-14 22:31:44 +02:00
|
|
|
#include <boost/bind.hpp>
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
2009-11-25 07:55:34 +01:00
|
|
|
#include "libtorrent/assert.hpp"
|
2006-11-14 14:36:10 +01:00
|
|
|
#include "libtorrent/file_pool.hpp"
|
2008-07-18 01:41:46 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2010-08-23 10:48:02 +02:00
|
|
|
#include "libtorrent/file_storage.hpp" // for file_entry
|
2006-11-14 14:36:10 +01:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2011-08-07 06:41:10 +02:00
|
|
|
|
|
|
|
file_pool::file_pool(int size)
|
|
|
|
: m_size(size)
|
|
|
|
, m_low_prio_io(true)
|
|
|
|
#if TORRENT_CLOSE_MAY_BLOCK
|
|
|
|
, m_stop_thread(false)
|
|
|
|
, m_closer_thread(boost::bind(&file_pool::closer_thread_fun, this))
|
|
|
|
#endif
|
2014-01-06 04:50:25 +01:00
|
|
|
{
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2014-01-06 04:50:25 +01:00
|
|
|
m_in_use = 1337;
|
|
|
|
#endif
|
|
|
|
}
|
2011-08-07 06:41:10 +02:00
|
|
|
|
|
|
|
file_pool::~file_pool()
|
|
|
|
{
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2014-01-06 04:50:25 +01:00
|
|
|
m_in_use = 0;
|
|
|
|
#endif
|
2011-08-07 06:41:10 +02:00
|
|
|
#if TORRENT_CLOSE_MAY_BLOCK
|
|
|
|
mutex::scoped_lock l(m_closer_mutex);
|
|
|
|
m_stop_thread = true;
|
|
|
|
l.unlock();
|
|
|
|
// wait for hte closer thread to finish closing all files
|
|
|
|
m_closer_thread.join();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TORRENT_CLOSE_MAY_BLOCK
|
|
|
|
void file_pool::closer_thread_fun()
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
mutex::scoped_lock l(m_closer_mutex);
|
|
|
|
if (m_stop_thread)
|
|
|
|
{
|
|
|
|
l.unlock();
|
|
|
|
m_queued_for_close.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find a file that doesn't have any other threads referencing
|
|
|
|
// it. Since only those files can be closed in this thead
|
|
|
|
std::vector<boost::intrusive_ptr<file> >::iterator i = std::find_if(
|
|
|
|
m_queued_for_close.begin(), m_queued_for_close.end()
|
|
|
|
, boost::bind(&file::refcount, boost::bind(&boost::intrusive_ptr<file>::get, _1)) == 1);
|
|
|
|
|
|
|
|
if (i == m_queued_for_close.end())
|
|
|
|
{
|
|
|
|
l.unlock();
|
2013-08-19 05:54:45 +02:00
|
|
|
// none of the files are ready to be closed yet
|
2011-08-07 06:41:10 +02:00
|
|
|
// because they're still in use by other threads
|
|
|
|
// hold off for a while
|
2013-08-19 05:54:45 +02:00
|
|
|
sleep(100);
|
2011-08-07 06:41:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ok, first pull the file out of the queue, release the mutex
|
|
|
|
// (since closing the file may block) and then close it.
|
|
|
|
boost::intrusive_ptr<file> f = *i;
|
|
|
|
m_queued_for_close.erase(i);
|
|
|
|
l.unlock();
|
|
|
|
f->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-07 02:56:40 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
void set_low_priority(boost::intrusive_ptr<file> const& f)
|
|
|
|
{
|
|
|
|
// file prio is only supported on vista and up
|
|
|
|
// so load the functions dynamically
|
|
|
|
typedef enum _FILE_INFO_BY_HANDLE_CLASS {
|
|
|
|
FileBasicInfo,
|
|
|
|
FileStandardInfo,
|
|
|
|
FileNameInfo,
|
|
|
|
FileRenameInfo,
|
|
|
|
FileDispositionInfo,
|
|
|
|
FileAllocationInfo,
|
|
|
|
FileEndOfFileInfo,
|
|
|
|
FileStreamInfo,
|
|
|
|
FileCompressionInfo,
|
|
|
|
FileAttributeTagInfo,
|
|
|
|
FileIdBothDirectoryInfo,
|
|
|
|
FileIdBothDirectoryRestartInfo,
|
|
|
|
FileIoPriorityHintInfo,
|
|
|
|
FileRemoteProtocolInfo,
|
|
|
|
MaximumFileInfoByHandleClass
|
|
|
|
} FILE_INFO_BY_HANDLE_CLASS, *PFILE_INFO_BY_HANDLE_CLASS;
|
|
|
|
|
|
|
|
typedef enum _PRIORITY_HINT {
|
|
|
|
IoPriorityHintVeryLow = 0,
|
|
|
|
IoPriorityHintLow,
|
|
|
|
IoPriorityHintNormal,
|
|
|
|
MaximumIoPriorityHintType
|
|
|
|
} PRIORITY_HINT;
|
|
|
|
|
|
|
|
typedef struct _FILE_IO_PRIORITY_HINT_INFO {
|
|
|
|
PRIORITY_HINT PriorityHint;
|
|
|
|
} FILE_IO_PRIORITY_HINT_INFO, *PFILE_IO_PRIORITY_HINT_INFO;
|
|
|
|
|
|
|
|
typedef BOOL (WINAPI *SetFileInformationByHandle_t)(HANDLE hFile, FILE_INFO_BY_HANDLE_CLASS FileInformationClass, LPVOID lpFileInformation, DWORD dwBufferSize);
|
|
|
|
static SetFileInformationByHandle_t SetFileInformationByHandle = NULL;
|
|
|
|
|
|
|
|
static bool failed_kernel_load = false;
|
|
|
|
|
|
|
|
if (failed_kernel_load) return;
|
|
|
|
|
|
|
|
if (SetFileInformationByHandle == NULL)
|
|
|
|
{
|
|
|
|
HMODULE kernel32 = LoadLibraryA("kernel32.dll");
|
|
|
|
if (kernel32 == NULL)
|
|
|
|
{
|
|
|
|
failed_kernel_load = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetFileInformationByHandle = (SetFileInformationByHandle_t)GetProcAddress(kernel32, "SetFileInformationByHandle");
|
|
|
|
if (SetFileInformationByHandle == NULL)
|
|
|
|
{
|
|
|
|
failed_kernel_load = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_ASSERT(SetFileInformationByHandle);
|
|
|
|
|
|
|
|
FILE_IO_PRIORITY_HINT_INFO io_hint;
|
|
|
|
io_hint.PriorityHint = IoPriorityHintLow;
|
|
|
|
SetFileInformationByHandle(f->native_handle(),
|
|
|
|
FileIoPriorityHintInfo, &io_hint, sizeof(io_hint));
|
|
|
|
}
|
|
|
|
#endif // TORRENT_WINDOWS
|
|
|
|
|
2010-08-23 10:48:02 +02:00
|
|
|
boost::intrusive_ptr<file> file_pool::open_file(void* st, std::string const& p
|
2013-08-12 09:30:57 +02:00
|
|
|
, int file_index, file_storage const& fs, int m, error_code& ec)
|
2006-11-14 14:36:10 +01:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(st != 0);
|
2009-10-26 02:29:39 +01:00
|
|
|
TORRENT_ASSERT(is_complete(p));
|
2009-01-11 03:02:34 +01:00
|
|
|
TORRENT_ASSERT((m & file::rw_mask) == file::read_only
|
|
|
|
|| (m & file::rw_mask) == file::read_write);
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2014-01-06 04:50:25 +01:00
|
|
|
TORRENT_ASSERT(m_in_use == 1337);
|
2013-08-12 09:30:57 +02:00
|
|
|
file_set::iterator i = m_files.find(std::make_pair(st, file_index));
|
2009-05-09 23:11:25 +02:00
|
|
|
if (i != m_files.end())
|
2006-11-14 14:36:10 +01:00
|
|
|
{
|
2009-05-09 23:11:25 +02:00
|
|
|
lru_file_entry& e = i->second;
|
2007-04-05 00:27:36 +02:00
|
|
|
e.last_use = time_now();
|
2006-11-14 14:36:10 +01:00
|
|
|
|
2009-01-25 02:58:49 +01:00
|
|
|
if (e.key != st && ((e.mode & file::rw_mask) != file::read_only
|
|
|
|
|| (m & file::rw_mask) != file::read_only))
|
2007-04-15 09:15:49 +02:00
|
|
|
{
|
|
|
|
// this means that another instance of the storage
|
|
|
|
// is using the exact same file.
|
2008-07-18 01:41:46 +02:00
|
|
|
#if BOOST_VERSION >= 103500
|
2009-11-29 08:06:38 +01:00
|
|
|
ec = errors::file_collision;
|
2008-07-18 01:41:46 +02:00
|
|
|
#endif
|
2010-08-23 10:48:02 +02:00
|
|
|
return boost::intrusive_ptr<file>();
|
2007-04-15 09:15:49 +02:00
|
|
|
}
|
2006-11-14 14:36:10 +01:00
|
|
|
|
|
|
|
e.key = st;
|
2008-10-19 07:03:17 +02:00
|
|
|
// if we asked for a file in write mode,
|
|
|
|
// and the cached file is is not opened in
|
|
|
|
// write mode, re-open it
|
2010-01-31 21:44:05 +01:00
|
|
|
if ((((e.mode & file::rw_mask) != file::read_write)
|
2009-01-11 03:02:34 +01:00
|
|
|
&& ((m & file::rw_mask) == file::read_write))
|
2011-04-25 04:15:18 +02:00
|
|
|
|| (e.mode & file::no_buffer) != (m & file::no_buffer)
|
|
|
|
|| (e.mode & file::random_access) != (m & file::random_access))
|
2006-11-14 14:36:10 +01:00
|
|
|
{
|
|
|
|
// close the file before we open it with
|
|
|
|
// the new read/write privilages
|
2010-08-23 10:48:02 +02:00
|
|
|
TORRENT_ASSERT(e.file_ptr->refcount() == 1);
|
2011-08-07 06:41:10 +02:00
|
|
|
|
|
|
|
#if TORRENT_CLOSE_MAY_BLOCK
|
|
|
|
mutex::scoped_lock l(m_closer_mutex);
|
|
|
|
m_queued_for_close.push_back(e.file_ptr);
|
|
|
|
l.unlock();
|
|
|
|
e.file_ptr = new file;
|
|
|
|
#else
|
2008-02-25 05:41:21 +01:00
|
|
|
e.file_ptr->close();
|
2011-08-07 06:41:10 +02:00
|
|
|
#endif
|
2013-08-12 09:30:57 +02:00
|
|
|
std::string full_path = fs.file_path(file_index, p);
|
2010-08-23 10:48:02 +02:00
|
|
|
if (!e.file_ptr->open(full_path, m, ec))
|
2008-02-25 05:41:21 +01:00
|
|
|
{
|
|
|
|
m_files.erase(i);
|
2010-08-23 10:48:02 +02:00
|
|
|
return boost::intrusive_ptr<file>();
|
2008-02-25 05:41:21 +01:00
|
|
|
}
|
2010-01-23 04:02:32 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (m_low_prio_io)
|
2013-01-07 02:56:40 +01:00
|
|
|
set_low_priority(e.file_ptr);
|
2010-01-23 04:02:32 +01:00
|
|
|
#endif
|
2008-07-20 18:34:01 +02:00
|
|
|
TORRENT_ASSERT(e.file_ptr->is_open());
|
2006-11-14 14:36:10 +01:00
|
|
|
e.mode = m;
|
|
|
|
}
|
2009-02-16 12:04:54 +01:00
|
|
|
TORRENT_ASSERT((e.mode & file::no_buffer) == (m & file::no_buffer));
|
2006-11-14 14:36:10 +01:00
|
|
|
return e.file_ptr;
|
|
|
|
}
|
|
|
|
// the file is not in our cache
|
|
|
|
if ((int)m_files.size() >= m_size)
|
|
|
|
{
|
|
|
|
// the file cache is at its maximum size, close
|
|
|
|
// the least recently used (lru) file from it
|
2009-05-09 23:11:25 +02:00
|
|
|
remove_oldest();
|
2006-11-14 14:36:10 +01:00
|
|
|
}
|
2008-02-25 05:41:21 +01:00
|
|
|
lru_file_entry e;
|
2008-07-18 01:41:46 +02:00
|
|
|
e.file_ptr.reset(new (std::nothrow)file);
|
2008-02-25 05:41:21 +01:00
|
|
|
if (!e.file_ptr)
|
|
|
|
{
|
2008-07-18 01:41:46 +02:00
|
|
|
ec = error_code(ENOMEM, get_posix_category());
|
2008-02-25 05:41:21 +01:00
|
|
|
return e.file_ptr;
|
|
|
|
}
|
2013-08-12 09:30:57 +02:00
|
|
|
std::string full_path = fs.file_path(file_index, p);
|
2010-08-23 10:48:02 +02:00
|
|
|
if (!e.file_ptr->open(full_path, m, ec))
|
|
|
|
return boost::intrusive_ptr<file>();
|
2013-01-07 02:56:40 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (m_low_prio_io)
|
|
|
|
set_low_priority(e.file_ptr);
|
|
|
|
#endif
|
2006-11-14 14:36:10 +01:00
|
|
|
e.mode = m;
|
|
|
|
e.key = st;
|
2013-08-12 09:30:57 +02:00
|
|
|
m_files.insert(std::make_pair(std::make_pair(st, file_index), e));
|
2008-07-20 18:34:01 +02:00
|
|
|
TORRENT_ASSERT(e.file_ptr->is_open());
|
2006-11-14 14:36:10 +01:00
|
|
|
return e.file_ptr;
|
|
|
|
}
|
|
|
|
|
2009-05-09 23:11:25 +02:00
|
|
|
void file_pool::remove_oldest()
|
|
|
|
{
|
2014-01-06 04:50:25 +01:00
|
|
|
TORRENT_ASSERT(m_in_use == 1337);
|
|
|
|
|
2009-05-09 23:11:25 +02:00
|
|
|
file_set::iterator i = std::min_element(m_files.begin(), m_files.end()
|
|
|
|
, boost::bind(&lru_file_entry::last_use, boost::bind(&file_set::value_type::second, _1))
|
|
|
|
< boost::bind(&lru_file_entry::last_use, boost::bind(&file_set::value_type::second, _2)));
|
|
|
|
if (i == m_files.end()) return;
|
2011-08-07 06:41:10 +02:00
|
|
|
|
|
|
|
#if TORRENT_CLOSE_MAY_BLOCK
|
2014-01-06 04:50:25 +01:00
|
|
|
mutex::scoped_lock l_(m_closer_mutex);
|
2011-08-07 06:41:10 +02:00
|
|
|
m_queued_for_close.push_back(i->second.file_ptr);
|
2014-01-06 04:50:25 +01:00
|
|
|
l_.unlock();
|
2011-08-07 06:41:10 +02:00
|
|
|
#endif
|
2009-05-09 23:11:25 +02:00
|
|
|
m_files.erase(i);
|
|
|
|
}
|
|
|
|
|
2010-11-25 00:49:22 +01:00
|
|
|
void file_pool::release(void* st, int file_index)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2014-01-06 04:50:25 +01:00
|
|
|
TORRENT_ASSERT(m_in_use == 1337);
|
2010-11-25 00:49:22 +01:00
|
|
|
file_set::iterator i = m_files.find(std::make_pair(st, file_index));
|
2011-08-07 06:41:10 +02:00
|
|
|
if (i == m_files.end()) return;
|
|
|
|
|
|
|
|
#if TORRENT_CLOSE_MAY_BLOCK
|
|
|
|
mutex::scoped_lock l2(m_closer_mutex);
|
|
|
|
m_queued_for_close.push_back(i->second.file_ptr);
|
|
|
|
l2.unlock();
|
|
|
|
#endif
|
|
|
|
m_files.erase(i);
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
|
2010-01-23 04:02:32 +01:00
|
|
|
// closes files belonging to the specified
|
|
|
|
// storage. If 0 is passed, all files are closed
|
2006-11-14 14:36:10 +01:00
|
|
|
void file_pool::release(void* st)
|
|
|
|
{
|
2014-01-06 09:03:05 +01:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2014-01-06 04:50:25 +01:00
|
|
|
TORRENT_ASSERT(m_in_use == 1337);
|
2010-01-23 04:02:32 +01:00
|
|
|
if (st == 0)
|
|
|
|
{
|
|
|
|
m_files.clear();
|
|
|
|
return;
|
|
|
|
}
|
2006-11-14 14:36:10 +01:00
|
|
|
|
2009-05-09 23:11:25 +02:00
|
|
|
for (file_set::iterator i = m_files.begin();
|
|
|
|
i != m_files.end();)
|
|
|
|
{
|
|
|
|
if (i->second.key == st)
|
|
|
|
m_files.erase(i++);
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
2006-11-14 14:36:10 +01:00
|
|
|
}
|
|
|
|
|
2006-11-14 16:53:38 +01:00
|
|
|
void file_pool::resize(int size)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(size > 0);
|
2014-01-06 04:50:25 +01:00
|
|
|
|
|
|
|
TORRENT_ASSERT(m_in_use == 1337);
|
|
|
|
if (size == m_size) return;
|
2014-01-06 08:59:47 +01:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2006-11-14 16:53:38 +01:00
|
|
|
m_size = size;
|
|
|
|
if (int(m_files.size()) <= m_size) return;
|
|
|
|
|
|
|
|
// close the least recently used files
|
|
|
|
while (int(m_files.size()) > m_size)
|
2009-05-09 23:11:25 +02:00
|
|
|
remove_oldest();
|
2006-11-14 16:53:38 +01:00
|
|
|
}
|
|
|
|
|
2006-11-14 14:36:10 +01:00
|
|
|
}
|
2010-08-23 10:48:02 +02:00
|
|
|
|