2008-01-09 14:20:31 +00:00
|
|
|
/****************************************************************************
|
2014-09-22 10:53:50 -06:00
|
|
|
* mm/mm_heap/mm_sem.c
|
2007-02-17 23:21:28 +00:00
|
|
|
*
|
2017-10-04 15:22:27 -06:00
|
|
|
* Copyright (C) 2007-2009, 2013, 2017 Gregory Nutt. All rights reserved.
|
2012-06-06 01:44:57 +00:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-17 23:21:28 +00: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 14:20:31 +00:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-17 23:21:28 +00: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 14:20:31 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-09 14:20:31 +00:00
|
|
|
/****************************************************************************
|
2007-02-17 23:21:28 +00:00
|
|
|
* Included Files
|
2008-01-09 14:20:31 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2013-03-08 20:36:18 +00:00
|
|
|
#include <nuttx/config.h>
|
2012-07-14 23:31:12 +00:00
|
|
|
|
2007-02-17 23:21:28 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2012-07-14 23:31:12 +00:00
|
|
|
|
2017-10-03 12:51:15 -06:00
|
|
|
#include <nuttx/semaphore.h>
|
2014-09-24 07:29:09 -06:00
|
|
|
#include <nuttx/mm/mm.h>
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-09 14:20:31 +00:00
|
|
|
/****************************************************************************
|
2009-12-15 01:39:03 +00:00
|
|
|
* Pre-processor Definitions
|
2008-01-09 14:20:31 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
|
|
|
/* Define the following to enable semaphore state monitoring */
|
|
|
|
//#define MONITOR_MM_SEMAPHORE 1
|
|
|
|
|
|
|
|
#ifdef MONITOR_MM_SEMAPHORE
|
2016-06-12 11:11:08 -06:00
|
|
|
# include <debug.h>
|
2016-06-16 12:33:32 -06:00
|
|
|
# define msemerr _err
|
|
|
|
# define msemwarn _warn
|
|
|
|
# define mseminfo _info
|
2007-02-17 23:21:28 +00:00
|
|
|
#else
|
2008-01-09 14:20:31 +00:00
|
|
|
# ifdef CONFIG_CPP_HAVE_VARARGS
|
2016-06-11 15:50:49 -06:00
|
|
|
# define msemerr(x...)
|
2016-06-12 11:13:34 -06:00
|
|
|
# define msemwarn(x...)
|
|
|
|
# define mseminfo(x...)
|
2008-01-09 14:20:31 +00:00
|
|
|
# else
|
2016-06-16 12:33:32 -06:00
|
|
|
# define msemerr (void)
|
2016-06-12 11:13:34 -06:00
|
|
|
# define msemwarn (void)
|
|
|
|
# define mseminfo (void)
|
2008-01-09 14:20:31 +00:00
|
|
|
# endif
|
2007-02-17 23:21:28 +00:00
|
|
|
#endif
|
|
|
|
|
2008-01-09 14:20:31 +00:00
|
|
|
/****************************************************************************
|
2007-02-17 23:21:28 +00:00
|
|
|
* Public Functions
|
2008-01-09 14:20:31 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-09 14:20:31 +00:00
|
|
|
/****************************************************************************
|
2007-03-14 18:58:21 +00:00
|
|
|
* Name: mm_seminitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the MM mutex
|
|
|
|
*
|
2008-01-09 14:20:31 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
void mm_seminitialize(FAR struct mm_heap_s *heap)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
2012-07-14 23:31:12 +00:00
|
|
|
/* Initialize the MM semaphore to one (to support one-at-a-time access to
|
2016-02-12 18:03:08 -06:00
|
|
|
* private data sets).
|
2007-02-17 23:21:28 +00:00
|
|
|
*/
|
|
|
|
|
2017-10-03 12:51:15 -06:00
|
|
|
(void)nxsem_init(&heap->mm_semaphore, 0, 1);
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
heap->mm_holder = -1;
|
|
|
|
heap->mm_counts_held = 0;
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 14:20:31 +00:00
|
|
|
/****************************************************************************
|
2007-03-14 18:58:21 +00:00
|
|
|
* Name: mm_trysemaphore
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 23:31:12 +00: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 18:58:21 +00:00
|
|
|
*
|
2008-01-09 14:20:31 +00:00
|
|
|
****************************************************************************/
|
2007-03-14 18:58:21 +00:00
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
int mm_trysemaphore(FAR struct mm_heap_s *heap)
|
2007-03-14 18:58:21 +00:00
|
|
|
{
|
|
|
|
pid_t my_pid = getpid();
|
2017-10-05 07:59:06 -06:00
|
|
|
int ret;
|
2007-03-14 18:58:21 +00:00
|
|
|
|
|
|
|
/* Do I already have the semaphore? */
|
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
if (heap->mm_holder == my_pid)
|
2007-03-14 18:58:21 +00:00
|
|
|
{
|
|
|
|
/* Yes, just increment the number of references that I have */
|
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
heap->mm_counts_held++;
|
2007-03-14 18:58:21 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-06 01:44:57 +00:00
|
|
|
/* Try to take the semaphore (perhaps waiting) */
|
2007-03-14 18:58:21 +00:00
|
|
|
|
2017-10-05 07:59:06 -06:00
|
|
|
ret = nxsem_trywait(&heap->mm_semaphore);
|
|
|
|
if (ret < 0)
|
2007-03-14 18:58:21 +00:00
|
|
|
{
|
2017-10-05 07:59:06 -06:00
|
|
|
return ret;
|
2007-03-14 18:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We have it. Claim the stak and return */
|
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
heap->mm_holder = my_pid;
|
|
|
|
heap->mm_counts_held = 1;
|
2007-03-14 18:58:21 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-09 14:20:31 +00:00
|
|
|
/****************************************************************************
|
2007-03-14 18:58:21 +00:00
|
|
|
* Name: mm_takesemaphore
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 23:31:12 +00:00
|
|
|
* Take the MM mutex. This is the normal action before all memory
|
|
|
|
* management actions.
|
2007-03-14 18:58:21 +00:00
|
|
|
*
|
2008-01-09 14:20:31 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
void mm_takesemaphore(FAR struct mm_heap_s *heap)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
|
|
|
pid_t my_pid = getpid();
|
|
|
|
|
|
|
|
/* Do I already have the semaphore? */
|
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
if (heap->mm_holder == my_pid)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
|
|
|
/* Yes, just increment the number of references that I have */
|
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
heap->mm_counts_held++;
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-04 15:22:27 -06:00
|
|
|
int ret;
|
|
|
|
|
2007-02-17 23:21:28 +00:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
2016-06-12 09:46:23 -06:00
|
|
|
mseminfo("PID=%d taking\n", my_pid);
|
2017-10-04 15:22:27 -06:00
|
|
|
do
|
2012-07-14 23:31:12 +00:00
|
|
|
{
|
2017-10-04 15:22:27 -06:00
|
|
|
ret = nxsem_wait(&heap->mm_semaphore);
|
|
|
|
|
|
|
|
/* The only case that an error should occur here is if the wait
|
|
|
|
* was awakened by a signal.
|
2012-07-14 23:31:12 +00:00
|
|
|
*/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2017-10-04 15:22:27 -06:00
|
|
|
DEBUGASSERT(ret == OK || ret == -EINTR);
|
2012-07-14 23:31:12 +00:00
|
|
|
}
|
2017-10-04 15:22:27 -06:00
|
|
|
while (ret == -EINTR);
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2017-10-04 15:22:27 -06:00
|
|
|
/* We have it (or some awful, unexpected error occurred). Claim
|
|
|
|
* the semaphore and return.
|
|
|
|
*/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
heap->mm_holder = my_pid;
|
|
|
|
heap->mm_counts_held = 1;
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 09:46:23 -06:00
|
|
|
mseminfo("Holder=%d count=%d\n", heap->mm_holder, heap->mm_counts_held);
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 14:20:31 +00:00
|
|
|
/****************************************************************************
|
2007-03-14 18:58:21 +00:00
|
|
|
* Name: mm_givesemaphore
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the MM mutex when it is not longer needed.
|
|
|
|
*
|
2008-01-09 14:20:31 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
void mm_givesemaphore(FAR struct mm_heap_s *heap)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
2016-06-12 11:11:08 -06:00
|
|
|
#if defined(CONFIG_DEBUG_ASSERTIONS) || \
|
|
|
|
(defined(MONITOR_MM_SEMAPHORE) && defined(CONFIG_DEBUG_INFO))
|
2007-02-17 23:21:28 +00:00
|
|
|
pid_t my_pid = getpid();
|
2011-06-18 20:02:40 +00:00
|
|
|
#endif
|
2007-02-17 23:21:28 +00:00
|
|
|
|
|
|
|
/* I better be holding at least one reference to the semaphore */
|
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
DEBUGASSERT(heap->mm_holder == my_pid);
|
2007-02-17 23:21:28 +00:00
|
|
|
|
|
|
|
/* Do I hold multiple references to the semphore */
|
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
if (heap->mm_counts_held > 1)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
|
|
|
/* Yes, just release one count and return */
|
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
heap->mm_counts_held--;
|
2016-06-12 11:11:08 -06:00
|
|
|
mseminfo("Holder=%d count=%d\n", heap->mm_holder,
|
|
|
|
heap->mm_counts_held);
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Nope, this is the last reference I have */
|
|
|
|
|
2016-06-12 09:46:23 -06:00
|
|
|
mseminfo("PID=%d giving\n", my_pid);
|
2014-05-28 14:09:58 -06:00
|
|
|
|
2013-03-08 18:29:56 +00:00
|
|
|
heap->mm_holder = -1;
|
|
|
|
heap->mm_counts_held = 0;
|
2017-10-03 15:35:24 -06:00
|
|
|
ASSERT(nxsem_post(&heap->mm_semaphore) == 0);
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
}
|