Skip to content

Commit

Permalink
Fix system calls return error processing (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc authored Aug 9, 2024
1 parent 00f1c00 commit 37fd830
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 59 deletions.
14 changes: 14 additions & 0 deletions include/zenoh-pico/system/platform-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#ifndef ZENOH_PICO_SYSTEM_PLATFORM_COMMON_H
#define ZENOH_PICO_SYSTEM_PLATFORM_COMMON_H

#include <stdbool.h>
#include <stdint.h>

#include "zenoh-pico/api/olv_macros.h"
#include "zenoh-pico/config.h"
#include "zenoh-pico/utils/result.h"

#if defined(ZENOH_LINUX) || defined(ZENOH_MACOS) || defined(ZENOH_BSD)
#include "zenoh-pico/system/platform/unix.h"
Expand Down Expand Up @@ -49,6 +51,18 @@
extern "C" {
#endif

void _z_report_system_error(int errcode);

#define _Z_CHECK_SYS_ERR(expr) \
do { \
int __res = expr; \
if (__res != 0) { \
_z_report_system_error(__res); \
return _Z_ERR_SYSTEM_GENERIC; \
} \
return _Z_RES_OK; \
} while (false)

/*------------------ Random ------------------*/
uint8_t z_random_u8(void);
uint16_t z_random_u16(void);
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/utils/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ typedef enum {

_Z_ERR_SCOUT_NO_RESULTS = -87,

_Z_ERR_SYSTEM_GENERIC = -80,
_Z_ERR_SYSTEM_TASK_FAILED = -79,
_Z_ERR_SYSTEM_OUT_OF_MEMORY = -78,

Expand Down
20 changes: 10 additions & 10 deletions src/system/arduino/esp32/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,26 @@ void _z_task_free(_z_task_t **task) {
}

/*------------------ Mutex ------------------*/
int8_t _z_mutex_init(_z_mutex_t *m) { return pthread_mutex_init(m, NULL); }
int8_t _z_mutex_init(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_init(m, NULL)); }

int8_t _z_mutex_drop(_z_mutex_t *m) { return pthread_mutex_destroy(m); }
int8_t _z_mutex_drop(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_destroy(m)); }

int8_t _z_mutex_lock(_z_mutex_t *m) { return pthread_mutex_lock(m); }
int8_t _z_mutex_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_lock(m)); }

int8_t _z_mutex_try_lock(_z_mutex_t *m) { return pthread_mutex_trylock(m); }
int8_t _z_mutex_try_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_trylock(m)); }

int8_t _z_mutex_unlock(_z_mutex_t *m) { return pthread_mutex_unlock(m); }
int8_t _z_mutex_unlock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_unlock(m)); }

/*------------------ Condvar ------------------*/
int8_t _z_condvar_init(_z_condvar_t *cv) { return pthread_cond_init(cv, NULL); }
int8_t _z_condvar_init(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_init(cv, NULL)); }

int8_t _z_condvar_free(_z_condvar_t *cv) { return pthread_cond_destroy(cv); }
int8_t _z_condvar_free(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_destroy(cv)); }

int8_t _z_condvar_signal(_z_condvar_t *cv) { return pthread_cond_signal(cv); }
int8_t _z_condvar_signal(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_signal(cv)); }

int8_t _z_condvar_signal_all(_z_condvar_t *cv) { return pthread_cond_broadcast(cv); }
int8_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_broadcast(cv)); }

int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wait(cv, m); }
int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_cond_wait(cv, m)); }
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
Expand Down
26 changes: 13 additions & 13 deletions src/system/emscripten/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ void z_free(void *ptr) { free(ptr); }
#if Z_FEATURE_MULTI_THREAD == 1
/*------------------ Task ------------------*/
int8_t _z_task_init(_z_task_t *task, pthread_attr_t *attr, void *(*fun)(void *), void *arg) {
return pthread_create(task, attr, fun, arg);
_Z_CHECK_SYS_ERR(pthread_create(task, attr, fun, arg));
}

int8_t _z_task_join(_z_task_t *task) { return pthread_join(*task, NULL); }
int8_t _z_task_join(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_join(*task, NULL)); }

int8_t _z_task_cancel(_z_task_t *task) { return pthread_cancel(*task); }
int8_t _z_task_cancel(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_cancel(*task)); }

void _z_task_free(_z_task_t **task) {
_z_task_t *ptr = *task;
Expand All @@ -59,26 +59,26 @@ void _z_task_free(_z_task_t **task) {
}

/*------------------ Mutex ------------------*/
int8_t _z_mutex_init(_z_mutex_t *m) { return pthread_mutex_init(m, 0); }
int8_t _z_mutex_init(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_init(m, 0)); }

