xiaoxiang781216 commented on code in PR #19733:
URL: https://github.com/apache/nuttx/pull/19733#discussion_r3738448073


##########
include/nuttx/semaphore.h:
##########
@@ -74,6 +74,22 @@
 
 #define NXSEM_MHOLDER(s)      ((FAR atomic_t *)&((s)->val.mholder))
 
+/* Encode a task id as a mutex holder.  The holder field keeps the id in
+ * the low 31 bits because bit 31 is the blocking flag, and a task id can
+ * be negative here: nxsched_gettid() reports -ESRCH for a context that no
+ * longer maps to a task, which is exactly the state a task is in while
+ * nxtask_exit() tears it down and its group's mutexes get their final
+ * lock and unlock.  Storing such an id unmasked would raise the blocking
+ * flag by accident, and an unlock would then try to wake waiters that do
+ * not exist.  Masking keeps a lock and unlock from the same context
+ * consistent with each other, whatever the id's sign.
+ *
+ * The ids -1 and -2 would alias NXSEM_MRESET and NXSEM_NO_MHOLDER, but
+ * nxsched_gettid() produces only valid ids and -ESRCH, which is -3.
+ */
+
+#define NXSEM_MHOLDER_ENCODE(tid) ((uint32_t)(tid) & ~NXSEM_MBLOCKING_BIT)

Review Comment:
   NXSEM_MAKE_MHOLDER?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to