2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2022-09-06 08:18:45 +02:00
|
|
|
* mm/mm_heap/mm_lock.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2024-09-12 16:44:55 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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>
|
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
|
|
|
/****************************************************************************
|
2022-09-06 08:18:45 +02:00
|
|
|
* Name: mm_lock
|
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
|
2022-09-06 08:18:45 +02:00
|
|
|
* when it is impossible to wait on a mutex:
|
2021-07-07 15:21:42 +02:00
|
|
|
* 1.The idle process performs the memory corruption check.
|
|
|
|
* 2.The task/thread free the memory in the exiting process.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2022-09-06 08:18:45 +02:00
|
|
|
* heap - heap instance want to take mutex
|
2021-07-07 15:21:42 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2022-11-05 23:44:47 +01:00
|
|
|
* 0 if the lock can be taken, otherwise negative errno.
|
2007-03-14 19:58:21 +01:00
|
|
|
*
|
2008-01-09 15:20:31 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 19:58:21 +01:00
|
|
|
|
2022-11-05 23:44:47 +01:00
|
|
|
int mm_lock(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
|
|
|
{
|
2023-06-24 11:35:48 +02:00
|
|
|
# if !defined(CONFIG_SMP)
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Check the mutex value, if held by someone, then return false.
|
2022-02-18 06:26:08 +01:00
|
|
|
* Or, touch the heap internal data directly.
|
|
|
|
*/
|
|
|
|
|
2022-11-05 23:44:47 +01:00
|
|
|
return nxmutex_is_locked(&heap->mm_lock) ? -EAGAIN : 0;
|
2023-06-24 11:35:48 +02:00
|
|
|
# else
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Can't take mutex in SMP interrupt handler */
|
2021-07-07 15:21:42 +02:00
|
|
|
|
2022-11-05 23:44:47 +01:00
|
|
|
return -EAGAIN;
|
2023-06-24 11:35:48 +02:00
|
|
|
# endif
|
2020-04-08 10:03:01 +02:00
|
|
|
}
|
2021-07-07 15:21:42 +02:00
|
|
|
#endif
|
2020-04-08 10:03:01 +02:00
|
|
|
|
2023-02-01 11:05:58 +01:00
|
|
|
/* _SCHED_GETTID() returns the task ID of the task at the head of the
|
|
|
|
* ready-to-run task list. mm_lock() may be called during context
|
2021-07-07 15:21:42 +02:00
|
|
|
* 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).
|
|
|
|
*
|
2023-02-01 11:05:58 +01:00
|
|
|
* This is handled by _SCHED_GETTID() 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
|
|
|
|
2023-02-01 11:05:58 +01:00
|
|
|
if (_SCHED_GETTID() < 0)
|
2007-03-14 19:58:21 +01:00
|
|
|
{
|
2022-11-05 23:44:47 +01:00
|
|
|
return -ESRCH;
|
2007-03-14 19:58:21 +01:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
else
|
|
|
|
{
|
2022-11-05 23:44:47 +01:00
|
|
|
return nxmutex_lock(&heap->mm_lock);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-09 15:20:31 +01:00
|
|
|
/****************************************************************************
|
2022-09-06 08:18:45 +02:00
|
|
|
* Name: mm_unlock
|
2007-03-14 19:58:21 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
void mm_unlock(FAR struct mm_heap_s *heap)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2022-02-14 09:47:42 +01:00
|
|
|
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
|
|
|
if (up_interrupt_context())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
DEBUGVERIFY(nxmutex_unlock(&heap->mm_lock));
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|