Skip to content

Commit 35087de

Browse files
committed
porting/linux: Fix ble_npl_mutex_pend failing
pthread_mutex_timedlock fails if invalid struct timespec values are provided.
1 parent 94ee7f6 commit 35087de

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

porting/npl/linux/src/os_mutex.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ ble_npl_mutex_pend(struct ble_npl_mutex *mu, uint32_t timeout)
7171
mu->wait.tv_sec += timeout / 1000;
7272
mu->wait.tv_nsec += (timeout % 1000) * 1000000;
7373

74+
/* struct timespec tv_nsec holds nanosecond and allowed range is
75+
* 0 - 999999999, otherwise pthread_mutex_timedlock returns EINVAL
76+
*/
77+
if (mu->wait.tv_nsec >= 1000000000) {
78+
mu->wait.tv_sec += mu->wait.tv_nsec / 1000000000;
79+
mu->wait.tv_nsec = mu->wait.tv_nsec % 1000000000;
80+
}
81+
7482
err = pthread_mutex_timedlock(&mu->lock, &mu->wait);
7583
if (err == ETIMEDOUT) {
7684
return BLE_NPL_TIMEOUT;

0 commit comments

Comments
 (0)