porting/linux: Fix semaphore and mutex pend implementations

This commit is contained in:
Michał Narajowski
2019-02-08 16:50:41 +01:00
parent 44569e1c72
commit 27c61ea33d
2 changed files with 34 additions and 34 deletions
+19 -12
View File
@@ -17,14 +17,12 @@
* under the License.
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "os/os.h"
#include "nimble/nimble_npl.h"
#include <pthread.h>
ble_npl_error_t
ble_npl_mutex_init(struct ble_npl_mutex *mu)
{
@@ -56,19 +54,28 @@ ble_npl_mutex_release(struct ble_npl_mutex *mu)
ble_npl_error_t
ble_npl_mutex_pend(struct ble_npl_mutex *mu, uint32_t timeout)
{
int err;
if (!mu) {
return BLE_NPL_INVALID_PARAM;
}
assert(&mu->lock);
if (timeout == BLE_NPL_WAIT_FOREVER) {
err = pthread_mutex_lock(&mu->lock);
} else {
err = clock_gettime(CLOCK_REALTIME, &mu->wait);
if (err) {
return BLE_NPL_ERROR;
}
mu->wait.tv_sec = timeout / 1000;
mu->wait.tv_nsec = (timeout % 1000) * 1000000;
mu->wait.tv_nsec %= 1000000000;
mu->wait.tv_sec += timeout / 1000;
mu->wait.tv_nsec += (timeout % 1000) * 1000000;
if (pthread_mutex_timedlock(&mu->lock, &mu->wait)) {
return BLE_NPL_TIMEOUT;
err = pthread_mutex_timedlock(&mu->lock, &mu->wait);
if (err == ETIMEDOUT) {
return BLE_NPL_TIMEOUT;
}
}
return BLE_NPL_OK;
return (err) ? BLE_NPL_ERROR : BLE_NPL_OK;
}
+15 -22
View File
@@ -18,15 +18,11 @@
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "os/os.h"
#include "nimble/nimble_npl.h"
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include "os/os.h"
#include "nimble/nimble_npl.h"
ble_npl_error_t
ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
@@ -64,24 +60,21 @@ ble_npl_sem_pend(struct ble_npl_sem *sem, uint32_t timeout)
return BLE_NPL_INVALID_PARAM;
}
err = clock_gettime(CLOCK_REALTIME, &wait);
if (err) {
return BLE_NPL_ERROR;
}
wait.tv_sec += timeout / 1000;
wait.tv_nsec += (timeout % 1000) * 1000000;
wait.tv_nsec %= 1000000000;
if (timeout == BLE_NPL_WAIT_FOREVER) {
err = sem_wait(&sem->lock);
}
else
{
if (sem_timedwait(&sem->lock, &wait)) {
assert(errno == ETIMEDOUT);
return BLE_NPL_TIMEOUT;
}
} else {
err = clock_gettime(CLOCK_REALTIME, &wait);
if (err) {
return BLE_NPL_ERROR;
}
wait.tv_sec += timeout / 1000;
wait.tv_nsec += (timeout % 1000) * 1000000;
err = sem_timedwait(&sem->lock, &wait);
if (err && errno == ETIMEDOUT) {
return BLE_NPL_TIMEOUT;
}
}
return (err) ? BLE_NPL_ERROR : BLE_NPL_OK;