2023-10-14 13:07:45 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2023-07-25 Shell first version
|
2024-03-28 23:42:56 +08:00
|
|
|
* 2023-11-25 Shell Add pgrp, session lock API
|
2023-10-14 13:07:45 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define DBG_TAG "lwp.internal"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "lwp_internal.h"
|
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
static rt_err_t _mutex_take_safe(rt_mutex_t mtx, rt_int32_t timeout, int flags)
|
2023-10-14 13:07:45 +08:00
|
|
|
{
|
2024-02-23 17:49:15 +08:00
|
|
|
LWP_DEF_RETURN_CODE(rc);
|
2023-10-14 13:07:45 +08:00
|
|
|
int retry;
|
|
|
|
rt_int32_t effect_timeout;
|
|
|
|
|
|
|
|
#ifdef LWP_DEBUG
|
|
|
|
rt_thread_t thread = rt_thread_self();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mtx)
|
|
|
|
{
|
|
|
|
effect_timeout = timeout;
|
|
|
|
#if DBG_LVL == DBG_LOG && defined(LWP_DEBUG)
|
|
|
|
int exception;
|
|
|
|
rt_list_t *node = RT_NULL;
|
|
|
|
struct rt_mutex *tak_obj = RT_NULL;
|
|
|
|
if (!rt_list_isempty(&(thread->taken_object_list)) && timeout == RT_WAITING_FOREVER)
|
|
|
|
{
|
|
|
|
exception = 1;
|
|
|
|
effect_timeout = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exception = 0;
|
|
|
|
}
|
|
|
|
#endif /* DBG_LOG && defined(LWP_DEBUG) */
|
|
|
|
|
|
|
|
do {
|
|
|
|
retry = 0;
|
2024-03-28 23:42:56 +08:00
|
|
|
if (flags & LWP_MTX_FLAGS_INTR)
|
2023-10-14 13:07:45 +08:00
|
|
|
rc = rt_mutex_take_interruptible(mtx, effect_timeout);
|
|
|
|
else
|
2024-03-28 23:42:56 +08:00
|
|
|
rc = rt_mutex_take_killable(mtx, effect_timeout);
|
2023-10-14 13:07:45 +08:00
|
|
|
|
|
|
|
#ifdef LWP_DEBUG
|
|
|
|
if (rc == RT_EOK)
|
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
if (!(flags & LWP_MTX_FALGS_NESTED) && rt_mutex_get_hold(mtx) > 1)
|
2023-10-14 13:07:45 +08:00
|
|
|
{
|
|
|
|
LOG_W("Already hold the lock");
|
2023-10-21 20:14:45 +08:00
|
|
|
rt_backtrace();
|
2023-10-14 13:07:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rc == -RT_ETIMEOUT)
|
|
|
|
{
|
|
|
|
#if DBG_LVL == DBG_LOG
|
|
|
|
if (exception)
|
|
|
|
{
|
|
|
|
rt_list_for_each(node, &(thread->taken_object_list))
|
|
|
|
{
|
|
|
|
tak_obj = rt_list_entry(node, struct rt_mutex, taken_list);
|
|
|
|
if (rt_mutex_get_owner(tak_obj)->stat & RT_THREAD_SUSPEND_MASK)
|
|
|
|
LOG_D("Potential dead lock - Taken: %s, Try take: %s",
|
|
|
|
tak_obj->parent.parent.name, mtx->parent.parent.name);
|
|
|
|
}
|
|
|
|
rt_backtrace();
|
|
|
|
retry = 1;
|
|
|
|
exception = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (rc != -RT_EINTR)
|
|
|
|
{
|
|
|
|
char tname[RT_NAME_MAX];
|
|
|
|
rt_thread_get_name(thread, tname, sizeof(tname));
|
|
|
|
LOG_W("Possible kernel corruption detected on thread %s with errno %ld", tname, rc);
|
|
|
|
}
|
|
|
|
#endif /* LWP_DEBUG */
|
|
|
|
} while (retry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-30 17:58:38 +08:00
|
|
|
rc = -RT_ERROR;
|
2023-10-14 13:07:45 +08:00
|
|
|
LOG_W("%s: mtx should not be NULL", __func__);
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
LWP_RETURN(rc);
|
2023-10-14 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_err_t lwp_mutex_take_safe(rt_mutex_t mtx, rt_int32_t timeout, int flags)
|
2023-10-14 13:07:45 +08:00
|
|
|
{
|
2024-02-23 17:49:15 +08:00
|
|
|
LWP_DEF_RETURN_CODE(rc);
|
2024-03-28 23:42:56 +08:00
|
|
|
rc = _mutex_take_safe(mtx, timeout, flags);
|
2024-02-23 17:49:15 +08:00
|
|
|
LWP_RETURN(rc);
|
2023-10-14 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t lwp_mutex_release_safe(rt_mutex_t mtx)
|
|
|
|
{
|
2024-02-23 17:49:15 +08:00
|
|
|
LWP_DEF_RETURN_CODE(rc);
|
2023-10-14 13:07:45 +08:00
|
|
|
|
|
|
|
rc = rt_mutex_release(mtx);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
LOG_I("%s: release failed with code %ld", __func__, rc);
|
2023-10-21 20:14:45 +08:00
|
|
|
rt_backtrace();
|
2023-10-14 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
LWP_RETURN(rc);
|
2023-10-14 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_err_t lwp_critical_enter(struct rt_lwp *lwp, int flags)
|
2023-10-14 13:07:45 +08:00
|
|
|
{
|
|
|
|
rt_err_t rc;
|
2024-03-28 23:42:56 +08:00
|
|
|
do {
|
|
|
|
rc = lwp_mutex_take_safe(&lwp->lwp_lock, RT_WAITING_FOREVER, flags);
|
|
|
|
} while (rc != RT_EOK && !(flags & LWP_MTX_FLAGS_INTR) && rc == -RT_EINTR);
|
2023-10-14 13:07:45 +08:00
|
|
|
|
|
|
|
/* if current process is force killed */
|
2024-03-28 23:42:56 +08:00
|
|
|
if (rc != RT_EOK && rc != -RT_EINTR)
|
2023-10-14 13:07:45 +08:00
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
LOG_I("%s: unexpected return code = %ld", __func__, rc);
|
2023-10-14 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t lwp_critical_exit(struct rt_lwp *lwp)
|
|
|
|
{
|
|
|
|
return lwp_mutex_release_safe(&lwp->lwp_lock);
|
|
|
|
}
|
2024-03-28 23:42:56 +08:00
|
|
|
|
|
|
|
rt_err_t lwp_pgrp_critical_enter(struct rt_processgroup *pgrp, int flags)
|
|
|
|
{
|
|
|
|
rt_err_t rc;
|
|
|
|
do {
|
|
|
|
rc = lwp_mutex_take_safe(&pgrp->mutex, RT_WAITING_FOREVER, flags);
|
|
|
|
} while (rc != RT_EOK && !(flags & LWP_MTX_FLAGS_INTR) && rc == -RT_EINTR);
|
|
|
|
|
|
|
|
/* if current process is force killed */
|
|
|
|
if (rc != RT_EOK && rc != -RT_EINTR)
|
|
|
|
{
|
|
|
|
LOG_I("%s: unexpected return code = %ld", __func__, rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t lwp_pgrp_critical_exit(struct rt_processgroup *pgrp)
|
|
|
|
{
|
|
|
|
return lwp_mutex_release_safe(&pgrp->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t lwp_sess_critical_enter(struct rt_session *sess, int flags)
|
|
|
|
{
|
|
|
|
rt_err_t rc;
|
|
|
|
do {
|
|
|
|
rc = lwp_mutex_take_safe(&sess->mutex, RT_WAITING_FOREVER, flags);
|
|
|
|
} while (rc != RT_EOK && !(flags & LWP_MTX_FLAGS_INTR) && rc == -RT_EINTR);
|
|
|
|
|
|
|
|
/* if current process is force killed */
|
|
|
|
if (rc != RT_EOK && rc != -RT_EINTR)
|
|
|
|
{
|
|
|
|
LOG_I("%s: unexpected return code = %ld", __func__, rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t lwp_sess_critical_exit(struct rt_session *sess)
|
|
|
|
{
|
|
|
|
return lwp_mutex_release_safe(&sess->mutex);
|
|
|
|
}
|