2014-06-03 20:41:34 +02:00
|
|
|
/****************************************************************************
|
2017-05-09 15:34:59 +02:00
|
|
|
* mm/iob/iob_alloc.c
|
2014-06-03 20:41:34 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01: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-06-03 20:41:34 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-03 20:41:34 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01: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-06-03 20:41:34 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-06-22 19:27:57 +02:00
|
|
|
#include <assert.h>
|
2016-01-23 00:35:06 +01:00
|
|
|
#include <errno.h>
|
2014-06-22 19:27:57 +02:00
|
|
|
|
2016-02-14 15:38:44 +01:00
|
|
|
#include <nuttx/irq.h>
|
2014-06-06 17:35:31 +02:00
|
|
|
#include <nuttx/arch.h>
|
2017-05-11 01:36:08 +02:00
|
|
|
#include <nuttx/sched.h>
|
2017-05-09 15:34:59 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2014-06-03 20:41:34 +02:00
|
|
|
|
|
|
|
#include "iob.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-22 19:27:57 +02:00
|
|
|
* Private Functions
|
2014-06-03 20:41:34 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-16 19:03:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_alloc_committed
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate an I/O buffer by taking the buffer at the head of the committed
|
|
|
|
* list.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
static FAR struct iob_s *iob_alloc_committed(enum iob_user_e consumerid)
|
2017-05-16 19:03:35 +02:00
|
|
|
{
|
|
|
|
FAR struct iob_s *iob = NULL;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* We don't know what context we are called from so we use extreme measures
|
|
|
|
* to protect the committed list: We disable interrupts very briefly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
/* Take the I/O buffer from the head of the committed list */
|
|
|
|
|
|
|
|
iob = g_iob_committed;
|
|
|
|
if (iob != NULL)
|
|
|
|
{
|
|
|
|
/* Remove the I/O buffer from the committed list */
|
|
|
|
|
|
|
|
g_iob_committed = iob->io_flink;
|
|
|
|
|
|
|
|
/* Put the I/O buffer in a known state */
|
|
|
|
|
|
|
|
iob->io_flink = NULL; /* Not in a chain */
|
|
|
|
iob->io_len = 0; /* Length of the data in the entry */
|
|
|
|
iob->io_offset = 0; /* Offset to the beginning of data */
|
|
|
|
iob->io_pktlen = 0; /* Total length of the packet */
|
2019-08-17 00:42:25 +02:00
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
|
|
|
|
defined(CONFIG_MM_IOB) && !defined(CONFIG_FS_PROCFS_EXCLUDE_IOBINFO)
|
|
|
|
iob_stats_onalloc(consumerid);
|
|
|
|
#endif
|
2017-05-16 19:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
return iob;
|
|
|
|
}
|
|
|
|
|
2014-06-22 19:27:57 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_allocwait
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate an I/O buffer, waiting if necessary. This function cannot be
|
|
|
|
* called from any interrupt level logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
static FAR struct iob_s *iob_allocwait(bool throttled,
|
|
|
|
enum iob_user_e consumerid)
|
2014-06-22 19:27:57 +02:00
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
irqstate_t flags;
|
2014-06-24 19:53:19 +02:00
|
|
|
FAR sem_t *sem;
|
2015-07-01 22:33:37 +02:00
|
|
|
int ret = OK;
|
2014-06-22 19:27:57 +02:00
|
|
|
|
2014-06-24 19:53:19 +02:00
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
/* Select the semaphore count to check. */
|
|
|
|
|
2014-06-24 23:38:00 +02:00
|
|
|
sem = (throttled ? &g_throttle_sem : &g_iob_sem);
|
2014-06-24 19:53:19 +02:00
|
|
|
#else
|
|
|
|
sem = &g_iob_sem;
|
|
|
|
#endif
|
|
|
|
|
2014-06-22 19:27:57 +02:00
|
|
|
/* The following must be atomic; interrupt must be disabled so that there
|
|
|
|
* is no conflict with interrupt level I/O buffer allocations. This is
|
|
|
|
* not as bad as it sounds because interrupts will be re-enabled while
|
|
|
|
* we are waiting for I/O buffers to become free.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 15:38:44 +01:00
|
|
|
flags = enter_critical_section();
|
2017-05-16 19:03:35 +02:00
|
|
|
|
|
|
|
/* Try to get an I/O buffer. If successful, the semaphore count will be
|
|
|
|
* decremented atomically.
|
|
|
|
*/
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
iob = iob_tryalloc(throttled, consumerid);
|
2017-05-16 19:03:35 +02:00
|
|
|
while (ret == OK && iob == NULL)
|
2014-06-22 19:27:57 +02:00
|
|
|
{
|
2017-05-16 19:03:35 +02:00
|
|
|
/* If not successful, then the semaphore count was less than or equal
|
|
|
|
* to zero (meaning that there are no free buffers). We need to wait
|
|
|
|
* for an I/O buffer to be released and placed in the committed
|
|
|
|
* list.
|
2014-06-22 19:27:57 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
ret = nxsem_wait_uninterruptible(sem);
|
|
|
|
if (ret >= 0)
|
2017-05-16 19:03:35 +02:00
|
|
|
{
|
|
|
|
/* When we wake up from wait successfully, an I/O buffer was
|
2019-01-26 16:24:12 +01:00
|
|
|
* freed and we hold a count for one IOB.
|
2017-05-16 19:03:35 +02:00
|
|
|
*/
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
iob = iob_alloc_committed(consumerid);
|
2017-05-16 19:03:35 +02:00
|
|
|
if (iob == NULL)
|
|
|
|
{
|
2019-01-26 16:24:12 +01:00
|
|
|
/* We need release our count so that it is available to
|
2016-01-22 23:15:10 +01:00
|
|
|
* iob_tryalloc(), perhaps allowing another thread to take our
|
|
|
|
* count. In that event, iob_tryalloc() will fail above and
|
|
|
|
* we will have to wait again.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(sem);
|
2019-08-17 00:42:25 +02:00
|
|
|
iob = iob_tryalloc(throttled, consumerid);
|
2016-01-22 23:15:10 +01:00
|
|
|
}
|
2019-08-17 00:42:25 +02:00
|
|
|
|
|
|
|
/* REVISIT: I think this logic should be moved inside of
|
|
|
|
* iob_alloc_committed, so that it can exist inside of the critical
|
|
|
|
* section along with all other sem count changes.
|
|
|
|
*/
|
|
|
|
|
2019-01-26 16:24:12 +01:00
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (throttled)
|
|
|
|
{
|
|
|
|
g_iob_sem.semcount--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_throttle_sem.semcount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-22 19:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:38:44 +01:00
|
|
|
leave_critical_section(flags);
|
2014-06-22 19:27:57 +02:00
|
|
|
return iob;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_alloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate an I/O buffer by taking the buffer at the head of the free list.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
FAR struct iob_s *iob_alloc(bool throttled, enum iob_user_e consumerid)
|
2014-06-22 19:27:57 +02:00
|
|
|
{
|
|
|
|
/* Were we called from the interrupt level? */
|
|
|
|
|
2017-05-11 01:36:08 +02:00
|
|
|
if (up_interrupt_context() || sched_idletask())
|
2014-06-22 19:27:57 +02:00
|
|
|
{
|
|
|
|
/* Yes, then try to allocate an I/O buffer without waiting */
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
return iob_tryalloc(throttled, consumerid);
|
2014-06-22 19:27:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Then allocate an I/O buffer, waiting as necessary */
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
return iob_allocwait(throttled, consumerid);
|
2014-06-22 19:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-28 04:23:42 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_tryalloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Try to allocate an I/O buffer by taking the buffer at the head of the
|
|
|
|
* free list without waiting for a buffer to become free.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
FAR struct iob_s *iob_tryalloc(bool throttled, enum iob_user_e consumerid)
|
2015-01-28 04:23:42 +01:00
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
irqstate_t flags;
|
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
FAR sem_t *sem;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
/* Select the semaphore count to check. */
|
|
|
|
|
|
|
|
sem = (throttled ? &g_throttle_sem : &g_iob_sem);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We don't know what context we are called from so we use extreme measures
|
|
|
|
* to protect the free list: We disable interrupts very briefly.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 15:38:44 +01:00
|
|
|
flags = enter_critical_section();
|
2015-01-28 04:23:42 +01:00
|
|
|
|
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
/* If there are free I/O buffers for this allocation */
|
|
|
|
|
|
|
|
if (sem->semcount > 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Take the I/O buffer from the head of the free list */
|
|
|
|
|
|
|
|
iob = g_iob_freelist;
|
2017-05-16 19:03:35 +02:00
|
|
|
if (iob != NULL)
|
2015-01-28 04:23:42 +01:00
|
|
|
{
|
|
|
|
/* Remove the I/O buffer from the free list and decrement the
|
|
|
|
* counting semaphore(s) that tracks the number of available
|
|
|
|
* IOBs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
g_iob_freelist = iob->io_flink;
|
|
|
|
|
|
|
|
/* Take a semaphore count. Note that we cannot do this in
|
2017-10-05 15:59:06 +02:00
|
|
|
* in the orthodox way by calling nxsem_wait() or nxsem_trywait()
|
2015-01-28 04:23:42 +01:00
|
|
|
* because this function may be called from an interrupt
|
|
|
|
* handler. Fortunately we know at at least one free buffer
|
|
|
|
* so a simple decrement is all that is needed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
g_iob_sem.semcount--;
|
|
|
|
DEBUGASSERT(g_iob_sem.semcount >= 0);
|
|
|
|
|
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
/* The throttle semaphore is a little more complicated because
|
|
|
|
* it can be negative! Decrementing is still safe, however.
|
|
|
|
*/
|
|
|
|
|
|
|
|
g_throttle_sem.semcount--;
|
|
|
|
DEBUGASSERT(g_throttle_sem.semcount >= -CONFIG_IOB_THROTTLE);
|
|
|
|
#endif
|
2019-08-17 00:42:25 +02:00
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
|
|
|
|
defined(CONFIG_MM_IOB) && !defined(CONFIG_FS_PROCFS_EXCLUDE_IOBINFO)
|
|
|
|
iob_stats_onalloc(consumerid);
|
|
|
|
#endif
|
|
|
|
|
2016-02-14 15:38:44 +01:00
|
|
|
leave_critical_section(flags);
|
2015-01-28 04:23:42 +01:00
|
|
|
|
|
|
|
/* Put the I/O buffer in a known state */
|
|
|
|
|
|
|
|
iob->io_flink = NULL; /* Not in a chain */
|
|
|
|
iob->io_len = 0; /* Length of the data in the entry */
|
|
|
|
iob->io_offset = 0; /* Offset to the beginning of data */
|
|
|
|
iob->io_pktlen = 0; /* Total length of the packet */
|
|
|
|
return iob;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:38:44 +01:00
|
|
|
leave_critical_section(flags);
|
2015-01-28 04:23:42 +01:00
|
|
|
return NULL;
|
|
|
|
}
|