From 2276479f0baa353f261ea98b44413856e5ed98d0 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Mon, 1 Jul 2024 16:52:45 +0200 Subject: [PATCH] Make z_mutex_t owned --- CMakeLists.txt | 1 + examples/unix/c11/z_get.c | 8 ++-- examples/unix/c11/z_get_attachment.c | 8 ++-- examples/unix/c11/z_ping.c | 10 ++--- examples/unix/c99/z_get.c | 8 ++-- examples/unix/c99/z_ping.c | 10 ++--- examples/windows/z_get.c | 8 ++-- examples/windows/z_ping.c | 10 ++--- include/zenoh-pico/api/macros.h | 5 ++- include/zenoh-pico/api/olv_macros.h | 40 +++++++++++++------ include/zenoh-pico/api/primitives.h | 38 +++++++++--------- include/zenoh-pico/collections/fifo_mt.h | 2 +- include/zenoh-pico/collections/ring_mt.h | 2 +- include/zenoh-pico/net/session.h | 2 +- include/zenoh-pico/session/session.h | 2 +- include/zenoh-pico/system/platform-common.h | 17 +++++--- .../system/platform/arduino/esp32.h | 2 +- .../system/platform/arduino/opencr.h | 2 +- .../zenoh-pico/system/platform/emscripten.h | 2 +- include/zenoh-pico/system/platform/espidf.h | 2 +- include/zenoh-pico/system/platform/flipper.h | 2 +- .../system/platform/freertos_plus_tcp.h | 2 +- include/zenoh-pico/system/platform/mbed.h | 2 +- include/zenoh-pico/system/platform/unix.h | 2 +- include/zenoh-pico/system/platform/void.h | 2 +- include/zenoh-pico/system/platform/windows.h | 2 +- include/zenoh-pico/system/platform/zephyr.h | 2 +- include/zenoh-pico/transport/transport.h | 10 ++--- src/collections/fifo_mt.c | 18 ++++----- src/collections/ring_mt.c | 16 ++++---- src/session/utils.c | 6 +-- src/system/arduino/esp32/system.c | 12 +++--- src/system/arduino/opencr/system.c | 12 +++--- src/system/emscripten/system.c | 12 +++--- src/system/espidf/system.c | 12 +++--- src/system/flipper/system.c | 12 +++--- src/system/freertos_plus_tcp/system.c | 12 +++--- src/system/mbed/system.cpp | 12 +++--- src/system/platform-common.c | 18 +++++++++ src/system/unix/system.c | 12 +++--- src/system/windows/system.c | 12 +++--- src/system/zephyr/system.c | 12 +++--- src/transport/multicast/lease.c | 8 ++-- src/transport/multicast/read.c | 4 +- src/transport/multicast/rx.c | 8 ++-- src/transport/multicast/transport.c | 18 ++++----- src/transport/multicast/tx.c | 10 ++--- src/transport/raweth/rx.c | 4 +- src/transport/raweth/tx.c | 14 ++++--- src/transport/unicast/read.c | 4 +- src/transport/unicast/rx.c | 4 +- src/transport/unicast/transport.c | 14 +++---- src/transport/unicast/tx.c | 10 ++--- 53 files changed, 261 insertions(+), 218 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..812e74df5 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -20,7 +20,7 @@ #if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 static z_condvar_t cond; -static z_mutex_t mutex; +static z_owned_mutex_t mutex; void reply_dropper(void *ctx) { (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(&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..15f510aa3 100644 --- a/examples/unix/c11/z_get_attachment.c +++ b/examples/unix/c11/z_get_attachment.c @@ -44,7 +44,7 @@ typedef struct kv_pairs_rx_t { #if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 static z_condvar_t cond; -static z_mutex_t mutex; +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); @@ -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(&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..7b1bac5cc 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -28,7 +28,7 @@ #define DEFAULT_WARMUP_MS 1000 static z_condvar_t cond; -static z_mutex_t mutex; +static z_owned_mutex_t mutex; void callback(const z_loaned_sample_t* sample, void* context) { (void)sample; @@ -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(&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(&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..9d15f2dc6 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -20,7 +20,7 @@ #if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 z_condvar_t cond; -z_mutex_t mutex; +z_owned_mutex_t mutex; void reply_dropper(void *ctx) { (void)(ctx); @@ -117,7 +117,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(&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_session_loan_mut(&s)); diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 26a90bfb7..f25ba53d6 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -29,7 +29,7 @@ #define DEFAULT_WARMUP_MS 1000 static z_condvar_t cond; -static z_mutex_t mutex; +static z_owned_mutex_t mutex; void callback(const z_loaned_sample_t* sample, void* context) { (void)sample; @@ -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_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(&cond, z_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(&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_undeclare_subscriber(z_subscriber_move(&sub)); diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 651e244e4..fe2ca7d7a 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -19,7 +19,7 @@ #if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 z_condvar_t cond; -z_mutex_t mutex; +z_owned_mutex_t mutex; void reply_dropper(void *ctx) { (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(&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..c51f74a8a 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -28,7 +28,7 @@ #define DEFAULT_WARMUP_MS 1000 static z_condvar_t cond; -static z_mutex_t mutex; +static z_owned_mutex_t mutex; void callback(const z_loaned_sample_t* sample, void* context) { (void)sample; @@ -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(&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(&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 06e60cddd..504160ffe 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -53,6 +53,7 @@ z_owned_slice_t : z_slice_loan, \ z_owned_bytes_t : z_bytes_loan, \ z_owned_encoding_t : z_encoding_loan, \ + z_owned_mutex_t : z_mutex_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, \ @@ -78,6 +79,7 @@ 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_mutex_t : z_mutex_loan_mut, \ z_owned_reply_err_t : z_reply_err_loan_mut \ )(&x) /** @@ -108,13 +110,14 @@ 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_mutex_t *: z_mutex_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) /** diff --git a/include/zenoh-pico/api/olv_macros.h b/include/zenoh-pico/api/olv_macros.h index 8146af71c..53c950838 100644 --- a/include/zenoh-pico/api/olv_macros.h +++ b/include/zenoh-pico/api/olv_macros.h @@ -43,19 +43,26 @@ 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_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_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; } \ @@ -114,6 +121,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 6eb9481a8..85f282a5c 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1134,23 +1134,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_DEF(z_loaned_config_t, z_owned_config_t, config) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_scouting_config_t, z_owned_scouting_config_t, scouting_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(string) +_Z_OWNED_FUNCTIONS_DEF(keyexpr) +_Z_OWNED_FUNCTIONS_DEF(config) +_Z_OWNED_FUNCTIONS_DEF(scouting_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_CLOSURE_DEF(z_owned_closure_sample_t, closure_sample) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_query_t, closure_query) @@ -1158,8 +1158,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..ff03dfbe2 100644 --- a/include/zenoh-pico/collections/fifo_mt.h +++ b/include/zenoh-pico/collections/fifo_mt.h @@ -24,7 +24,7 @@ typedef struct { _z_fifo_t _fifo; #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_t _mutex; + z_owned_mutex_t _mutex; z_condvar_t _cv_not_full; z_condvar_t _cv_not_empty; #endif diff --git a/include/zenoh-pico/collections/ring_mt.h b/include/zenoh-pico/collections/ring_mt.h index a52ee62d6..8183cf59d 100644 --- a/include/zenoh-pico/collections/ring_mt.h +++ b/include/zenoh-pico/collections/ring_mt.h @@ -24,7 +24,7 @@ typedef struct { _z_ring_t _ring; #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_t _mutex; + z_owned_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..85986bb62 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_owned_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..4db0fd058 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -138,7 +138,7 @@ _Z_LIST_DEFINE(_z_pending_query, _z_pending_query_t) typedef struct { #if Z_FEATURE_MULTI_THREAD == 1 - z_mutex_t _mutex; + z_owned_mutex_t _mutex; z_condvar_t _cond_var; #endif // Z_FEATURE_MULTI_THREAD == 1 _z_reply_data_list_t *_replies; diff --git a/include/zenoh-pico/system/platform-common.h b/include/zenoh-pico/system/platform-common.h index 081a5a9c4..e2a11f989 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) @@ -68,19 +69,23 @@ int8_t zp_task_cancel(z_task_t *task); void z_task_free(z_task_t **task); /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m); -int8_t z_mutex_free(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_lock(z_mutex_t *m); -int8_t z_mutex_trylock(z_mutex_t *m); -int8_t z_mutex_unlock(z_mutex_t *m); +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_signal(z_condvar_t *cv); -int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m); +int8_t z_condvar_wait(z_condvar_t *cv, z_loaned_mutex_t *m); #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/include/zenoh-pico/system/platform/arduino/esp32.h b/include/zenoh-pico/system/platform/arduino/esp32.h index abdc37f67..5a3866fcf 100644 --- a/include/zenoh-pico/system/platform/arduino/esp32.h +++ b/include/zenoh-pico/system/platform/arduino/esp32.h @@ -26,7 +26,7 @@ #if Z_FEATURE_MULTI_THREAD == 1 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_mutex_t _z_mutex_t; typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/arduino/opencr.h b/include/zenoh-pico/system/platform/arduino/opencr.h index af16ce60b..20c02b0b9 100644 --- a/include/zenoh-pico/system/platform/arduino/opencr.h +++ b/include/zenoh-pico/system/platform/arduino/opencr.h @@ -23,7 +23,7 @@ #if Z_FEATURE_MULTI_THREAD == 1 typedef void *z_task_t; typedef void *z_task_attr_t; -typedef void *z_mutex_t; +typedef void *_z_mutex_t; typedef void *z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/emscripten.h b/include/zenoh-pico/system/platform/emscripten.h index b89921197..94718bf3c 100644 --- a/include/zenoh-pico/system/platform/emscripten.h +++ b/include/zenoh-pico/system/platform/emscripten.h @@ -24,7 +24,7 @@ typedef pthread_t z_task_t; typedef pthread_attr_t z_task_attr_t; -typedef pthread_mutex_t z_mutex_t; +typedef pthread_mutex_t _z_mutex_t; typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/espidf.h b/include/zenoh-pico/system/platform/espidf.h index b7353b561..77fa43067 100644 --- a/include/zenoh-pico/system/platform/espidf.h +++ b/include/zenoh-pico/system/platform/espidf.h @@ -39,7 +39,7 @@ typedef struct { TaskHandle_t handle; EventGroupHandle_t join_event; } z_task_t; -typedef pthread_mutex_t z_mutex_t; +typedef pthread_mutex_t _z_mutex_t; typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/flipper.h b/include/zenoh-pico/system/platform/flipper.h index 124714afe..2b09eda54 100644 --- a/include/zenoh-pico/system/platform/flipper.h +++ b/include/zenoh-pico/system/platform/flipper.h @@ -29,7 +29,7 @@ #if Z_FEATURE_MULTI_THREAD == 1 typedef FuriThread* z_task_t; typedef uint32_t z_task_attr_t; -typedef FuriMutex* z_mutex_t; +typedef FuriMutex* _z_mutex_t; typedef void* z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/freertos_plus_tcp.h b/include/zenoh-pico/system/platform/freertos_plus_tcp.h index f85269dab..2d5d243a1 100644 --- a/include/zenoh-pico/system/platform/freertos_plus_tcp.h +++ b/include/zenoh-pico/system/platform/freertos_plus_tcp.h @@ -35,7 +35,7 @@ typedef struct { EventGroupHandle_t join_event; } z_task_t; -typedef SemaphoreHandle_t z_mutex_t; +typedef SemaphoreHandle_t _z_mutex_t; typedef void *z_condvar_t; #endif // Z_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/mbed.h b/include/zenoh-pico/system/platform/mbed.h index 170b230f3..91c5ea96c 100644 --- a/include/zenoh-pico/system/platform/mbed.h +++ b/include/zenoh-pico/system/platform/mbed.h @@ -25,7 +25,7 @@ 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_attr_t; // Workaround as MBED is a C++ library -typedef void *z_mutex_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 diff --git a/include/zenoh-pico/system/platform/unix.h b/include/zenoh-pico/system/platform/unix.h index 8fd10421e..e9fd0f39f 100644 --- a/include/zenoh-pico/system/platform/unix.h +++ b/include/zenoh-pico/system/platform/unix.h @@ -27,7 +27,7 @@ typedef pthread_t z_task_t; typedef pthread_attr_t z_task_attr_t; -typedef pthread_mutex_t z_mutex_t; +typedef pthread_mutex_t _z_mutex_t; typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/void.h b/include/zenoh-pico/system/platform/void.h index b6fc1dd99..38ae6312a 100644 --- a/include/zenoh-pico/system/platform/void.h +++ b/include/zenoh-pico/system/platform/void.h @@ -20,7 +20,7 @@ #if Z_FEATURE_MULTI_THREAD == 1 typedef void *z_task_t; typedef void *z_task_attr_t; -typedef void *z_mutex_t; +typedef void *_z_mutex_t; typedef void *z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/windows.h b/include/zenoh-pico/system/platform/windows.h index d003be987..c91b21c8e 100644 --- a/include/zenoh-pico/system/platform/windows.h +++ b/include/zenoh-pico/system/platform/windows.h @@ -23,7 +23,7 @@ #if Z_FEATURE_MULTI_THREAD == 1 typedef HANDLE *z_task_t; typedef void *z_task_attr_t; // Not used in Windows -typedef SRWLOCK z_mutex_t; +typedef SRWLOCK _z_mutex_t; typedef CONDITION_VARIABLE z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/zephyr.h b/include/zenoh-pico/system/platform/zephyr.h index da7b41997..086f03bfd 100644 --- a/include/zenoh-pico/system/platform/zephyr.h +++ b/include/zenoh-pico/system/platform/zephyr.h @@ -33,7 +33,7 @@ #if Z_FEATURE_MULTI_THREAD == 1 typedef pthread_t z_task_t; typedef pthread_attr_t z_task_attr_t; -typedef pthread_mutex_t z_mutex_t; +typedef pthread_mutex_t _z_mutex_t; typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index d00c3c8de..a9b35022d 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_owned_mutex_t _mutex_rx; + z_owned_mutex_t _mutex_tx; #endif // Z_FEATURE_MULTI_THREAD == 1 _z_link_t _link; @@ -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_owned_mutex_t _mutex_rx; + z_owned_mutex_t _mutex_tx; // Peer list mutex - z_mutex_t _mutex_peer; + z_owned_mutex_t _mutex_peer; #endif // Z_FEATURE_MULTI_THREAD == 1 _z_link_t _link; diff --git a/src/collections/fifo_mt.c b/src/collections/fifo_mt.c index 072fd6cc2..1068d9be7 100644 --- a/src/collections/fifo_mt.c +++ b/src/collections/fifo_mt.c @@ -50,7 +50,7 @@ _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_mutex_drop(&fifo->_mutex); z_condvar_free(&fifo->_cv_not_full); z_condvar_free(&fifo->_cv_not_empty); #endif @@ -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(z_mutex_loan_mut(&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, z_mutex_loan_mut(&f->_mutex))) } else { _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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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, z_mutex_loan_mut(&f->_mutex))) } else { _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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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_mutex_unlock(&f->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_unlock(z_mutex_loan_mut(&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..d47e8106b 100644 --- a/src/collections/ring_mt.c +++ b/src/collections/ring_mt.c @@ -47,7 +47,7 @@ _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_mutex_drop(&ring->_mutex); z_condvar_free(&ring->_cv_not_empty); #endif @@ -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(z_mutex_loan_mut(&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_mutex_unlock(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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, z_mutex_loan_mut(&r->_mutex))) } } - _Z_RETURN_IF_ERR(z_mutex_unlock(&r->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_unlock(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&r->_mutex))) #endif if (src != NULL) { diff --git a/src/session/utils.c b/src/session/utils.c index 556694bb8..1f7793a76 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -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(z_mutex_loan_mut(&zn->_mutex_inner)); } +void _zp_session_unlock_mutex(_z_session_t *zn) { (void)z_mutex_unlock(z_mutex_loan_mut(&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..00c109b97 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -94,15 +94,15 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, NULL); } +int8_t z_mutex_init(z_owned_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_owned_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_loaned_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_loaned_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_loaned_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, NULL); } @@ -111,7 +111,7 @@ 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_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_loaned_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..2a03f4cb0 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -76,15 +76,15 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { return -1; } +int8_t z_mutex_init(z_owned_mutex_t *m) { return -1; } -int8_t z_mutex_free(z_mutex_t *m) { return -1; } +int8_t z_mutex_drop(z_owned_mutex_t *m) { return -1; } -int8_t z_mutex_lock(z_mutex_t *m) { return -1; } +int8_t z_mutex_lock(z_loaned_mutex_t *m) { return -1; } -int8_t z_mutex_trylock(z_mutex_t *m) { return -1; } +int8_t z_mutex_try_lock(z_loaned_mutex_t *m) { return -1; } -int8_t z_mutex_unlock(z_mutex_t *m) { return -1; } +int8_t z_mutex_unlock(z_loaned_mutex_t *m) { return -1; } /*------------------ Condvar ------------------*/ int8_t z_condvar_init(z_condvar_t *cv) { return -1; } @@ -93,7 +93,7 @@ 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_wait(z_condvar_t *cv, z_loaned_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..0377b4bb9 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -59,15 +59,15 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_owned_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_owned_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_loaned_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_loaned_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_loaned_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, 0); } @@ -76,7 +76,7 @@ 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_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_loaned_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..7e3e3a05d 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -126,15 +126,15 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, NULL); } +int8_t z_mutex_init(z_owned_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_owned_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_loaned_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_loaned_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_loaned_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, NULL); } @@ -143,7 +143,7 @@ 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_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_loaned_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..8b0229545 100644 --- a/src/system/flipper/system.c +++ b/src/system/flipper/system.c @@ -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_owned_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_owned_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_loaned_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_loaned_mutex_t* m) { return -1; } -int8_t z_mutex_unlock(z_mutex_t* m) { +int8_t z_mutex_unlock(z_loaned_mutex_t* m) { if (m == NULL) { return -1; } @@ -147,7 +147,7 @@ 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_wait(z_condvar_t* cv, z_loaned_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..115b41fb0 100644 --- a/src/system/freertos_plus_tcp/system.c +++ b/src/system/freertos_plus_tcp/system.c @@ -135,28 +135,28 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { +int8_t z_mutex_init(z_owned_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_owned_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_loaned_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_loaned_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_loaned_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_wait(z_condvar_t *cv, z_loaned_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..95942a48c 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -66,24 +66,24 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { +int8_t z_mutex_init(z_owned_mutex_t *m) { *m = new Mutex(); return 0; } -int8_t z_mutex_free(z_mutex_t *m) { +int8_t z_mutex_drop(z_owned_mutex_t *m) { delete ((Mutex *)*m); return 0; } -int8_t z_mutex_lock(z_mutex_t *m) { +int8_t z_mutex_lock(z_loaned_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_loaned_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_loaned_mutex_t *m) { ((Mutex *)*m)->unlock(); return 0; } @@ -101,7 +101,7 @@ int8_t z_condvar_signal(z_condvar_t *cv) { 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_loaned_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..94d4e809b --- /dev/null +++ b/src/system/platform-common.c @@ -0,0 +1,18 @@ +// +// 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" + +_Z_OWNED_FUNCTIONS_SYSTEM_IMPL(_z_mutex_t, mutex) diff --git a/src/system/unix/system.c b/src/system/unix/system.c index 49857ac7e..e5e27fa11 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -116,15 +116,15 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { return (int8_t)pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_owned_mutex_t *m) { return (int8_t)pthread_mutex_init(&m->_val, 0); } -int8_t z_mutex_free(z_mutex_t *m) { return (int8_t)pthread_mutex_destroy(m); } +int8_t z_mutex_drop(z_owned_mutex_t *m) { return (int8_t)pthread_mutex_destroy(&m->_val); } -int8_t z_mutex_lock(z_mutex_t *m) { return (int8_t)pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_loaned_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_loaned_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_loaned_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); } @@ -133,7 +133,7 @@ int8_t z_condvar_free(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_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_loaned_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..64a12e1d2 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -90,25 +90,25 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { +int8_t z_mutex_init(z_owned_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_owned_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_loaned_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_loaned_mutex_t *m) { int8_t ret = _Z_RES_OK; if (TryAcquireSRWLockExclusive(m) == 0) { ret = _Z_ERR_GENERIC; @@ -116,7 +116,7 @@ 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_loaned_mutex_t *m) { int8_t ret = _Z_RES_OK; ReleaseSRWLockExclusive(m); return ret; @@ -141,7 +141,7 @@ int8_t z_condvar_signal(z_condvar_t *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_loaned_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..62f003dc6 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -94,15 +94,15 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_owned_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_owned_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_loaned_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_loaned_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_loaned_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, 0); } @@ -111,7 +111,7 @@ 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_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_loaned_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..5c583a954 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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&ztm->_mutex_peer)); it = ztm->_peers; while (it != NULL) { @@ -181,7 +181,7 @@ 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(z_mutex_loan_mut(&ztm->_mutex_peer)); } return 0; } diff --git a/src/transport/multicast/read.c b/src/transport/multicast/read.c index b4de50730..1f01a13d5 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(z_mutex_loan_mut(&ztm->_mutex_rx)); // Prepare the buffer _z_zbuf_reset(&ztm->_zbuf); @@ -123,7 +123,7 @@ 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(z_mutex_loan_mut(&ztm->_mutex_rx)); return NULL; } diff --git a/src/transport/multicast/rx.c b/src/transport/multicast/rx.c index 6e9c5a098..57d86d2e9 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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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..a0cf56d89 100644 --- a/src/transport/multicast/transport.c +++ b/src/transport/multicast/transport.c @@ -58,11 +58,11 @@ int8_t _z_multicast_transport_create(_z_transport_t *zt, _z_link_t *zl, if (ret == _Z_RES_OK) { 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); @@ -192,9 +192,9 @@ void _z_multicast_transport_clear(_z_transport_t *zt) { z_task_free(&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..d01011aa3 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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&ztm->_mutex_tx)); #endif // Z_FEATURE_MULTI_THREAD == 1 } diff --git a/src/transport/raweth/rx.c b/src/transport/raweth/rx.c index dfba004a6..5db96ae55 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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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..0a3b38f43 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -31,7 +31,9 @@ #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(z_mutex_loan_mut(&ztm->_mutex_tx)); +} #else static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { _ZP_UNUSED(ztm); } #endif @@ -196,7 +198,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(z_mutex_loan_mut(&ztm->_mutex_tx)); #endif // Reset wbuf _z_wbuf_reset(&ztm->_wbuf); @@ -214,7 +216,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(z_mutex_loan_mut(&ztm->_mutex_tx)); #endif return ret; @@ -229,9 +231,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(z_mutex_loan_mut(&ztm->_mutex_tx)); } else { - if (z_mutex_trylock(&ztm->_mutex_tx) != (int8_t)0) { + if (z_mutex_try_lock(z_mutex_loan_mut(&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 +316,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(z_mutex_loan_mut(&ztm->_mutex_tx)); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; } diff --git a/src/transport/unicast/read.c b/src/transport/unicast/read.c index eb19fca2c..22928a603 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(z_mutex_loan_mut(&ztu->_mutex_rx)); // Prepare the buffer _z_zbuf_reset(&ztu->_zbuf); @@ -116,7 +116,7 @@ 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(z_mutex_loan_mut(&ztu->_mutex_rx)); return NULL; } diff --git a/src/transport/unicast/rx.c b/src/transport/unicast/rx.c index d8049f79d..c3e6218ba 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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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..da26720aa 100644 --- a/src/transport/unicast/transport.c +++ b/src/transport/unicast/transport.c @@ -42,7 +42,7 @@ int8_t _z_unicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _z_transpo if (ret == _Z_RES_OK) { 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); @@ -299,8 +299,8 @@ void _z_unicast_transport_clear(_z_transport_t *zt) { } // 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..64ad74277 100644 --- a/src/transport/unicast/tx.c +++ b/src/transport/unicast/tx.c @@ -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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&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(z_mutex_loan_mut(&ztu->_mutex_tx)); #endif // Z_FEATURE_MULTI_THREAD == 1 }