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
|
|
|
*
|
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
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01: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.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
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>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2020-05-04 12:55:11 +02:00
|
|
|
#include <debug.h>
|
2012-07-15 01:31:12 +02:00
|
|
|
|
2021-07-07 15:21:42 +02:00
|
|
|
#include <nuttx/arch.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
|
|
|
|
2021-03-02 09:03:00 +01:00
|
|
|
#include "mm_heap/mm.h"
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
_SEM_INIT(&heap->mm_semaphore, 0, 1);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2021-07-07 15:21:42 +02:00
|
|
|
* Name: mm_takesemaphore
|
2007-03-14 19:58:21 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2021-07-07 15:21:42 +02:00
|
|
|
* Take the MM mutex. This may be called from the OS in certain conditions
|
|
|
|
* when it is impossible to wait on a semaphore:
|
|
|
|
* 1.The idle process performs the memory corruption check.
|
|
|
|
* 2.The task/thread free the memory in the exiting process.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* heap - heap instance want to take semaphore
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* true if the semaphore can be taken, otherwise false.
|
2007-03-14 19:58:21 +01:00
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2021-07-07 15:21:42 +02:00
|
|
|
bool mm_takesemaphore(FAR struct mm_heap_s *heap)
|
2007-03-14 19:58:21 +01:00
|
|
|
{
|
2021-07-07 15:21:42 +02:00
|
|
|
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
|
|
|
/* Check current environment */
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2021-07-07 15:21:42 +02:00
|
|
|
if (up_interrupt_context())
|
2020-04-08 10:03:01 +02:00
|
|
|
{
|
2021-07-07 15:21:42 +02:00
|
|
|
/* Can't take semaphore in the interrupt handler */
|
|
|
|
|
|
|
|
return false;
|
2020-04-08 10:03:01 +02:00
|
|
|
}
|
2021-07-07 15:21:42 +02:00
|
|
|
else
|
|
|
|
#endif
|
2020-04-08 10:03:01 +02:00
|
|
|
|
2021-07-07 15:21:42 +02:00
|
|
|
/* getpid() returns the task ID of the task at the head of the ready-to-
|
|
|
|
* run task list. mm_takesemaphore() may be called during context
|
|
|
|
* switches. There are certain situations during context switching when
|
|
|
|
* the OS data structures are in flux and then can't be freed immediately
|
|
|
|
* (e.g. the running thread stack).
|
|
|
|
*
|
|
|
|
* This is handled by getpid() to return the special value -ESRCH to
|
|
|
|
* indicate this special situation.
|
2018-12-27 16:32:47 +01:00
|
|
|
*/
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2021-07-07 15:21:42 +02:00
|
|
|
if (getpid() < 0)
|
2007-03-14 19:58:21 +01:00
|
|
|
{
|
2021-07-07 15:21:42 +02:00
|
|
|
return false;
|
2007-03-14 19:58:21 +01:00
|
|
|
}
|
2021-07-07 15:21:42 +02:00
|
|
|
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
|
|
|
else if (sched_idletask())
|
2007-03-14 19:58:21 +01:00
|
|
|
{
|
2018-12-20 17:19:52 +01:00
|
|
|
/* Try to take the semaphore */
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2021-07-07 15:21:42 +02:00
|
|
|
return _SEM_TRYWAIT(&heap->mm_semaphore) >= 0;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2021-07-07 15:21:42 +02:00
|
|
|
#endif
|
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) */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
do
|
2012-07-15 01:31:12 +02:00
|
|
|
{
|
2021-07-03 16:16:25 +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)
|
|
|
|
{
|
2020-05-02 16:05:41 +02:00
|
|
|
ret = _SEM_ERRVAL(ret);
|
2018-02-20 19:24:53 +01:00
|
|
|
DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
|
2017-10-07 15:16:33 +02:00
|
|
|
}
|
2012-07-15 01:31:12 +02:00
|
|
|
}
|
2021-07-07 15:21:42 +02:00
|
|
|
while (ret < 0);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-07-07 15:21:42 +02:00
|
|
|
return true;
|
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
|
|
|
{
|
2021-07-07 15:21:42 +02:00
|
|
|
DEBUGVERIFY(_SEM_POST(&heap->mm_semaphore));
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|