int8_t _z_mutex_drop(_z_mutex_t *m) { return pthread_mutex_destroy(m); }
int8_t _z_mutex_drop(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_destroy(m)); }

int8_t _z_mutex_lock(_z_mutex_t *m) { return pthread_mutex_lock(m); }
int8_t _z_mutex_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_lock(m)); }

int8_t _z_mutex_try_lock(_z_mutex_t *m) { return pthread_mutex_trylock(m); }
int8_t _z_mutex_try_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_trylock(m)); }

int8_t _z_mutex_unlock(_z_mutex_t *m) { return pthread_mutex_unlock(m); }
int8_t _z_mutex_unlock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_unlock(m)); }

/*------------------ Condvar ------------------*/
int8_t _z_condvar_init(_z_condvar_t *cv) { return pthread_cond_init(cv, 0); }
int8_t _z_condvar_init(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_init(cv, 0)); }

int8_t _z_condvar_drop(_z_condvar_t *cv) { return pthread_cond_destroy(cv); }
int8_t _z_condvar_drop(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_destroy(cv)); }

int8_t _z_condvar_signal(_z_condvar_t *cv) { return pthread_cond_signal(cv); }
int8_t _z_condvar_signal(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_signal(cv)); }

int8_t _z_condvar_signal_all(_z_condvar_t *cv) { return pthread_cond_broadcast(cv); }
int8_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_broadcast(cv)); }

int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wait(cv, m); }
int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_cond_wait(cv, m)); }
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
Expand Down
20 changes: 10 additions & 10 deletions src/system/espidf/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,26 @@ void _z_task_free(_z_task_t **task) {
}

/*------------------ Mutex ------------------*/
int8_t _z_mutex_init(_z_mutex_t *m) { return pthread_mutex_init(m, NULL); }
int8_t _z_mutex_init(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_init(m, NULL)); }

int8_t _z_mutex_drop(_z_mutex_t *m) { return pthread_mutex_destroy(m); }
int8_t _z_mutex_drop(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_destroy(m)); }

int8_t _z_mutex_lock(_z_mutex_t *m) { return pthread_mutex_lock(m); }
int8_t _z_mutex_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_lock(m)); }

int8_t _z_mutex_try_lock(_z_mutex_t *m) { return pthread_mutex_trylock(m); }
int8_t _z_mutex_try_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_trylock(m)); }

int8_t _z_mutex_unlock(_z_mutex_t *m) { return pthread_mutex_unlock(m); }
int8_t _z_mutex_unlock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_unlock(m)); }

/*------------------ Condvar ------------------*/
int8_t _z_condvar_init(_z_condvar_t *cv) { return pthread_cond_init(cv, NULL); }
int8_t _z_condvar_init(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_init(cv, NULL)); }

int8_t _z_condvar_drop(_z_condvar_t *cv) { return pthread_cond_destroy(cv); }
int8_t _z_condvar_drop(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_destroy(cv)); }

int8_t _z_condvar_signal(_z_condvar_t *cv) { return pthread_cond_signal(cv); }
int8_t _z_condvar_signal(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_signal(cv)); }

int8_t _z_condvar_signal_all(_z_condvar_t *cv) { return pthread_cond_broadcast(cv); }
int8_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_broadcast(cv)); }

int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wait(cv, m); }
int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_cond_wait(cv, m)); }
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
Expand Down
3 changes: 3 additions & 0 deletions src/system/platform-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include "zenoh-pico/system/platform-common.h"

#include "zenoh-pico/api/olv_macros.h"
#include "zenoh-pico/utils/logging.h"

void _z_report_system_error(int errcode) { _Z_ERROR("System error: %i", errcode); }

#if Z_FEATURE_MULTI_THREAD == 1

Expand Down
26 changes: 13 additions & 13 deletions src/system/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ void z_free(void *ptr) { free(ptr); }
#if Z_FEATURE_MULTI_THREAD == 1
/*------------------ Task ------------------*/
int8_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) {
return (int8_t)pthread_create(task, attr, fun, arg);
_Z_CHECK_SYS_ERR(pthread_create(task, attr, fun, arg));
}

int8_t _z_task_join(_z_task_t *task) { return (int8_t)pthread_join(*task, NULL); }
int8_t _z_task_join(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_join(*task, NULL)); }

int8_t _z_task_cancel(_z_task_t *task) { return (int8_t)pthread_cancel(*task); }
int8_t _z_task_cancel(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_cancel(*task)); }

