From e3f5d49b2527317d0f9277ba84ac6735f2accf73 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 2 Jul 2024 12:48:36 +0200 Subject: [PATCH] Add owned/loaned for task/mutex/condvar (#484) --- CMakeLists.txt | 1 + examples/unix/c11/z_get.c | 14 ++-- examples/unix/c11/z_get_attachment.c | 14 ++-- examples/unix/c11/z_ping.c | 16 ++--- examples/unix/c99/z_get.c | 14 ++-- examples/unix/c99/z_ping.c | 16 ++--- examples/windows/z_get.c | 14 ++-- examples/windows/z_ping.c | 16 ++--- include/zenoh-pico/api/macros.h | 25 ++++++- include/zenoh-pico/api/olv_macros.h | 56 ++++++++++------ include/zenoh-pico/api/primitives.h | 38 +++++------ include/zenoh-pico/collections/fifo_mt.h | 6 +- include/zenoh-pico/collections/ring_mt.h | 4 +- include/zenoh-pico/net/session.h | 2 +- include/zenoh-pico/session/session.h | 4 +- include/zenoh-pico/system/platform-common.h | 66 ++++++++++++++----- .../system/platform/arduino/esp32.h | 6 +- .../system/platform/arduino/opencr.h | 6 +- .../zenoh-pico/system/platform/emscripten.h | 6 +- include/zenoh-pico/system/platform/espidf.h | 6 +- include/zenoh-pico/system/platform/flipper.h | 6 +- .../system/platform/freertos_plus_tcp.h | 6 +- include/zenoh-pico/system/platform/mbed.h | 6 +- include/zenoh-pico/system/platform/unix.h | 6 +- include/zenoh-pico/system/platform/void.h | 6 +- include/zenoh-pico/system/platform/windows.h | 6 +- include/zenoh-pico/system/platform/zephyr.h | 6 +- .../zenoh-pico/transport/multicast/lease.h | 2 +- include/zenoh-pico/transport/multicast/read.h | 2 +- include/zenoh-pico/transport/raweth/read.h | 2 +- include/zenoh-pico/transport/transport.h | 18 ++--- include/zenoh-pico/transport/unicast/lease.h | 2 +- include/zenoh-pico/transport/unicast/read.h | 2 +- src/collections/fifo_mt.c | 34 +++++----- src/collections/ring_mt.c | 24 +++---- src/net/session.c | 4 +- src/session/utils.c | 8 +-- src/system/arduino/esp32/system.c | 30 ++++----- src/system/arduino/opencr/system.c | 28 ++++---- src/system/emscripten/system.c | 28 ++++---- src/system/espidf/system.c | 26 ++++---- src/system/flipper/system.c | 26 ++++---- src/system/freertos_plus_tcp/system.c | 26 ++++---- src/system/mbed/system.cpp | 28 ++++---- src/system/platform-common.c | 53 +++++++++++++++ src/system/unix/system.c | 28 ++++---- src/system/windows/system.c | 28 ++++---- src/system/zephyr/system.c | 28 ++++---- src/transport/multicast/lease.c | 14 ++-- src/transport/multicast/read.c | 10 +-- src/transport/multicast/rx.c | 8 +-- src/transport/multicast/transport.c | 32 ++++----- src/transport/multicast/tx.c | 10 +-- src/transport/raweth/read.c | 6 +- src/transport/raweth/rx.c | 4 +- src/transport/raweth/tx.c | 12 ++-- src/transport/unicast/lease.c | 6 +- src/transport/unicast/read.c | 10 +-- src/transport/unicast/rx.c | 4 +- src/transport/unicast/transport.c | 26 ++++---- src/transport/unicast/tx.c | 14 ++-- 61 files changed, 540 insertions(+), 415 deletions(-) create mode 100644 src/system/platform-common.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d35cb139..d23d59480 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -255,6 +255,7 @@ file(GLOB_RECURSE Sources "src/session/*.c" "src/transport/*.c" "src/utils/*.c" + "src/system/platform-common.c" ) if(WITH_ZEPHYR) diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index b996ab3d8..220d4bb1d 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -19,14 +19,14 @@ #include #if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 -static z_condvar_t cond; -static z_mutex_t mutex; +static z_owned_condvar_t cond; +static z_owned_mutex_t mutex; void reply_dropper(void *ctx) { (void)(ctx); printf(">> Received query final notification\n"); - z_condvar_signal(&cond); - z_condvar_free(&cond); + z_condvar_signal(z_loan_mut(cond)); + z_drop(z_move(cond)); } void reply_handler(const z_loaned_reply_t *reply, void *ctx) { @@ -116,7 +116,7 @@ int main(int argc, char **argv) { return -1; } - z_mutex_lock(&mutex); + z_mutex_lock(z_loan_mut(mutex)); printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts; z_get_options_default(&opts); @@ -133,8 +133,8 @@ int main(int argc, char **argv) { printf("Unable to send query.\n"); return -1; } - z_condvar_wait(&cond, &mutex); - z_mutex_unlock(&mutex); + z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); + z_mutex_unlock(z_loan_mut(mutex)); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan_mut(s)); diff --git a/examples/unix/c11/z_get_attachment.c b/examples/unix/c11/z_get_attachment.c index e736aa45f..4fb379950 100644 --- a/examples/unix/c11/z_get_attachment.c +++ b/examples/unix/c11/z_get_attachment.c @@ -43,8 +43,8 @@ typedef struct kv_pairs_rx_t { #define KVP_LEN 16 #if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 -static z_condvar_t cond; -static z_mutex_t mutex; +static z_owned_condvar_t cond; +static z_owned_mutex_t mutex; _Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) { kv_pairs_tx_t *kvs = (kv_pairs_tx_t *)(context); @@ -93,8 +93,8 @@ void drop_attachment(kv_pairs_rx_t *kvp) { void reply_dropper(void *ctx) { (void)(ctx); printf(">> Received query final notification\n"); - z_condvar_signal(&cond); - z_condvar_free(&cond); + z_condvar_signal(z_loan_mut(cond)); + z_drop(z_move(cond)); } void reply_handler(const z_loaned_reply_t *reply, void *ctx) { @@ -194,7 +194,7 @@ int main(int argc, char **argv) { return -1; } - z_mutex_lock(&mutex); + z_mutex_lock(z_loan_mut(mutex)); printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts; z_get_options_default(&opts); @@ -219,8 +219,8 @@ int main(int argc, char **argv) { printf("Unable to send query.\n"); return -1; } - z_condvar_wait(&cond, &mutex); - z_mutex_unlock(&mutex); + z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); + z_mutex_unlock(z_loan_mut(mutex)); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan_mut(s)); diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index aae8608c8..e399a88b2 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -27,17 +27,17 @@ #define DEFAULT_PING_NB 100 #define DEFAULT_WARMUP_MS 1000 -static z_condvar_t cond; -static z_mutex_t mutex; +static z_owned_condvar_t cond; +static z_owned_mutex_t mutex; void callback(const z_loaned_sample_t* sample, void* context) { (void)sample; (void)context; - z_condvar_signal(&cond); + z_condvar_signal(z_loan_mut(cond)); } void drop(void* context) { (void)context; - z_condvar_free(&cond); + z_drop(z_move(cond)); } struct args_t { @@ -100,7 +100,7 @@ int main(int argc, char** argv) { for (unsigned int i = 0; i < args.size; i++) { data[i] = (uint8_t)(i % 10); } - z_mutex_lock(&mutex); + z_mutex_lock(z_loan_mut(mutex)); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); z_clock_t warmup_start = z_clock_now(); @@ -111,7 +111,7 @@ int main(int argc, char** argv) { z_bytes_serialize_from_slice(&payload, data, args.size); z_publisher_put(z_loan(pub), z_move(payload), NULL); - z_condvar_wait(&cond, &mutex); + z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); elapsed_us = z_clock_elapsed_us(&warmup_start); } } @@ -123,13 +123,13 @@ int main(int argc, char** argv) { z_bytes_serialize_from_slice(&payload, data, args.size); z_publisher_put(z_loan(pub), z_move(payload), NULL); - z_condvar_wait(&cond, &mutex); + z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); results[i] = z_clock_elapsed_us(&measure_start); } for (unsigned int i = 0; i < args.number_of_pings; i++) { printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2); } - z_mutex_unlock(&mutex); + z_mutex_unlock(z_loan_mut(mutex)); z_free(results); z_free(data); z_drop(z_move(pub)); diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index ec816fe2e..819742367 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -19,14 +19,14 @@ #include #if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 -z_condvar_t cond; -z_mutex_t mutex; +z_owned_condvar_t cond; +z_owned_mutex_t mutex; void reply_dropper(void *ctx) { (void)(ctx); printf(">> Received query final notification\n"); - z_condvar_signal(&cond); - z_condvar_free(&cond); + z_condvar_signal(z_condvar_loan_mut(&cond)); + z_condvar_drop(z_condvar_move(&cond)); } void reply_handler(const z_loaned_reply_t *reply, void *ctx) { @@ -117,7 +117,7 @@ int main(int argc, char **argv) { return -1; } - z_mutex_lock(&mutex); + z_mutex_lock(z_mutex_loan_mut(&mutex)); printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts; z_get_options_default(&opts); @@ -133,8 +133,8 @@ int main(int argc, char **argv) { printf("Unable to send query.\n"); return -1; } - z_condvar_wait(&cond, &mutex); - z_mutex_unlock(&mutex); + z_condvar_wait(z_condvar_loan_mut(&cond), z_mutex_loan_mut(&mutex)); + z_mutex_unlock(z_mutex_loan_mut(&mutex)); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_session_loan_mut(&s)); diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 26a90bfb7..46f956181 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -28,17 +28,17 @@ #define DEFAULT_PING_NB 100 #define DEFAULT_WARMUP_MS 1000 -static z_condvar_t cond; -static z_mutex_t mutex; +static z_owned_condvar_t cond; +static z_owned_mutex_t mutex; void callback(const z_loaned_sample_t* sample, void* context) { (void)sample; (void)context; - z_condvar_signal(&cond); + z_condvar_signal(z_condvar_loan_mut(&cond)); } void drop(void* context) { (void)context; - z_condvar_free(&cond); + z_condvar_drop(z_condvar_move(&cond)); } struct args_t { @@ -103,7 +103,7 @@ int main(int argc, char** argv) { for (unsigned int i = 0; i < args.size; i++) { data[i] = (uint8_t)(i % 10); } - z_mutex_lock(&mutex); + z_mutex_lock(z_mutex_loan_mut(&mutex)); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); z_clock_t warmup_start = z_clock_now(); @@ -114,7 +114,7 @@ int main(int argc, char** argv) { z_bytes_serialize_from_slice(&payload, data, args.size); z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); - z_condvar_wait(&cond, &mutex); + z_condvar_wait(z_condvar_loan_mut(&cond), z_mutex_loan_mut(&mutex)); elapsed_us = z_clock_elapsed_us(&warmup_start); } } @@ -127,13 +127,13 @@ int main(int argc, char** argv) { z_bytes_serialize_from_slice(&payload, data, args.size); z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); - z_condvar_wait(&cond, &mutex); + z_condvar_wait(z_condvar_loan_mut(&cond), z_mutex_loan_mut(&mutex)); results[i] = z_clock_elapsed_us(&measure_start); } for (unsigned int i = 0; i < args.number_of_pings; i++) { printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2); } - z_mutex_unlock(&mutex); + z_mutex_unlock(z_mutex_loan_mut(&mutex)); z_free(results); z_free(data); z_undeclare_subscriber(z_subscriber_move(&sub)); diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 651e244e4..d14cedb80 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -18,14 +18,14 @@ #include #if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 -z_condvar_t cond; -z_mutex_t mutex; +z_owned_condvar_t cond; +z_owned_mutex_t mutex; void reply_dropper(void *ctx) { (void)(ctx); printf(">> Received query final notification\n"); - z_condvar_signal(&cond); - z_condvar_free(&cond); + z_condvar_signal(z_loan_mut(cond)); + z_drop(z_move(cond)); } void reply_handler(const z_loaned_reply_t *reply, void *ctx) { @@ -81,7 +81,7 @@ int main(int argc, char **argv) { return -1; } - z_mutex_lock(&mutex); + z_mutex_lock(z_loan_mut(mutex)); printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts; z_get_options_default(&opts); @@ -97,8 +97,8 @@ int main(int argc, char **argv) { printf("Unable to send query.\n"); return -1; } - z_condvar_wait(&cond, &mutex); - z_mutex_unlock(&mutex); + z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); + z_mutex_unlock(z_loan_mut(mutex)); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan_mut(s)); diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index 5f2a3309a..725cfde17 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -27,17 +27,17 @@ #define DEFAULT_PING_NB 100 #define DEFAULT_WARMUP_MS 1000 -static z_condvar_t cond; -static z_mutex_t mutex; +static z_owned_condvar_t cond; +static z_owned_mutex_t mutex; void callback(const z_loaned_sample_t* sample, void* context) { (void)sample; (void)context; - z_condvar_signal(&cond); + z_condvar_signal(z_loan_mut(cond)); } void drop(void* context) { (void)context; - z_condvar_free(&cond); + z_drop(z_move(cond)); } struct args_t { @@ -99,7 +99,7 @@ int main(int argc, char** argv) { for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; } - z_mutex_lock(&mutex); + z_mutex_lock(z_loan_mut(mutex)); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); z_clock_t warmup_start = z_clock_now(); @@ -110,7 +110,7 @@ int main(int argc, char** argv) { z_bytes_serialize_from_slice(&payload, data, args.size); z_publisher_put(z_loan(pub), z_move(payload), NULL); - z_condvar_wait(&cond, &mutex); + z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); elapsed_us = z_clock_elapsed_us(&warmup_start); } } @@ -123,13 +123,13 @@ int main(int argc, char** argv) { z_bytes_serialize_from_slice(&payload, data, args.size); z_publisher_put(z_loan(pub), z_move(payload), NULL); - z_condvar_wait(&cond, &mutex); + z_condvar_wait(z_loan_mut(cond), z_loan_mut(mutex)); results[i] = z_clock_elapsed_us(&measure_start); } for (unsigned int i = 0; i < args.number_of_pings; i++) { printf("%d bytes: seq=%d rtt=%luus, lat=%luus\n", args.size, i, results[i], results[i] / 2); } - z_mutex_unlock(&mutex); + z_mutex_unlock(z_loan_mut(mutex)); z_free(results); z_free(data); z_drop(z_move(pub)); diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 52e090420..2540bbd4c 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -52,6 +52,9 @@ z_owned_slice_t : z_slice_loan, \ z_owned_bytes_t : z_bytes_loan, \ z_owned_encoding_t : z_encoding_loan, \ + z_owned_task_t : z_task_loan, \ + z_owned_mutex_t : z_mutex_loan, \ + z_owned_condvar_t : z_condvar_loan, \ z_owned_fifo_handler_query_t : z_fifo_handler_query_loan, \ z_owned_fifo_handler_reply_t : z_fifo_handler_reply_loan, \ z_owned_fifo_handler_sample_t : z_fifo_handler_sample_loan, \ @@ -76,6 +79,9 @@ z_owned_query_t : z_query_loan_mut, \ z_owned_slice_t : z_slice_loan_mut, \ z_owned_bytes_t : z_bytes_loan_mut, \ + z_owned_task_t : z_task_loan_mut, \ + z_owned_mutex_t : z_mutex_loan_mut, \ + z_owned_condvar_t : z_condvar_loan_mut, \ z_owned_reply_err_t : z_reply_err_loan_mut \ )(&x) /** @@ -105,13 +111,16 @@ z_owned_closure_reply_t * : z_closure_reply_drop, \ z_owned_closure_hello_t * : z_closure_hello_drop, \ z_owned_closure_zid_t * : z_closure_zid_drop, \ + z_owned_task_t *: z_task_drop, \ + z_owned_mutex_t *: z_mutex_drop, \ + z_owned_condvar_t *: z_condvar_drop, \ z_owned_fifo_handler_query_t * : z_fifo_handler_query_drop, \ z_owned_fifo_handler_reply_t * : z_fifo_handler_reply_drop, \ z_owned_fifo_handler_sample_t * : z_fifo_handler_sample_drop, \ z_owned_ring_handler_query_t * : z_ring_handler_query_drop, \ z_owned_ring_handler_reply_t * : z_ring_handler_reply_drop, \ z_owned_ring_handler_sample_t * : z_ring_handler_sample_drop, \ - z_owned_reply_err_t : z_reply_err_drop \ + z_owned_reply_err_t * : z_reply_err_drop \ )(x) /** @@ -208,6 +217,9 @@ z_owned_slice_t : z_slice_move, \ z_owned_bytes_t : z_bytes_move, \ z_owned_encoding_t : z_encoding_move, \ + z_owned_task_t : z_task_move, \ + z_owned_mutex_t : z_mutex_move, \ + z_owned_condvar_t : z_condvar_move, \ z_owned_ring_handler_query_t : z_ring_handler_query_move, \ z_owned_ring_handler_reply_t : z_ring_handler_reply_move, \ z_owned_ring_handler_sample_t : z_ring_handler_sample_move, \ @@ -318,6 +330,9 @@ inline const z_loaned_query_t* z_loan(const z_owned_query_t& x) { return z_query inline const z_loaned_slice_t* z_loan(const z_owned_slice_t& x) { return z_slice_loan(&x); } inline const z_loaned_bytes_t* z_loan(const z_owned_bytes_t& x) { return z_bytes_loan(&x); } inline const z_loaned_encoding_t* z_loan(const z_owned_encoding_t& x) { return z_encoding_loan(&x); } +inline const z_loaned_task_t* z_loan(const z_owned_task_t& x) { return z_task_loan(&x); } +inline const z_loaned_mutex_t* z_loan(const z_owned_mutex_t& x) { return z_mutex_loan(&x); } +inline const z_loaned_condvar_t* z_loan(const z_owned_condvar_t& x) { return z_condvar_loan(&x); } inline const z_loaned_reply_err_t* z_loan(const z_owned_reply_err_t& x) { return z_reply_err_loan(&x); } // z_loan_mut definition @@ -336,7 +351,10 @@ inline z_loaned_sample_t* z_loan_mut(z_owned_sample_t& x) { return z_sample_loan inline z_loaned_query_t* z_loan_mut(z_owned_query_t& x) { return z_query_loan_mut(&x); } inline z_loaned_slice_t* z_loan_mut(z_owned_slice_t& x) { return z_slice_loan_mut(&x); } inline z_loaned_bytes_t* z_loan_mut(z_owned_bytes_t& x) { return z_bytes_loan_mut(&x); } -inline z_loaned_encoding_t* z_loan(z_owned_encoding_t& x) { return z_encoding_loan_mut(&x); } +inline z_loaned_encoding_t* z_loan_mut(z_owned_encoding_t& x) { return z_encoding_loan_mut(&x); } +inline z_loaned_task_t* z_loan_mut(z_owned_task_t& x) { return z_task_loan_mut(&x); } +inline z_loaned_mutex_t* z_loan_mut(z_owned_mutex_t& x) { return z_mutex_loan_mut(&x); } +inline z_loaned_condvar_t* z_loan_mut(z_owned_condvar_t& x) { return z_condvar_loan_mut(&x); } inline z_loaned_reply_err_t* z_loan_mut(z_owned_reply_err_t& x) { return z_reply_err_loan_mut(&x); } // z_drop definition @@ -355,6 +373,9 @@ inline void z_drop(z_owned_sample_t* v) { z_sample_drop(v); } inline void z_drop(z_owned_query_t* v) { z_query_drop(v); } inline void z_drop(z_owned_bytes_t* v) { z_bytes_drop(v); } inline void z_drop(z_owned_encoding_t* v) { z_encoding_drop(v); } +inline void z_drop(z_owned_task_t* v) { z_task_drop(v); } +inline void z_drop(z_owned_mutex_t* v) { z_mutex_drop(v); } +inline void z_drop(z_owned_condvar_t* v) { z_condvar_drop(v); } inline void z_drop(z_owned_reply_err_t* v) { z_reply_err_drop(v); } inline void z_drop(z_owned_closure_sample_t* v) { z_closure_sample_drop(v); } inline void z_drop(z_owned_closure_query_t* v) { z_closure_query_drop(v); } diff --git a/include/zenoh-pico/api/olv_macros.h b/include/zenoh-pico/api/olv_macros.h index 6aea6999a..b57fc68b8 100644 --- a/include/zenoh-pico/api/olv_macros.h +++ b/include/zenoh-pico/api/olv_macros.h @@ -49,27 +49,34 @@ type _val; \ } z_view_##name##_t; -#define _Z_OWNED_FUNCTIONS_DEF(loanedtype, ownedtype, name) \ - _Bool z_##name##_check(const ownedtype *obj); \ - const loanedtype *z_##name##_loan(const ownedtype *obj); \ - loanedtype *z_##name##_loan_mut(ownedtype *obj); \ - ownedtype *z_##name##_move(ownedtype *obj); \ - int8_t z_##name##_clone(ownedtype *obj, const loanedtype *src); \ - void z_##name##_drop(ownedtype *obj); \ - void z_##name##_null(ownedtype *obj); - -#define _Z_OWNED_FUNCTIONS_NO_COPY_DEF(loanedtype, ownedtype, name) \ - _Bool z_##name##_check(const ownedtype *obj); \ - const loanedtype *z_##name##_loan(const ownedtype *obj); \ - loanedtype *z_##name##_loan_mut(ownedtype *obj); \ - ownedtype *z_##name##_move(ownedtype *obj); \ - void z_##name##_drop(ownedtype *obj); \ - void z_##name##_null(ownedtype *obj); - -#define _Z_VIEW_FUNCTIONS_DEF(loanedtype, viewtype, name) \ - const loanedtype *z_view_##name##_loan(const viewtype *name); \ - loanedtype *z_view_##name##_loan_mut(viewtype *name); \ - void z_view_##name##_null(viewtype *name); +#define _Z_OWNED_FUNCTIONS_DEF(name) \ + _Bool z_##name##_check(const z_owned_##name##_t *obj); \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj); \ + z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj); \ + z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj); \ + int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src); \ + void z_##name##_drop(z_owned_##name##_t *obj); \ + void z_##name##_null(z_owned_##name##_t *obj); + +#define _Z_OWNED_FUNCTIONS_NO_COPY_DEF(name) \ + _Bool z_##name##_check(const z_owned_##name##_t *obj); \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj); \ + z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj); \ + z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj); \ + void z_##name##_drop(z_owned_##name##_t *obj); \ + void z_##name##_null(z_owned_##name##_t *obj); + +#define _Z_OWNED_FUNCTIONS_SYSTEM_DEF(name) \ + _Bool z_##name##_check(const z_owned_##name##_t *obj); \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj); \ + z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj); \ + z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj); \ + void z_##name##_null(z_owned_##name##_t *obj); + +#define _Z_VIEW_FUNCTIONS_DEF(name) \ + const z_loaned_##name##_t *z_view_##name##_loan(const z_view_##name##_t *name); \ + z_loaned_##name##_t *z_view_##name##_loan_mut(z_view_##name##_t *name); \ + void z_view_##name##_null(z_view_##name##_t *name); #define _Z_OWNED_FUNCTIONS_PTR_IMPL(type, name, f_copy, f_free) \ _Bool z_##name##_check(const z_owned_##name##_t *obj) { return obj->_val != NULL; } \ @@ -138,6 +145,13 @@ } \ } +#define _Z_OWNED_FUNCTIONS_SYSTEM_IMPL(type, name) \ + _Bool z_##name##_check(const z_owned_##name##_t *obj) { return obj != NULL; } \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj) { return &obj->_val; } \ + z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj) { return &obj->_val; } \ + void z_##name##_null(z_owned_##name##_t *obj) { (void)obj; } \ + z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj) { return obj; } + #define _Z_VIEW_FUNCTIONS_IMPL(type, name) \ const z_loaned_##name##_t *z_view_##name##_loan(const z_view_##name##_t *obj) { return &obj->_val; } \ z_loaned_##name##_t *z_view_##name##_loan_mut(z_view_##name##_t *obj) { return &obj->_val; } diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 3e977f402..246068df5 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1033,23 +1033,23 @@ int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_loaned_hello_handler_ int8_t z_closure_zid(z_owned_closure_zid_t *closure, z_id_handler_t call, z_dropper_handler_t drop, void *context); /**************** Loans ****************/ -_Z_OWNED_FUNCTIONS_DEF(z_loaned_string_t, z_owned_string_t, string) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_keyexpr_t, z_owned_keyexpr_t, keyexpr) -_Z_OWNED_FUNCTIONS_NO_COPY_DEF(z_loaned_config_t, z_owned_config_t, config) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_session_t, z_owned_session_t, session) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_subscriber_t, z_owned_subscriber_t, subscriber) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_publisher_t, z_owned_publisher_t, publisher) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_queryable_t, z_owned_queryable_t, queryable) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_hello_t, z_owned_hello_t, hello) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_reply_t, z_owned_reply_t, reply) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_string_array_t, z_owned_string_array_t, string_array) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_sample_t, z_owned_sample_t, sample) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_query_t, z_owned_query_t, query) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_slice_t, z_owned_slice_t, slice) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_bytes_t, z_owned_bytes_t, bytes) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_bytes_writer_t, z_owned_bytes_writer_t, bytes_writer) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_reply_err_t, z_owned_reply_err_t, reply_err) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_encoding_t, z_owned_encoding_t, encoding) +_Z_OWNED_FUNCTIONS_DEF(string) +_Z_OWNED_FUNCTIONS_DEF(keyexpr) +_Z_OWNED_FUNCTIONS_NO_COPY_DEF(config) +_Z_OWNED_FUNCTIONS_DEF(session) +_Z_OWNED_FUNCTIONS_DEF(subscriber) +_Z_OWNED_FUNCTIONS_DEF(publisher) +_Z_OWNED_FUNCTIONS_DEF(queryable) +_Z_OWNED_FUNCTIONS_DEF(hello) +_Z_OWNED_FUNCTIONS_DEF(reply) +_Z_OWNED_FUNCTIONS_DEF(string_array) +_Z_OWNED_FUNCTIONS_DEF(sample) +_Z_OWNED_FUNCTIONS_DEF(query) +_Z_OWNED_FUNCTIONS_DEF(slice) +_Z_OWNED_FUNCTIONS_DEF(bytes) +_Z_OWNED_FUNCTIONS_DEF(bytes_writer) +_Z_OWNED_FUNCTIONS_DEF(reply_err) +_Z_OWNED_FUNCTIONS_DEF(encoding) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_sample_t, closure_sample) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_query_t, closure_query) @@ -1057,8 +1057,8 @@ _Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_reply_t, closure_reply) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_hello_t, closure_hello) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_zid_t, closure_zid) -_Z_VIEW_FUNCTIONS_DEF(z_loaned_keyexpr_t, z_view_keyexpr_t, keyexpr) -_Z_VIEW_FUNCTIONS_DEF(z_loaned_string_t, z_view_string_t, string) +_Z_VIEW_FUNCTIONS_DEF(keyexpr) +_Z_VIEW_FUNCTIONS_DEF(string) /** * Loans a :c:type:`z_owned_sample_t`. diff --git a/include/zenoh-pico/collections/fifo_mt.h b/include/zenoh-pico/collections/fifo_mt.h index 14d2848db..dded358ee 100644 --- a/include/zenoh-pico/collections/fifo_mt.h +++ b/include/zenoh-pico/collections/fifo_mt.h @@ -24,9 +24,9 @@ typedef struct { _z_fifo_t _fifo; #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_t _mutex; - z_condvar_t _cv_not_full; - z_condvar_t _cv_not_empty; + _z_mutex_t _mutex; + _z_condvar_t _cv_not_full; + _z_condvar_t _cv_not_empty; #endif } _z_fifo_mt_t; diff --git a/include/zenoh-pico/collections/ring_mt.h b/include/zenoh-pico/collections/ring_mt.h index a52ee62d6..3fc0174b2 100644 --- a/include/zenoh-pico/collections/ring_mt.h +++ b/include/zenoh-pico/collections/ring_mt.h @@ -24,8 +24,8 @@ typedef struct { _z_ring_t _ring; #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_t _mutex; - z_condvar_t _cv_not_empty; + _z_mutex_t _mutex; + _z_condvar_t _cv_not_empty; #endif } _z_ring_mt_t; diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index d4190c9e7..7be2fb6a6 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -29,7 +29,7 @@ */ typedef struct _z_session_t { #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_t _mutex_inner; + _z_mutex_t _mutex_inner; #endif // Z_FEATURE_MULTI_THREAD == 1 // Zenoh-pico is considering a single transport per session. diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index fbfdb5a62..a4cfe3c44 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -138,8 +138,8 @@ _Z_LIST_DEFINE(_z_pending_query, _z_pending_query_t) typedef struct { #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_t _mutex; - z_condvar_t _cond_var; + _z_mutex_t _mutex; + _z_condvar_t _cond_var; #endif // Z_FEATURE_MULTI_THREAD == 1 _z_reply_data_list_t *_replies; } _z_pending_query_collect_t; diff --git a/include/zenoh-pico/system/platform-common.h b/include/zenoh-pico/system/platform-common.h index 081a5a9c4..18b3c33a8 100644 --- a/include/zenoh-pico/system/platform-common.h +++ b/include/zenoh-pico/system/platform-common.h @@ -17,6 +17,7 @@ #include +#include "zenoh-pico/api/olv_macros.h" #include "zenoh-pico/config.h" #if defined(ZENOH_LINUX) || defined(ZENOH_MACOS) || defined(ZENOH_BSD) @@ -60,28 +61,63 @@ void *z_malloc(size_t size); void *z_realloc(void *ptr, size_t size); void z_free(void *ptr); -#if Z_FEATURE_MULTI_THREAD == 1 +#if Z_FEATURE_MULTI_THREAD == 0 +// dummy types for correct macros work +typedef void *_z_task_t; +typedef void *_z_mutex_t; +typedef void *_z_condvar_t; +typedef void *z_task_attr_t; +#endif + /*------------------ Thread ------------------*/ -int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg); -int8_t z_task_join(z_task_t *task); -int8_t zp_task_cancel(z_task_t *task); -void z_task_free(z_task_t **task); +int8_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg); +int8_t _z_task_join(_z_task_t *task); +int8_t _z_task_cancel(_z_task_t *task); +void _z_task_drop(_z_task_t **task); + +_Z_OWNED_TYPE_VALUE(_z_task_t, task) +_Z_LOANED_TYPE(_z_task_t, task) +_Z_OWNED_FUNCTIONS_SYSTEM_DEF(task) + +int8_t z_task_init(z_owned_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg); +int8_t z_task_join(z_loaned_task_t *task); +void z_task_drop(z_owned_task_t *task); /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m); -int8_t z_mutex_free(z_mutex_t *m); +int8_t _z_mutex_init(_z_mutex_t *m); +int8_t _z_mutex_drop(_z_mutex_t *m); + +int8_t _z_mutex_lock(_z_mutex_t *m); +int8_t _z_mutex_try_lock(_z_mutex_t *m); +int8_t _z_mutex_unlock(_z_mutex_t *m); -int8_t z_mutex_lock(z_mutex_t *m); -int8_t z_mutex_trylock(z_mutex_t *m); -int8_t z_mutex_unlock(z_mutex_t *m); +_Z_OWNED_TYPE_VALUE(_z_mutex_t, mutex) +_Z_LOANED_TYPE(_z_mutex_t, mutex) +_Z_OWNED_FUNCTIONS_SYSTEM_DEF(mutex) + +int8_t z_mutex_init(z_owned_mutex_t *m); +int8_t z_mutex_drop(z_owned_mutex_t *m); + +int8_t z_mutex_lock(z_loaned_mutex_t *m); +int8_t z_mutex_try_lock(z_loaned_mutex_t *m); +int8_t z_mutex_unlock(z_loaned_mutex_t *m); /*------------------ CondVar ------------------*/ -int8_t z_condvar_init(z_condvar_t *cv); -int8_t z_condvar_free(z_condvar_t *cv); +int8_t _z_condvar_init(_z_condvar_t *cv); +int8_t _z_condvar_drop(_z_condvar_t *cv); + +int8_t _z_condvar_signal(_z_condvar_t *cv); +int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m); + +_Z_OWNED_TYPE_VALUE(_z_condvar_t, condvar) +_Z_LOANED_TYPE(_z_condvar_t, condvar) +_Z_OWNED_FUNCTIONS_SYSTEM_DEF(condvar) + +int8_t z_condvar_init(z_owned_condvar_t *cv); +int8_t z_condvar_drop(z_owned_condvar_t *cv); -int8_t z_condvar_signal(z_condvar_t *cv); -int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m); -#endif // Z_FEATURE_MULTI_THREAD == 1 +int8_t z_condvar_signal(z_loaned_condvar_t *cv); +int8_t z_condvar_wait(z_loaned_condvar_t *cv, z_loaned_mutex_t *m); /*------------------ Sleep ------------------*/ int z_sleep_us(size_t time); diff --git a/include/zenoh-pico/system/platform/arduino/esp32.h b/include/zenoh-pico/system/platform/arduino/esp32.h index abdc37f67..9f8c34da9 100644 --- a/include/zenoh-pico/system/platform/arduino/esp32.h +++ b/include/zenoh-pico/system/platform/arduino/esp32.h @@ -24,10 +24,10 @@ #endif // Z_FEATURE_MULTI_THREAD == 1 #if Z_FEATURE_MULTI_THREAD == 1 -typedef TaskHandle_t z_task_t; +typedef TaskHandle_t _z_task_t; typedef void *z_task_attr_t; // Not used in ESP32 -typedef pthread_mutex_t z_mutex_t; -typedef pthread_cond_t z_condvar_t; +typedef pthread_mutex_t _z_mutex_t; +typedef pthread_cond_t _z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/system/platform/arduino/opencr.h b/include/zenoh-pico/system/platform/arduino/opencr.h index af16ce60b..44b0f4a8f 100644 --- a/include/zenoh-pico/system/platform/arduino/opencr.h +++ b/include/zenoh-pico/system/platform/arduino/opencr.h @@ -21,10 +21,10 @@ #include "zenoh-pico/config.h" #if Z_FEATURE_MULTI_THREAD == 1 -typedef void *z_task_t; +typedef void *_z_task_t; typedef void *z_task_attr_t; -typedef void *z_mutex_t; -typedef void *z_condvar_t; +typedef void *_z_mutex_t; +typedef void *_z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/system/platform/emscripten.h b/include/zenoh-pico/system/platform/emscripten.h index b89921197..edb9c6b44 100644 --- a/include/zenoh-pico/system/platform/emscripten.h +++ b/include/zenoh-pico/system/platform/emscripten.h @@ -22,10 +22,10 @@ #if Z_FEATURE_MULTI_THREAD == 1 #include -typedef pthread_t z_task_t; +typedef pthread_t _z_task_t; typedef pthread_attr_t z_task_attr_t; -typedef pthread_mutex_t z_mutex_t; -typedef pthread_cond_t z_condvar_t; +typedef pthread_mutex_t _z_mutex_t; +typedef pthread_cond_t _z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 typedef double z_clock_t; diff --git a/include/zenoh-pico/system/platform/espidf.h b/include/zenoh-pico/system/platform/espidf.h index b7353b561..aa09362b4 100644 --- a/include/zenoh-pico/system/platform/espidf.h +++ b/include/zenoh-pico/system/platform/espidf.h @@ -38,9 +38,9 @@ typedef struct { typedef struct { TaskHandle_t handle; EventGroupHandle_t join_event; -} z_task_t; -typedef pthread_mutex_t z_mutex_t; -typedef pthread_cond_t z_condvar_t; +} _z_task_t; +typedef pthread_mutex_t _z_mutex_t; +typedef pthread_cond_t _z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/system/platform/flipper.h b/include/zenoh-pico/system/platform/flipper.h index 124714afe..4a51074f6 100644 --- a/include/zenoh-pico/system/platform/flipper.h +++ b/include/zenoh-pico/system/platform/flipper.h @@ -27,10 +27,10 @@ #define FLIPPER_SERIAL_TIMEOUT_MS 200 #if Z_FEATURE_MULTI_THREAD == 1 -typedef FuriThread* z_task_t; +typedef FuriThread* _z_task_t; typedef uint32_t z_task_attr_t; -typedef FuriMutex* z_mutex_t; -typedef void* z_condvar_t; +typedef FuriMutex* _z_mutex_t; +typedef void* _z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/system/platform/freertos_plus_tcp.h b/include/zenoh-pico/system/platform/freertos_plus_tcp.h index f85269dab..81ea0389a 100644 --- a/include/zenoh-pico/system/platform/freertos_plus_tcp.h +++ b/include/zenoh-pico/system/platform/freertos_plus_tcp.h @@ -33,10 +33,10 @@ typedef struct { typedef struct { TaskHandle_t handle; EventGroupHandle_t join_event; -} z_task_t; +} _z_task_t; -typedef SemaphoreHandle_t z_mutex_t; -typedef void *z_condvar_t; +typedef SemaphoreHandle_t _z_mutex_t; +typedef void *_z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef TickType_t z_clock_t; diff --git a/include/zenoh-pico/system/platform/mbed.h b/include/zenoh-pico/system/platform/mbed.h index 170b230f3..e444dc930 100644 --- a/include/zenoh-pico/system/platform/mbed.h +++ b/include/zenoh-pico/system/platform/mbed.h @@ -23,10 +23,10 @@ typedef int _z_socket_t; #if Z_FEATURE_MULTI_THREAD == 1 -typedef void *z_task_t; // Workaround as MBED is a C++ library +typedef void *_z_task_t; // Workaround as MBED is a C++ library typedef void *z_task_attr_t; // Workaround as MBED is a C++ library -typedef void *z_mutex_t; // Workaround as MBED is a C++ library -typedef void *z_condvar_t; // Workaround as MBED is a C++ library +typedef void *_z_mutex_t; // Workaround as MBED is a C++ library +typedef void *_z_condvar_t; // Workaround as MBED is a C++ library #endif // Z_FEATURE_MULTI_THREAD == 1 typedef void *z_clock_t; // Not defined diff --git a/include/zenoh-pico/system/platform/unix.h b/include/zenoh-pico/system/platform/unix.h index 8fd10421e..cea7b5868 100644 --- a/include/zenoh-pico/system/platform/unix.h +++ b/include/zenoh-pico/system/platform/unix.h @@ -25,10 +25,10 @@ #if Z_FEATURE_MULTI_THREAD == 1 #include -typedef pthread_t z_task_t; +typedef pthread_t _z_task_t; typedef pthread_attr_t z_task_attr_t; -typedef pthread_mutex_t z_mutex_t; -typedef pthread_cond_t z_condvar_t; +typedef pthread_mutex_t _z_mutex_t; +typedef pthread_cond_t _z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/system/platform/void.h b/include/zenoh-pico/system/platform/void.h index b6fc1dd99..cda1b50ab 100644 --- a/include/zenoh-pico/system/platform/void.h +++ b/include/zenoh-pico/system/platform/void.h @@ -18,10 +18,10 @@ #include "zenoh-pico/config.h" #if Z_FEATURE_MULTI_THREAD == 1 -typedef void *z_task_t; +typedef void *_z_task_t; typedef void *z_task_attr_t; -typedef void *z_mutex_t; -typedef void *z_condvar_t; +typedef void *_z_mutex_t; +typedef void *_z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 typedef void *z_clock_t; diff --git a/include/zenoh-pico/system/platform/windows.h b/include/zenoh-pico/system/platform/windows.h index d003be987..57513e20a 100644 --- a/include/zenoh-pico/system/platform/windows.h +++ b/include/zenoh-pico/system/platform/windows.h @@ -21,10 +21,10 @@ #include "zenoh-pico/config.h" #if Z_FEATURE_MULTI_THREAD == 1 -typedef HANDLE *z_task_t; +typedef HANDLE *_z_task_t; typedef void *z_task_attr_t; // Not used in Windows -typedef SRWLOCK z_mutex_t; -typedef CONDITION_VARIABLE z_condvar_t; +typedef SRWLOCK _z_mutex_t; +typedef CONDITION_VARIABLE _z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 typedef LARGE_INTEGER z_clock_t; diff --git a/include/zenoh-pico/system/platform/zephyr.h b/include/zenoh-pico/system/platform/zephyr.h index da7b41997..966edaa52 100644 --- a/include/zenoh-pico/system/platform/zephyr.h +++ b/include/zenoh-pico/system/platform/zephyr.h @@ -31,10 +31,10 @@ #include "zenoh-pico/config.h" #if Z_FEATURE_MULTI_THREAD == 1 -typedef pthread_t z_task_t; +typedef pthread_t _z_task_t; typedef pthread_attr_t z_task_attr_t; -typedef pthread_mutex_t z_mutex_t; -typedef pthread_cond_t z_condvar_t; +typedef pthread_mutex_t _z_mutex_t; +typedef pthread_cond_t _z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/transport/multicast/lease.h b/include/zenoh-pico/transport/multicast/lease.h index 628eaa088..e9202aa11 100644 --- a/include/zenoh-pico/transport/multicast/lease.h +++ b/include/zenoh-pico/transport/multicast/lease.h @@ -23,7 +23,7 @@ int8_t _zp_multicast_stop_lease_task(_z_transport_multicast_t *ztm); void *_zp_multicast_lease_task(void *ztm_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && (Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1) -int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, z_task_attr_t *attr, z_task_t *task); +int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, z_task_attr_t *attr, _z_task_t *task); #else int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, void *attr, void *task); #endif /* Z_FEATURE_MULTI_THREAD == 1 && (Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1) */ diff --git a/include/zenoh-pico/transport/multicast/read.h b/include/zenoh-pico/transport/multicast/read.h index 67b046397..5a0fda213 100644 --- a/include/zenoh-pico/transport/multicast/read.h +++ b/include/zenoh-pico/transport/multicast/read.h @@ -22,7 +22,7 @@ int8_t _zp_multicast_stop_read_task(_z_transport_t *zt); void *_zp_multicast_read_task(void *ztm_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_MULTICAST_TRANSPORT == 1 -int8_t _zp_multicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task); +int8_t _zp_multicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, _z_task_t *task); #else int8_t _zp_multicast_start_read_task(_z_transport_t *zt, void *attr, void *task); #endif /* #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_MULTICAST_TRANSPORT == 1 */ diff --git a/include/zenoh-pico/transport/raweth/read.h b/include/zenoh-pico/transport/raweth/read.h index 3df0d2979..7c98e1c87 100644 --- a/include/zenoh-pico/transport/raweth/read.h +++ b/include/zenoh-pico/transport/raweth/read.h @@ -22,7 +22,7 @@ int8_t _zp_raweth_stop_read_task(_z_transport_t *zt); void *_zp_raweth_read_task(void *ztm_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_RAWETH_TRANSPORT == 1 -int8_t _zp_raweth_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task); +int8_t _zp_raweth_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, _z_task_t *task); #else int8_t _zp_raweth_start_read_task(_z_transport_t *zt, void *attr, void *task); #endif /* Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_RAWETH_TRANSPORT == 1 */ diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index d00c3c8de..d82b68bf6 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -69,8 +69,8 @@ typedef struct { #if Z_FEATURE_MULTI_THREAD == 1 // TX and RX mutexes - z_mutex_t _mutex_rx; - z_mutex_t _mutex_tx; + _z_mutex_t _mutex_rx; + _z_mutex_t _mutex_tx; #endif // Z_FEATURE_MULTI_THREAD == 1 _z_link_t _link; @@ -96,8 +96,8 @@ typedef struct { volatile _z_zint_t _lease; #if Z_FEATURE_MULTI_THREAD == 1 - z_task_t *_read_task; - z_task_t *_lease_task; + _z_task_t *_read_task; + _z_task_t *_lease_task; volatile _Bool _read_task_running; volatile _Bool _lease_task_running; #endif // Z_FEATURE_MULTI_THREAD == 1 @@ -112,11 +112,11 @@ typedef struct _z_transport_multicast_t { #if Z_FEATURE_MULTI_THREAD == 1 // TX and RX mutexes - z_mutex_t _mutex_rx; - z_mutex_t _mutex_tx; + _z_mutex_t _mutex_rx; + _z_mutex_t _mutex_tx; // Peer list mutex - z_mutex_t _mutex_peer; + _z_mutex_t _mutex_peer; #endif // Z_FEATURE_MULTI_THREAD == 1 _z_link_t _link; @@ -138,8 +138,8 @@ typedef struct _z_transport_multicast_t { _zp_f_send_tmsg _send_f; #if Z_FEATURE_MULTI_THREAD == 1 - z_task_t *_read_task; - z_task_t *_lease_task; + _z_task_t *_read_task; + _z_task_t *_lease_task; volatile _Bool _read_task_running; volatile _Bool _lease_task_running; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/transport/unicast/lease.h b/include/zenoh-pico/transport/unicast/lease.h index 8ccad0e7b..17361c32e 100644 --- a/include/zenoh-pico/transport/unicast/lease.h +++ b/include/zenoh-pico/transport/unicast/lease.h @@ -22,7 +22,7 @@ int8_t _zp_unicast_stop_lease_task(_z_transport_t *zt); void *_zp_unicast_lease_task(void *ztu_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_UNICAST_TRANSPORT == 1 -int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task); +int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, z_task_attr_t *attr, _z_task_t *task); #else int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, void *attr, void *task); #endif /* Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_UNICAST_TRANSPORT == 1 */ diff --git a/include/zenoh-pico/transport/unicast/read.h b/include/zenoh-pico/transport/unicast/read.h index c3c17bc05..91c7b9356 100644 --- a/include/zenoh-pico/transport/unicast/read.h +++ b/include/zenoh-pico/transport/unicast/read.h @@ -22,7 +22,7 @@ int8_t _zp_unicast_stop_read_task(_z_transport_t *zt); void *_zp_unicast_read_task(void *ztu_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_UNICAST_TRANSPORT == 1 -int8_t _zp_unicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task); +int8_t _zp_unicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, _z_task_t *task); #else int8_t _zp_unicast_start_read_task(_z_transport_t *zt, void *attr, void *task); #endif /* Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_UNICAST_TRANSPORT == 1 */ diff --git a/src/collections/fifo_mt.c b/src/collections/fifo_mt.c index 072fd6cc2..da6b7c535 100644 --- a/src/collections/fifo_mt.c +++ b/src/collections/fifo_mt.c @@ -23,9 +23,9 @@ int8_t _z_fifo_mt_init(_z_fifo_mt_t *fifo, size_t capacity) { _Z_RETURN_IF_ERR(_z_fifo_init(&fifo->_fifo, capacity)) #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(z_mutex_init(&fifo->_mutex)) - _Z_RETURN_IF_ERR(z_condvar_init(&fifo->_cv_not_full)) - _Z_RETURN_IF_ERR(z_condvar_init(&fifo->_cv_not_empty)) + _Z_RETURN_IF_ERR(_z_mutex_init(&fifo->_mutex)) + _Z_RETURN_IF_ERR(_z_condvar_init(&fifo->_cv_not_full)) + _Z_RETURN_IF_ERR(_z_condvar_init(&fifo->_cv_not_empty)) #endif return _Z_RES_OK; @@ -50,9 +50,9 @@ _z_fifo_mt_t *_z_fifo_mt_new(size_t capacity) { void _z_fifo_mt_clear(_z_fifo_mt_t *fifo, z_element_free_f free_f) { #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_free(&fifo->_mutex); - z_condvar_free(&fifo->_cv_not_full); - z_condvar_free(&fifo->_cv_not_empty); + _z_mutex_drop(&fifo->_mutex); + _z_condvar_drop(&fifo->_cv_not_full); + _z_condvar_drop(&fifo->_cv_not_empty); #endif _z_fifo_clear(&fifo->_fifo, free_f); @@ -72,16 +72,16 @@ int8_t _z_fifo_mt_push(const void *elem, void *context, z_element_free_f element _z_fifo_mt_t *f = (_z_fifo_mt_t *)context; #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(z_mutex_lock(&f->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_lock(&f->_mutex)) while (elem != NULL) { elem = _z_fifo_push(&f->_fifo, (void *)elem); if (elem != NULL) { - _Z_RETURN_IF_ERR(z_condvar_wait(&f->_cv_not_full, &f->_mutex)) + _Z_RETURN_IF_ERR(_z_condvar_wait(&f->_cv_not_full, &f->_mutex)) } else { - _Z_RETURN_IF_ERR(z_condvar_signal(&f->_cv_not_empty)) + _Z_RETURN_IF_ERR(_z_condvar_signal(&f->_cv_not_empty)) } } - _Z_RETURN_IF_ERR(z_mutex_unlock(&f->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_unlock(&f->_mutex)) #else // Z_FEATURE_MULTI_THREAD == 1 _z_fifo_push_drop(&f->_fifo, (void *)elem, element_free); #endif // Z_FEATURE_MULTI_THREAD == 1 @@ -94,16 +94,16 @@ int8_t _z_fifo_mt_pull(void *dst, void *context, z_element_move_f element_move) #if Z_FEATURE_MULTI_THREAD == 1 void *src = NULL; - _Z_RETURN_IF_ERR(z_mutex_lock(&f->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_lock(&f->_mutex)) while (src == NULL) { src = _z_fifo_pull(&f->_fifo); if (src == NULL) { - _Z_RETURN_IF_ERR(z_condvar_wait(&f->_cv_not_empty, &f->_mutex)) + _Z_RETURN_IF_ERR(_z_condvar_wait(&f->_cv_not_empty, &f->_mutex)) } else { - _Z_RETURN_IF_ERR(z_condvar_signal(&f->_cv_not_full)) + _Z_RETURN_IF_ERR(_z_condvar_signal(&f->_cv_not_full)) } } - _Z_RETURN_IF_ERR(z_mutex_unlock(&f->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_unlock(&f->_mutex)) element_move(dst, src); #else // Z_FEATURE_MULTI_THREAD == 1 void *src = _z_fifo_pull(&f->_fifo); @@ -120,12 +120,12 @@ int8_t _z_fifo_mt_try_pull(void *dst, void *context, z_element_move_f element_mo #if Z_FEATURE_MULTI_THREAD == 1 void *src = NULL; - _Z_RETURN_IF_ERR(z_mutex_lock(&f->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_lock(&f->_mutex)) src = _z_fifo_pull(&f->_fifo); if (src != NULL) { - _Z_RETURN_IF_ERR(z_condvar_signal(&f->_cv_not_full)) + _Z_RETURN_IF_ERR(_z_condvar_signal(&f->_cv_not_full)) } - _Z_RETURN_IF_ERR(z_mutex_unlock(&f->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_unlock(&f->_mutex)) #else // Z_FEATURE_MULTI_THREAD == 1 void *src = _z_fifo_pull(&f->_fifo); #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/collections/ring_mt.c b/src/collections/ring_mt.c index c3b68953f..dd24593ea 100644 --- a/src/collections/ring_mt.c +++ b/src/collections/ring_mt.c @@ -23,8 +23,8 @@ int8_t _z_ring_mt_init(_z_ring_mt_t *ring, size_t capacity) { _Z_RETURN_IF_ERR(_z_ring_init(&ring->_ring, capacity)) #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(z_mutex_init(&ring->_mutex)) - _Z_RETURN_IF_ERR(z_condvar_init(&ring->_cv_not_empty)) + _Z_RETURN_IF_ERR(_z_mutex_init(&ring->_mutex)) + _Z_RETURN_IF_ERR(_z_condvar_init(&ring->_cv_not_empty)) #endif return _Z_RES_OK; } @@ -47,8 +47,8 @@ _z_ring_mt_t *_z_ring_mt_new(size_t capacity) { void _z_ring_mt_clear(_z_ring_mt_t *ring, z_element_free_f free_f) { #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_free(&ring->_mutex); - z_condvar_free(&ring->_cv_not_empty); + _z_mutex_drop(&ring->_mutex); + _z_condvar_drop(&ring->_cv_not_empty); #endif _z_ring_clear(&ring->_ring, free_f); @@ -68,14 +68,14 @@ int8_t _z_ring_mt_push(const void *elem, void *context, z_element_free_f element _z_ring_mt_t *r = (_z_ring_mt_t *)context; #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(z_mutex_lock(&r->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_lock(&r->_mutex)) #endif _z_ring_push_force_drop(&r->_ring, (void *)elem, element_free); #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(z_condvar_signal(&r->_cv_not_empty)) - _Z_RETURN_IF_ERR(z_mutex_unlock(&r->_mutex)) + _Z_RETURN_IF_ERR(_z_condvar_signal(&r->_cv_not_empty)) + _Z_RETURN_IF_ERR(_z_mutex_unlock(&r->_mutex)) #endif return _Z_RES_OK; } @@ -85,14 +85,14 @@ int8_t _z_ring_mt_pull(void *dst, void *context, z_element_move_f element_move) #if Z_FEATURE_MULTI_THREAD == 1 void *src = NULL; - _Z_RETURN_IF_ERR(z_mutex_lock(&r->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_lock(&r->_mutex)) while (src == NULL) { src = _z_ring_pull(&r->_ring); if (src == NULL) { - _Z_RETURN_IF_ERR(z_condvar_wait(&r->_cv_not_empty, &r->_mutex)) + _Z_RETURN_IF_ERR(_z_condvar_wait(&r->_cv_not_empty, &r->_mutex)) } } - _Z_RETURN_IF_ERR(z_mutex_unlock(&r->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_unlock(&r->_mutex)) element_move(dst, src); #else // Z_FEATURE_MULTI_THREAD == 1 void *src = _z_ring_pull(&r->_ring); @@ -108,13 +108,13 @@ int8_t _z_ring_mt_try_pull(void *dst, void *context, z_element_move_f element_mo _z_ring_mt_t *r = (_z_ring_mt_t *)context; #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(z_mutex_lock(&r->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_lock(&r->_mutex)) #endif void *src = _z_ring_pull(&r->_ring); #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(z_mutex_unlock(&r->_mutex)) + _Z_RETURN_IF_ERR(_z_mutex_unlock(&r->_mutex)) #endif if (src != NULL) { diff --git a/src/net/session.c b/src/net/session.c index 846c7ca6d..5ed899547 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -186,7 +186,7 @@ int8_t _zp_send_join(_z_session_t *zn) { return _z_send_join(&zn->_tp); } int8_t _zp_start_read_task(_z_session_t *zn, z_task_attr_t *attr) { int8_t ret = _Z_RES_OK; // Allocate task - z_task_t *task = (z_task_t *)z_malloc(sizeof(z_task_t)); + _z_task_t *task = (_z_task_t *)z_malloc(sizeof(_z_task_t)); if (task == NULL) { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -215,7 +215,7 @@ int8_t _zp_start_read_task(_z_session_t *zn, z_task_attr_t *attr) { int8_t _zp_start_lease_task(_z_session_t *zn, z_task_attr_t *attr) { int8_t ret = _Z_RES_OK; // Allocate task - z_task_t *task = (z_task_t *)z_malloc(sizeof(z_task_t)); + _z_task_t *task = (_z_task_t *)z_malloc(sizeof(_z_task_t)); if (task == NULL) { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } diff --git a/src/session/utils.c b/src/session/utils.c index 556694bb8..0db332c32 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -74,7 +74,7 @@ int8_t _z_session_init(_z_session_t *zn, _z_id_t *zid) { #endif #if Z_FEATURE_MULTI_THREAD == 1 - ret = z_mutex_init(&zn->_mutex_inner); + ret = _z_mutex_init(&zn->_mutex_inner); if (ret != _Z_RES_OK) { _z_transport_clear(&zn->_tp); return ret; @@ -119,7 +119,7 @@ void _z_session_clear(_z_session_t *zn) { _z_flush_interest(zn); #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_free(&zn->_mutex_inner); + _z_mutex_drop(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 } @@ -134,8 +134,8 @@ int8_t _z_session_close(_z_session_t *zn, uint8_t reason) { } #if Z_FEATURE_MULTI_THREAD == 1 -void _zp_session_lock_mutex(_z_session_t *zn) { (void)z_mutex_lock(&zn->_mutex_inner); } -void _zp_session_unlock_mutex(_z_session_t *zn) { (void)z_mutex_unlock(&zn->_mutex_inner); } +void _zp_session_lock_mutex(_z_session_t *zn) { (void)_z_mutex_lock(&zn->_mutex_inner); } +void _zp_session_unlock_mutex(_z_session_t *zn) { (void)_z_mutex_unlock(&zn->_mutex_inner); } #else void _zp_session_lock_mutex(_z_session_t *zn) { _ZP_UNUSED(zn); } void _zp_session_unlock_mutex(_z_session_t *zn) { _ZP_UNUSED(zn); } diff --git a/src/system/arduino/esp32/system.c b/src/system/arduino/esp32/system.c index 6c82757a5..9d07e9d66 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -60,7 +60,7 @@ void z_task_wrapper(z_task_arg *targ) { } /*------------------ Task ------------------*/ -int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { int ret = 0; z_task_arg *z_arg = (z_task_arg *)z_malloc(sizeof(z_task_arg)); @@ -77,41 +77,41 @@ int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), vo return ret; } -int8_t z_task_join(z_task_t *task) { +int8_t _z_task_join(_z_task_t *task) { // Note: task/thread join not supported on FreeRTOS API, so we force its deletion instead. - return zp_task_cancel(task); + return _z_task_cancel(task); } -int8_t zp_task_cancel(z_task_t *task) { +int8_t _z_task_cancel(_z_task_t *task) { vTaskDelete(*task); return 0; } -void z_task_free(z_task_t **task) { - z_task_t *ptr = *task; +void _z_task_drop(_z_task_t **task) { + _z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ 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) { return pthread_mutex_init(m, NULL); } -int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t _z_mutex_drop(_z_mutex_t *m) { return 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) { return pthread_mutex_lock(m); } -int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t _z_mutex_try_lock(_z_mutex_t *m) { return 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) { return 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) { return 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) { return 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) { return pthread_cond_signal(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) { return pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/arduino/opencr/system.c b/src/system/arduino/opencr/system.c index ed017d200..7b0a98edc 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -63,37 +63,37 @@ void z_free(void *ptr) { #error "Multi-threading not supported yet on OpenCR port. Disable it by defining Z_FEATURE_MULTI_THREAD=0" /*------------------ Task ------------------*/ -int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { return -1; } +int8_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { return -1; } -int8_t z_task_join(z_task_t *task) { return -1; } +int8_t _z_task_join(_z_task_t *task) { return -1; } -int8_t zp_task_cancel(z_task_t *task) { return -1; } +int8_t _z_task_cancel(_z_task_t *task) { return -1; } -void z_task_free(z_task_t **task) { - z_task_t *ptr = *task; +void _z_task_drop(_z_task_t **task) { + _z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { return -1; } +int8_t _z_mutex_init(_z_mutex_t *m) { return -1; } -int8_t z_mutex_free(z_mutex_t *m) { return -1; } +int8_t _z_mutex_drop(_z_mutex_t *m) { return -1; } -int8_t z_mutex_lock(z_mutex_t *m) { return -1; } +int8_t _z_mutex_lock(_z_mutex_t *m) { return -1; } -int8_t z_mutex_trylock(z_mutex_t *m) { return -1; } +int8_t _z_mutex_try_lock(_z_mutex_t *m) { return -1; } -int8_t z_mutex_unlock(z_mutex_t *m) { return -1; } +int8_t _z_mutex_unlock(_z_mutex_t *m) { return -1; } /*------------------ Condvar ------------------*/ -int8_t z_condvar_init(z_condvar_t *cv) { return -1; } +int8_t _z_condvar_init(_z_condvar_t *cv) { return -1; } -int8_t z_condvar_free(z_condvar_t *cv) { return -1; } +int8_t _z_condvar_drop(_z_condvar_t *cv) { return -1; } -int8_t z_condvar_signal(z_condvar_t *cv) { return -1; } +int8_t _z_condvar_signal(_z_condvar_t *cv) { return -1; } -int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return -1; } +int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return -1; } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/emscripten/system.c b/src/system/emscripten/system.c index 121724269..998f3a35d 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -44,39 +44,39 @@ 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) { +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); } -int8_t z_task_join(z_task_t *task) { return pthread_join(*task, NULL); } +int8_t _z_task_join(_z_task_t *task) { return pthread_join(*task, NULL); } -int8_t zp_task_cancel(z_task_t *task) { return pthread_cancel(*task); } +int8_t _z_task_cancel(_z_task_t *task) { return pthread_cancel(*task); } -void z_task_free(z_task_t **task) { - z_task_t *ptr = *task; +void _z_task_drop(_z_task_t **task) { + _z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ 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) { return pthread_mutex_init(m, 0); } -int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t _z_mutex_drop(_z_mutex_t *m) { return 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) { return pthread_mutex_lock(m); } -int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t _z_mutex_try_lock(_z_mutex_t *m) { return 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) { return 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) { return pthread_cond_init(cv, 0); } -int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t _z_condvar_drop(_z_condvar_t *cv) { return 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) { return pthread_cond_signal(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) { return pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/espidf/system.c b/src/system/espidf/system.c index e6d3d018f..9a4351890 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -74,7 +74,7 @@ static z_task_attr_t z_default_task_attr = { }; /*------------------ Thread ------------------*/ -int8_t z_task_init(z_task_t *task, z_task_attr_t *arg_attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(_z_task_t *task, z_task_attr_t *arg_attr, void *(*fun)(void *), void *arg) { z_task_attr_t *attr = arg_attr; z_task_arg *z_arg = (z_task_arg *)z_malloc(sizeof(z_task_arg)); if (z_arg == NULL) { @@ -110,40 +110,40 @@ int8_t z_task_init(z_task_t *task, z_task_attr_t *arg_attr, void *(*fun)(void *) return 0; } -int8_t z_task_join(z_task_t *task) { +int8_t _z_task_join(_z_task_t *task) { xEventGroupWaitBits(task->join_event, 1, pdFALSE, pdFALSE, portMAX_DELAY); return 0; } -int8_t z_task_cancel(z_task_t *task) { +int8_t z_task_cancel(_z_task_t *task) { vTaskDelete(task->handle); return 0; } -void z_task_free(z_task_t **task) { +void _z_task_drop(_z_task_t **task) { z_free((*task)->join_event); z_free(*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) { return pthread_mutex_init(m, NULL); } -int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t _z_mutex_drop(_z_mutex_t *m) { return 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) { return pthread_mutex_lock(m); } -int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t _z_mutex_try_lock(_z_mutex_t *m) { return 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) { return 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) { return pthread_cond_init(cv, NULL); } -int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t _z_condvar_drop(_z_condvar_t *cv) { return 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) { return pthread_cond_signal(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) { return pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/flipper/system.c b/src/system/flipper/system.c index e723f6aa5..d443c246a 100644 --- a/src/system/flipper/system.c +++ b/src/system/flipper/system.c @@ -62,7 +62,7 @@ void z_free(void* ptr) { return free(ptr); } /*------------------ Task ------------------*/ -int8_t z_task_init(z_task_t* task, z_task_attr_t* attr, void* (*fun)(void*), void* arg) { +int8_t _z_task_init(_z_task_t* task, z_task_attr_t* attr, void* (*fun)(void*), void* arg) { if (task == NULL) { return -1; } @@ -80,16 +80,16 @@ int8_t z_task_init(z_task_t* task, z_task_attr_t* attr, void* (*fun)(void*), voi return _Z_RES_OK; } -int8_t z_task_join(z_task_t* task) { +int8_t _z_task_join(_z_task_t* task) { if (task == NULL) { return -1; } return furi_thread_join(*task); } -int8_t zp_task_cancel(z_task_t* task) { return -1; } +int8_t _z_task_cancel(_z_task_t* task) { return -1; } -void z_task_free(z_task_t** task) { +void _z_task_drop(_z_task_t** task) { if (task == NULL || *task == NULL) { return; } @@ -98,7 +98,7 @@ void z_task_free(z_task_t** task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t* m) { +int8_t _z_mutex_init(_z_mutex_t* m) { if (m == NULL) { return -1; } @@ -106,7 +106,7 @@ int8_t z_mutex_init(z_mutex_t* m) { return (*m != 0) ? _Z_RES_OK : _Z_ERR_SYSTEM_TASK_FAILED; } -int8_t z_mutex_free(z_mutex_t* m) { +int8_t _z_mutex_drop(_z_mutex_t* m) { if (m == NULL) { return -1; } @@ -118,7 +118,7 @@ int8_t z_mutex_free(z_mutex_t* m) { return 0; } -int8_t z_mutex_lock(z_mutex_t* m) { +int8_t _z_mutex_lock(_z_mutex_t* m) { if (m == NULL) { return -1; } @@ -128,9 +128,9 @@ int8_t z_mutex_lock(z_mutex_t* m) { return furi_mutex_acquire(*m, FuriWaitForever); } -int8_t z_mutex_trylock(z_mutex_t* m) { return -1; } +int8_t _z_mutex_try_lock(_z_mutex_t* m) { return -1; } -int8_t z_mutex_unlock(z_mutex_t* m) { +int8_t _z_mutex_unlock(_z_mutex_t* m) { if (m == NULL) { return -1; } @@ -141,13 +141,13 @@ int8_t z_mutex_unlock(z_mutex_t* m) { } /*------------------ Condvar ------------------*/ -int8_t z_condvar_init(z_condvar_t* cv) { return -1; } +int8_t _z_condvar_init(_z_condvar_t* cv) { return -1; } -int8_t z_condvar_free(z_condvar_t* cv) { return -1; } +int8_t _z_condvar_drop(_z_condvar_t* cv) { return -1; } -int8_t z_condvar_signal(z_condvar_t* cv) { return -1; } +int8_t _z_condvar_signal(_z_condvar_t* cv) { return -1; } -int8_t z_condvar_wait(z_condvar_t* cv, z_mutex_t* m) { return -1; } +int8_t _z_condvar_wait(_z_condvar_t* cv, _z_mutex_t* m) { return -1; } /*------------------ Sleep ------------------*/ int z_sleep_us(size_t time) { diff --git a/src/system/freertos_plus_tcp/system.c b/src/system/freertos_plus_tcp/system.c index 025ccc44a..0c0e11c02 100644 --- a/src/system/freertos_plus_tcp/system.c +++ b/src/system/freertos_plus_tcp/system.c @@ -85,7 +85,7 @@ static z_task_attr_t z_default_task_attr = { }; /*------------------ Thread ------------------*/ -int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { z_task_arg *z_arg = (z_task_arg *)z_malloc(sizeof(z_task_arg)); if (z_arg == NULL) { return -1; @@ -119,44 +119,44 @@ int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), vo return 0; } -int8_t z_task_join(z_task_t *task) { +int8_t _z_task_join(_z_task_t *task) { xEventGroupWaitBits(task->join_event, 1, pdFALSE, pdFALSE, portMAX_DELAY); return 0; } -int8_t zp_task_cancel(z_task_t *task) { +int8_t _z_task_cancel(_z_task_t *task) { vTaskDelete(task->handle); return 0; } -void z_task_free(z_task_t **task) { +void _z_task_drop(_z_task_t **task) { z_free((*task)->join_event); z_free(*task); } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { +int8_t _z_mutex_init(_z_mutex_t *m) { *m = xSemaphoreCreateRecursiveMutex(); return *m == NULL ? -1 : 0; } -int8_t z_mutex_free(z_mutex_t *m) { +int8_t _z_mutex_drop(_z_mutex_t *m) { z_free(*m); return 0; } -int8_t z_mutex_lock(z_mutex_t *m) { return xSemaphoreTakeRecursive(*m, portMAX_DELAY) == pdTRUE ? 0 : -1; } +int8_t _z_mutex_lock(_z_mutex_t *m) { return xSemaphoreTakeRecursive(*m, portMAX_DELAY) == pdTRUE ? 0 : -1; } -int8_t z_mutex_trylock(z_mutex_t *m) { return xSemaphoreTakeRecursive(*m, 0) == pdTRUE ? 0 : -1; } +int8_t _z_mutex_try_lock(_z_mutex_t *m) { return xSemaphoreTakeRecursive(*m, 0) == pdTRUE ? 0 : -1; } -int8_t z_mutex_unlock(z_mutex_t *m) { return xSemaphoreGiveRecursive(*m) == pdTRUE ? 0 : -1; } +int8_t _z_mutex_unlock(_z_mutex_t *m) { return xSemaphoreGiveRecursive(*m) == pdTRUE ? 0 : -1; } /*------------------ CondVar ------------------*/ // Condition variables not supported in FreeRTOS -int8_t z_condvar_init(z_condvar_t *cv) { return -1; } -int8_t z_condvar_free(z_condvar_t *cv) { return -1; } -int8_t z_condvar_signal(z_condvar_t *cv) { return -1; } -int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return -1; } +int8_t _z_condvar_init(_z_condvar_t *cv) { return -1; } +int8_t _z_condvar_drop(_z_condvar_t *cv) { return -1; } +int8_t _z_condvar_signal(_z_condvar_t *cv) { return -1; } +int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return -1; } #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/mbed/system.cpp b/src/system/mbed/system.cpp index 7b2ffc65f..ff8b024ad 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -41,67 +41,67 @@ 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) { +int8_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { *task = new Thread(); mbed::Callback c = mbed::Callback(fun, arg); return ((Thread *)*task)->start(c); } -int8_t z_task_join(z_task_t *task) { +int8_t _z_task_join(_z_task_t *task) { int res = ((Thread *)*task)->join(); delete ((Thread *)*task); return res; } -int8_t zp_task_cancel(z_task_t *task) { +int8_t _z_task_cancel(_z_task_t *task) { int res = ((Thread *)*task)->terminate(); delete ((Thread *)*task); return res; } -void z_task_free(z_task_t **task) { - z_task_t *ptr = *task; +void _z_task_drop(_z_task_t **task) { + _z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { +int8_t _z_mutex_init(_z_mutex_t *m) { *m = new Mutex(); return 0; } -int8_t z_mutex_free(z_mutex_t *m) { +int8_t _z_mutex_drop(_z_mutex_t *m) { delete ((Mutex *)*m); return 0; } -int8_t z_mutex_lock(z_mutex_t *m) { +int8_t _z_mutex_lock(_z_mutex_t *m) { ((Mutex *)*m)->lock(); return 0; } -int8_t z_mutex_trylock(z_mutex_t *m) { return (((Mutex *)*m)->trylock() == true) ? 0 : -1; } +int8_t _z_mutex_try_lock(_z_mutex_t *m) { return (((Mutex *)*m)->trylock() == true) ? 0 : -1; } -int8_t z_mutex_unlock(z_mutex_t *m) { +int8_t _z_mutex_unlock(_z_mutex_t *m) { ((Mutex *)*m)->unlock(); return 0; } /*------------------ Condvar ------------------*/ -int8_t z_condvar_init(z_condvar_t *cv) { return 0; } +int8_t _z_condvar_init(_z_condvar_t *cv) { return 0; } -int8_t z_condvar_free(z_condvar_t *cv) { +int8_t _z_condvar_drop(_z_condvar_t *cv) { delete ((ConditionVariable *)*cv); return 0; } -int8_t z_condvar_signal(z_condvar_t *cv) { +int8_t _z_condvar_signal(_z_condvar_t *cv) { ((ConditionVariable *)*cv)->notify_all(); return 0; } -int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { +int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { *cv = new ConditionVariable(*((Mutex *)*m)); ((ConditionVariable *)*cv)->wait(); return 0; diff --git a/src/system/platform-common.c b/src/system/platform-common.c new file mode 100644 index 000000000..1642dce42 --- /dev/null +++ b/src/system/platform-common.c @@ -0,0 +1,53 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, + +#include "zenoh-pico/system/platform-common.h" + +#include "zenoh-pico/api/olv_macros.h" + +#if Z_FEATURE_MULTI_THREAD == 1 + +/*------------------ Thread ------------------*/ +_Z_OWNED_FUNCTIONS_SYSTEM_IMPL(_z_task_t, task) + +int8_t z_task_init(z_owned_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { + return _z_task_init(&task->_val, attr, fun, arg); +} + +int8_t z_task_join(z_loaned_task_t *task) { return _z_task_join(task); } + +void z_task_drop(z_owned_task_t *task) { + _z_task_t *ptr = &task->_val; + _z_task_drop(&ptr); +} + +/*------------------ Mutex ------------------*/ +_Z_OWNED_FUNCTIONS_SYSTEM_IMPL(_z_mutex_t, mutex) + +int8_t z_mutex_init(z_owned_mutex_t *m) { return _z_mutex_init(&m->_val); } +int8_t z_mutex_drop(z_owned_mutex_t *m) { return _z_mutex_drop(&m->_val); } + +int8_t z_mutex_lock(z_loaned_mutex_t *m) { return _z_mutex_lock(m); } +int8_t z_mutex_try_lock(z_loaned_mutex_t *m) { return _z_mutex_try_lock(m); } +int8_t z_mutex_unlock(z_loaned_mutex_t *m) { return _z_mutex_unlock(m); } + +/*------------------ CondVar ------------------*/ +_Z_OWNED_FUNCTIONS_SYSTEM_IMPL(_z_condvar_t, condvar) + +int8_t z_condvar_init(z_owned_condvar_t *cv) { return _z_condvar_init(&cv->_val); } +int8_t z_condvar_drop(z_owned_condvar_t *cv) { return _z_condvar_drop(&cv->_val); } + +int8_t z_condvar_signal(z_loaned_condvar_t *cv) { return _z_condvar_signal(cv); } +int8_t z_condvar_wait(z_loaned_condvar_t *cv, z_loaned_mutex_t *m) { return _z_condvar_wait(cv, m); } + +#endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/system/unix/system.c b/src/system/unix/system.c index 49857ac7e..7d13fba19 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -101,39 +101,39 @@ 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) { +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); } -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) { return (int8_t)pthread_join(*task, NULL); } -int8_t zp_task_cancel(z_task_t *task) { return (int8_t)pthread_cancel(*task); } +int8_t _z_task_cancel(_z_task_t *task) { return (int8_t)pthread_cancel(*task); } -void z_task_free(z_task_t **task) { - z_task_t *ptr = *task; +void _z_task_drop(_z_task_t **task) { + _z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ 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) { return (int8_t)pthread_mutex_init(m, 0); } -int8_t z_mutex_free(z_mutex_t *m) { return (int8_t)pthread_mutex_destroy(m); } +int8_t _z_mutex_drop(_z_mutex_t *m) { return (int8_t)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) { return (int8_t)pthread_mutex_lock(m); } -int8_t z_mutex_trylock(z_mutex_t *m) { return (int8_t)pthread_mutex_trylock(m); } +int8_t _z_mutex_try_lock(_z_mutex_t *m) { return (int8_t)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) { return (int8_t)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) { return (int8_t)pthread_cond_init(cv, 0); } -int8_t z_condvar_free(z_condvar_t *cv) { return (int8_t)pthread_cond_destroy(cv); } +int8_t _z_condvar_drop(_z_condvar_t *cv) { return (int8_t)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) { return (int8_t)pthread_cond_signal(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) { return (int8_t)pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/windows/system.c b/src/system/windows/system.c index 3997df9ab..f1a8a6c04 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -60,7 +60,7 @@ 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) { +int8_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { (void)(attr); int8_t ret = _Z_RES_OK; *task = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fun, arg, 0, NULL); @@ -70,45 +70,45 @@ int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), vo return ret; } -int8_t z_task_join(z_task_t *task) { +int8_t _z_task_join(_z_task_t *task) { int8_t ret = _Z_RES_OK; WaitForSingleObject(*task, INFINITE); return ret; } -int8_t zp_task_cancel(z_task_t *task) { +int8_t _z_task_cancel(_z_task_t *task) { int8_t ret = _Z_RES_OK; TerminateThread(*task, 0); return ret; } -void z_task_free(z_task_t **task) { - z_task_t *ptr = *task; +void _z_task_drop(_z_task_t **task) { + _z_task_t *ptr = *task; CloseHandle(*ptr); z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { +int8_t _z_mutex_init(_z_mutex_t *m) { int8_t ret = _Z_RES_OK; InitializeSRWLock(m); return ret; } -int8_t z_mutex_free(z_mutex_t *m) { +int8_t _z_mutex_drop(_z_mutex_t *m) { (void)(m); int8_t ret = _Z_RES_OK; return ret; } -int8_t z_mutex_lock(z_mutex_t *m) { +int8_t _z_mutex_lock(_z_mutex_t *m) { int8_t ret = _Z_RES_OK; AcquireSRWLockExclusive(m); return ret; } -int8_t z_mutex_trylock(z_mutex_t *m) { +int8_t _z_mutex_try_lock(_z_mutex_t *m) { int8_t ret = _Z_RES_OK; if (TryAcquireSRWLockExclusive(m) == 0) { ret = _Z_ERR_GENERIC; @@ -116,32 +116,32 @@ int8_t z_mutex_trylock(z_mutex_t *m) { return ret; } -int8_t z_mutex_unlock(z_mutex_t *m) { +int8_t _z_mutex_unlock(_z_mutex_t *m) { int8_t ret = _Z_RES_OK; ReleaseSRWLockExclusive(m); return ret; } /*------------------ Condvar ------------------*/ -int8_t z_condvar_init(z_condvar_t *cv) { +int8_t _z_condvar_init(_z_condvar_t *cv) { int8_t ret = _Z_RES_OK; InitializeConditionVariable(cv); return ret; } -int8_t z_condvar_free(z_condvar_t *cv) { +int8_t _z_condvar_drop(_z_condvar_t *cv) { (void)(cv); int8_t ret = _Z_RES_OK; return ret; } -int8_t z_condvar_signal(z_condvar_t *cv) { +int8_t _z_condvar_signal(_z_condvar_t *cv) { int8_t ret = _Z_RES_OK; WakeConditionVariable(cv); return ret; } -int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { +int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { int8_t ret = _Z_RES_OK; SleepConditionVariableSRW(cv, m, INFINITE, 0); return ret; diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index ec1dccb6e..42a4f97c7 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -71,7 +71,7 @@ K_THREAD_STACK_ARRAY_DEFINE(thread_stack_area, Z_THREADS_NUM, Z_PTHREAD_STACK_SI static int thread_index = 0; /*------------------ Task ------------------*/ -int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { z_task_attr_t *lattr = NULL; z_task_attr_t tmp; if (attr == NULL) { @@ -83,35 +83,35 @@ int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), vo return 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) { return pthread_join(*task, NULL); } -int8_t zp_task_cancel(z_task_t *task) { return pthread_cancel(*task); } +int8_t _z_task_cancel(_z_task_t *task) { return pthread_cancel(*task); } -void z_task_free(z_task_t **task) { - z_task_t *ptr = *task; +void _z_task_drop(_z_task_t **task) { + _z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ 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) { return pthread_mutex_init(m, 0); } -int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t _z_mutex_drop(_z_mutex_t *m) { return 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) { return pthread_mutex_lock(m); } -int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t _z_mutex_try_lock(_z_mutex_t *m) { return 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) { return 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) { return pthread_cond_init(cv, 0); } -int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t _z_condvar_drop(_z_condvar_t *cv) { return 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) { return pthread_cond_signal(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) { return pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/transport/multicast/lease.c b/src/transport/multicast/lease.c index 5c313a8af..b1f331986 100644 --- a/src/transport/multicast/lease.c +++ b/src/transport/multicast/lease.c @@ -99,7 +99,7 @@ void *_zp_multicast_lease_task(void *ztm_arg) { _z_transport_peer_entry_list_t *it = NULL; while (ztm->_lease_task_running == true) { - z_mutex_lock(&ztm->_mutex_peer); + _z_mutex_lock(&ztm->_mutex_peer); if (next_lease <= 0) { it = ztm->_peers; @@ -157,13 +157,13 @@ void *_zp_multicast_lease_task(void *ztm_arg) { } } - z_mutex_unlock(&ztm->_mutex_peer); + _z_mutex_unlock(&ztm->_mutex_peer); // The keep alive and lease intervals are expressed in milliseconds z_sleep_ms((size_t)interval); // Decrement all intervals - z_mutex_lock(&ztm->_mutex_peer); + _z_mutex_lock(&ztm->_mutex_peer); it = ztm->_peers; while (it != NULL) { @@ -181,16 +181,16 @@ void *_zp_multicast_lease_task(void *ztm_arg) { next_keep_alive = next_keep_alive - interval; next_join = next_join - interval; - z_mutex_unlock(&ztm->_mutex_peer); + _z_mutex_unlock(&ztm->_mutex_peer); } return 0; } -int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, z_task_attr_t *attr, z_task_t *task) { +int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, z_task_attr_t *attr, _z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(z_task_t)); + (void)memset(task, 0, sizeof(_z_task_t)); // Init task - if (z_task_init(task, attr, _zp_multicast_lease_task, ztm) != _Z_RES_OK) { + if (_z_task_init(task, attr, _zp_multicast_lease_task, ztm) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/multicast/read.c b/src/transport/multicast/read.c index b4de50730..624013ef9 100644 --- a/src/transport/multicast/read.c +++ b/src/transport/multicast/read.c @@ -52,7 +52,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { _z_transport_multicast_t *ztm = (_z_transport_multicast_t *)ztm_arg; // Acquire and keep the lock - z_mutex_lock(&ztm->_mutex_rx); + _z_mutex_lock(&ztm->_mutex_rx); // Prepare the buffer _z_zbuf_reset(&ztm->_zbuf); @@ -123,15 +123,15 @@ void *_zp_multicast_read_task(void *ztm_arg) { // Move the read position of the read buffer _z_zbuf_set_rpos(&ztm->_zbuf, _z_zbuf_get_rpos(&ztm->_zbuf) + to_read); } - z_mutex_unlock(&ztm->_mutex_rx); + _z_mutex_unlock(&ztm->_mutex_rx); return NULL; } -int8_t _zp_multicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task) { +int8_t _zp_multicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, _z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(z_task_t)); + (void)memset(task, 0, sizeof(_z_task_t)); // Init task - if (z_task_init(task, attr, _zp_multicast_read_task, &zt->_transport._multicast) != _Z_RES_OK) { + if (_z_task_init(task, attr, _zp_multicast_read_task, &zt->_transport._multicast) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/multicast/rx.c b/src/transport/multicast/rx.c index 6e9c5a098..badd82bc0 100644 --- a/src/transport/multicast/rx.c +++ b/src/transport/multicast/rx.c @@ -36,7 +36,7 @@ static int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_trans #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - z_mutex_lock(&ztm->_mutex_rx); + _z_mutex_lock(&ztm->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 size_t to_read = 0; @@ -83,7 +83,7 @@ static int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_trans } #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztm->_mutex_rx); + _z_mutex_unlock(&ztm->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; @@ -126,7 +126,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t int8_t ret = _Z_RES_OK; #if Z_FEATURE_MULTI_THREAD == 1 // Acquire and keep the lock - z_mutex_lock(&ztm->_mutex_peer); + _z_mutex_lock(&ztm->_mutex_peer); #endif // Z_FEATURE_MULTI_THREAD == 1 // Mark the session that we have received data from this peer @@ -342,7 +342,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t } #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztm->_mutex_peer); + _z_mutex_unlock(&ztm->_mutex_peer); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; diff --git a/src/transport/multicast/transport.c b/src/transport/multicast/transport.c index 6f6057fd8..ed903b4db 100644 --- a/src/transport/multicast/transport.c +++ b/src/transport/multicast/transport.c @@ -52,17 +52,17 @@ int8_t _z_multicast_transport_create(_z_transport_t *zt, _z_link_t *zl, } #if Z_FEATURE_MULTI_THREAD == 1 // Initialize the mutexes - ret = z_mutex_init(&ztm->_mutex_tx); + ret = _z_mutex_init(&ztm->_mutex_tx); if (ret == _Z_RES_OK) { - ret = z_mutex_init(&ztm->_mutex_rx); + ret = _z_mutex_init(&ztm->_mutex_rx); if (ret == _Z_RES_OK) { - ret = z_mutex_init(&ztm->_mutex_peer); + ret = _z_mutex_init(&ztm->_mutex_peer); if (ret != _Z_RES_OK) { - z_mutex_free(&ztm->_mutex_tx); - z_mutex_free(&ztm->_mutex_rx); + _z_mutex_drop(&ztm->_mutex_tx); + _z_mutex_drop(&ztm->_mutex_rx); } } else { - z_mutex_free(&ztm->_mutex_tx); + _z_mutex_drop(&ztm->_mutex_tx); } } #endif // Z_FEATURE_MULTI_THREAD == 1 @@ -79,9 +79,9 @@ int8_t _z_multicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _Z_ERROR("Not enough memory to allocate transport tx rx buffers!"); #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_free(&ztm->_mutex_tx); - z_mutex_free(&ztm->_mutex_rx); - z_mutex_free(&ztm->_mutex_peer); + _z_mutex_drop(&ztm->_mutex_tx); + _z_mutex_drop(&ztm->_mutex_rx); + _z_mutex_drop(&ztm->_mutex_peer); #endif // Z_FEATURE_MULTI_THREAD == 1 _z_wbuf_clear(&ztm->_wbuf); @@ -184,17 +184,17 @@ void _z_multicast_transport_clear(_z_transport_t *zt) { #if Z_FEATURE_MULTI_THREAD == 1 // Clean up tasks if (ztm->_read_task != NULL) { - z_task_join(ztm->_read_task); - z_task_free(&ztm->_read_task); + _z_task_join(ztm->_read_task); + _z_task_drop(&ztm->_read_task); } if (ztm->_lease_task != NULL) { - z_task_join(ztm->_lease_task); - z_task_free(&ztm->_lease_task); + _z_task_join(ztm->_lease_task); + _z_task_drop(&ztm->_lease_task); } // Clean up the mutexes - z_mutex_free(&ztm->_mutex_tx); - z_mutex_free(&ztm->_mutex_rx); - z_mutex_free(&ztm->_mutex_peer); + _z_mutex_drop(&ztm->_mutex_tx); + _z_mutex_drop(&ztm->_mutex_rx); + _z_mutex_drop(&ztm->_mutex_peer); #endif // Z_FEATURE_MULTI_THREAD == 1 // Clean up the buffers diff --git a/src/transport/multicast/tx.c b/src/transport/multicast/tx.c index 5de35fe4f..3f7726db0 100644 --- a/src/transport/multicast/tx.c +++ b/src/transport/multicast/tx.c @@ -46,7 +46,7 @@ int8_t _z_multicast_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - z_mutex_lock(&ztm->_mutex_tx); + _z_mutex_lock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 // Prepare the buffer eventually reserving space for the message length @@ -65,7 +65,7 @@ int8_t _z_multicast_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport } #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztm->_mutex_tx); + _z_mutex_unlock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; @@ -82,11 +82,11 @@ int8_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_m _Bool drop = false; if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_lock(&ztm->_mutex_tx); + _z_mutex_lock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 } else { #if Z_FEATURE_MULTI_THREAD == 1 - int8_t locked = z_mutex_trylock(&ztm->_mutex_tx); + int8_t locked = _z_mutex_try_lock(&ztm->_mutex_tx); if (locked != (int8_t)0) { _Z_INFO("Dropping zenoh message because of congestion control"); // We failed to acquire the lock, drop the message @@ -153,7 +153,7 @@ int8_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_m } #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztm->_mutex_tx); + _z_mutex_unlock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 } diff --git a/src/transport/raweth/read.c b/src/transport/raweth/read.c index dfb540700..ee02e4ffd 100644 --- a/src/transport/raweth/read.c +++ b/src/transport/raweth/read.c @@ -87,11 +87,11 @@ void *_zp_raweth_read_task(void *ztm_arg) { return NULL; } -int8_t _zp_raweth_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task) { +int8_t _zp_raweth_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, _z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(z_task_t)); + (void)memset(task, 0, sizeof(_z_task_t)); // Init task - if (z_task_init(task, attr, _zp_raweth_read_task, &zt->_transport._raweth) != _Z_RES_OK) { + if (_z_task_init(task, attr, _zp_raweth_read_task, &zt->_transport._raweth) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/raweth/rx.c b/src/transport/raweth/rx.c index dfba004a6..a1fdb9d1e 100644 --- a/src/transport/raweth/rx.c +++ b/src/transport/raweth/rx.c @@ -79,7 +79,7 @@ int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_messa #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - z_mutex_lock(&ztm->_mutex_rx); + _z_mutex_lock(&ztm->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 // Prepare the buffer @@ -107,7 +107,7 @@ int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_messa } #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztm->_mutex_rx); + _z_mutex_unlock(&ztm->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index 72784f6b6..9393d20bc 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -31,7 +31,7 @@ #if Z_FEATURE_RAWETH_TRANSPORT == 1 #if Z_FEATURE_MULTI_THREAD == 1 -static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { z_mutex_unlock(&ztm->_mutex_tx); } +static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { _z_mutex_unlock(&ztm->_mutex_tx); } #else static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { _ZP_UNUSED(ztm); } #endif @@ -196,7 +196,7 @@ int8_t _z_raweth_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport_me _Z_DEBUG(">> send session message"); #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_lock(&ztm->_mutex_tx); + _z_mutex_lock(&ztm->_mutex_tx); #endif // Reset wbuf _z_wbuf_reset(&ztm->_wbuf); @@ -214,7 +214,7 @@ int8_t _z_raweth_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport_me ztm->_transmitted = true; #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztm->_mutex_tx); + _z_mutex_unlock(&ztm->_mutex_tx); #endif return ret; @@ -229,9 +229,9 @@ int8_t _z_raweth_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg, // Acquire the lock and drop the message if needed #if Z_FEATURE_MULTI_THREAD == 1 if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { - z_mutex_lock(&ztm->_mutex_tx); + _z_mutex_lock(&ztm->_mutex_tx); } else { - if (z_mutex_trylock(&ztm->_mutex_tx) != (int8_t)0) { + if (_z_mutex_try_lock(&ztm->_mutex_tx) != (int8_t)0) { _Z_INFO("Dropping zenoh message because of congestion control"); // We failed to acquire the lock, drop the message return ret; @@ -314,7 +314,7 @@ int8_t _z_raweth_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg, #endif } #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztm->_mutex_tx); + _z_mutex_unlock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; } diff --git a/src/transport/unicast/lease.c b/src/transport/unicast/lease.c index 9dfad2244..c6125a39f 100644 --- a/src/transport/unicast/lease.c +++ b/src/transport/unicast/lease.c @@ -95,11 +95,11 @@ void *_zp_unicast_lease_task(void *ztu_arg) { return 0; } -int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task) { +int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, z_task_attr_t *attr, _z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(z_task_t)); + (void)memset(task, 0, sizeof(_z_task_t)); // Init task - if (z_task_init(task, attr, _zp_unicast_lease_task, &zt->_transport._unicast) != _Z_RES_OK) { + if (_z_task_init(task, attr, _zp_unicast_lease_task, &zt->_transport._unicast) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/unicast/read.c b/src/transport/unicast/read.c index eb19fca2c..8b1227aba 100644 --- a/src/transport/unicast/read.c +++ b/src/transport/unicast/read.c @@ -50,7 +50,7 @@ void *_zp_unicast_read_task(void *ztu_arg) { _z_transport_unicast_t *ztu = (_z_transport_unicast_t *)ztu_arg; // Acquire and keep the lock - z_mutex_lock(&ztu->_mutex_rx); + _z_mutex_lock(&ztu->_mutex_rx); // Prepare the buffer _z_zbuf_reset(&ztu->_zbuf); @@ -116,15 +116,15 @@ void *_zp_unicast_read_task(void *ztu_arg) { // Move the read position of the read buffer _z_zbuf_set_rpos(&ztu->_zbuf, _z_zbuf_get_rpos(&ztu->_zbuf) + to_read); } - z_mutex_unlock(&ztu->_mutex_rx); + _z_mutex_unlock(&ztu->_mutex_rx); return NULL; } -int8_t _zp_unicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task) { +int8_t _zp_unicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, _z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(z_task_t)); + (void)memset(task, 0, sizeof(_z_task_t)); // Init task - if (z_task_init(task, attr, _zp_unicast_read_task, &zt->_transport._unicast) != _Z_RES_OK) { + if (_z_task_init(task, attr, _zp_unicast_read_task, &zt->_transport._unicast) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/unicast/rx.c b/src/transport/unicast/rx.c index d8049f79d..1046f214f 100644 --- a/src/transport/unicast/rx.c +++ b/src/transport/unicast/rx.c @@ -33,7 +33,7 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag int8_t ret = _Z_RES_OK; #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - z_mutex_lock(&ztu->_mutex_rx); + _z_mutex_lock(&ztu->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 size_t to_read = 0; @@ -86,7 +86,7 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag } #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztu->_mutex_rx); + _z_mutex_unlock(&ztu->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; diff --git a/src/transport/unicast/transport.c b/src/transport/unicast/transport.c index 1cbce4700..29c0f0783 100644 --- a/src/transport/unicast/transport.c +++ b/src/transport/unicast/transport.c @@ -38,11 +38,11 @@ int8_t _z_unicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _z_transpo #if Z_FEATURE_MULTI_THREAD == 1 // Initialize the mutexes - ret = z_mutex_init(&zt->_transport._unicast._mutex_tx); + ret = _z_mutex_init(&zt->_transport._unicast._mutex_tx); if (ret == _Z_RES_OK) { - ret = z_mutex_init(&zt->_transport._unicast._mutex_rx); + ret = _z_mutex_init(&zt->_transport._unicast._mutex_rx); if (ret != _Z_RES_OK) { - z_mutex_free(&zt->_transport._unicast._mutex_tx); + _z_mutex_drop(&zt->_transport._unicast._mutex_tx); } } #endif // Z_FEATURE_MULTI_THREAD == 1 @@ -86,8 +86,8 @@ int8_t _z_unicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _z_transpo _Z_ERROR("Not enough memory to allocate transport tx rx buffers!"); #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_free(&zt->_transport._unicast._mutex_tx); - z_mutex_free(&zt->_transport._unicast._mutex_rx); + _z_mutex_drop(&zt->_transport._unicast._mutex_tx); + _z_mutex_drop(&zt->_transport._unicast._mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 _z_wbuf_clear(&zt->_transport._unicast._wbuf); @@ -115,8 +115,8 @@ int8_t _z_unicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _z_transpo _z_wbuf_clear(&zt->_transport._unicast._dbuf_best_effort); #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_free(&zt->_transport._unicast._mutex_tx); - z_mutex_free(&zt->_transport._unicast._mutex_rx); + _z_mutex_drop(&zt->_transport._unicast._mutex_tx); + _z_mutex_drop(&zt->_transport._unicast._mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 _z_wbuf_clear(&zt->_transport._unicast._wbuf); @@ -290,17 +290,17 @@ void _z_unicast_transport_clear(_z_transport_t *zt) { #if Z_FEATURE_MULTI_THREAD == 1 // Clean up tasks if (ztu->_read_task != NULL) { - z_task_join(ztu->_read_task); - z_task_free(&ztu->_read_task); + _z_task_join(ztu->_read_task); + _z_task_drop(&ztu->_read_task); } if (ztu->_lease_task != NULL) { - z_task_join(ztu->_lease_task); - z_task_free(&ztu->_lease_task); + _z_task_join(ztu->_lease_task); + _z_task_drop(&ztu->_lease_task); } // Clean up the mutexes - z_mutex_free(&ztu->_mutex_tx); - z_mutex_free(&ztu->_mutex_rx); + _z_mutex_drop(&ztu->_mutex_tx); + _z_mutex_drop(&ztu->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 // Clean up the buffers diff --git a/src/transport/unicast/tx.c b/src/transport/unicast/tx.c index 3eb220e56..78c8bb125 100644 --- a/src/transport/unicast/tx.c +++ b/src/transport/unicast/tx.c @@ -12,7 +12,7 @@ // ZettaScale Zenoh Team, // -#include "zenoh-pico/transport/unicast/tx.h" +#include "zenoh-pico/transport/common/tx.h" #include @@ -20,7 +20,7 @@ #include "zenoh-pico/protocol/codec/network.h" #include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/protocol/iobuf.h" -#include "zenoh-pico/transport/common/tx.h" +#include "zenoh-pico/transport/unicast/tx.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -49,7 +49,7 @@ int8_t _z_unicast_send_t_msg(_z_transport_unicast_t *ztu, const _z_transport_mes #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - z_mutex_lock(&ztu->_mutex_tx); + _z_mutex_lock(&ztu->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 // Prepare the buffer eventually reserving space for the message length @@ -68,7 +68,7 @@ int8_t _z_unicast_send_t_msg(_z_transport_unicast_t *ztu, const _z_transport_mes } #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztu->_mutex_tx); + _z_mutex_unlock(&ztu->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; @@ -85,11 +85,11 @@ int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg _Bool drop = false; if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_lock(&ztu->_mutex_tx); + _z_mutex_lock(&ztu->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 } else { #if Z_FEATURE_MULTI_THREAD == 1 - int8_t locked = z_mutex_trylock(&ztu->_mutex_tx); + int8_t locked = _z_mutex_try_lock(&ztu->_mutex_tx); if (locked != (int8_t)0) { _Z_INFO("Dropping zenoh message because of congestion control"); // We failed to acquire the lock, drop the message @@ -163,7 +163,7 @@ int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg } #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_unlock(&ztu->_mutex_tx); + _z_mutex_unlock(&ztu->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 }