jlaitine commented on code in PR #16194: URL: https://github.com/apache/nuttx/pull/16194#discussion_r2071307167
########## sched/semaphore/sem_trywait.c: ########## @@ -74,29 +77,57 @@ int nxsem_trywait_slow(FAR sem_t *sem) /* If the semaphore is available, give it to the requesting task */ - semcount = atomic_read(NXSEM_COUNT(sem)); + if (mutex) + { + new = nxsched_gettid(); + } + + old = atomic_read(NXSEM_COUNT(sem)); do { - if (semcount <= 0) + if (mutex) { - leave_critical_section(flags); - return -EAGAIN; + if (NXSEM_MACQUIRED(old)) + { + goto out; + } + } + else + { + if (old <= 0) + { + goto out; + } + + new = old - 1; } } - while (!atomic_try_cmpxchg_acquire(NXSEM_COUNT(sem), - &semcount, semcount - 1)); + while (!atomic_try_cmpxchg_acquire(plock, &old, new)); /* It is, let the task take the semaphore */ ret = nxsem_protect_wait(sem); if (ret < 0) { - atomic_fetch_add(NXSEM_COUNT(sem), 1); + if (mutex) + { + atomic_set(NXSEM_MHOLDER(sem), NXSEM_NO_MHOLDER); + } + else + { + atomic_fetch_add(NXSEM_COUNT(sem), 1); + } + leave_critical_section(flags); return ret; } - nxsem_add_holder(sem); + if (!mutex) Review Comment: Sem with priority inheritance can surely be a mutex. I am not sure I understood the question; but you don't need the holder structure for mutex if the trywait succeeds; and actually can't add it here. The holder structure is only needed if you get blocked, in which case it is added for the current holder. This is the same as in sem_wait. -- 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