void _z_task_free(_z_task_t **task) {
_z_task_t *ptr = *task;
Expand All @@ -116,26 +116,26 @@ void _z_task_free(_z_task_t **task) {
}

/*------------------ Mutex ------------------*/
int8_t _z_mutex_init(_z_mutex_t *m) { return (int8_t)pthread_mutex_init(m, 0); }
int8_t _z_mutex_init(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_init(m, 0)); }

int8_t _z_mutex_drop(_z_mutex_t *m) { return (int8_t)pthread_mutex_destroy(m); }
int8_t _z_mutex_drop(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_destroy(m)); }

int8_t _z_mutex_lock(_z_mutex_t *m) { return (int8_t)pthread_mutex_lock(m); }
int8_t _z_mutex_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_lock(m)); }

int8_t _z_mutex_try_lock(_z_mutex_t *m) { return (int8_t)pthread_mutex_trylock(m); }
int8_t _z_mutex_try_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_trylock(m)); }

int8_t _z_mutex_unlock(_z_mutex_t *m) { return (int8_t)pthread_mutex_unlock(m); }
int8_t _z_mutex_unlock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_unlock(m)); }

/*------------------ Condvar ------------------*/
int8_t _z_condvar_init(_z_condvar_t *cv) { return (int8_t)pthread_cond_init(cv, 0); }
int8_t _z_condvar_init(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_init(cv, 0)); }

int8_t _z_condvar_drop(_z_condvar_t *cv) { return (int8_t)pthread_cond_destroy(cv); }
int8_t _z_condvar_drop(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_destroy(cv)); }

int8_t _z_condvar_signal(_z_condvar_t *cv) { return (int8_t)pthread_cond_signal(cv); }
int8_t _z_condvar_signal(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_signal(cv)); }

int8_t _z_condvar_signal_all(_z_condvar_t *cv) { return (int8_t)pthread_cond_broadcast(cv); }
int8_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_broadcast(cv)); }

int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return (int8_t)pthread_cond_wait(cv, m); }
int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_cond_wait(cv, m)); }
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
Expand Down
26 changes: 13 additions & 13 deletions src/system/zephyr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ int8_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *),
lattr = &tmp;
}

return pthread_create(task, lattr, fun, arg);
_Z_CHECK_SYS_ERR(pthread_create(task, lattr, fun, arg));
}

int8_t _z_task_join(_z_task_t *task) { return pthread_join(*task, NULL); }
int8_t _z_task_join(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_join(*task, NULL)); }

int8_t _z_task_cancel(_z_task_t *task) { return pthread_cancel(*task); }
int8_t _z_task_cancel(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_cancel(*task)); }

void _z_task_free(_z_task_t **task) {
_z_task_t *ptr = *task;
Expand All @@ -94,31 +94,31 @@ void _z_task_free(_z_task_t **task) {
}

/*------------------ Mutex ------------------*/
int8_t _z_mutex_init(_z_mutex_t *m) { return pthread_mutex_init(m, 0); }
int8_t _z_mutex_init(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_init(m, 0)); }

int8_t _z_mutex_drop(_z_mutex_t *m) {
if (m == NULL) {
return 0;
}
return pthread_mutex_destroy(m);
_Z_CHECK_SYS_ERR(pthread_mutex_destroy(m));
}

int8_t _z_mutex_lock(_z_mutex_t *m) { return pthread_mutex_lock(m); }
int8_t _z_mutex_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_lock(m)); }

int8_t _z_mutex_try_lock(_z_mutex_t *m) { return pthread_mutex_trylock(m); }
int8_t _z_mutex_try_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_trylock(m)); }

int8_t _z_mutex_unlock(_z_mutex_t *m) { return pthread_mutex_unlock(m); }
int8_t _z_mutex_unlock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_unlock(m)); }

/*------------------ Condvar ------------------*/
int8_t _z_condvar_init(_z_condvar_t *cv) { return pthread_cond_init(cv, 0); }
int8_t _z_condvar_init(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_init(cv, 0)); }

int8_t _z_condvar_drop(_z_condvar_t *cv) { return pthread_cond_destroy(cv); }
int8_t _z_condvar_drop(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_destroy(cv)); }

int8_t _z_condvar_signal(_z_condvar_t *cv) { return pthread_cond_signal(cv); }
int8_t _z_condvar_signal(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_signal(cv)); }

int8_t _z_condvar_signal_all(_z_condvar_t *cv) { return pthread_cond_broadcast(cv); }
int8_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_broadcast(cv)); }

int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wait(cv, m); }
int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_cond_wait(cv, m)); }
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
Expand Down

0 comments on commit 37fd830

Please sign in to comment.