2014-10-06 16:10:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/aio/aio_initialize.c
|
|
|
|
*
|
2017-10-04 23:22:27 +02:00
|
|
|
* Copyright (C) 2014, 2017 Gregory Nutt. All rights reserved.
|
2014-10-06 16:10:02 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <queue.h>
|
|
|
|
|
2014-10-06 23:54:00 +02:00
|
|
|
#include <nuttx/sched.h>
|
|
|
|
|
2014-10-06 16:10:02 +02:00
|
|
|
#include "aio/aio.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_AIO
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is an array of pre-allocated AIO containers */
|
|
|
|
|
|
|
|
static struct aio_container_s g_aioc_alloc[CONFIG_FS_NAIOC];
|
|
|
|
|
|
|
|
/* This is a list of free AIO containers */
|
|
|
|
|
|
|
|
static dq_queue_t g_aioc_free;
|
|
|
|
|
|
|
|
/* This counting semaphore tracks the number of free AIO containers */
|
|
|
|
|
|
|
|
static sem_t g_aioc_freesem;
|
|
|
|
|
|
|
|
/* This binary semaphore supports exclusive access to the list of pending
|
2014-10-06 23:54:00 +02:00
|
|
|
* asynchronous I/O. g_aio_holder and a_aio_count support the reentrant
|
|
|
|
* lock.
|
2014-10-06 16:10:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
static sem_t g_aio_exclsem;
|
2014-10-06 23:54:00 +02:00
|
|
|
static pid_t g_aio_holder;
|
|
|
|
static uint16_t g_aio_count;
|
2014-10-06 16:10:02 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
/* This is a list of pending asynchronous I/O. The user must hold the
|
|
|
|
* lock on this list in order to access the list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dq_queue_t g_aio_pending;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2016-04-12 02:16:04 +02:00
|
|
|
|
2014-10-06 16:10:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: aio_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform one-time initialization of the asynchronous I/O sub-system
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void aio_initialize(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Initialize counting semaphores */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
(void)nxsem_init(&g_aioc_freesem, 0, CONFIG_FS_NAIOC);
|
2019-01-27 16:39:33 +01:00
|
|
|
(void)nxsem_setprotocol(&g_aioc_freesem, SEM_PRIO_NONE);
|
2017-10-03 20:51:15 +02:00
|
|
|
(void)nxsem_init(&g_aio_exclsem, 0, 1);
|
2014-10-06 16:10:02 +02:00
|
|
|
|
2014-10-06 23:54:00 +02:00
|
|
|
g_aio_holder = INVALID_PROCESS_ID;
|
|
|
|
|
2014-10-06 16:10:02 +02:00
|
|
|
/* Initialize the container queues */
|
|
|
|
|
|
|
|
dq_init(&g_aioc_free);
|
|
|
|
dq_init(&g_aio_pending);
|
|
|
|
|
|
|
|
/* Add all of the pre-allocated AIO containers to the free list */
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_FS_NAIOC; i++)
|
|
|
|
{
|
2014-10-06 23:54:00 +02:00
|
|
|
/* Add the container to the free list */
|
2014-10-06 16:10:02 +02:00
|
|
|
|
2014-10-07 00:15:24 +02:00
|
|
|
dq_addlast(&g_aioc_alloc[i].aioc_link, &g_aioc_free);
|
2014-10-06 16:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: aio_lock/aio_unlock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Take/give the lock on the pending asynchronous I/O list
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void aio_lock(void)
|
|
|
|
{
|
2014-10-06 23:54:00 +02:00
|
|
|
pid_t me = getpid();
|
|
|
|
|
|
|
|
/* Does this thread already hold the semaphore? */
|
|
|
|
|
|
|
|
if (g_aio_holder == me)
|
2014-10-06 16:10:02 +02:00
|
|
|
{
|
2014-10-06 23:54:00 +02:00
|
|
|
/* Yes, just increment the counts held */
|
|
|
|
|
|
|
|
DEBUGASSERT(g_aio_count > 0 && g_aio_count < UINT16_MAX);
|
|
|
|
g_aio_count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
|
|
|
|
2014-10-06 23:54:00 +02:00
|
|
|
/* No.. take the semaphore */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
do
|
2014-10-06 23:54:00 +02:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&g_aio_exclsem);
|
|
|
|
|
|
|
|
/* The only case that an error should occur here is if the wait
|
|
|
|
* was awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(ret == OK || ret == -EINTR);
|
2014-10-06 23:54:00 +02:00
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
2014-10-06 23:54:00 +02:00
|
|
|
|
|
|
|
/* And mark it as ours */
|
|
|
|
|
|
|
|
g_aio_holder = me;
|
|
|
|
g_aio_count = 1;
|
2014-10-06 16:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void aio_unlock(void)
|
|
|
|
{
|
2014-10-06 23:54:00 +02:00
|
|
|
DEBUGASSERT(g_aio_holder == getpid() && g_aio_count > 0);
|
|
|
|
|
|
|
|
/* Would decrementing the count release the lock? */
|
|
|
|
|
|
|
|
if (g_aio_count <= 1)
|
|
|
|
{
|
|
|
|
/* Yes.. that we will no longer be the holder */
|
|
|
|
|
|
|
|
g_aio_holder = INVALID_PROCESS_ID;
|
|
|
|
g_aio_count = 0;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&g_aio_exclsem);
|
2014-10-06 23:54:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, just decrement the count. We still hold the lock. */
|
|
|
|
|
|
|
|
g_aio_count--;
|
|
|
|
}
|
2014-10-06 16:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: aioc_alloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new AIO container by taking the next, pre-allocated
|
|
|
|
* container from the free list. This function will wait until
|
|
|
|
* aioc_free() is called in the event that there is no free container
|
|
|
|
* available in the free list.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* A reference to the allocated AIO container. This allocation never
|
|
|
|
* fails because the logic will wait in the event that there is no free
|
|
|
|
* container.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-06 19:02:58 +02:00
|
|
|
FAR struct aio_container_s *aioc_alloc(void)
|
2014-10-06 16:10:02 +02:00
|
|
|
{
|
|
|
|
FAR struct aio_container_s *aioc;
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2014-10-06 16:10:02 +02:00
|
|
|
|
2014-10-06 23:54:00 +02:00
|
|
|
/* Take a count from semaphore, thus guaranteeing that we have an AIO
|
|
|
|
* container set aside for us.
|
2014-10-06 16:10:02 +02:00
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
do
|
2014-10-06 16:10:02 +02:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&g_aioc_freesem);
|
|
|
|
|
|
|
|
/* The only case that an error should occur here is if the wait was
|
|
|
|
* awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(ret == OK || ret == -EINTR);
|
2014-10-06 16:10:02 +02:00
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
2014-10-06 16:10:02 +02:00
|
|
|
|
|
|
|
/* Get our AIO container */
|
|
|
|
|
|
|
|
aio_lock();
|
|
|
|
aioc = (FAR struct aio_container_s *)dq_remfirst(&g_aioc_free);
|
|
|
|
aio_unlock();
|
|
|
|
|
|
|
|
DEBUGASSERT(aioc);
|
|
|
|
return aioc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: aioc_free
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free an AIO container by returning it to the free list and, perhaps,
|
|
|
|
* awakening any threads waiting for that resource
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* aioc - The AIO container to be free
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void aioc_free(FAR struct aio_container_s *aioc)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(aioc);
|
|
|
|
|
|
|
|
/* Return the container to the free list */
|
|
|
|
|
|
|
|
aio_lock();
|
|
|
|
dq_addlast(&aioc->aioc_link, &g_aioc_free);
|
|
|
|
aio_unlock();
|
|
|
|
|
|
|
|
/* The post the counting semaphore, announcing the availability of the
|
|
|
|
* free AIO container.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&g_aioc_freesem);
|
2014-10-06 16:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_FS_AIO */
|