xiaoxiang781216 commented on code in PR #16030: URL: https://github.com/apache/nuttx/pull/16030#discussion_r2013297079
########## libs/libc/semaphore/sem_wait.c: ########## @@ -98,3 +98,43 @@ int sem_wait(FAR sem_t *sem) leave_cancellation_point(); return ERROR; } + +/**************************************************************************** + * Name: nxsem_wait + * + * Description: + * This function attempts to lock the semaphore referenced by 'sem'. If + * the semaphore value is (<=) zero, then the calling task will not return + * until it successfully acquires the lock. + * + * This is an internal OS interface. It is functionally equivalent to + * sem_wait except that: + * + * - It is not a cancellation point, and + * - It does not modify the errno value. + * + * Input Parameters: + * sem - Semaphore descriptor. + * + * Returned Value: + * This is an internal OS interface and should not be used by applications. + * It follows the NuttX internal error return policy: Zero (OK) is + * returned on success. A negated errno value is returned on failure. + * Possible returned errors: + * + * - EINVAL: Invalid attempt to get the semaphore + * - EINTR: The wait was interrupted by the receipt of a signal. + * + ****************************************************************************/ + +#if !defined(CONFIG_BUILD_FLAT) && !defined(__KERNEL__) +int nxsem_wait(FAR sem_t *sem) Review Comment: why not remove nxsem_wati and replace line 86 with nxsem_wait_impl directly ########## include/nuttx/sem_fast.h: ########## @@ -0,0 +1,184 @@ +/**************************************************************************** + * include/nuttx/sem_fast.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_SEM_FAST_H +#define __INCLUDE_NUTTX_SEM_FAST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/atomic.h> +#include <nuttx/semaphore.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: nxsem_*_slow + * + * Description: + * Kernel default implementations for the nxsem functions. These are + * called when the fast locking / unlocking can't be performed. For the + * parameters and return values, see the _impl functions below. + */ + +int nxsem_trywait_slow(sem_t *sem); +int nxsem_wait_slow(sem_t *sem); +int nxsem_post_slow(sem_t *sem); + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/* Mutex/semaphore fast path using atomics is not relevant when using + * CONFIG_LIBC_ARCH_ATOMIC, since libc atomic implementation uses spinlocks. + * Spinlocks should not be called from libc, and also in the kernel it is + * faster to take the slow path directly. + */ + +#ifndef CONFIG_LIBC_ARCH_ATOMIC + +/**************************************************************************** + * Name: nxsem_trywait_impl + * + * Description: + * Try to wait on semaphore using fast atomic exchange. If the atomic + * exchange can't be used, call the default implementation in kernel. + * + * Input Parameters: + * sem - The semaphore to wait on + * + * Returned Value: + * OK - Semaphore taken + * -EINVAL - Invalid attempt to get the semaphore + * -EAGAIN - The semaphore is not available. + * + * Assumptions: + * None + * + ****************************************************************************/ + +static inline_function int nxsem_trywait_impl(sem_t *sem) Review Comment: the patch could be better by: 1. rename nxsem_*_impl to nxsem_* 2. remove nxsem_* in sched/semaphore 3. remove nxsem_* in libs/libc/semaphore ########## include/nuttx/sem_fast.h: ########## @@ -0,0 +1,184 @@ +/**************************************************************************** + * include/nuttx/sem_fast.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_SEM_FAST_H +#define __INCLUDE_NUTTX_SEM_FAST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/atomic.h> +#include <nuttx/semaphore.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: nxsem_*_slow + * + * Description: + * Kernel default implementations for the nxsem functions. These are + * called when the fast locking / unlocking can't be performed. For the + * parameters and return values, see the _impl functions below. + */ + +int nxsem_trywait_slow(sem_t *sem); +int nxsem_wait_slow(sem_t *sem); +int nxsem_post_slow(sem_t *sem); + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/* Mutex/semaphore fast path using atomics is not relevant when using + * CONFIG_LIBC_ARCH_ATOMIC, since libc atomic implementation uses spinlocks. + * Spinlocks should not be called from libc, and also in the kernel it is + * faster to take the slow path directly. + */ + +#ifndef CONFIG_LIBC_ARCH_ATOMIC + +/**************************************************************************** + * Name: nxsem_trywait_impl + * + * Description: + * Try to wait on semaphore using fast atomic exchange. If the atomic + * exchange can't be used, call the default implementation in kernel. + * + * Input Parameters: + * sem - The semaphore to wait on + * + * Returned Value: + * OK - Semaphore taken + * -EINVAL - Invalid attempt to get the semaphore + * -EAGAIN - The semaphore is not available. + * + * Assumptions: + * None + * + ****************************************************************************/ + +static inline_function int nxsem_trywait_impl(sem_t *sem) Review Comment: ```suggestion static inline_function int nxsem_trywait(sem_t *sem) ``` ########## include/nuttx/sem_fast.h: ########## @@ -0,0 +1,184 @@ +/**************************************************************************** + * include/nuttx/sem_fast.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_SEM_FAST_H +#define __INCLUDE_NUTTX_SEM_FAST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/atomic.h> +#include <nuttx/semaphore.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: nxsem_*_slow + * + * Description: + * Kernel default implementations for the nxsem functions. These are + * called when the fast locking / unlocking can't be performed. For the + * parameters and return values, see the _impl functions below. + */ + +int nxsem_trywait_slow(sem_t *sem); +int nxsem_wait_slow(sem_t *sem); +int nxsem_post_slow(sem_t *sem); + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/* Mutex/semaphore fast path using atomics is not relevant when using + * CONFIG_LIBC_ARCH_ATOMIC, since libc atomic implementation uses spinlocks. + * Spinlocks should not be called from libc, and also in the kernel it is + * faster to take the slow path directly. + */ + +#ifndef CONFIG_LIBC_ARCH_ATOMIC + +/**************************************************************************** + * Name: nxsem_trywait_impl + * + * Description: + * Try to wait on semaphore using fast atomic exchange. If the atomic + * exchange can't be used, call the default implementation in kernel. + * + * Input Parameters: + * sem - The semaphore to wait on + * + * Returned Value: + * OK - Semaphore taken + * -EINVAL - Invalid attempt to get the semaphore + * -EAGAIN - The semaphore is not available. + * + * Assumptions: + * None + * + ****************************************************************************/ + +static inline_function int nxsem_trywait_impl(sem_t *sem) +{ + if ((sem->flags & SEM_TYPE_MUTEX) +#if defined(CONFIG_PRIORITY_PROTECT) || defined(CONFIG_PRIORITY_INHERITANCE) + && (sem->flags & (SEM_TYPE_MUTEX | SEM_PRIO_MASK)) == + (SEM_TYPE_MUTEX | SEM_PRIO_NONE) +#endif + ) + { + int32_t old = 1; + return atomic_try_cmpxchg_acquire(NXSEM_COUNT(sem), &old, 0) ? + OK : -EAGAIN; + } + + return nxsem_trywait_slow(sem); +} + +/**************************************************************************** + * Name: nxsem_wait_impl + * + * Description: + * Wait on semaphore using fast atomic exchange. If the atomic + * exchange can't be used, call the default implementation in kernel. + * + * Input Parameters: + * sem - The semaphore to wait on + * + * Returned Value: + * OK - Semaphore taken + * -EINVAL - Invalid attempt to get the semaphore + * -EINTR - The wait was interrupted by the receipt of a signal. + * + * Assumptions: + * None + * + ****************************************************************************/ + +static inline_function int nxsem_wait_impl(sem_t *sem) +{ + if ((sem->flags & SEM_TYPE_MUTEX) +#if defined(CONFIG_PRIORITY_PROTECT) || defined(CONFIG_PRIORITY_INHERITANCE) + && (sem->flags & (SEM_TYPE_MUTEX | SEM_PRIO_MASK)) == Review Comment: ditto ########## include/nuttx/sem_fast.h: ########## @@ -0,0 +1,184 @@ +/**************************************************************************** + * include/nuttx/sem_fast.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_SEM_FAST_H +#define __INCLUDE_NUTTX_SEM_FAST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/atomic.h> +#include <nuttx/semaphore.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: nxsem_*_slow + * + * Description: + * Kernel default implementations for the nxsem functions. These are + * called when the fast locking / unlocking can't be performed. For the + * parameters and return values, see the _impl functions below. + */ + +int nxsem_trywait_slow(sem_t *sem); Review Comment: add FAR ########## include/nuttx/sem_fast.h: ########## @@ -0,0 +1,184 @@ +/**************************************************************************** + * include/nuttx/sem_fast.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_SEM_FAST_H +#define __INCLUDE_NUTTX_SEM_FAST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/atomic.h> +#include <nuttx/semaphore.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: nxsem_*_slow + * + * Description: + * Kernel default implementations for the nxsem functions. These are + * called when the fast locking / unlocking can't be performed. For the + * parameters and return values, see the _impl functions below. + */ + +int nxsem_trywait_slow(sem_t *sem); +int nxsem_wait_slow(sem_t *sem); +int nxsem_post_slow(sem_t *sem); + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/* Mutex/semaphore fast path using atomics is not relevant when using + * CONFIG_LIBC_ARCH_ATOMIC, since libc atomic implementation uses spinlocks. + * Spinlocks should not be called from libc, and also in the kernel it is + * faster to take the slow path directly. + */ + +#ifndef CONFIG_LIBC_ARCH_ATOMIC + +/**************************************************************************** + * Name: nxsem_trywait_impl + * + * Description: + * Try to wait on semaphore using fast atomic exchange. If the atomic + * exchange can't be used, call the default implementation in kernel. + * + * Input Parameters: + * sem - The semaphore to wait on + * + * Returned Value: + * OK - Semaphore taken + * -EINVAL - Invalid attempt to get the semaphore + * -EAGAIN - The semaphore is not available. + * + * Assumptions: + * None + * + ****************************************************************************/ + +static inline_function int nxsem_trywait_impl(sem_t *sem) +{ + if ((sem->flags & SEM_TYPE_MUTEX) +#if defined(CONFIG_PRIORITY_PROTECT) || defined(CONFIG_PRIORITY_INHERITANCE) + && (sem->flags & (SEM_TYPE_MUTEX | SEM_PRIO_MASK)) == + (SEM_TYPE_MUTEX | SEM_PRIO_NONE) +#endif + ) + { + int32_t old = 1; + return atomic_try_cmpxchg_acquire(NXSEM_COUNT(sem), &old, 0) ? + OK : -EAGAIN; + } + + return nxsem_trywait_slow(sem); +} + +/**************************************************************************** + * Name: nxsem_wait_impl + * + * Description: + * Wait on semaphore using fast atomic exchange. If the atomic + * exchange can't be used, call the default implementation in kernel. + * + * Input Parameters: + * sem - The semaphore to wait on + * + * Returned Value: + * OK - Semaphore taken + * -EINVAL - Invalid attempt to get the semaphore + * -EINTR - The wait was interrupted by the receipt of a signal. + * + * Assumptions: + * None + * + ****************************************************************************/ + +static inline_function int nxsem_wait_impl(sem_t *sem) +{ + if ((sem->flags & SEM_TYPE_MUTEX) +#if defined(CONFIG_PRIORITY_PROTECT) || defined(CONFIG_PRIORITY_INHERITANCE) + && (sem->flags & (SEM_TYPE_MUTEX | SEM_PRIO_MASK)) == + (SEM_TYPE_MUTEX | SEM_PRIO_NONE) +#endif + ) + { + int32_t old = 1; + if (atomic_try_cmpxchg_acquire(NXSEM_COUNT(sem), &old, 0)) + { + return OK; + } + } + + return nxsem_wait_slow(sem); +} + +/**************************************************************************** + * Name: nxsem_post_impl + * + * Description: + * Try to release a semaphore using fast atomic exchange + * + * Input Parameters: + * sem - The semaphore to post + * + * Returned Value: + * OK - Semaphore postaed + * Negated error value on any error + * + * Assumptions: + * None + * + ****************************************************************************/ + +static inline_function int nxsem_post_impl(sem_t *sem) +{ + if ((sem->flags & SEM_TYPE_MUTEX) +#if defined(CONFIG_PRIORITY_PROTECT) || defined(CONFIG_PRIORITY_INHERITANCE) + && (sem->flags & (SEM_TYPE_MUTEX | SEM_PRIO_MASK)) == Review Comment: ditto ########## include/nuttx/sem_fast.h: ########## @@ -0,0 +1,184 @@ +/**************************************************************************** + * include/nuttx/sem_fast.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_SEM_FAST_H +#define __INCLUDE_NUTTX_SEM_FAST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/atomic.h> +#include <nuttx/semaphore.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: nxsem_*_slow + * + * Description: + * Kernel default implementations for the nxsem functions. These are + * called when the fast locking / unlocking can't be performed. For the + * parameters and return values, see the _impl functions below. + */ + +int nxsem_trywait_slow(sem_t *sem); +int nxsem_wait_slow(sem_t *sem); +int nxsem_post_slow(sem_t *sem); + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/* Mutex/semaphore fast path using atomics is not relevant when using + * CONFIG_LIBC_ARCH_ATOMIC, since libc atomic implementation uses spinlocks. + * Spinlocks should not be called from libc, and also in the kernel it is + * faster to take the slow path directly. + */ + +#ifndef CONFIG_LIBC_ARCH_ATOMIC + +/**************************************************************************** + * Name: nxsem_trywait_impl + * + * Description: + * Try to wait on semaphore using fast atomic exchange. If the atomic + * exchange can't be used, call the default implementation in kernel. + * + * Input Parameters: + * sem - The semaphore to wait on + * + * Returned Value: + * OK - Semaphore taken + * -EINVAL - Invalid attempt to get the semaphore + * -EAGAIN - The semaphore is not available. + * + * Assumptions: + * None + * + ****************************************************************************/ + +static inline_function int nxsem_trywait_impl(sem_t *sem) +{ + if ((sem->flags & SEM_TYPE_MUTEX) +#if defined(CONFIG_PRIORITY_PROTECT) || defined(CONFIG_PRIORITY_INHERITANCE) + && (sem->flags & (SEM_TYPE_MUTEX | SEM_PRIO_MASK)) == Review Comment: ``` && (sem->flags & SEM_PRIO_MASK) == SEM_PRIO_NONE) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org