Skip to content

Commit a599dbd

Browse files
benwrsnashif
authored andcommitted
kernel: add emphasis to nano_sem_take/k_sem_take return code difference
The reversal of the meaning of a value of 0 from k_sem_take vs nano_sem_take has caused some issue when porting code from the legacy API to the new API, so put some emphasis on this difference. - Add a note in the API description. - Put the call to k_sem_take and the reversal of the return value inside of nano_sem_take on one line so that grepping on it shows the reversal. Change-Id: I2f4ba58dc087176d68b55371fa6e367b72559e70 Signed-off-by: Benjamin Walsh <[email protected]>
1 parent 4344299 commit a599dbd

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

include/kernel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,12 @@ extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
16941694
* @param timeout Waiting period to take the semaphore (in milliseconds),
16951695
* or one of the special values K_NO_WAIT and K_FOREVER.
16961696
*
1697+
* @note When porting code from the nanokernel legacy API to the new API, be
1698+
* careful with the return value of this function. The return value is the
1699+
* reverse of the one of nano_sem_take family of APIs: 0 means success, and
1700+
* non-zero means failure, while the nano_sem_take family returns 1 for success
1701+
* and 0 for failure.
1702+
*
16971703
* @retval 0 Semaphore taken.
16981704
* @retval -EBUSY Returned without waiting.
16991705
* @retval -EAGAIN Waiting period timed out.

include/legacy.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,9 @@ static inline __deprecated void nano_sem_give(struct nano_sem *sem)
10601060
static inline __deprecated int nano_sem_take(struct nano_sem *sem,
10611061
int32_t timeout_in_ticks)
10621062
{
1063-
return k_sem_take((struct k_sem *)sem, _ticks_to_ms(timeout_in_ticks))
1064-
== 0 ? 1 : 0;
1063+
int32_t ms = _ticks_to_ms(timeout_in_ticks);
1064+
1065+
return k_sem_take((struct k_sem *)sem, ms) == 0 ? 1 : 0;
10651066
}
10661067

10671068
/**

0 commit comments

Comments
 (0)