Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@148: Chris@148: /* Chris@148: Sonic Visualiser Chris@148: An audio file viewer and annotation editor. Chris@148: Centre for Digital Music, Queen Mary, University of London. Chris@148: This file copyright 2006 Chris Cannam. Chris@148: Chris@148: This program is free software; you can redistribute it and/or Chris@148: modify it under the terms of the GNU General Public License as Chris@148: published by the Free Software Foundation; either version 2 of the Chris@148: License, or (at your option) any later version. See the file Chris@148: COPYING included with this distribution for more information. Chris@148: */ Chris@148: Chris@148: #include "MatrixFile.h" Chris@148: #include "base/TempDirectory.h" Chris@150: #include "system/System.h" Chris@148: #include "base/Profiler.h" Chris@148: #include "base/Exceptions.h" Chris@148: Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: Chris@148: #include Chris@148: Chris@148: #include Chris@148: #include Chris@148: Chris@148: #include Chris@148: #include Chris@148: Chris@148: //#define DEBUG_MATRIX_FILE 1 Chris@148: //#define DEBUG_MATRIX_FILE_READ_SET 1 Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE_READ_SET Chris@148: #define DEBUG_MATRIX_FILE 1 Chris@148: #endif Chris@148: Chris@148: std::map MatrixFile::m_refcount; Chris@148: QMutex MatrixFile::m_refcountMutex; Chris@148: Chris@148: MatrixFile::ResizeableBitsetMap MatrixFile::m_columnBitsets; Chris@148: QMutex MatrixFile::m_columnBitsetWriteMutex; Chris@148: Chris@148: FileReadThread *MatrixFile::m_readThread = 0; Chris@148: Chris@148: static size_t totalStorage = 0; Chris@148: static size_t totalMemory = 0; Chris@148: static size_t totalCount = 0; Chris@148: Chris@148: MatrixFile::MatrixFile(QString fileBase, Mode mode, Chris@148: size_t cellSize, bool eagerCache) : Chris@148: m_fd(-1), Chris@148: m_mode(mode), Chris@148: m_flags(0), Chris@148: m_fmode(0), Chris@148: m_cellSize(cellSize), Chris@148: m_width(0), Chris@148: m_height(0), Chris@148: m_headerSize(2 * sizeof(size_t)), Chris@148: m_defaultCacheWidth(1024), Chris@148: m_prevX(0), Chris@148: m_eagerCache(eagerCache), Chris@148: m_requestToken(-1), Chris@148: m_spareData(0), Chris@148: m_columnBitset(0) Chris@148: { Chris@148: Profiler profiler("MatrixFile::MatrixFile", true); Chris@148: Chris@148: if (!m_readThread) { Chris@148: m_readThread = new FileReadThread; Chris@148: m_readThread->start(); Chris@148: } Chris@148: Chris@148: m_cache.data = 0; Chris@148: Chris@148: QDir tempDir(TempDirectory::getInstance()->getPath()); Chris@148: QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase))); Chris@148: bool newFile = !QFileInfo(fileName).exists(); Chris@148: Chris@148: if (newFile && m_mode == ReadOnly) { Chris@148: std::cerr << "ERROR: MatrixFile::MatrixFile: Read-only mode " Chris@148: << "specified, but cache file does not exist" << std::endl; Chris@148: throw FileNotFound(fileName); Chris@148: } Chris@148: Chris@148: if (!newFile && m_mode == ReadWrite) { Chris@148: std::cerr << "Note: MatrixFile::MatrixFile: Read/write mode " Chris@148: << "specified, but file already exists; falling back to " Chris@148: << "read-only mode" << std::endl; Chris@148: m_mode = ReadOnly; Chris@148: } Chris@148: Chris@148: if (!eagerCache && m_mode == ReadOnly) { Chris@148: std::cerr << "WARNING: MatrixFile::MatrixFile: Eager cacheing not " Chris@148: << "specified, but file is open in read-only mode -- cache " Chris@148: << "will not be used" << std::endl; Chris@148: } Chris@148: Chris@148: m_flags = 0; Chris@148: m_fmode = S_IRUSR | S_IWUSR; Chris@148: Chris@148: if (m_mode == ReadWrite) { Chris@148: m_flags = O_RDWR | O_CREAT; Chris@148: } else { Chris@148: m_flags = O_RDONLY; Chris@148: } Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE Chris@148: std::cerr << "MatrixFile::MatrixFile: opening " << fileName.toStdString() << "..." << std::endl; Chris@148: #endif Chris@148: Chris@148: if ((m_fd = ::open(fileName.toLocal8Bit(), m_flags, m_fmode)) < 0) { Chris@148: ::perror("Open failed"); Chris@148: std::cerr << "ERROR: MatrixFile::MatrixFile: " Chris@148: << "Failed to open cache file \"" Chris@148: << fileName.toStdString() << "\""; Chris@148: if (m_mode == ReadWrite) std::cerr << " for writing"; Chris@148: std::cerr << std::endl; Chris@148: throw FailedToOpenFile(fileName); Chris@148: } Chris@148: Chris@148: if (newFile) { Chris@148: resize(0, 0); // write header Chris@148: } else { Chris@148: size_t header[2]; Chris@148: if (::read(m_fd, header, 2 * sizeof(size_t)) < 0) { Chris@148: perror("Read failed"); Chris@148: std::cerr << "ERROR: MatrixFile::MatrixFile: " Chris@148: << "Failed to read header (fd " << m_fd << ", file \"" Chris@148: << fileName.toStdString() << "\")" << std::endl; Chris@148: throw FileReadFailed(fileName); Chris@148: } Chris@148: m_width = header[0]; Chris@148: m_height = header[1]; Chris@148: seekTo(0, 0); Chris@148: } Chris@148: Chris@148: m_fileName = fileName; Chris@148: Chris@148: m_columnBitsetWriteMutex.lock(); Chris@148: Chris@148: if (m_columnBitsets.find(m_fileName) == m_columnBitsets.end()) { Chris@148: m_columnBitsets[m_fileName] = new ResizeableBitset; Chris@148: } Chris@148: m_columnBitset = m_columnBitsets[m_fileName]; Chris@148: Chris@148: m_columnBitsetWriteMutex.unlock(); Chris@148: Chris@148: QMutexLocker locker(&m_refcountMutex); Chris@148: ++m_refcount[fileName]; Chris@148: Chris@148: std::cerr << "MatrixFile(" << this << "): fd " << m_fd << ", file " << fileName.toStdString() << ", ref " << m_refcount[fileName] << std::endl; Chris@148: Chris@148: // std::cerr << "MatrixFile::MatrixFile: Done, size is " << "(" << m_width << ", " << m_height << ")" << std::endl; Chris@148: Chris@148: ++totalCount; Chris@148: Chris@148: } Chris@148: Chris@148: MatrixFile::~MatrixFile() Chris@148: { Chris@148: char *requestData = 0; Chris@148: Chris@148: if (m_requestToken >= 0) { Chris@148: FileReadThread::Request request; Chris@148: if (m_readThread->getRequest(m_requestToken, request)) { Chris@148: requestData = request.data; Chris@148: } Chris@148: m_readThread->cancel(m_requestToken); Chris@148: } Chris@148: Chris@148: if (requestData) free(requestData); Chris@148: if (m_cache.data) free(m_cache.data); Chris@148: if (m_spareData) free(m_spareData); Chris@148: Chris@148: if (m_fd >= 0) { Chris@148: if (::close(m_fd) < 0) { Chris@148: ::perror("MatrixFile::~MatrixFile: close failed"); Chris@148: } Chris@148: } Chris@148: Chris@148: if (m_fileName != "") { Chris@148: Chris@148: QMutexLocker locker(&m_refcountMutex); Chris@148: Chris@148: if (--m_refcount[m_fileName] == 0) { Chris@148: Chris@148: if (::unlink(m_fileName.toLocal8Bit())) { Chris@148: ::perror("Unlink failed"); Chris@148: std::cerr << "WARNING: MatrixFile::~MatrixFile: reference count reached 0, but failed to unlink file \"" << m_fileName.toStdString() << "\"" << std::endl; Chris@148: } else { Chris@148: std::cerr << "deleted " << m_fileName.toStdString() << std::endl; Chris@148: } Chris@148: Chris@148: QMutexLocker locker2(&m_columnBitsetWriteMutex); Chris@148: m_columnBitsets.erase(m_fileName); Chris@148: delete m_columnBitset; Chris@148: } Chris@148: } Chris@148: Chris@148: totalStorage -= (m_headerSize + (m_width * m_height * m_cellSize)); Chris@148: totalMemory -= (2 * m_defaultCacheWidth * m_height * m_cellSize); Chris@148: totalCount --; Chris@148: Chris@148: std::cerr << "MatrixFile::~MatrixFile: " << std::endl; Chris@148: std::cerr << "Total storage now " << totalStorage/1024 << "K, theoretical max memory " Chris@148: << totalMemory/1024 << "K in " << totalCount << " instances" << std::endl; Chris@148: Chris@148: } Chris@148: Chris@148: void Chris@148: MatrixFile::resize(size_t w, size_t h) Chris@148: { Chris@148: Profiler profiler("MatrixFile::resize", true); Chris@148: Chris@148: assert(m_mode == ReadWrite); Chris@148: Chris@148: QMutexLocker locker(&m_fdMutex); Chris@148: Chris@148: totalStorage -= (m_headerSize + (m_width * m_height * m_cellSize)); Chris@148: totalMemory -= (2 * m_defaultCacheWidth * m_height * m_cellSize); Chris@148: Chris@148: off_t off = m_headerSize + (w * h * m_cellSize); Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE Chris@148: std::cerr << "MatrixFile::resize(" << w << ", " << h << "): resizing file" << std::endl; Chris@148: #endif Chris@148: Chris@148: if (w * h < m_width * m_height) { Chris@148: if (::ftruncate(m_fd, off) < 0) { Chris@148: ::perror("WARNING: MatrixFile::resize: ftruncate failed"); Chris@148: throw FileOperationFailed(m_fileName, "ftruncate"); Chris@148: } Chris@148: } Chris@148: Chris@148: m_width = 0; Chris@148: m_height = 0; Chris@148: Chris@148: if (::lseek(m_fd, 0, SEEK_SET) == (off_t)-1) { Chris@148: ::perror("ERROR: MatrixFile::resize: Seek to write header failed"); Chris@148: throw FileOperationFailed(m_fileName, "lseek"); Chris@148: } Chris@148: Chris@148: size_t header[2]; Chris@148: header[0] = w; Chris@148: header[1] = h; Chris@148: if (::write(m_fd, header, 2 * sizeof(size_t)) != 2 * sizeof(size_t)) { Chris@148: ::perror("ERROR: MatrixFile::resize: Failed to write header"); Chris@148: throw FileOperationFailed(m_fileName, "write"); Chris@148: } Chris@148: Chris@148: if (w > 0 && m_defaultCacheWidth > w) { Chris@148: m_defaultCacheWidth = w; Chris@148: } Chris@148: Chris@148: static size_t maxCacheMB = 16; Chris@148: if (2 * m_defaultCacheWidth * h * m_cellSize > maxCacheMB * 1024 * 1024) { //!!! Chris@148: m_defaultCacheWidth = (maxCacheMB * 1024 * 1024) / (2 * h * m_cellSize); Chris@148: if (m_defaultCacheWidth < 16) m_defaultCacheWidth = 16; Chris@148: } Chris@148: Chris@148: if (m_columnBitset) { Chris@148: QMutexLocker locker(&m_columnBitsetWriteMutex); Chris@148: m_columnBitset->resize(w); Chris@148: } Chris@148: Chris@148: if (m_cache.data) { Chris@148: free(m_cache.data); Chris@148: m_cache.data = 0; Chris@148: } Chris@148: Chris@148: if (m_spareData) { Chris@148: free(m_spareData); Chris@148: m_spareData = 0; Chris@148: } Chris@148: Chris@148: m_width = w; Chris@148: m_height = h; Chris@148: Chris@148: totalStorage += (m_headerSize + (m_width * m_height * m_cellSize)); Chris@148: totalMemory += (2 * m_defaultCacheWidth * m_height * m_cellSize); Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE Chris@148: std::cerr << "MatrixFile::resize(" << w << ", " << h << "): cache width " Chris@148: << m_defaultCacheWidth << ", storage " Chris@148: << (m_headerSize + w * h * m_cellSize) << ", mem " Chris@148: << (2 * h * m_defaultCacheWidth * m_cellSize) << std::endl; Chris@148: Chris@148: std::cerr << "Total storage " << totalStorage/1024 << "K, theoretical max memory " Chris@148: << totalMemory/1024 << "K in " << totalCount << " instances" << std::endl; Chris@148: #endif Chris@148: Chris@148: seekTo(0, 0); Chris@148: } Chris@148: Chris@148: void Chris@148: MatrixFile::reset() Chris@148: { Chris@148: Profiler profiler("MatrixFile::reset", true); Chris@148: Chris@148: assert (m_mode == ReadWrite); Chris@148: Chris@148: if (m_eagerCache) { Chris@148: void *emptyCol = calloc(m_height, m_cellSize); Chris@148: for (size_t x = 0; x < m_width; ++x) setColumnAt(x, emptyCol); Chris@148: free(emptyCol); Chris@148: } Chris@148: Chris@148: if (m_columnBitset) { Chris@148: QMutexLocker locker(&m_columnBitsetWriteMutex); Chris@148: m_columnBitset->resize(m_width); Chris@148: } Chris@148: } Chris@148: Chris@148: void Chris@148: MatrixFile::getColumnAt(size_t x, void *data) Chris@148: { Chris@148: // Profiler profiler("MatrixFile::getColumnAt"); Chris@148: Chris@148: // assert(haveSetColumnAt(x)); Chris@148: Chris@148: if (getFromCache(x, 0, m_height, data)) return; Chris@148: Chris@148: // Profiler profiler2("MatrixFile::getColumnAt (uncached)"); Chris@148: Chris@148: ssize_t r = 0; Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE Chris@148: std::cerr << "MatrixFile::getColumnAt(" << x << ")" Chris@148: << ": reading the slow way"; Chris@148: Chris@148: if (m_requestToken >= 0 && Chris@148: x >= m_requestingX && Chris@148: x < m_requestingX + m_requestingWidth) { Chris@148: Chris@148: std::cerr << " (awaiting " << m_requestingX << ", " << m_requestingWidth << " from disk)"; Chris@148: } Chris@148: Chris@148: std::cerr << std::endl; Chris@148: #endif Chris@148: Chris@148: m_fdMutex.lock(); Chris@148: Chris@148: if (seekTo(x, 0)) { Chris@148: r = ::read(m_fd, data, m_height * m_cellSize); Chris@148: } Chris@148: Chris@148: m_fdMutex.unlock(); Chris@148: Chris@148: if (r < 0) { Chris@148: ::perror("MatrixFile::getColumnAt: read failed"); Chris@148: throw FileReadFailed(m_fileName); Chris@148: } Chris@148: Chris@148: return; Chris@148: } Chris@148: Chris@148: bool Chris@148: MatrixFile::getFromCache(size_t x, size_t ystart, size_t ycount, void *data) Chris@148: { Chris@148: m_cacheMutex.lock(); Chris@148: Chris@148: if (!m_cache.data || x < m_cache.x || x >= m_cache.x + m_cache.width) { Chris@148: bool left = (m_cache.data && x < m_cache.x); Chris@148: m_cacheMutex.unlock(); Chris@148: primeCache(x, left); // this doesn't take effect until a later callback Chris@148: m_prevX = x; Chris@148: return false; Chris@148: } Chris@148: Chris@148: memcpy(data, Chris@148: m_cache.data + m_cellSize * ((x - m_cache.x) * m_height + ystart), Chris@148: ycount * m_cellSize); Chris@148: Chris@148: m_cacheMutex.unlock(); Chris@148: Chris@148: if (m_cache.x > 0 && x < m_prevX && x < m_cache.x + m_cache.width/4) { Chris@148: primeCache(x, true); Chris@148: } Chris@148: Chris@148: if (m_cache.x + m_cache.width < m_width && Chris@148: x > m_prevX && Chris@148: x > m_cache.x + (m_cache.width * 3) / 4) { Chris@148: primeCache(x, false); Chris@148: } Chris@148: Chris@148: m_prevX = x; Chris@148: return true; Chris@148: } Chris@148: Chris@148: void Chris@148: MatrixFile::setColumnAt(size_t x, const void *data) Chris@148: { Chris@148: assert(m_mode == ReadWrite); Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE_READ_SET Chris@148: std::cerr << "MatrixFile::setColumnAt(" << x << ")" << std::endl; Chris@148: #endif Chris@148: Chris@148: ssize_t w = 0; Chris@148: bool seekFailed = false; Chris@148: Chris@148: m_fdMutex.lock(); Chris@148: Chris@148: if (seekTo(x, 0)) { Chris@148: w = ::write(m_fd, data, m_height * m_cellSize); Chris@148: } else { Chris@148: seekFailed = true; Chris@148: } Chris@148: Chris@148: m_fdMutex.unlock(); Chris@148: Chris@148: if (!seekFailed && w != ssize_t(m_height * m_cellSize)) { Chris@148: ::perror("WARNING: MatrixFile::setColumnAt: write failed"); Chris@148: throw FileOperationFailed(m_fileName, "write"); Chris@148: } else if (seekFailed) { Chris@148: throw FileOperationFailed(m_fileName, "seek"); Chris@148: } else { Chris@148: QMutexLocker locker(&m_columnBitsetWriteMutex); Chris@148: m_columnBitset->set(x); Chris@148: } Chris@148: } Chris@148: Chris@148: void Chris@148: MatrixFile::suspend() Chris@148: { Chris@148: QMutexLocker locker(&m_fdMutex); Chris@148: QMutexLocker locker2(&m_cacheMutex); Chris@148: Chris@148: if (m_fd < 0) return; // already suspended Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE Chris@148: std::cerr << "MatrixFile(" << this << ":" << m_fileName.toStdString() << ")::suspend(): fd was " << m_fd << std::endl; Chris@148: #endif Chris@148: Chris@148: if (m_requestToken >= 0) { Chris@148: void *data = 0; Chris@148: FileReadThread::Request request; Chris@148: if (m_readThread->getRequest(m_requestToken, request)) { Chris@148: data = request.data; Chris@148: } Chris@148: m_readThread->cancel(m_requestToken); Chris@148: if (data) free(data); Chris@148: m_requestToken = -1; Chris@148: } Chris@148: Chris@148: if (m_cache.data) { Chris@148: free(m_cache.data); Chris@148: m_cache.data = 0; Chris@148: } Chris@148: Chris@148: if (m_spareData) { Chris@148: free(m_spareData); Chris@148: m_spareData = 0; Chris@148: } Chris@148: Chris@148: if (::close(m_fd) < 0) { Chris@148: ::perror("WARNING: MatrixFile::suspend: close failed"); Chris@148: throw FileOperationFailed(m_fileName, "close"); Chris@148: } Chris@148: Chris@148: m_fd = -1; Chris@148: } Chris@148: Chris@148: void Chris@148: MatrixFile::resume() Chris@148: { Chris@148: if (m_fd >= 0) return; Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE Chris@148: std::cerr << "MatrixFile(" << this << ")::resume()" << std::endl; Chris@148: #endif Chris@148: Chris@148: if ((m_fd = ::open(m_fileName.toLocal8Bit(), m_flags, m_fmode)) < 0) { Chris@148: ::perror("Open failed"); Chris@148: std::cerr << "ERROR: MatrixFile::resume: " Chris@148: << "Failed to open cache file \"" Chris@148: << m_fileName.toStdString() << "\""; Chris@148: if (m_mode == ReadWrite) std::cerr << " for writing"; Chris@148: std::cerr << std::endl; Chris@148: throw FailedToOpenFile(m_fileName); Chris@148: } Chris@148: Chris@148: std::cerr << "MatrixFile(" << this << ":" << m_fileName.toStdString() << ")::resume(): fd is " << m_fd << std::endl; Chris@148: } Chris@148: Chris@148: void Chris@148: MatrixFile::primeCache(size_t x, bool goingLeft) Chris@148: { Chris@148: // Profiler profiler("MatrixFile::primeCache"); Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE_READ_SET Chris@148: std::cerr << "MatrixFile::primeCache(" << x << ", " << goingLeft << ")" << std::endl; Chris@148: #endif Chris@148: Chris@148: size_t rx = x; Chris@148: size_t rw = m_defaultCacheWidth; Chris@148: Chris@148: size_t left = rw / 3; Chris@148: if (goingLeft) left = (rw * 2) / 3; Chris@148: Chris@148: if (rx > left) rx -= left; Chris@148: else rx = 0; Chris@148: Chris@148: if (rx + rw > m_width) rw = m_width - rx; Chris@148: Chris@148: if (!m_eagerCache) { Chris@148: Chris@148: size_t ti = 0; Chris@148: Chris@148: for (ti = 0; ti < rw; ++ti) { Chris@148: if (!m_columnBitset->get(rx + ti)) break; Chris@148: } Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE Chris@148: if (ti < rw) { Chris@148: std::cerr << "eagerCache is false and there's a hole at " Chris@148: << rx + ti << ", reducing rw from " << rw << " to " Chris@148: << ti << std::endl; Chris@148: } Chris@148: #endif Chris@148: Chris@148: rw = std::min(rw, ti); Chris@148: if (rw < 10 || rx + rw <= x) return; Chris@148: } Chris@148: Chris@148: QMutexLocker locker(&m_cacheMutex); Chris@148: Chris@148: FileReadThread::Request request; Chris@148: Chris@148: if (m_requestToken >= 0 && Chris@148: m_readThread->getRequest(m_requestToken, request)) { Chris@148: Chris@148: if (x >= m_requestingX && Chris@148: x < m_requestingX + m_requestingWidth) { Chris@148: Chris@148: if (m_readThread->isReady(m_requestToken)) { Chris@148: Chris@148: if (!request.successful) { Chris@148: std::cerr << "ERROR: MatrixFile::primeCache: Last request was unsuccessful" << std::endl; Chris@148: throw FileReadFailed(m_fileName); Chris@148: } Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE_READ_SET Chris@148: std::cerr << "last request is ready! (" << m_requestingX << ", "<< m_requestingWidth << ")" << std::endl; Chris@148: #endif Chris@148: Chris@148: m_cache.x = (request.start - m_headerSize) / (m_height * m_cellSize); Chris@148: m_cache.width = request.size / (m_height * m_cellSize); Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE_READ_SET Chris@148: std::cerr << "received last request: actual size is: " << m_cache.x << ", " << m_cache.width << std::endl; Chris@148: #endif Chris@148: Chris@148: if (m_cache.data) { Chris@148: if (m_spareData) free(m_spareData); Chris@148: m_spareData = m_cache.data; Chris@148: } Chris@148: m_cache.data = request.data; Chris@148: Chris@148: m_readThread->done(m_requestToken); Chris@148: m_requestToken = -1; Chris@148: } Chris@148: Chris@148: // already requested something covering this area; wait for it Chris@148: return; Chris@148: } Chris@148: Chris@148: // the current request is no longer of any use Chris@148: m_readThread->cancel(m_requestToken); Chris@148: Chris@148: // crude way to avoid leaking the data Chris@148: while (!m_readThread->isCancelled(m_requestToken)) { Chris@148: usleep(10000); Chris@148: } Chris@148: Chris@148: #ifdef DEBUG_MATRIX_FILE_READ_SET Chris@148: std::cerr << "cancelled " << m_requestToken << std::endl; Chris@148: #endif Chris@148: Chris@148: if (m_spareData) free(m_spareData); Chris@148: m_spareData = request.data; Chris@148: m_readThread->done(m_requestToken); Chris@148: Chris@148: m_requestToken = -1; Chris@148: } Chris@148: Chris@148: if (m_fd < 0) { Chris@148: m_fdMutex.lock(); Chris@148: if (m_fd < 0) resume(); Chris@148: m_fdMutex.unlock(); Chris@148: } Chris@148: Chris@148: request.fd = m_fd; Chris@148: request.mutex = &m_fdMutex; Chris@148: request.start = m_headerSize + rx * m_height * m_cellSize; Chris@148: request.size = rw * m_height * m_cellSize; Chris@148: request.data = (char *)realloc(m_spareData, rw * m_height * m_cellSize); Chris@148: MUNLOCK(request.data, rw * m_height * m_cellSize); Chris@148: m_spareData = 0; Chris@148: Chris@148: m_requestingX = rx; Chris@148: m_requestingWidth = rw; Chris@148: Chris@148: int token = m_readThread->request(request); Chris@148: #ifdef DEBUG_MATRIX_FILE_READ_SET Chris@148: std::cerr << "MatrixFile::primeCache: request token is " Chris@148: << token << " (x = [" << rx << "], w = [" << rw << "], left = [" << goingLeft << "])" << std::endl; Chris@148: #endif Chris@148: m_requestToken = token; Chris@148: } Chris@148: Chris@148: bool Chris@148: MatrixFile::seekTo(size_t x, size_t y) Chris@148: { Chris@148: if (m_fd < 0) resume(); Chris@148: Chris@148: off_t off = m_headerSize + (x * m_height + y) * m_cellSize; Chris@148: Chris@148: if (::lseek(m_fd, off, SEEK_SET) == (off_t)-1) { Chris@148: ::perror("Seek failed"); Chris@148: std::cerr << "ERROR: MatrixFile::seekTo(" << x << ", " << y Chris@148: << ") failed" << std::endl; Chris@148: return false; Chris@148: } Chris@148: Chris@148: return true; Chris@148: } Chris@148: