gpt4 book ai didi

c++ - 如何在 Mac OS X 中获取 aio 信号处理程序的用户数据

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:33 24 4
gpt4 key购买 nike

我试图在 Mac OS X 下使用 aio_* 函数进行异步文件 IO,但我在将某种形式的用户数据输入信号处理程序时遇到了问题。

这是设置操作的代码:

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。

此外,the answer to this question似乎表明应该完全忽视 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 确实被完全忽略了。我真的不知道 aio_* 函数在 Mac OS X 上的预期用途是什么。无论如何,它们似乎都不能以上述方式使用。我是不是误解了什么,或者 aio_* 函数是我的用例的死胡同?

最佳答案

Mac OS X 不支持实时信号(posix specification 中的[RTS]),这是将用户数据指针添加到信号的POSIX 部分。这使得 Mac OS X 很难有效地与 AIO 一起使用。您唯一的选择是遍历所有未完成的工作并处理完成的工作。

关于c++ - 如何在 Mac OS X 中获取 aio 信号处理程序的用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5116151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com