2014-10-06 16:10:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/aio/aio_initialize.c
|
|
|
|
*
|
2020-03-30 17:00:51 +02:00
|
|
|
* 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
|
2014-10-06 16:10:02 +02:00
|
|
|
*
|
2020-03-30 17:00:51 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-10-06 16:10:02 +02:00
|
|
|
*
|
2020-03-30 17:00:51 +02:00
|
|
|
* 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.
|
2014-10-06 16:10:02 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2014-10-06 23:54:00 +02:00
|
|
|
#include <nuttx/sched.h>
|
2022-05-29 17:59:14 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2014-10-06 23:54:00 +02:00
|
|
|
|
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 */
|
|
|
|
|
2022-10-19 17:57:25 +02:00
|
|
|
static sem_t g_aioc_freesem = SEM_INITIALIZER(CONFIG_FS_NAIOC);
|
2014-10-06 16:10:02 +02:00
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
/* This binary lock 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
|
|
|
*/
|
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
static rmutex_t g_aio_lock = NXRMUTEX_INITIALIZER;
|
2014-10-06 16:10:02 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
2019-12-01 20:01:16 +01:00
|
|
|
|
2014-10-06 16:10:02 +02:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* 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:
|
2020-03-30 17:00:51 +02:00
|
|
|
* aio_lock() return -ECANCELED if the calling thread is canceled.
|
2014-10-06 16:10:02 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 17:00:51 +02:00
|
|
|
int aio_lock(void)
|
2014-10-06 16:10:02 +02:00
|
|
|
{
|
2022-05-29 17:59:14 +02:00
|
|
|
return nxrmutex_lock(&g_aio_lock);
|
2014-10-06 16:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void aio_unlock(void)
|
|
|
|
{
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_unlock(&g_aio_lock);
|
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
|
|
|
{
|
2020-03-30 17:00:51 +02:00
|
|
|
FAR struct aio_container_s *aioc = NULL;
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2020-03-30 17:00:51 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&g_aioc_freesem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-06 16:10:02 +02:00
|
|
|
|
|
|
|
/* Get our AIO container */
|
|
|
|
|
2020-03-30 17:00:51 +02:00
|
|
|
ret = aio_lock();
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
aioc = (FAR struct aio_container_s *)dq_remfirst(&g_aioc_free);
|
|
|
|
aio_unlock();
|
|
|
|
|
|
|
|
DEBUGASSERT(aioc);
|
|
|
|
}
|
2014-10-06 16:10:02 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-03-30 17:00:51 +02:00
|
|
|
int ret;
|
|
|
|
|
2014-10-06 16:10:02 +02:00
|
|
|
DEBUGASSERT(aioc);
|
|
|
|
|
|
|
|
/* Return the container to the free list */
|
|
|
|
|
2020-03-30 17:00:51 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = aio_lock();
|
|
|
|
|
|
|
|
/* The only possible error should be if we were awakened only by
|
|
|
|
* thread cancellation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(ret == OK || ret == -ECANCELED);
|
|
|
|
}
|
|
|
|
while (ret < 0);
|
|
|
|
|
2014-10-06 16:10:02 +02:00
|
|
|
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 */
|