aio_write在发布版本上失败

时间:2011-04-06 21:47:02

标签: aio

我正在使用aio_write它正在调试版本中工作但不在发布版本中。我已检查构造函数是否已初始化所有内容,并且我没有收到有关未初始化变量的任何警告。该类以16K块的形式收集要写入光盘的数据。如果数据小于16K,即使在发布版本中也可以。如果数据大于16K,则仅写入第一个块。 WriteBuffer :: ContinueWriteToFile无限期地返回WriteFileState_Active。

WriteBuffer_posix.h:

class WriteBufferPlatformData
{
public:
    WriteBufferPlatformData();
    ~WriteBufferPlatformData();

    aiocb aioData;
    WriteBuffer::BufferVector::iterator currentBuffer;
};

WriteBuffer_posix.cpp:

WriteBufferPlatformData::WriteBufferPlatformData() :
    aioData(),
    currentBuffer()
{
    memset(&aioData,0,sizeof(aioData));
    aioData.aio_fildes=-1;
}

WriteBufferPlatformData::~WriteBufferPlatformData()
{
    if (0<=aioData.aio_fildes) {
        close(aioData.aio_fildes);
    }
}

WriteBuffer::WriteFileState WriteBuffer::StartWriteToFile(const char * filename)
{
    NYMPH_ASSERT(0>m_platformData->aioData.aio_fildes);

    m_platformData->aioData.aio_fildes=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);

    NYMPH_ASSERT2(0<=m_platformData->aioData.aio_fildes,"Could not open file for writing: %s (%d)",filename,errno);
    if (0>m_platformData->aioData.aio_fildes) {
        return WriteFileState_Failed;
    }

    if (m_buffers.empty()) {
        close(m_platformData->aioData.aio_fildes);
        m_platformData->aioData.aio_fildes=-1;
        return WriteFileState_Complete;
    }

    m_isWriting=true;
    m_platformData->currentBuffer=m_buffers.begin();
    return ContinueWriteToFile();
}

WriteBuffer::WriteFileState WriteBuffer::ContinueWriteToFile()
{
    NYMPH_ASSERT(0<=m_platformData->aioData.aio_fildes);

    if (0!=m_platformData->aioData.aio_nbytes) {
        int writeErrno=aio_error(&(m_platformData->aioData));
        if (EINPROGRESS==writeErrno) {
            return WriteFileState_Active;
        }

        NYMPH_ASSERT(aio_return(&(m_platformData->aioData))==m_platformData->aioData.aio_nbytes);
        m_platformData->aioData.aio_nbytes=0;
        ++(m_platformData->currentBuffer);

        if (m_buffers.end()==m_platformData->currentBuffer) {
            close(m_platformData->aioData.aio_fildes);
            m_platformData->aioData.aio_fildes=-1;
            return WriteFileState_Complete;
        }
    }

    if (0==m_platformData->aioData.aio_nbytes) {
        m_platformData->aioData.aio_buf=*(m_platformData->currentBuffer);
        if (m_buffers.back()==m_platformData->aioData.aio_buf) {
            m_platformData->aioData.aio_nbytes=m_offset;
        } else {
            m_platformData->aioData.aio_nbytes=kBufferSize;
        }
        m_platformData->aioData.aio_offset=lseek(m_platformData->aioData.aio_fildes,0,SEEK_END);

        if (0!=aio_write(&(m_platformData->aioData))) {
            m_platformData->aioData.aio_nbytes=0;
            NYMPH_ASSERT(EAGAIN==errno);
            if (EAGAIN!=errno) {
                close(m_platformData->aioData.aio_fildes);
                m_platformData->aioData.aio_fildes=-1;
                return WriteFileState_Failed;
            }
        }
    }

    return WriteFileState_Active;
}

1 个答案:

答案 0 :(得分:0)

尽管我在手册页中已经阅读过,但似乎需要调用aio_return并更改NYMPH_ASSERT(aio_return ...到NYMPH_VERIFY(aio_return ...更正了问题。