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

发布于 2024-10-19 14:16:42 字数 2741 浏览 0 评论 0原文

我尝试在 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。

另外,这个问题的答案似乎表明 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_* 函数是否对我的用例来说是一个死胡同?

I am attempting to use the aio_* functions for asynchronous file IO under Mac OS X, but I am having problems with getting some form of user data into the signal handler.

This is the code that sets up an operation:

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
};

This is then called from somewhere else like this:

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
}

My signal handling setup looks like this:

sigemptyset(&m_CurrentSIGHandler.sa_mask);
m_CurrentSIGHandler.sa_sigaction = aio_completion_handler;
m_CurrentSIGHandler.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &m_CurrentSIGHandler, &m_PreviousSIGHandler);

and the actual handler like this:

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
        // ...
    }
}

So the problem is that the si_value.sival_ptr is always NULL in the signal handler, instead of being the aio_context pointer that I set in the aiocb struct. I must have misunderstood something on how to do this, so can anyone tell me what I am doing wrong?

I am running on MacOSX 10.6, but I am (at least attempting to) compiling for 10.5 if that matters.

Also, the answer to this question seems to indicate that AIO should be disregarded entirely - is this really the case?

Update:

I found someone else having the same problem at http://lists.apple.com/archives/darwin-dev/2008/Oct/msg00054.html .

I also reviewed the kernel code at http://www.opensource.apple.com/source/xnu/xnu-1504.9.26/bsd/kern/kern_aio.c and if I understand it correctly, sigev_value is indeed completely ignored. I'm really at a loss here on what the expected usage of the aio_* functions is on Mac OS X. It does not appear to be the case that they can be used in the manner above anyway. Have I misunderstood something or are the aio_* functions a dead end for my use case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

跨年 2024-10-26 14:16:42

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

Mac OS X does not support real time signals ([RTS] in the posix specification), which is the section of POSIX which adds userdata pointers to signals. This makes Mac OS X very hard to use efficiently with AIO. Your only option is to loop through all your outstanding jobs and handle the ones that are complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文