首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Mac中获取aio信号处理程序的用户数据

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

Stack Overflow用户
提问于 2011-02-25 10:33:48
回答 1查看 1.4K关注 0票数 4

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

这是设置操作的代码:

代码语言:javascript
复制
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
};

然后从这样的其他地方调用它:

代码语言:javascript
复制
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
}

我的信号处理设置如下:

代码语言:javascript
复制
sigemptyset(&m_CurrentSIGHandler.sa_mask);
m_CurrentSIGHandler.sa_sigaction = aio_completion_handler;
m_CurrentSIGHandler.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &m_CurrentSIGHandler, &m_PreviousSIGHandler);

实际的处理程序是这样的:

代码语言:javascript
复制
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始终是空的,而不是我在aiocb结构中设置的aio_context指针。我一定是误解了该怎么做,所以有人能告诉我我做错了什么吗?

我正在运行MacOSX 10.6,但如果这一点重要的话,我正在(至少试图)为10.5编译。

而且,这个问题的答案似乎表明,应该完全忽略AIO --真的是这样吗?

更新:

我在http://lists.apple.com/archives/darwin-dev/2008/Oct/msg00054.html发现了其他人也有同样的问题。

我还回顾了aio.c的内核代码,如果我正确理解它,sigev_value确实会被完全忽略。在Mac上,我对aio_*函数的预期使用情况感到困惑。无论如何,它们似乎并不是可以以上述方式使用的。我是否误解了什么,或者aio_*函数是我的用例的死胡同?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-15 04:40:55

Mac不支持实时信号(posix规范中的RTS),这是POSIX中添加用户数据指针到信号的部分。这使得Mac很难在AIO中高效地使用。您唯一的选择是循环遍历所有未完成的任务,并处理那些已经完成的任务。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5116151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档