2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2014-09-22 18:53:50 +02:00
|
|
|
* mm/mm_heap/mm_sem.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2017-10-04 23:22:27 +02:00
|
|
|
* Copyright (C) 2007-2009, 2013, 2017 Gregory Nutt. All rights reserved.
|
2012-06-06 03:44:57 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-01-09 15:20:31 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 21:36:18 +01:00
|
|
|
#include <nuttx/config.h>
|
2012-07-15 01:31:12 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <unistd.h>
|
2017-10-07 16:22:18 +02:00
|
|
|
#include <semaphore.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2012-07-15 01:31:12 +02:00
|
|
|
|
2018-12-20 17:19:52 +01:00
|
|
|
#include <nuttx/sched.h>
|
2017-10-03 20:51:15 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2014-09-24 15:29:09 +02:00
|
|
|
#include <nuttx/mm/mm.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2018-03-20 13:34:38 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
# include <nuttx/irq.h>
|
|
|
|
#endif
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2009-12-15 02:39:03 +01:00
|
|
|
* Pre-processor Definitions
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-07 15:16:33 +02:00
|
|
|
/* Internal nxsem_* interfaces are not available in the user space in
|
2017-10-08 16:13:47 +02:00
|
|
|
* PROTECTED and KERNEL builds. In that context, the application semaphore
|
2017-10-07 15:16:33 +02:00
|
|
|
* interfaces must be used. The differences between the two sets of
|
|
|
|
* interfaces are: (1) the nxsem_* interfaces do not cause cancellation
|
|
|
|
* points and (2) they do not modify the errno variable.
|
|
|
|
*
|
2017-10-08 16:13:47 +02:00
|
|
|
* See additional definitions in include/nuttx/semaphore.h
|
|
|
|
*
|
2017-10-07 15:16:33 +02:00
|
|
|
* REVISIT: The fact that sem_wait() is a cancellation point is an issue
|
|
|
|
* and does cause a violation: It makes all of the memory management
|
2017-10-08 16:13:47 +02:00
|
|
|
* interfaces into cancellation points when used from user space in the
|
|
|
|
* PROTECTED and KERNEL builds.
|
2017-10-07 15:16:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
2017-10-08 16:13:47 +02:00
|
|
|
# define _SEM_GETERROR(r)
|
2017-10-07 15:16:33 +02:00
|
|
|
#else
|
2017-10-08 16:13:47 +02:00
|
|
|
# define _SEM_GETERROR(r) (r) = -errno
|
2017-10-07 15:16:33 +02:00
|
|
|
#endif
|
|
|
|
|
2018-12-20 20:39:06 +01:00
|
|
|
/* Define MONITOR_MM_SEMAPHORE to enable semaphore state monitoring */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#ifdef MONITOR_MM_SEMAPHORE
|
2016-06-12 19:11:08 +02:00
|
|
|
# include <debug.h>
|
2016-06-16 20:33:32 +02:00
|
|
|
# define msemerr _err
|
|
|
|
# define msemwarn _warn
|
|
|
|
# define mseminfo _info
|
2007-02-18 00:21:28 +01:00
|
|
|
#else
|
2008-01-09 15:20:31 +01:00
|
|
|
# ifdef CONFIG_CPP_HAVE_VARARGS
|
2016-06-11 23:50:49 +02:00
|
|
|
# define msemerr(x...)
|
2016-06-12 19:13:34 +02:00
|
|
|
# define msemwarn(x...)
|
|
|
|
# define mseminfo(x...)
|
2008-01-09 15:20:31 +01:00
|
|
|
# else
|
2016-06-16 20:33:32 +02:00
|
|
|
# define msemerr (void)
|
2016-06-12 19:13:34 +02:00
|
|
|
# define msemwarn (void)
|
|
|
|
# define mseminfo (void)
|
2008-01-09 15:20:31 +01:00
|
|
|
# endif
|
2007-02-18 00:21:28 +01:00
|
|
|
#endif
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 19:58:21 +01:00
|
|
|
* Name: mm_seminitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the MM mutex
|
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
void mm_seminitialize(FAR struct mm_heap_s *heap)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Initialize the MM semaphore to one (to support one-at-a-time access to
|
2016-02-13 01:03:08 +01:00
|
|
|
* private data sets).
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2017-10-08 16:13:47 +02:00
|
|
|
(void)nxsem_init(&heap->mm_semaphore, 0, 1);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
heap->mm_holder = -1;
|
|
|
|
heap->mm_counts_held = 0;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 19:58:21 +01:00
|
|
|
* Name: mm_trysemaphore
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-15 01:31:12 +02:00
|
|
|
* Try to take the MM mutex. This is called only from the OS in certain
|
|
|
|
* conditions when it is necessary to have exclusive access to the memory
|
|
|
|
* manager but it is impossible to wait on a semaphore (e.g., the idle
|
|
|
|
* process when it performs its background memory cleanup).
|
2007-03-14 19:58:21 +01:00
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
int mm_trysemaphore(FAR struct mm_heap_s *heap)
|
2007-03-14 19:58:21 +01:00
|
|
|
{
|
2018-03-20 13:34:38 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
#endif
|
2018-12-20 17:19:52 +01:00
|
|
|
FAR struct tcb_s *rtcb = sched_self();
|
|
|
|
pid_t my_pid;
|
2017-10-05 15:59:06 +02:00
|
|
|
int ret;
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2018-12-20 17:19:52 +01:00
|
|
|
/* sched_self() returns the TCB at the head of the ready-to-run list.
|
|
|
|
* mm_trysemaphore() is an intimate component of the memory garbage
|
|
|
|
* collection logic and, hence, may be called in many different contexts.
|
|
|
|
*
|
|
|
|
* It may be called during context switches for example. There are certain
|
|
|
|
* conditions when the the OS data structures are in flux and where the
|
|
|
|
* task at the head of the ready-to-run task list is not actually running.
|
|
|
|
* Granting the semaphore access in this case is known to result in heap
|
|
|
|
* corruption and must be avoided.
|
|
|
|
*/
|
|
|
|
|
2007-03-14 19:58:21 +01:00
|
|
|
/* Do I already have the semaphore? */
|
|
|
|
|
2018-12-20 17:19:52 +01:00
|
|
|
my_pid = rtcb->pid;
|
2018-12-20 20:39:06 +01:00
|
|
|
if (heap->mm_holder == my_pid && rtcb->task_state == TSTATE_TASK_RUNNING)
|
2007-03-14 19:58:21 +01:00
|
|
|
{
|
|
|
|
/* Yes, just increment the number of references that I have */
|
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
heap->mm_counts_held++;
|
2018-03-20 13:34:38 +01:00
|
|
|
ret = OK;
|
2007-03-14 19:58:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-20 17:19:52 +01:00
|
|
|
/* Try to take the semaphore */
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2017-10-08 16:13:47 +02:00
|
|
|
ret = _SEM_TRYWAIT(&heap->mm_semaphore);
|
2017-10-05 15:59:06 +02:00
|
|
|
if (ret < 0)
|
2007-03-14 19:58:21 +01:00
|
|
|
{
|
2017-10-08 16:13:47 +02:00
|
|
|
_SEM_GETERROR(ret);
|
2018-03-20 13:34:38 +01:00
|
|
|
goto errout;
|
2007-03-14 19:58:21 +01:00
|
|
|
}
|
|
|
|
|
2017-10-07 16:22:18 +02:00
|
|
|
/* We have it. Claim the heap and return */
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
heap->mm_holder = my_pid;
|
|
|
|
heap->mm_counts_held = 1;
|
2018-03-20 13:34:38 +01:00
|
|
|
ret = OK;
|
2007-03-14 19:58:21 +01:00
|
|
|
}
|
2018-03-20 13:34:38 +01:00
|
|
|
|
|
|
|
errout:
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
leave_critical_section(flags);
|
|
|
|
#endif
|
|
|
|
return ret;
|
2007-03-14 19:58:21 +01:00
|
|
|
}
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 19:58:21 +01:00
|
|
|
* Name: mm_takesemaphore
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-15 01:31:12 +02:00
|
|
|
* Take the MM mutex. This is the normal action before all memory
|
|
|
|
* management actions.
|
2007-03-14 19:58:21 +01:00
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
void mm_takesemaphore(FAR struct mm_heap_s *heap)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2018-03-20 13:34:38 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
pid_t my_pid = getpid();
|
|
|
|
|
|
|
|
/* Do I already have the semaphore? */
|
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
if (heap->mm_holder == my_pid)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
/* Yes, just increment the number of references that I have */
|
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
heap->mm_counts_held++;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
2016-06-12 17:46:23 +02:00
|
|
|
mseminfo("PID=%d taking\n", my_pid);
|
2017-10-04 23:22:27 +02:00
|
|
|
do
|
2012-07-15 01:31:12 +02:00
|
|
|
{
|
2017-10-08 16:13:47 +02:00
|
|
|
ret = _SEM_WAIT(&heap->mm_semaphore);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
|
|
|
/* The only case that an error should occur here is if the wait
|
|
|
|
* was awakened by a signal.
|
2012-07-15 01:31:12 +02:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-07 15:16:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
2018-02-20 19:24:53 +01:00
|
|
|
DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
|
2017-10-07 15:16:33 +02:00
|
|
|
#else
|
2018-01-31 00:57:36 +01:00
|
|
|
int errcode = get_errno();
|
2018-02-20 19:24:53 +01:00
|
|
|
DEBUGASSERT(errcode == EINTR || errcode == ECANCELED);
|
2017-10-07 15:16:33 +02:00
|
|
|
ret = -errcode;
|
|
|
|
#endif
|
|
|
|
}
|
2012-07-15 01:31:12 +02:00
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* We have it (or some awful, unexpected error occurred). Claim
|
|
|
|
* the semaphore and return.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
heap->mm_holder = my_pid;
|
|
|
|
heap->mm_counts_held = 1;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 13:34:38 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
leave_critical_section(flags);
|
|
|
|
#endif
|
2016-06-12 17:46:23 +02:00
|
|
|
mseminfo("Holder=%d count=%d\n", heap->mm_holder, heap->mm_counts_held);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 19:58:21 +01:00
|
|
|
* Name: mm_givesemaphore
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the MM mutex when it is not longer needed.
|
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
void mm_givesemaphore(FAR struct mm_heap_s *heap)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2018-03-20 13:34:38 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
#endif
|
2016-06-12 19:11:08 +02:00
|
|
|
#if defined(CONFIG_DEBUG_ASSERTIONS) || \
|
|
|
|
(defined(MONITOR_MM_SEMAPHORE) && defined(CONFIG_DEBUG_INFO))
|
2007-02-18 00:21:28 +01:00
|
|
|
pid_t my_pid = getpid();
|
2011-06-18 22:02:40 +02:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* I better be holding at least one reference to the semaphore */
|
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
DEBUGASSERT(heap->mm_holder == my_pid);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2018-12-20 17:19:52 +01:00
|
|
|
/* Do I hold multiple references to the semaphore */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
if (heap->mm_counts_held > 1)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
/* Yes, just release one count and return */
|
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
heap->mm_counts_held--;
|
2016-06-12 19:11:08 +02:00
|
|
|
mseminfo("Holder=%d count=%d\n", heap->mm_holder,
|
|
|
|
heap->mm_counts_held);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Nope, this is the last reference I have */
|
|
|
|
|
2016-06-12 17:46:23 +02:00
|
|
|
mseminfo("PID=%d giving\n", my_pid);
|
2014-05-28 22:09:58 +02:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
heap->mm_holder = -1;
|
|
|
|
heap->mm_counts_held = 0;
|
2017-10-08 16:13:47 +02:00
|
|
|
DEBUGVERIFY(_SEM_POST(&heap->mm_semaphore));
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2018-03-20 13:34:38 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
leave_critical_section(flags);
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|