2023-06-19 10:40:09 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/misc/deadlock.c
|
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2023-06-19 10:40:09 +08: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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/mutex.h>
|
|
|
|
#include <nuttx/sched.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct deadlock_info_s
|
|
|
|
{
|
|
|
|
FAR pid_t *pid;
|
|
|
|
size_t count;
|
|
|
|
size_t found;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-09-06 22:41:33 +08:00
|
|
|
* Name: collect_deadlock
|
2023-06-19 10:40:09 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void collect_deadlock(FAR struct tcb_s *tcb, FAR void *arg)
|
|
|
|
{
|
2023-09-06 22:41:33 +08:00
|
|
|
if (tcb->task_state == TSTATE_WAIT_SEM)
|
2023-06-19 10:40:09 +08:00
|
|
|
{
|
|
|
|
FAR sem_t *sem = tcb->waitobj;
|
|
|
|
|
2023-09-06 22:41:33 +08:00
|
|
|
if (sem != NULL && (sem->flags & SEM_TYPE_MUTEX) != 0)
|
2023-06-19 10:40:09 +08:00
|
|
|
{
|
2023-09-06 22:41:33 +08:00
|
|
|
FAR struct deadlock_info_s *info = arg;
|
|
|
|
size_t index;
|
2023-06-19 10:40:09 +08:00
|
|
|
|
2023-09-06 22:41:33 +08:00
|
|
|
for (index = 0; index < info->found; index++)
|
2023-06-19 10:40:09 +08:00
|
|
|
{
|
2023-09-06 22:41:33 +08:00
|
|
|
if (info->pid[index] == tcb->pid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-06-19 10:40:09 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 22:41:33 +08:00
|
|
|
for (index = info->found; index < info->count; index++)
|
|
|
|
{
|
|
|
|
pid_t next;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
next = ((FAR mutex_t *)sem)->holder;
|
|
|
|
for (i = info->found; i < index; i++)
|
|
|
|
{
|
|
|
|
if (info->pid[i] == next)
|
|
|
|
{
|
|
|
|
info->found = index;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info->pid[index] = tcb->pid;
|
|
|
|
tcb = nxsched_get_tcb(next);
|
|
|
|
}
|
|
|
|
}
|
2023-06-19 10:40:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsched_collect_deadlock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if there is a deadlock and get the thread pid of the deadlock.
|
|
|
|
*
|
|
|
|
* Input parameters:
|
|
|
|
* pid - The array to store the thread pid of the deadlock.
|
|
|
|
* count - The size of the pid array.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The number of thread deadlocks.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
size_t nxsched_collect_deadlock(FAR pid_t *pid, size_t count)
|
|
|
|
{
|
|
|
|
struct deadlock_info_s info;
|
|
|
|
|
|
|
|
info.pid = pid;
|
|
|
|
info.count = count;
|
|
|
|
info.found = 0;
|
|
|
|
nxsched_foreach(collect_deadlock, &info);
|
|
|
|
return info.found;
|
|
|
|
}
|