diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 16ee0bca4..e9103b9e4 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -50,6 +50,7 @@ namespace libtorrent , buffer_size(0) , piece(0) , offset(0) + , priority(0) {} enum action_t @@ -72,6 +73,12 @@ namespace libtorrent // to the error message std::string str; + // priority decides whether or not this + // job will skip entries in the queue or + // not. It always skips in front of entries + // with lower priority + int priority; + // this is called when operation completes boost::function callback; }; diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index e52196c76..505be8b6c 100755 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -202,7 +202,8 @@ namespace libtorrent void async_read( peer_request const& r , boost::function const& handler - , char* buffer = 0); + , char* buffer = 0 + , int priority = 0); void async_write( peer_request const& r diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index e07a884cf..9096cff0e 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -89,8 +89,15 @@ namespace libtorrent namespace { + // The semantic of this operator is: + // shouls lhs come before rhs in the job queue bool operator<(disk_io_job const& lhs, disk_io_job const& rhs) { + // NOTE: comparison inverted to make higher priority + // skip _in_front_of_ lower priority + if (lhs.priority > rhs.priority) return true; + if (lhs.priority < rhs.priority) return false; + if (lhs.storage.get() < rhs.storage.get()) return true; if (lhs.storage.get() > rhs.storage.get()) return false; if (lhs.piece < rhs.piece) return true; diff --git a/src/storage.cpp b/src/storage.cpp index e1ddd20ae..7124fab96 100755 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -1084,7 +1084,8 @@ namespace libtorrent void piece_manager::async_read( peer_request const& r , boost::function const& handler - , char* buffer) + , char* buffer + , int priority) { disk_io_job j; j.storage = this; @@ -1093,6 +1094,7 @@ namespace libtorrent j.offset = r.start; j.buffer_size = r.length; j.buffer = buffer; + j.priority = priority; // if a buffer is not specified, only one block can be read // since that is the size of the pool allocator's buffers assert(r.length <= 16 * 1024 || buffer != 0);