Skip to content

Commit

Permalink
z_recv/z_try_recv without the need of z_check (#570)
Browse files Browse the repository at this point in the history
* make z_recv/z_try_recv return z_result_t to allow differentiating between different cases without the need to call z_check on constructed object

* format
  • Loading branch information
DenisBiryukov91 authored Aug 6, 2024
1 parent 8740ed2 commit 56332aa
Show file tree
Hide file tree
Showing 22 changed files with 178 additions and 143 deletions.
2 changes: 1 addition & 1 deletion docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Query
z_get(z_loan(s), z_loan(key_expr), "", z_move(closure), NULL);
z_owned_reply_t reply;
for (z_recv(z_loan(handler), &reply); z_check(reply); z_recv(z_loan(handler), &reply)) {
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
if (z_reply_is_ok(&reply)) {
const z_loaned_sample_t* sample = z_reply_ok(&reply);
z_view_string_t key_string;
Expand Down
2 changes: 1 addition & 1 deletion examples/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int main(int argc, char **argv) {
z_move(opts)); // here, the send is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply;

for (z_recv(z_loan(handler), &reply); z_check(reply); z_recv(z_loan(handler), &reply)) {
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));

Expand Down
2 changes: 1 addition & 1 deletion examples/z_get_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main(int argc, char **argv) {
z_fifo_channel_reply_new(&closure, &handler, 16);
zc_liveliness_get(z_loan(s), z_loan(keyexpr), z_move(closure), NULL);
z_owned_reply_t reply;
for (z_recv(z_loan(handler), &reply); z_check(reply); z_recv(z_loan(handler), &reply)) {
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));
z_view_string_t key_str;
Expand Down
2 changes: 1 addition & 1 deletion examples/z_get_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int main(int argc, char** argv) {
z_move(opts)); // here, the send is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply;

for (z_recv(z_loan(handler), &reply); z_check(reply); z_recv(z_loan(handler), &reply)) {
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t* sample = z_reply_ok(z_loan(reply));

Expand Down
6 changes: 3 additions & 3 deletions examples/z_non_blocking_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ int main(int argc, char **argv) {
z_get(z_loan(s), z_loan(keyexpr), "", z_move(closure),
z_move(opts)); // here, the closure is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply;
for (bool has_more = z_try_recv(z_loan(handler), &reply); has_more;
has_more = z_try_recv(z_loan(handler), &reply)) {
if (!z_check(reply)) {
for (z_result_t res = z_try_recv(z_loan(handler), &reply); res != Z_CHANNEL_DISCONNECTED;
res = z_try_recv(z_loan(handler), &reply)) {
if (res != Z_OK) {
z_sleep_ms(50);
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/z_pub_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ int main(int argc, char **argv) {
ze_owned_publication_cache_t pub_cache;
z_view_keyexpr_t ke;
z_view_keyexpr_from_str(&ke, keyexpr);
ze_declare_publication_cache(&pub_cache, z_loan(s), z_loan(ke), &pub_cache_opts);
if (!z_check(pub_cache)) {

if (ze_declare_publication_cache(&pub_cache, z_loan(s), z_loan(ke), &pub_cache_opts) != Z_OK) {
printf("Unable to declare publication cache for key expression!\n");
exit(-1);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ int main(int argc, char **argv) {
if (c == -1) {
z_sleep_s(1);
} else {
z_try_recv(z_loan(handler), &sample);
if (z_check(sample)) {
z_result_t res = z_try_recv(z_loan(handler), &sample);
if (res == Z_OK) {
handle_sample(z_loan(sample));
z_drop(z_move(sample));
}
Expand Down
2 changes: 1 addition & 1 deletion examples/z_queryable_with_channels.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main(int argc, char **argv) {

printf("^C to quit...\n");
z_owned_query_t oquery;
for (z_recv(z_loan(handler), &oquery); z_check(oquery); z_recv(z_loan(handler), &oquery)) {
for (z_result_t res = z_recv(z_loan(handler), &oquery); res == Z_OK; res = z_recv(z_loan(handler), &oquery)) {
const z_loaned_query_t *query = z_loan(oquery);
z_view_string_t key_string;
z_keyexpr_as_view_string(z_query_keyexpr(query), &key_string);
Expand Down
94 changes: 51 additions & 43 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -2364,19 +2364,21 @@ const struct z_loaned_fifo_handler_query_t *z_fifo_handler_query_loan(const stru
ZENOHC_API void z_fifo_handler_query_null(struct z_owned_fifo_handler_query_t *this_);
/**
* Returns query from the fifo buffer. If there are no more pending queries will block until next query is received, or until
* the channel is dropped (normally when Queryable is dropped). In the later case will return ``false`` and query will be
* in the gravestone state.
* the channel is dropped (normally when Queryable is dropped).
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the query will be in the gravestone state),
* `Z_CHANNEL_NODATA` if the channel is still alive, but its buffer is empty (the query will be in the gravestone state).
*/
ZENOHC_API
bool z_fifo_handler_query_recv(const struct z_loaned_fifo_handler_query_t *this_,
struct z_owned_query_t *query);
z_result_t z_fifo_handler_query_recv(const struct z_loaned_fifo_handler_query_t *this_,
struct z_owned_query_t *query);
/**
* Returns query from the fifo buffer. If there are no more pending queries will return immediately (with query set to its gravestone state).
* Will return false if the channel is dropped (normally when Queryable is dropped) and there are no more queries in the fifo.
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the query will be in the gravestone state),
* `Z_CHANNEL_NODATA` if the channel is still alive, but its buffer is empty (the query will be in the gravestone state).
*/
ZENOHC_API
bool z_fifo_handler_query_try_recv(const struct z_loaned_fifo_handler_query_t *this_,
struct z_owned_query_t *query);
z_result_t z_fifo_handler_query_try_recv(const struct z_loaned_fifo_handler_query_t *this_,
struct z_owned_query_t *query);
/**
* Returns ``true`` if handler is valid, ``false`` if it is in gravestone state.
*/
Expand All @@ -2396,19 +2398,20 @@ const struct z_loaned_fifo_handler_reply_t *z_fifo_handler_reply_loan(const stru
ZENOHC_API void z_fifo_handler_reply_null(struct z_owned_fifo_handler_reply_t *this_);
/**
* Returns reply from the fifo buffer. If there are no more pending replies will block until next reply is received, or until
* the channel is dropped (normally when all replies are received). In the later case will return ``false`` and reply will be
* in the gravestone state.
* the channel is dropped (normally when all replies are received).
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the reply will be in the gravestone state).
*/
ZENOHC_API
bool z_fifo_handler_reply_recv(const struct z_loaned_fifo_handler_reply_t *this_,
struct z_owned_reply_t *reply);
z_result_t z_fifo_handler_reply_recv(const struct z_loaned_fifo_handler_reply_t *this_,
struct z_owned_reply_t *reply);
/**
* Returns reply from the fifo buffer. If there are no more pending replies will return immediately (with reply set to its gravestone state).
* Will return false if the channel is dropped (normally when all replies are received) and there are no more replies in the fifo.
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the reply will be in the gravestone state),
* `Z_CHANNEL_NODATA` if the channel is still alive, but its buffer is empty (the reply will be in the gravestone state).
*/
ZENOHC_API
bool z_fifo_handler_reply_try_recv(const struct z_loaned_fifo_handler_reply_t *this_,
struct z_owned_reply_t *reply);
z_result_t z_fifo_handler_reply_try_recv(const struct z_loaned_fifo_handler_reply_t *this_,
struct z_owned_reply_t *reply);
/**
* Returns ``true`` if handler is valid, ``false`` if it is in gravestone state.
*/
Expand All @@ -2428,19 +2431,21 @@ const struct z_loaned_fifo_handler_sample_t *z_fifo_handler_sample_loan(const st
ZENOHC_API void z_fifo_handler_sample_null(struct z_owned_fifo_handler_sample_t *this_);
/**
* Returns sample from the fifo buffer. If there are no more pending replies will block until next sample is received, or until
* the channel is dropped (normally when there are no more samples to receive). In the later case will return ``false`` and sample will be
* in the gravestone state.
* the channel is dropped (normally when there are no more samples to receive).
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the sample will be in the gravestone state).
*/
ZENOHC_API
bool z_fifo_handler_sample_recv(const struct z_loaned_fifo_handler_sample_t *this_,
struct z_owned_sample_t *sample);
z_result_t z_fifo_handler_sample_recv(const struct z_loaned_fifo_handler_sample_t *this_,
struct z_owned_sample_t *sample);
/**
* Returns sample from the fifo buffer. If there are no more pending replies will return immediately (with sample set to its gravestone state).
* Will return false if the channel is dropped (normally when there are no more samples to receive) and there are no more replies in the fifo.
* Returns sample from the fifo buffer.
* If there are no more pending replies will return immediately (with sample set to its gravestone state).
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the sample will be in the gravestone state),
* `Z_CHANNEL_NODATA` if the channel is still alive, but its buffer is empty (the sample will be in the gravestone state).
*/
ZENOHC_API
bool z_fifo_handler_sample_try_recv(const struct z_loaned_fifo_handler_sample_t *this_,
struct z_owned_sample_t *sample);
z_result_t z_fifo_handler_sample_try_recv(const struct z_loaned_fifo_handler_sample_t *this_,
struct z_owned_sample_t *sample);
/**
* Query data from the matching queryables in the system.
* Replies are provided through a callback function.
Expand Down Expand Up @@ -3187,19 +3192,20 @@ const struct z_loaned_ring_handler_query_t *z_ring_handler_query_loan(const stru
ZENOHC_API void z_ring_handler_query_null(struct z_owned_ring_handler_query_t *this_);
/**
* Returns query from the ring buffer. If there are no more pending queries will block until next query is received, or until
* the channel is dropped (normally when Queryable is dropped). In the later case will return ``false`` and query will be
* in the gravestone state.
* the channel is dropped (normally when Queryable is dropped).
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the query will be in the gravestone state).
*/
ZENOHC_API
bool z_ring_handler_query_recv(const struct z_loaned_ring_handler_query_t *this_,
struct z_owned_query_t *query);
z_result_t z_ring_handler_query_recv(const struct z_loaned_ring_handler_query_t *this_,
struct z_owned_query_t *query);
/**
* Returns query from the ring buffer. If there are no more pending queries will return immediately (with query set to its gravestone state).
* Will return false if the channel is dropped (normally when Queryable is dropped) and there are no more queries in the fifo.
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the query will be in the gravestone state),
* Z_CHANNEL_NODATA if the channel is still alive, but its buffer is empty (the query will be in the gravestone state).
*/
ZENOHC_API
bool z_ring_handler_query_try_recv(const struct z_loaned_ring_handler_query_t *this_,
struct z_owned_query_t *query);
z_result_t z_ring_handler_query_try_recv(const struct z_loaned_ring_handler_query_t *this_,
struct z_owned_query_t *query);
/**
* Returns ``true`` if handler is valid, ``false`` if it is in gravestone state.
*/
Expand All @@ -3219,19 +3225,20 @@ const struct z_loaned_ring_handler_reply_t *z_ring_handler_reply_loan(const stru
ZENOHC_API void z_ring_handler_reply_null(struct z_owned_ring_handler_reply_t *this_);
/**
* Returns reply from the ring buffer. If there are no more pending replies will block until next reply is received, or until
* the channel is dropped (normally when all replies are received). In the later case will return ``false`` and reply will be
* in the gravestone state.
* the channel is dropped (normally when all replies are received).
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the reply will be in the gravestone state).
*/
ZENOHC_API
bool z_ring_handler_reply_recv(const struct z_loaned_ring_handler_reply_t *this_,
struct z_owned_reply_t *reply);
z_result_t z_ring_handler_reply_recv(const struct z_loaned_ring_handler_reply_t *this_,
struct z_owned_reply_t *reply);
/**
* Returns reply from the ring buffer. If there are no more pending replies will return immediately (with reply set to its gravestone state).
* Will return false if the channel is dropped (normally when all replies are received) and there are no more replies in the fifo.
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the reply will be in the gravestone state),
* `Z_CHANNEL_NODATA` if the channel is still alive, but its buffer is empty (the reply will be in the gravestone state).
*/
ZENOHC_API
bool z_ring_handler_reply_try_recv(const struct z_loaned_ring_handler_reply_t *this_,
struct z_owned_reply_t *reply);
z_result_t z_ring_handler_reply_try_recv(const struct z_loaned_ring_handler_reply_t *this_,
struct z_owned_reply_t *reply);
/**
* Returns ``true`` if handler is valid, ``false`` if it is in gravestone state.
*/
Expand All @@ -3251,19 +3258,20 @@ const struct z_loaned_ring_handler_sample_t *z_ring_handler_sample_loan(const st
ZENOHC_API void z_ring_handler_sample_null(struct z_owned_ring_handler_sample_t *this_);
/**
* Returns sample from the ring buffer. If there are no more pending replies will block until next sample is received, or until
* the channel is dropped (normally when there are no more samples to receive). In the later case will return ``false`` and sample will be
* in the gravestone state.
* the channel is dropped (normally when there are no more replies to receive).
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the sample will be in the gravestone state).
*/
ZENOHC_API
bool z_ring_handler_sample_recv(const struct z_loaned_ring_handler_sample_t *this_,
struct z_owned_sample_t *sample);
z_result_t z_ring_handler_sample_recv(const struct z_loaned_ring_handler_sample_t *this_,
struct z_owned_sample_t *sample);
/**
* Returns sample from the ring buffer. If there are no more pending replies will return immediately (with sample set to its gravestone state).
* Will return false if the channel is dropped (normally when there are no more samples to receive) and there are no more replies in the fifo.
* @return 0 in case of success, `Z_CHANNEL_DISCONNECTED` if channel was dropped (the sample will be in the gravestone state),
* `Z_CHANNEL_NODATA` if the channel is still alive, but its buffer is empty (the sample will be in the gravestone state).
*/
ZENOHC_API
bool z_ring_handler_sample_try_recv(const struct z_loaned_ring_handler_sample_t *this_,
struct z_owned_sample_t *sample);
z_result_t z_ring_handler_sample_try_recv(const struct z_loaned_ring_handler_sample_t *this_,
struct z_owned_sample_t *sample);
/**
* Returns sample attachment.
*
Expand Down
2 changes: 2 additions & 0 deletions include/zenoh_concrete.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <stdint.h>
#include <stdlib.h>
#define DEFAULT_SCOUTING_TIMEOUT 1000
#define Z_CHANNEL_DISCONNECTED 1
#define Z_CHANNEL_NODATA 2
#define Z_OK 0
#define Z_EINVAL -1
#define Z_EPARSE -2
Expand Down
24 changes: 12 additions & 12 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,42 +421,42 @@ inline void z_closure(
};


inline bool z_try_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) {
inline z_result_t z_try_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) {
return z_fifo_handler_query_try_recv(this_, query);
};
inline bool z_try_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) {
inline z_result_t z_try_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) {
return z_fifo_handler_reply_try_recv(this_, reply);
};
inline bool z_try_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) {
inline z_result_t z_try_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) {
return z_fifo_handler_sample_try_recv(this_, sample);
};
inline bool z_try_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) {
inline z_result_t z_try_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) {
return z_ring_handler_query_try_recv(this_, query);
};
inline bool z_try_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) {
inline z_result_t z_try_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) {
return z_ring_handler_reply_try_recv(this_, reply);
};
inline bool z_try_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) {
inline z_result_t z_try_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) {
return z_ring_handler_sample_try_recv(this_, sample);
};


inline bool z_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) {
inline z_result_t z_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) {
return z_fifo_handler_query_recv(this_, query);
};
inline bool z_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) {
inline z_result_t z_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) {
return z_fifo_handler_reply_recv(this_, reply);
};
inline bool z_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) {
inline z_result_t z_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) {
return z_fifo_handler_sample_recv(this_, sample);
};
inline bool z_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) {
inline z_result_t z_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) {
return z_ring_handler_query_recv(this_, query);
};
inline bool z_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) {
inline z_result_t z_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) {
return z_ring_handler_reply_recv(this_, reply);
};
inline bool z_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) {
inline z_result_t z_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) {
return z_ring_handler_sample_recv(this_, sample);
};

Expand Down
Loading

0 comments on commit 56332aa

Please sign in to comment.