Skip to content

Commit

Permalink
Make z_mutex_t owned
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Jul 1, 2024
1 parent 4411046 commit 2276479
Show file tree
Hide file tree
Showing 53 changed files with 261 additions and 218 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions examples/unix/c11/z_get_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down
10 changes: 5 additions & 5 deletions examples/unix/c11/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
Expand All @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions examples/unix/c99/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down
10 changes: 5 additions & 5 deletions examples/unix/c99/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
Expand All @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions examples/windows/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down
10 changes: 5 additions & 5 deletions examples/windows/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
Expand All @@ -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));
Expand Down
5 changes: 4 additions & 1 deletion include/zenoh-pico/api/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand All @@ -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)
/**
Expand Down Expand Up @@ -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)

/**
Expand Down
40 changes: 27 additions & 13 deletions include/zenoh-pico/api/olv_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; } \
Expand Down Expand Up @@ -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; }
Expand Down
38 changes: 19 additions & 19 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -1134,32 +1134,32 @@ 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)
_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`.
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/collections/fifo_mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 2276479

Please sign in to comment.