如何在Mac OS X中为aio信号处理程序获取用户数据 [英] How to get user data for aio signal handler in Mac OS X

查看:136
本文介绍了如何在Mac OS X中为aio信号处理程序获取用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Mac OS X下将aio_ *函数用于异步文件IO,但是在将某种形式的用户数据放入信号处理程序中时遇到问题.

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.

这是设置操作的代码:

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

所以问题是信号处理程序中的si_value.sival_ptr始终为NULL,而不是我在aiocb结构中设置的aio_context指针.我一定对操作方法有误解,所以谁能告诉我我做错了什么?

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?

我正在MacOSX 10.6上运行,但如果有问题,我(至少试图)编译为10.5.

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

此外,这个问题的答案似乎表明应该完全忽略AIO-确实是这样吗?

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

更新:

我在 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?

推荐答案

Mac OS X不支持实时信号(

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.

这篇关于如何在Mac OS X中为aio信号处理程序获取用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