diff --git a/docs/api.rst b/docs/api.rst index a970ebdef..d14991a2d 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -244,12 +244,45 @@ It is guaranteed that: - ``drop`` will only be called **once**, and **after every** ``call`` has ended. - The two previous guarantees imply that ``call`` and ``drop`` are never called concurrently. +Represents a `sample` closure. -.. autoctype:: types.h::z_owned_closure_sample_t -.. autoctype:: types.h::z_owned_closure_query_t -.. autoctype:: types.h::z_owned_closure_reply_t -.. autoctype:: types.h::z_owned_closure_hello_t -.. autoctype:: types.h::z_owned_closure_zid_t +.. c:type:: types.h::z_owned_closure_sample_t + +Represents a loaned `sample` closure. + +.. c:type:: types.h::z_loaned_closure_sample_t + +Represents a `query` closure. + +.. c:type:: types.h::z_owned_closure_query_t + +Represents a loaned `query` closure. + +.. c:type:: types.h::z_loaned_closure_query_t + +Represents a `reply` closure. + +.. c:type:: types.h::z_owned_closure_reply_t + +Represents a loaned `reply` closure. + +.. c:type:: types.h::z_loaned_closure_reply_t + +Represents a `hello` closure. + +.. c:type:: types.h::z_owned_closure_hello_t + +Represents a loaned `hello` closure. + +.. c:type:: types.h::z_loaned_closure_hello_t + +Represents a `Zenoh id` closure. + +.. c:type:: types.h::z_owned_closure_zid_t + +Represents a loaned `Zenoh id` closure. + +.. c:type:: types.h::z_loaned_closure_zid_t Zenoh Functions diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 06e60cddd..3d2f267d8 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -59,7 +59,12 @@ z_owned_ring_handler_query_t : z_ring_handler_query_loan, \ z_owned_ring_handler_reply_t : z_ring_handler_reply_loan, \ z_owned_ring_handler_sample_t : z_ring_handler_sample_loan, \ - z_owned_reply_err_t : z_reply_err_loan \ + z_owned_reply_err_t : z_reply_err_loan, \ + z_owned_closure_sample_t : z_closure_sample_loan, \ + z_owned_closure_reply_t : z_closure_reply_loan, \ + z_owned_closure_query_t : z_closure_query_loan, \ + z_owned_closure_hello_t : z_closure_hello_loan, \ + z_owned_closure_zid_t : z_closure_zid_loan \ )(&x) #define z_loan_mut(x) _Generic((x), \ @@ -155,11 +160,11 @@ * x: The closure to call */ #define z_call(x, ...) \ - _Generic((x), z_owned_closure_sample_t : z_closure_sample_call, \ - z_owned_closure_query_t : z_closure_query_call, \ - z_owned_closure_reply_t : z_closure_reply_call, \ - z_owned_closure_hello_t : z_closure_hello_call, \ - z_owned_closure_zid_t : z_closure_zid_call \ + _Generic((x), z_loaned_closure_sample_t : z_closure_sample_call, \ + z_loaned_closure_query_t : z_closure_query_call, \ + z_loaned_closure_reply_t : z_closure_reply_call, \ + z_loaned_closure_hello_t : z_closure_hello_call, \ + z_loaned_closure_zid_t : z_closure_zid_call \ ) (&x, __VA_ARGS__) #define z_try_recv(x, ...) \ @@ -283,9 +288,9 @@ #define _z_closure_overloader(closure, callback, dropper, ctx, ...) \ do { \ - (closure)->call = callback; \ - (closure)->drop = dropper; \ - (closure)->context = ctx; \ + (closure)->_val.call = callback; \ + (closure)->_val.drop = dropper; \ + (closure)->_val.context = ctx; \ } while (0); /** @@ -326,7 +331,11 @@ inline const z_loaned_slice_t* z_loan(const z_owned_slice_t& x) { return z_slice 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_reply_err_t* z_loan(const z_owned_reply_err_t& x) { return z_reply_err_loan(&x); } - +inline const z_loaned_closure_sample_t* z_loan(const z_owned_closure_sample_t& x) { return z_closure_sample_loan(&x); } +inline const z_loaned_closure_reply_t* z_loan(const z_owned_closure_reply_t& x) { return z_closure_reply_loan(&x); } +inline const z_loaned_closure_query_t* z_loan(const z_owned_closure_query_t& x) { return z_closure_query_loan(&x); } +inline const z_loaned_closure_hello_t* z_loan(const z_owned_closure_hello_t& x) { return z_closure_hello_loan(&x); } +inline const z_loaned_closure_zid_t* z_loan(const z_owned_closure_zid_t& x) { return z_closure_zid_loan(&x); } // z_loan_mut definition inline z_loaned_keyexpr_t* z_loan_mut(z_owned_keyexpr_t& x) { return z_keyexpr_loan_mut(&x); } inline z_loaned_keyexpr_t* z_loan_mut(z_view_keyexpr_t& x) { return z_view_keyexpr_loan_mut(&x); } @@ -418,15 +427,15 @@ inline bool z_check(const z_owned_reply_err_t& v) { return z_reply_err_check(&v) // z_call definition -inline void z_call(const z_owned_closure_sample_t &closure, const z_loaned_sample_t *sample) +inline void z_call(const z_loaned_closure_sample_t &closure, const z_loaned_sample_t *sample) { z_closure_sample_call(&closure, sample); } -inline void z_call(const z_owned_closure_query_t &closure, const z_loaned_query_t *query) +inline void z_call(const z_loaned_closure_query_t &closure, const z_loaned_query_t *query) { z_closure_query_call(&closure, query); } -inline void z_call(const z_owned_closure_reply_t &closure, const z_loaned_reply_t *reply) +inline void z_call(const z_loaned_closure_reply_t &closure, const z_loaned_reply_t *reply) { z_closure_reply_call(&closure, reply); } -inline void z_call(const z_owned_closure_hello_t &closure, const z_loaned_hello_t *hello) +inline void z_call(const z_loaned_closure_hello_t &closure, const z_loaned_hello_t *hello) { z_closure_hello_call(&closure, hello); } -inline void z_call(const z_owned_closure_zid_t &closure, const z_id_t *zid) +inline void z_call(const z_loaned_closure_zid_t &closure, const z_id_t *zid) { z_closure_zid_call(&closure, zid); } inline void z_closure( @@ -434,45 +443,45 @@ inline void z_closure( void (*call)(const z_loaned_hello_t*, void*), void (*drop)(void*) = NULL, void *context = NULL) { - closure->context = context; - closure->drop = drop; - closure->call = call; + closure->_val.context = context; + closure->_val.drop = drop; + closure->_val.call = call; }; inline void z_closure( z_owned_closure_query_t* closure, void (*call)(const z_loaned_query_t*, void*), void (*drop)(void*) = NULL, void *context = NULL) { - closure->context = context; - closure->drop = drop; - closure->call = call; + closure->_val.context = context; + closure->_val.drop = drop; + closure->_val.call = call; }; inline void z_closure( z_owned_closure_reply_t* closure, void (*call)(const z_loaned_reply_t*, void*), void (*drop)(void*) = NULL, void *context = NULL) { - closure->context = context; - closure->drop = drop; - closure->call = call; + closure->_val.context = context; + closure->_val.drop = drop; + closure->_val.call = call; }; inline void z_closure( z_owned_closure_sample_t* closure, void (*call)(const z_loaned_sample_t*, void*), void (*drop)(void*) = NULL, void *context = NULL) { - closure->context = context; - closure->drop = drop; - closure->call = call; + closure->_val.context = context; + closure->_val.drop = drop; + closure->_val.call = call; }; inline void z_closure( z_owned_closure_zid_t* closure, void (*call)(const z_id_t*, void*), void (*drop)(void*) = NULL, void *context = NULL) { - closure->context = context; - closure->drop = drop; - closure->call = call; + closure->_val.context = context; + closure->_val.drop = drop; + closure->_val.call = call; }; inline bool z_try_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) { @@ -711,6 +720,46 @@ template <> struct z_owned_to_loaned_type_t { typedef z_loaned_subscriber_t type; }; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_sample_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_sample_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_reply_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_reply_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_query_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_query_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_hello_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_hello_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_zid_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_zid_t type; +}; #endif diff --git a/include/zenoh-pico/api/olv_macros.h b/include/zenoh-pico/api/olv_macros.h index 8146af71c..b71165ee3 100644 --- a/include/zenoh-pico/api/olv_macros.h +++ b/include/zenoh-pico/api/olv_macros.h @@ -118,34 +118,36 @@ 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; } -#define _Z_OWNED_FUNCTIONS_CLOSURE_DEF(ownedtype, name) \ - _Bool z_##name##_check(const ownedtype *val); \ - ownedtype *z_##name##_move(ownedtype *val); \ - void z_##name##_drop(ownedtype *val); \ - void z_##name##_null(ownedtype *name); - -#define _Z_OWNED_FUNCTIONS_CLOSURE_IMPL(ownedtype, name, f_call, f_drop) \ - _Bool z_##name##_check(const ownedtype *val) { return val->call != NULL; } \ - ownedtype *z_##name##_move(ownedtype *val) { return val; } \ - void z_##name##_drop(ownedtype *val) { \ - if (val->drop != NULL) { \ - (val->drop)(val->context); \ - val->drop = NULL; \ - } \ - val->call = NULL; \ - val->context = NULL; \ - } \ - void z_##name##_null(ownedtype *val) { \ - val->call = NULL; \ - val->drop = NULL; \ - val->context = NULL; \ - } \ - int8_t z_##name(ownedtype *closure, f_call call, f_drop drop, void *context) { \ - closure->call = call; \ - closure->drop = drop; \ - closure->context = context; \ - \ - return _Z_RES_OK; \ +#define _Z_OWNED_FUNCTIONS_CLOSURE_DEF(name) \ + _Bool z_##name##_check(const z_owned_##name##_t *val); \ + z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val); \ + void z_##name##_drop(z_owned_##name##_t *val); \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *val); \ + void z_##name##_null(z_owned_##name##_t *name); + +#define _Z_OWNED_FUNCTIONS_CLOSURE_IMPL(name, f_call, f_drop) \ + _Bool z_##name##_check(const z_owned_##name##_t *val) { return val->_val.call != NULL; } \ + z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val) { return val; } \ + void z_##name##_drop(z_owned_##name##_t *val) { \ + if (val->_val.drop != NULL) { \ + (val->_val.drop)(val->_val.context); \ + val->_val.drop = NULL; \ + } \ + val->_val.call = NULL; \ + val->_val.context = NULL; \ + } \ + void z_##name##_null(z_owned_##name##_t *val) { \ + val->_val.call = NULL; \ + val->_val.drop = NULL; \ + val->_val.context = NULL; \ + } \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *val) { return &val->_val; } \ + int8_t z_##name(z_owned_##name##_t *closure, f_call call, f_drop drop, void *context) { \ + closure->_val.call = call; \ + closure->_val.drop = drop; \ + closure->_val.context = context; \ + \ + return _Z_RES_OK; \ } // Gets internal value from refcounted type (e.g. z_loaned_session_t, z_query_t) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 6eb9481a8..2c3ae2d08 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1152,11 +1152,11 @@ _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_CLOSURE_DEF(z_owned_closure_sample_t, closure_sample) -_Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_query_t, closure_query) -_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_OWNED_FUNCTIONS_CLOSURE_DEF(closure_sample) +_Z_OWNED_FUNCTIONS_CLOSURE_DEF(closure_query) +_Z_OWNED_FUNCTIONS_CLOSURE_DEF(closure_reply) +_Z_OWNED_FUNCTIONS_CLOSURE_DEF(closure_hello) +_Z_OWNED_FUNCTIONS_CLOSURE_DEF(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) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 5458c2de0..179bfa0a4 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -456,6 +456,11 @@ _Bool z_string_array_is_empty(const z_loaned_string_array_t *a); typedef void (*z_dropper_handler_t)(void *arg); typedef _z_data_handler_t z_data_handler_t; +typedef struct { + void *context; + z_data_handler_t call; + z_dropper_handler_t drop; +} _z_closure_sample_t; /** * Represents the sample closure. * @@ -466,16 +471,18 @@ typedef _z_data_handler_t z_data_handler_t; * z_data_handler_t call: `void *call(const struct z_sample_t*, const void *context)` is the callback function. * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. */ -typedef struct { - void *context; - z_data_handler_t call; - z_dropper_handler_t drop; -} z_owned_closure_sample_t; +_Z_OWNED_TYPE_VALUE(_z_closure_sample_t, closure_sample) +_Z_LOANED_TYPE(_z_closure_sample_t, closure_sample) -void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_loaned_sample_t *sample); +void z_closure_sample_call(const z_loaned_closure_sample_t *closure, const z_loaned_sample_t *sample); typedef _z_queryable_handler_t z_queryable_handler_t; +typedef struct { + void *context; + z_queryable_handler_t call; + z_dropper_handler_t drop; +} _z_closure_query_t; /** * Represents the query callback closure. * @@ -488,16 +495,18 @@ typedef _z_queryable_handler_t z_queryable_handler_t; * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -typedef struct { - void *context; - z_queryable_handler_t call; - z_dropper_handler_t drop; -} z_owned_closure_query_t; +_Z_OWNED_TYPE_VALUE(_z_closure_query_t, closure_query) +_Z_LOANED_TYPE(_z_closure_query_t, closure_query) -void z_closure_query_call(const z_owned_closure_query_t *closure, const z_loaned_query_t *query); +void z_closure_query_call(const z_loaned_closure_query_t *closure, const z_loaned_query_t *query); typedef _z_reply_handler_t z_reply_handler_t; +typedef struct { + void *context; + z_reply_handler_t call; + z_dropper_handler_t drop; +} _z_closure_reply_t; /** * Represents the query reply callback closure. * @@ -510,16 +519,18 @@ typedef _z_reply_handler_t z_reply_handler_t; * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -typedef struct { - void *context; - z_reply_handler_t call; - z_dropper_handler_t drop; -} z_owned_closure_reply_t; +_Z_OWNED_TYPE_VALUE(_z_closure_reply_t, closure_reply) +_Z_LOANED_TYPE(_z_closure_reply_t, closure_reply) -void z_closure_reply_call(const z_owned_closure_reply_t *closure, const z_loaned_reply_t *reply); +void z_closure_reply_call(const z_loaned_closure_reply_t *closure, const z_loaned_reply_t *reply); typedef void (*z_loaned_hello_handler_t)(const z_loaned_hello_t *hello, void *arg); +typedef struct { + void *context; + z_loaned_hello_handler_t call; + z_dropper_handler_t drop; +} _z_closure_hello_t; /** * Represents the Zenoh ID callback closure. * @@ -532,16 +543,18 @@ typedef void (*z_loaned_hello_handler_t)(const z_loaned_hello_t *hello, void *ar * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -typedef struct { - void *context; - z_loaned_hello_handler_t call; - z_dropper_handler_t drop; -} z_owned_closure_hello_t; +_Z_OWNED_TYPE_VALUE(_z_closure_hello_t, closure_hello) +_Z_LOANED_TYPE(_z_closure_hello_t, closure_hello) -void z_closure_hello_call(const z_owned_closure_hello_t *closure, const z_loaned_hello_t *hello); +void z_closure_hello_call(const z_loaned_closure_hello_t *closure, const z_loaned_hello_t *hello); typedef void (*z_id_handler_t)(const z_id_t *id, void *arg); +typedef struct { + void *context; + z_id_handler_t call; + z_dropper_handler_t drop; +} _z_closure_zid_t; /** * Represents the Zenoh ID callback closure. * @@ -553,13 +566,10 @@ typedef void (*z_id_handler_t)(const z_id_t *id, void *arg); * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -typedef struct { - void *context; - z_id_handler_t call; - z_dropper_handler_t drop; -} z_owned_closure_zid_t; +_Z_OWNED_TYPE_VALUE(_z_closure_zid_t, closure_zid) +_Z_LOANED_TYPE(_z_closure_zid_t, closure_zid) -void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id); +void z_closure_zid_call(const z_loaned_closure_zid_t *closure, const z_id_t *id); #ifdef __cplusplus } diff --git a/include/zenoh-pico/transport/multicast.h b/include/zenoh-pico/transport/multicast.h index fa1c873b6..487ef6dae 100644 --- a/include/zenoh-pico/transport/multicast.h +++ b/include/zenoh-pico/transport/multicast.h @@ -17,7 +17,7 @@ #include "zenoh-pico/api/types.h" -void _zp_multicast_fetch_zid(const _z_transport_t *zt, z_owned_closure_zid_t *callback); +void _zp_multicast_fetch_zid(const _z_transport_t *zt, _z_closure_zid_t *callback); void _zp_multicast_info_session(const _z_transport_t *zt, _z_config_t *ps); #endif /* ZENOH_PICO_MULTICAST_H */ diff --git a/include/zenoh-pico/transport/unicast.h b/include/zenoh-pico/transport/unicast.h index bcfbcc1ef..652384e47 100644 --- a/include/zenoh-pico/transport/unicast.h +++ b/include/zenoh-pico/transport/unicast.h @@ -17,7 +17,7 @@ #include "zenoh-pico/api/types.h" -void _zp_unicast_fetch_zid(const _z_transport_t *zt, z_owned_closure_zid_t *callback); +void _zp_unicast_fetch_zid(const _z_transport_t *zt, _z_closure_zid_t *callback); void _zp_unicast_info_session(const _z_transport_t *zt, _z_config_t *ps); #endif /* ZENOH_PICO_UNICAST_H */ diff --git a/src/api/api.c b/src/api/api.c index 179606460..b68ed4976 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -641,31 +641,31 @@ const z_loaned_keyexpr_t *z_query_keyexpr(const z_loaned_query_t *query) { retur const z_loaned_bytes_t *z_query_payload(const z_loaned_query_t *query) { return &query->in->val._value.payload; } const z_loaned_encoding_t *z_query_encoding(const z_loaned_query_t *query) { return &query->in->val._value.encoding; } -void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_loaned_sample_t *sample) { +void z_closure_sample_call(const z_loaned_closure_sample_t *closure, const z_loaned_sample_t *sample) { if (closure->call != NULL) { (closure->call)(sample, closure->context); } } -void z_closure_query_call(const z_owned_closure_query_t *closure, const z_loaned_query_t *query) { +void z_closure_query_call(const z_loaned_closure_query_t *closure, const z_loaned_query_t *query) { if (closure->call != NULL) { (closure->call)(query, closure->context); } } -void z_closure_reply_call(const z_owned_closure_reply_t *closure, const z_loaned_reply_t *reply) { +void z_closure_reply_call(const z_loaned_closure_reply_t *closure, const z_loaned_reply_t *reply) { if (closure->call != NULL) { (closure->call)(reply, closure->context); } } -void z_closure_hello_call(const z_owned_closure_hello_t *closure, const z_loaned_hello_t *hello) { +void z_closure_hello_call(const z_loaned_closure_hello_t *closure, const z_loaned_hello_t *hello) { if (closure->call != NULL) { (closure->call)(hello, closure->context); } } -void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) { +void z_closure_zid_call(const z_loaned_closure_zid_t *closure, const z_id_t *id) { if (closure->call != NULL) { (closure->call)(id, closure->context); } @@ -737,11 +737,11 @@ static _z_encoding_t _z_encoding_from_owned(const z_owned_encoding_t *encoding) _Z_OWNED_FUNCTIONS_RC_IMPL(sample) _Z_OWNED_FUNCTIONS_RC_IMPL(session) -_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(z_owned_closure_sample_t, closure_sample, _z_data_handler_t, z_dropper_handler_t) -_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(z_owned_closure_query_t, closure_query, _z_queryable_handler_t, z_dropper_handler_t) -_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(z_owned_closure_reply_t, closure_reply, _z_reply_handler_t, z_dropper_handler_t) -_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(z_owned_closure_hello_t, closure_hello, z_loaned_hello_handler_t, z_dropper_handler_t) -_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(z_owned_closure_zid_t, closure_zid, z_id_handler_t, z_dropper_handler_t) +_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_sample, _z_data_handler_t, z_dropper_handler_t) +_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_query, _z_queryable_handler_t, z_dropper_handler_t) +_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_reply, _z_reply_handler_t, z_dropper_handler_t) +_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_hello, z_loaned_hello_handler_t, z_dropper_handler_t) +_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_zid, z_id_handler_t, z_dropper_handler_t) /************* Primitives **************/ typedef struct __z_hello_handler_wrapper_t { @@ -756,15 +756,15 @@ void __z_hello_handler(_z_hello_t *hello, __z_hello_handler_wrapper_t *wrapped_c int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callback) { int8_t ret = _Z_RES_OK; - void *ctx = callback->context; - callback->context = NULL; + void *ctx = callback->_val.context; + callback->_val.context = NULL; // TODO[API-NET]: When API and NET are a single layer, there is no wrap the user callback and args // to enclose the z_reply_t into a z_owned_reply_t. __z_hello_handler_wrapper_t *wrapped_ctx = (__z_hello_handler_wrapper_t *)z_malloc(sizeof(__z_hello_handler_wrapper_t)); if (wrapped_ctx != NULL) { - wrapped_ctx->user_call = callback->call; + wrapped_ctx->user_call = callback->_val.call; wrapped_ctx->ctx = ctx; char *opt_as_str = _z_config_get(config->_val, Z_CONFIG_SCOUTING_WHAT_KEY); @@ -791,13 +791,14 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb _z_uuid_to_bytes(zid.id, zid_str); } - _z_scout(what, zid, mcast_locator, timeout, __z_hello_handler, wrapped_ctx, callback->drop, ctx); + _z_scout(what, zid, mcast_locator, timeout, __z_hello_handler, wrapped_ctx, callback->_val.drop, ctx); z_free(wrapped_ctx); z_scouting_config_drop(config); } else { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } + z_closure_hello_null(callback); return ret; } @@ -838,18 +839,19 @@ int8_t z_info_peers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *cal switch (_Z_RC_IN_VAL(zs)._tp._type) { case _Z_TRANSPORT_MULTICAST_TYPE: case _Z_TRANSPORT_RAWETH_TYPE: - _zp_multicast_fetch_zid(&(_Z_RC_IN_VAL(zs)._tp), callback); + _zp_multicast_fetch_zid(&(_Z_RC_IN_VAL(zs)._tp), &callback->_val); break; default: break; } // Note and clear context - void *ctx = callback->context; - callback->context = NULL; + void *ctx = callback->_val.context; + callback->_val.context = NULL; // Drop if needed - if (callback->drop != NULL) { - callback->drop(ctx); + if (callback->_val.drop != NULL) { + callback->_val.drop(ctx); } + z_closure_zid_null(callback); return 0; } @@ -857,18 +859,19 @@ int8_t z_info_routers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *c // Call transport function switch (_Z_RC_IN_VAL(zs)._tp._type) { case _Z_TRANSPORT_UNICAST_TYPE: - _zp_unicast_fetch_zid(&(_Z_RC_IN_VAL(zs)._tp), callback); + _zp_unicast_fetch_zid(&(_Z_RC_IN_VAL(zs)._tp), &callback->_val); break; default: break; } // Note and clear context - void *ctx = callback->context; - callback->context = NULL; + void *ctx = callback->_val.context; + callback->_val.context = NULL; // Drop if needed - if (callback->drop != NULL) { - callback->drop(ctx); + if (callback->_val.drop != NULL) { + callback->_val.drop(ctx); } + z_closure_zid_null(callback); return 0; } @@ -1071,8 +1074,8 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co z_owned_closure_reply_t *callback, z_get_options_t *options) { int8_t ret = _Z_RES_OK; - void *ctx = callback->context; - callback->context = NULL; + void *ctx = callback->_val.context; + callback->_val.context = NULL; z_get_options_t opt; z_get_options_default(&opt); @@ -1096,14 +1099,16 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co _z_value_t value = {.payload = _z_bytes_from_owned_bytes(opt.payload), .encoding = _z_encoding_from_owned(opt.encoding)}; - ret = _z_query(&_Z_RC_IN_VAL(zs), *keyexpr, parameters, opt.target, opt.consolidation.mode, value, callback->call, - callback->drop, ctx, opt.timeout_ms, _z_bytes_from_owned_bytes(opt.attachment)); + ret = _z_query(&_Z_RC_IN_VAL(zs), *keyexpr, parameters, opt.target, opt.consolidation.mode, value, + callback->_val.call, callback->_val.drop, ctx, opt.timeout_ms, + _z_bytes_from_owned_bytes(opt.attachment)); if (opt.payload != NULL) { z_bytes_drop(opt.payload); } // Clean-up z_encoding_drop(opt.encoding); z_bytes_drop(opt.attachment); + z_closure_reply_null(callback); return ret; } @@ -1139,8 +1144,8 @@ void z_queryable_options_default(z_queryable_options_t *options) { options->comp int8_t z_declare_queryable(z_owned_queryable_t *queryable, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_closure_query_t *callback, const z_queryable_options_t *options) { - void *ctx = callback->context; - callback->context = NULL; + void *ctx = callback->_val.context; + callback->_val.context = NULL; _z_keyexpr_t key = *keyexpr; @@ -1161,8 +1166,9 @@ int8_t z_declare_queryable(z_owned_queryable_t *queryable, const z_loaned_sessio opt.complete = options->complete; } - queryable->_val = _z_declare_queryable(zs, key, opt.complete, callback->call, callback->drop, ctx); + queryable->_val = _z_declare_queryable(zs, key, opt.complete, callback->_val.call, callback->_val.drop, ctx); + z_closure_query_null(callback); return _Z_RES_OK; } @@ -1249,8 +1255,8 @@ void z_subscriber_options_default(z_subscriber_options_t *options) { options->re int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_closure_sample_t *callback, const z_subscriber_options_t *options) { - void *ctx = callback->context; - callback->context = NULL; + void *ctx = callback->_val.context; + callback->_val.context = NULL; char *suffix = NULL; _z_keyexpr_t key = *keyexpr; @@ -1288,11 +1294,11 @@ int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t if (options != NULL) { subinfo.reliability = options->reliability; } - _z_subscriber_t *int_sub = _z_declare_subscriber(zs, key, subinfo, callback->call, callback->drop, ctx); + _z_subscriber_t *int_sub = _z_declare_subscriber(zs, key, subinfo, callback->_val.call, callback->_val.drop, ctx); if (suffix != NULL) { z_free(suffix); } - + z_closure_sample_null(callback); sub->_val = int_sub; if (int_sub == NULL) { diff --git a/src/transport/multicast.c b/src/transport/multicast.c index de047590e..55b63c7df 100644 --- a/src/transport/multicast.c +++ b/src/transport/multicast.c @@ -30,7 +30,7 @@ #include "zenoh-pico/utils/logging.h" #if Z_FEATURE_MULTICAST_TRANSPORT == 1 -void _zp_multicast_fetch_zid(const _z_transport_t *zt, z_owned_closure_zid_t *callback) { +void _zp_multicast_fetch_zid(const _z_transport_t *zt, _z_closure_zid_t *callback) { void *ctx = callback->context; _z_transport_peer_entry_list_t *l = zt->_transport._multicast._peers; for (; l != NULL; l = _z_transport_peer_entry_list_tail(l)) { @@ -55,7 +55,7 @@ void _zp_multicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { } #else -void _zp_multicast_fetch_zid(const _z_transport_t *zt, z_owned_closure_zid_t *callback) { +void _zp_multicast_fetch_zid(const _z_transport_t *zt, _z_closure_zid_t *callback) { _ZP_UNUSED(zt); _ZP_UNUSED(callback); } diff --git a/src/transport/unicast.c b/src/transport/unicast.c index db869a0b5..96b1256a5 100644 --- a/src/transport/unicast.c +++ b/src/transport/unicast.c @@ -31,7 +31,7 @@ #include "zenoh-pico/utils/logging.h" #if Z_FEATURE_UNICAST_TRANSPORT == 1 -void _zp_unicast_fetch_zid(const _z_transport_t *zt, z_owned_closure_zid_t *callback) { +void _zp_unicast_fetch_zid(const _z_transport_t *zt, _z_closure_zid_t *callback) { void *ctx = callback->context; z_id_t id = zt->_transport._unicast._remote_zid; callback->call(&id, ctx); @@ -46,7 +46,7 @@ void _zp_unicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { } #else -void _zp_unicast_fetch_zid(const _z_transport_t *zt, z_owned_closure_zid_t *callback) { +void _zp_unicast_fetch_zid(const _z_transport_t *zt, _z_closure_zid_t *callback) { _ZP_UNUSED(zt); _ZP_UNUSED(callback); } diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 5554f4021..d2519bc08 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -245,6 +245,7 @@ int main(int argc, char **argv) { z_sleep_s(SLEEP); assert_eq(zids, 0); + z_closure(&_ret_closure_zid, zid_handler, NULL, NULL); _ret_int8 = z_info_routers_zid(z_loan(s1), z_move(_ret_closure_zid)); assert_eq(_ret_int8, 0); diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index aff99a581..84beac90d 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -34,7 +34,7 @@ .kind = 0, \ .qos = {0}}; \ z_loaned_sample_t sample = _z_sample_rc_new_from_val(s); \ - z_call(closure, &sample); \ + z_call(*z_loan(closure), &sample); \ } while (0); #define _RECV(handler, method, buf) \