我想使用Mac OS X下的aio_*函数进行异步文件IO,但在将某些形式的用户数据传递到信号处理程序时遇到了问题。
这是设置操作的代码:
所以问题在于,信号处理程序中的si_value.sival_ptr始终为NULL,而不是我在aiocb结构中设置的aio_context指针。我一定对如何做这件事有所误解,所以有人可以告诉我我做错了什么吗?
我正在运行MacOSX 10.6,但我正在尝试编译10.5,如果有影响的话。
此外,这个问题的答案似乎表明应该完全忽略AIO - 这是真的吗?
更新:
我发现另一个人在http://lists.apple.com/archives/darwin-dev/2008/Oct/msg00054.html也遇到了同样的问题。
我还审查了http://www.opensource.apple.com/source/xnu/xnu-1504.9.26/bsd/kern/kern_aio.c 上的内核代码,如果我理解正确,sigev_value 确实完全被忽略。在这里,我真的不知道 Mac OS X 上 aio_* 函数的预期使用方式。无论如何,它们似乎都不能以上述方式使用。我是否误解了什么或者 aio_* 函数对我的用例来说是死路一条?
这是设置操作的代码:
class aio_context {
public:
aio_context(int fildes, boost::uint64_t offset,
const MyBufferClassPtr &buffer)
{
// The aiocb struct must be zeroed
memset(&m_aiocb, 0, sizeof(struct aiocb));
// Set what to do
m_aiocb.aio_fildes = fildes;
m_aiocb.aio_buf = buffer->data();
m_aiocb.aio_nbytes = buffer->size();
m_aiocb.aio_offset = offset;
// Set notification
m_aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
m_aiocb.aio_sigevent.sigev_signo = SIGUSR1;
// ATTEMPT TO SET A VALUE THAT CAN BE READ IN THE HANDLER
m_aiocb.aio_sigevent.sigev_value.sival_ptr = this;
}
struct aiocb* GetAiocbp()
{
return &m_aiocb;
}
private:
struct aiocb m_aiocb;
// Some more context here
};
然后像这样从其他地方调用:
aio_context *ctx = new aio_context(file_descriptor, offset, data);
// set some more context here
int ret = aio_write(ctx->GetAiocbp());
if (0 != ret) {
// throw something
}
我的信号处理设置如下:
sigemptyset(&m_CurrentSIGHandler.sa_mask);
m_CurrentSIGHandler.sa_sigaction = aio_completion_handler;
m_CurrentSIGHandler.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &m_CurrentSIGHandler, &m_PreviousSIGHandler);
以及实际的处理程序如下:
void aio_completion_handler(int signo, siginfo_t *info, void *context)
{
if (info->si_signo == SIGUSR1) {
// Get the aio operation
aio_context *ctx = static_cast<aio_context *>(info->si_value.sival_ptr);
// THIS ASSERT ALWAYS FAILS - ctx IS NULL
assert(ctx);
// next check aio_error and aio_return using the aicb member of the ctx
// ...
}
}
所以问题在于,信号处理程序中的si_value.sival_ptr始终为NULL,而不是我在aiocb结构中设置的aio_context指针。我一定对如何做这件事有所误解,所以有人可以告诉我我做错了什么吗?
我正在运行MacOSX 10.6,但我正在尝试编译10.5,如果有影响的话。
此外,这个问题的答案似乎表明应该完全忽略AIO - 这是真的吗?
更新:
我发现另一个人在http://lists.apple.com/archives/darwin-dev/2008/Oct/msg00054.html也遇到了同样的问题。
我还审查了http://www.opensource.apple.com/source/xnu/xnu-1504.9.26/bsd/kern/kern_aio.c 上的内核代码,如果我理解正确,sigev_value 确实完全被忽略。在这里,我真的不知道 Mac OS X 上 aio_* 函数的预期使用方式。无论如何,它们似乎都不能以上述方式使用。我是否误解了什么或者 aio_* 函数对我的用例来说是死路一条?