mirror of
https://github.com/apache/nuttx.git
synced 2026-10-23 14:58:47 +00:00
Previously, lio_listio() called aio_read()/aio_write() to submit the I/O and only then initialized the per-request notification state (aio_priv based), so a worker thread could complete an operation before that state was set up (thread-unsafe), and the completion notification hijacked the per-request sigevent machinery. Rework the implementation: lio_listio() now links every aiocb of the batch into a list (lio_link) before any I/O is submitted. When an operation completes, aio_signal() removes its node from the list under aio_lock() and delivers the lio_listio completion notification only when the list becomes empty. The unused aio_priv field is replaced by the lio_link/lio_sigevent/lio_sigwork fields in struct aiocb. Co-developed-by: wushenhui <wushenhui@xiaomi.com> Signed-off-by: wushenhui <wushenhui@xiaomi.com> Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
138 lines
3.8 KiB
C
138 lines
3.8 KiB
C
/****************************************************************************
|
|
* fs/aio/aio_signal.c
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
* this work for additional information regarding copyright ownership. The
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
* "License"); you may not use this file except in compliance with the
|
|
* License. You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
* License for the specific language governing permissions and limitations
|
|
* under the License.
|
|
*
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <sched.h>
|
|
#include <signal.h>
|
|
#include <aio.h>
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <nuttx/debug.h>
|
|
|
|
#include <nuttx/signal.h>
|
|
|
|
#include "aio/aio.h"
|
|
|
|
#ifdef CONFIG_FS_AIO
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: aio_signal
|
|
*
|
|
* Description:
|
|
* Signal the client that an I/O has completed.
|
|
*
|
|
* Input Parameters:
|
|
* pid - ID of the task to signal
|
|
* aiocbp - Pointer to the asynchronous I/O state structure that includes
|
|
* information about how to signal the client
|
|
*
|
|
* Returned Value:
|
|
* Zero (OK) if the client was successfully signalled. Otherwise, a
|
|
* negated errno value is returned.
|
|
*
|
|
* Assumptions:
|
|
* This function runs only in the context of the worker thread.
|
|
*
|
|
****************************************************************************/
|
|
|
|
int aio_signal(pid_t pid, FAR struct aiocb *aiocbp)
|
|
{
|
|
union sigval value;
|
|
int status;
|
|
int ret;
|
|
|
|
DEBUGASSERT(aiocbp);
|
|
|
|
ret = OK; /* Assume success */
|
|
|
|
/* Signal the client */
|
|
|
|
ret = nxsig_notification(pid, &aiocbp->aio_sigevent,
|
|
SI_ASYNCIO, &aiocbp->aio_sigwork);
|
|
if (ret < 0)
|
|
{
|
|
ferr("ERROR: nxsig_notification failed: %d\n", ret);
|
|
}
|
|
|
|
/* Send the poll signal in any event in case the caller is waiting
|
|
* on sig_suspend();
|
|
*/
|
|
|
|
value.sival_ptr = aiocbp;
|
|
status = nxsig_queue(pid, SIGPOLL, value);
|
|
if (status < 0)
|
|
{
|
|
ferr("ERROR: nxsig_queue #2 failed: %d\n", status);
|
|
if (ret >= OK)
|
|
{
|
|
ret = status;
|
|
}
|
|
}
|
|
|
|
if (list_in_list(&aiocbp->lio_link))
|
|
{
|
|
/* This I/O is queued by lio_listio, remove this I/O from the list,
|
|
* signal the client when all I/O is completed
|
|
*/
|
|
|
|
aio_lock();
|
|
status = list_is_empty(&aiocbp->lio_link);
|
|
list_delete(&aiocbp->lio_link);
|
|
aio_unlock();
|
|
|
|
if (status)
|
|
{
|
|
status = nxsig_notification(pid, &aiocbp->lio_sigevent, SI_ASYNCIO,
|
|
&aiocbp->lio_sigwork);
|
|
if (status < 0)
|
|
{
|
|
ferr("ERROR: nxsig_notification failed: %d\n", status);
|
|
if (ret >= OK)
|
|
{
|
|
ret = status;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Make sure that errno is set correctly on return */
|
|
|
|
if (ret < 0)
|
|
{
|
|
set_errno(-ret);
|
|
return ERROR;
|
|
}
|
|
|
|
return OK;
|
|
}
|
|
|
|
#endif /* CONFIG_FS_AIO */
|