xiaoxiang781216 commented on code in PR #8645: URL: https://github.com/apache/nuttx/pull/8645#discussion_r1123468782
########## include/mutex.h: ########## @@ -0,0 +1,577 @@ +/**************************************************************************** + * include/mutex.h Review Comment: mutex.h is too general, it's easy to conflict with the 3rd party library or source code, if we put iit into include folder. ########## include/nuttx/mutex.h: ########## @@ -27,37 +27,94 @@ #include <assert.h> #include <stdbool.h> +#include <mutex.h> #include <nuttx/semaphore.h> /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define NXMUTEX_NO_HOLDER ((pid_t)-1) -#define NXMUTEX_INITIALIZER {NXSEM_INITIALIZER(1, SEM_TYPE_MUTEX | \ - SEM_PRIO_INHERIT), NXMUTEX_NO_HOLDER} -#define NXRMUTEX_INITIALIZER {NXMUTEX_INITIALIZER, 0} - -/**************************************************************************** - * Public Type Definitions - ****************************************************************************/ - -struct mutex_s -{ - sem_t sem; - pid_t holder; -}; - -typedef struct mutex_s mutex_t; - -struct rmutex_s -{ - mutex_t mutex; - unsigned int count; -}; - -typedef struct rmutex_s rmutex_t; +/* Most internal nxmutex_* interfaces are not available in the user space in + * PROTECTED and KERNEL builds. In that context, the application mutex + * interfaces must be used. The differences between the two sets of + * interfaces are: (1) the nxmutex_* interfaces do not cause cancellation + * points and (2) they do not modify the errno variable. + * + * This is only important when compiling libraries (libc or libnx) that are + * used both by the OS (libkc.a and libknx.a) or by the applications + * (libc.a and libnx.a). In that case, the correct interface must be + * used for the build context. + * + * REVISIT: In the flat build, the same functions must be used both by + * the OS and by applications. We have to use the normal user functions + * in this case or we will fail to set the errno or fail to create the + * cancellation point. + */ + +#if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__) +# define _MUTEX_INIT(m) nxmutex_init(m) +# define _MUTEX_DESTROY(m) nxmutex_destroy(m) +# define _MUTEX_BREAKLOCK(m, l) nxmutex_breaklock(m, l) +# define _MUTEX_RESTORELOCK(m, l) nxmutex_restorelock(m, l) +# define _MUTEX_LOCK(m) nxmutex_lock(m) +# define _MUTEX_IS_LOCKED(m) nxmutex_is_locked(m) +# define _MUTEX_IS_HOLD(m) nxmutex_is_hold(m) +# define _MUTEX_TIMEDLOCK(m, t) nxmutex_timedlock(m, t) +# define _MUTEX_CLOCKLOCK(m, t) nxmutex_clocklock(m, t) +# define _MUTEX_TRYLOCK(m) nxmutex_trylock(m) +# define _MUTEX_UNLOCK(m) nxmutex_unlock(m) +# define _MUTEX_SET_PROTOCOL(m, p) nxmutex_set_protocol(m, p) +# define _MUTEX_GET_PROTOCOL(m, p) nxmutex_get_protocol(m, p) + +# define _RMUTEX_INIT(rm) nxrmutex_init(rm) +# define _RMUTEX_DESTROY(rm) nxrmutex_destroy(rm) +# define _RMUTEX_BREAKLOCK(rm, l) nxrmutex_breaklock(rm, l) +# define _RMUTEX_RESTORELOCK(rm, l) nxrmutex_restorelock(rm, l) +# define _RMUTEX_LOCK(rm) nxrmutex_lock(rm) +# define _RMUTEX_IS_LOCKED(rm) nxrmutex_is_locked(rm) +# define _RMUTEX_IS_HOLD(m) nxrmutex_is_hold(m) +# define _RMUTEX_TIMEDLOCK(rm, t) nxrmutex_timedlock(rm, t) +# define _RMUTEX_CLOCKLOCK(rm, t) nxrmutex_clocklock(rm, t) +# define _RMUTEX_TRYLOCK(rm) nxrmutex_trylock(rm) +# define _RMUTEX_UNLOCK(rm) nxrmutex_unlock(rm) +# define _RMUTEX_SET_PROTOCOL(rm, p) nxrmutex_set_protocol(m, p) +# define _RMUTEX_GET_PROTOCOL(rm, p) nxrmutex_get_protocol(m, p) + +# define _MUTEX_ERRNO(r) (-(r)) +# define _MUTEX_ERRVAL(r) (r) +#else +# define _MUTEX_INIT(m) nxmtx_init(m) Review Comment: since mutex is a brand new type, we can design one set of function which return a negative error code directly instead modify errno. -- 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