a4218e2144
1. Add internal scheduler functions should begin with nxsched_, not sched_ 2. Follow the consistent naming patter of https://cwiki.apache.org/confluence/display/NUTTX/Naming+of+OS+Internal+Functions
189 lines
4.9 KiB
C
189 lines
4.9 KiB
C
/****************************************************************************
|
|
* fs/aio/aioc_contain.c
|
|
*
|
|
* 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 <sched.h>
|
|
#include <errno.h>
|
|
|
|
#include <nuttx/sched.h>
|
|
#include <nuttx/fs/fs.h>
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include "aio/aio.h"
|
|
|
|
#ifdef CONFIG_FS_AIO
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: aio_contain
|
|
*
|
|
* Description:
|
|
* Create and initialize a container for the provided AIO control block
|
|
*
|
|
* Input Parameters:
|
|
* aiocbp - The AIO control block pointer
|
|
*
|
|
* Returned Value:
|
|
* A reference to the new AIO control block container. This function
|
|
* will not fail but will wait if necessary for the resources to perform
|
|
* this operation. NULL will be returned on certain errors with the
|
|
* errno value already set appropriately.
|
|
*
|
|
****************************************************************************/
|
|
|
|
FAR struct aio_container_s *aio_contain(FAR struct aiocb *aiocbp)
|
|
{
|
|
FAR struct aio_container_s *aioc;
|
|
union
|
|
{
|
|
FAR struct file *filep;
|
|
#ifdef AIO_HAVE_PSOCK
|
|
FAR struct socket *psock;
|
|
#endif
|
|
FAR void *ptr;
|
|
} u;
|
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
struct sched_param param;
|
|
#endif
|
|
int ret;
|
|
|
|
#ifdef AIO_HAVE_PSOCK
|
|
if (aiocbp->aio_fildes < CONFIG_NFILE_DESCRIPTORS)
|
|
#endif
|
|
{
|
|
/* Get the file structure corresponding to the file descriptor. */
|
|
|
|
ret = fs_getfilep(aiocbp->aio_fildes, &u.filep);
|
|
if (ret < 0)
|
|
{
|
|
goto errout;
|
|
}
|
|
|
|
DEBUGASSERT(u.filep != NULL);
|
|
}
|
|
#ifdef AIO_HAVE_PSOCK
|
|
else
|
|
{
|
|
/* Get the socket structure corresponding to the socket descriptor */
|
|
|
|
u.psock = sockfd_socket(aiocbp->aio_fildes);
|
|
if (u.psock == NULL)
|
|
{
|
|
/* Does not return error information. EBADF is the most likely
|
|
* explanation.
|
|
*/
|
|
|
|
ret = -EBADF;
|
|
goto errout;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
/* Allocate the AIO control block container, waiting for one to become
|
|
* available if necessary. This should not fail except for in the case
|
|
* where the calling thread is canceled.
|
|
*/
|
|
|
|
aioc = aioc_alloc();
|
|
if (aioc != NULL)
|
|
{
|
|
/* Initialize the container */
|
|
|
|
memset(aioc, 0, sizeof(struct aio_container_s));
|
|
aioc->aioc_aiocbp = aiocbp;
|
|
aioc->u.ptr = u.ptr;
|
|
aioc->aioc_pid = getpid();
|
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
DEBUGVERIFY(nxsched_get_param (aioc->aioc_pid, ¶m));
|
|
aioc->aioc_prio = param.sched_priority;
|
|
#endif
|
|
|
|
/* Add the container to the pending transfer list. */
|
|
|
|
ret = aio_lock();
|
|
if (ret < 0)
|
|
{
|
|
aioc_free(aioc);
|
|
goto errout;
|
|
}
|
|
|
|
dq_addlast(&aioc->aioc_link, &g_aio_pending);
|
|
aio_unlock();
|
|
}
|
|
|
|
return aioc;
|
|
|
|
errout:
|
|
set_errno(-ret);
|
|
return NULL;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: aioc_decant
|
|
*
|
|
* Description:
|
|
* Remove the AIO control block from the container and free all resources
|
|
* used by the container.
|
|
*
|
|
* Input Parameters:
|
|
* aioc - Pointer to the AIO control block container
|
|
*
|
|
* Returned Value:
|
|
* A pointer to the no-longer contained AIO control block.
|
|
*
|
|
****************************************************************************/
|
|
|
|
FAR struct aiocb *aioc_decant(FAR struct aio_container_s *aioc)
|
|
{
|
|
FAR struct aiocb *aiocbp = NULL;
|
|
int ret;
|
|
|
|
DEBUGASSERT(aioc);
|
|
|
|
/* Remove the container to the pending transfer list. */
|
|
|
|
ret = aio_lock();
|
|
if (ret >= 0)
|
|
{
|
|
dq_rem(&aioc->aioc_link, &g_aio_pending);
|
|
|
|
/* De-cant the AIO control block and return the container to the free list */
|
|
|
|
aiocbp = aioc->aioc_aiocbp;
|
|
aioc_free(aioc);
|
|
|
|
aio_unlock();
|
|
}
|
|
|
|
return aiocbp;
|
|
}
|
|
|
|
#endif /* CONFIG_FS_AIO */
|