From 78d24a1a487abc509e60cc44e1934982f91ec05b Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 22 Feb 2024 18:53:34 +0100 Subject: [PATCH 1/7] add rust implementation of mutex and condvar --- examples/z_ping.c | 24 ++-- include/zenoh_commons.h | 23 ++++ src/lib.rs | 2 + src/platform/mod.rs | 16 +++ src/platform/synchronization.rs | 205 ++++++++++++++++++++++++++++++++ 5 files changed, 258 insertions(+), 12 deletions(-) create mode 100644 src/platform/mod.rs create mode 100644 src/platform/synchronization.rs diff --git a/examples/z_ping.c b/examples/z_ping.c index 535f1d079..e0b16b32e 100644 --- a/examples/z_ping.c +++ b/examples/z_ping.c @@ -15,11 +15,11 @@ #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) -pthread_cond_t cond; -pthread_mutex_t mutex; +zp_condvar_t cond; +zp_mutex_t mutex; -void callback(const z_sample_t* sample, void* context) { pthread_cond_signal(&cond); } -void drop(void* context) { pthread_cond_destroy(&cond); } +void callback(const z_sample_t* sample, void* context) { zp_condvar_signal(&cond); } +void drop(void* context) { zp_condvar_free(&cond); } struct args_t { unsigned int size; // -s @@ -43,8 +43,8 @@ int main(int argc, char** argv) { DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS); return 1; } - pthread_mutex_init(&mutex, NULL); - pthread_cond_init(&cond, NULL); + zp_mutex_init(&mutex); + zp_condvar_init(&cond); z_owned_config_t config = args.config_path ? zc_config_from_file(args.config_path) : z_config_default(); z_owned_session_t session = z_open(z_move(config)); z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); @@ -56,7 +56,7 @@ int main(int argc, char** argv) { for (int i = 0; i < args.size; i++) { data[i] = i % 10; } - pthread_mutex_lock(&mutex); + zp_mutex_lock(&mutex); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); struct timespec wmup_start, wmup_stop, wmup_timeout; @@ -66,9 +66,9 @@ int main(int argc, char** argv) { clock_gettime(CLOCK_REALTIME, &wmup_timeout); wmup_timeout.tv_sec += PING_TIMEOUT_SEC; z_publisher_put(z_loan(pub), data, args.size, NULL); - int s = pthread_cond_timedwait(&cond, &mutex, &wmup_timeout); + int s = zp_condvar_wait(&cond, &mutex); if (s != 0) { - handle_error_en(s, "pthread_cond_timedwait"); + handle_error_en(s, "zp_condvar_wait"); } clock_gettime(CLOCK_MONOTONIC, &wmup_stop); elapsed_us = @@ -82,9 +82,9 @@ int main(int argc, char** argv) { t_timeout.tv_sec += PING_TIMEOUT_SEC; clock_gettime(CLOCK_MONOTONIC, &t_start); z_publisher_put(z_loan(pub), data, args.size, NULL); - int s = pthread_cond_timedwait(&cond, &mutex, &t_timeout); + int s = zp_condvar_wait(&cond, &mutex); if (s != 0) { - handle_error_en(s, "pthread_cond_timedwait"); + handle_error_en(s, "zp_condvar_wait"); } clock_gettime(CLOCK_MONOTONIC, &t_stop); results[i] = (1000000 * (t_stop.tv_sec - t_start.tv_sec) + (t_stop.tv_nsec - t_start.tv_nsec) / 1000); @@ -92,7 +92,7 @@ int main(int argc, char** argv) { for (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); } - pthread_mutex_unlock(&mutex); + zp_mutex_unlock(&mutex); free(results); free(data); z_drop(z_move(sub)); diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 58e7a2856..4bc4dcfdb 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -957,6 +957,20 @@ typedef struct ze_querying_subscriber_options_t { typedef struct ze_querying_subscriber_t { const struct ze_owned_querying_subscriber_t *_0; } ze_querying_subscriber_t; +/** + * CondVar + * + */ +typedef struct zp_condvar_t { + size_t _0; +} zp_condvar_t; +/** + * Mutex + * + */ +typedef struct zp_mutex_t { + size_t _0; +} zp_mutex_t; ZENOHC_API extern const unsigned int Z_ROUTER; ZENOHC_API extern const unsigned int Z_PEER; ZENOHC_API extern const unsigned int Z_CLIENT; @@ -2526,3 +2540,12 @@ int8_t ze_undeclare_publication_cache(struct ze_owned_publication_cache_t *pub_c */ ZENOHC_API int8_t ze_undeclare_querying_subscriber(struct ze_owned_querying_subscriber_t *sub); +ZENOHC_API int8_t zp_condvar_free(struct zp_condvar_t *c); +ZENOHC_API int8_t zp_condvar_init(struct zp_condvar_t *c); +ZENOHC_API int8_t zp_condvar_signal(struct zp_condvar_t *c); +ZENOHC_API int8_t zp_condvar_wait(struct zp_condvar_t *c, struct zp_mutex_t *m); +ZENOHC_API int8_t zp_mutex_free(struct zp_mutex_t *m); +ZENOHC_API int8_t zp_mutex_init(struct zp_mutex_t *m); +ZENOHC_API int8_t zp_mutex_lock(struct zp_mutex_t *m); +ZENOHC_API int8_t zp_mutex_try_lock(struct zp_mutex_t *m); +ZENOHC_API int8_t zp_mutex_unlock(struct zp_mutex_t *m); diff --git a/src/lib.rs b/src/lib.rs index b508855e0..718475444 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,6 +49,8 @@ pub use publication_cache::*; mod querying_subscriber; pub use querying_subscriber::*; pub mod attachment; +pub use platform::*; +pub mod platform; #[cfg(feature = "shared-memory")] mod shm; diff --git a/src/platform/mod.rs b/src/platform/mod.rs new file mode 100644 index 000000000..0922f3546 --- /dev/null +++ b/src/platform/mod.rs @@ -0,0 +1,16 @@ +// +// Copyright (c) 2017, 2023 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, +// + +pub use synchronization::*; +mod synchronization; \ No newline at end of file diff --git a/src/platform/synchronization.rs b/src/platform/synchronization.rs new file mode 100644 index 000000000..3c3c5ca5c --- /dev/null +++ b/src/platform/synchronization.rs @@ -0,0 +1,205 @@ +use std::sync::{Condvar, Mutex, MutexGuard}; + +use crate::{impl_guarded_transmute, GuardedTransmute}; + + +pub struct ZPMutex<'a> { + mutex: Mutex<()>, + lock: Option> +} + +pub struct ZPMutexPtr { + data: Option>> +} + + +/// Mutex +/// +#[repr(C)] +#[derive(Clone, Copy)] +pub struct zp_mutex_t(usize); + +impl_guarded_transmute!(zp_mutex_t, ZPMutexPtr); +impl_guarded_transmute!(ZPMutexPtr, zp_mutex_t); + +const EBUSY: i8 = -1; +const EINVAL: i8 = -2; +const EPOISON: i8 = -10; + +#[no_mangle] +pub unsafe extern "C" fn zp_mutex_init(m: *mut zp_mutex_t) -> i8 { + if m.is_null() { + return EINVAL; + } + let t = ZPMutexPtr { + data: + Some(Box::new( + ZPMutex { + mutex: Mutex::new(()), + lock: None + } + )) + }; + *m = t.transmute(); + return 0; +} + +#[no_mangle] +pub unsafe extern "C" fn zp_mutex_free(m: *mut zp_mutex_t) -> i8 { + if m.is_null() { + return EINVAL; + } + let mut t = (*m).transmute(); + + t.data.take(); + *m = t.transmute(); + return 0; +} + +#[no_mangle] +pub unsafe extern "C" fn zp_mutex_lock(m: *mut zp_mutex_t) -> i8 { + if m.is_null() { + return EINVAL; + } + let mut t = (*m).transmute(); + if t.data.is_none() { + return EINVAL; + } + let mut_data = t.data.as_mut().unwrap(); + match mut_data.mutex.lock() { + Ok(new_lock) => { + let old_lock = mut_data.lock.replace(std::mem::transmute(new_lock)); + std::mem::forget(old_lock); + }, + Err(_) => { + return EPOISON; + } + } + + *m = t.transmute(); + return 0; +} + +#[no_mangle] +pub unsafe extern "C" fn zp_mutex_unlock(m: *mut zp_mutex_t) -> i8 { + if m.is_null() { + return EINVAL; + } + let mut t = (*m).transmute(); + if t.data.is_none() { + return EINVAL; + } + let mut_data = t.data.as_mut().unwrap(); + if mut_data.lock.is_none() { + return EINVAL; + } else { + mut_data.lock.take(); + } + *m = t.transmute(); + return 0; +} + +#[no_mangle] +pub unsafe extern "C" fn zp_mutex_try_lock(m: *mut zp_mutex_t) -> i8 { + if m.is_null() { + return EINVAL; + } + let mut t = (*m).transmute(); + if t.data.is_none() { + return EINVAL; + } + let mut_data = t.data.as_mut().unwrap(); + let new_lock = mut_data.mutex.try_lock(); + let mut ret: i8 = 0; + if new_lock.is_ok() { + let old_lock = mut_data.lock.replace(std::mem::transmute(new_lock.unwrap())); + std::mem::forget(old_lock); + } else { + std::mem::drop(new_lock); + ret = EBUSY; + } + *m = t.transmute(); + return ret; +} + + +struct ZPCondvarPtr { + data: Option> +} + +/// CondVar +/// +#[repr(C)] +#[derive(Clone, Copy)] +pub struct zp_condvar_t(usize); + +impl_guarded_transmute!(zp_condvar_t, ZPCondvarPtr); +impl_guarded_transmute!(ZPCondvarPtr, zp_condvar_t); + + +#[no_mangle] +pub unsafe extern "C" fn zp_condvar_init(c: *mut zp_condvar_t) -> i8 { + if c.is_null() { + return EINVAL; + } + let t: ZPCondvarPtr = ZPCondvarPtr { + data: Some(Box::new(Condvar::new())) + }; + *c = t.transmute(); + return 0; +} + +#[no_mangle] +pub unsafe extern "C" fn zp_condvar_free(c: *mut zp_condvar_t) -> i8 { + if c.is_null() { + return EINVAL; + } + let mut t = (*c).transmute(); + if t.data.is_none() { + return EINVAL; + } + t.data.take(); + *c = t.transmute(); + return 0; +} + +#[no_mangle] +pub unsafe extern "C" fn zp_condvar_signal(c: *mut zp_condvar_t) -> i8 { + if c.is_null() { + return EINVAL; + } + let t = (*c).transmute(); + if t.data.is_none() { + return EINVAL; + } + t.data.as_ref().unwrap().notify_one(); + *c = t.transmute(); + return 0; +} + +#[no_mangle] +pub unsafe extern "C" fn zp_condvar_wait(c: *mut zp_condvar_t, m: *mut zp_mutex_t) -> i8 { + if c.is_null() { + return EINVAL; + } + let tc = (*c).transmute(); + if tc.data.is_none() { + return EINVAL; + } + if m.is_null() { + return EINVAL; + } + let mut tm = (*m).transmute(); + if tm.data.is_none() || tm.data.as_ref().unwrap().lock.is_none() { + return EINVAL; + } + let mut_data = tm.data.as_mut().unwrap(); + let lock = mut_data.lock.take().unwrap(); + match tc.data.as_ref().unwrap().wait(lock) { + Ok(new_lock) => mut_data.lock = Some(std::mem::transmute(new_lock)), + Err(_) => return EPOISON + } + *c = tc.transmute(); + *m = tm.transmute(); + return 0; +} From 9d78f060bd5e786276942993f07b3b2b46204ddb Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 23 Feb 2024 14:03:13 +0100 Subject: [PATCH 2/7] add rust implementation of task --- include/zenoh_commons.h | 26 ++++-- src/platform/synchronization.rs | 154 ++++++++++++++++++++++++-------- 2 files changed, 137 insertions(+), 43 deletions(-) diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 4bc4dcfdb..2e0d04f06 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -958,7 +958,7 @@ typedef struct ze_querying_subscriber_t { const struct ze_owned_querying_subscriber_t *_0; } ze_querying_subscriber_t; /** - * CondVar + * Condvar * */ typedef struct zp_condvar_t { @@ -971,6 +971,16 @@ typedef struct zp_condvar_t { typedef struct zp_mutex_t { size_t _0; } zp_mutex_t; +/** + * Task + * + */ +typedef struct zp_task_t { + size_t _0; +} zp_task_t; +typedef struct zp_task_attr_t { + size_t _0; +} zp_task_attr_t; ZENOHC_API extern const unsigned int Z_ROUTER; ZENOHC_API extern const unsigned int Z_PEER; ZENOHC_API extern const unsigned int Z_CLIENT; @@ -2540,12 +2550,18 @@ int8_t ze_undeclare_publication_cache(struct ze_owned_publication_cache_t *pub_c */ ZENOHC_API int8_t ze_undeclare_querying_subscriber(struct ze_owned_querying_subscriber_t *sub); -ZENOHC_API int8_t zp_condvar_free(struct zp_condvar_t *c); -ZENOHC_API int8_t zp_condvar_init(struct zp_condvar_t *c); -ZENOHC_API int8_t zp_condvar_signal(struct zp_condvar_t *c); -ZENOHC_API int8_t zp_condvar_wait(struct zp_condvar_t *c, struct zp_mutex_t *m); +ZENOHC_API int8_t zp_condvar_free(struct zp_condvar_t *cv); +ZENOHC_API int8_t zp_condvar_init(struct zp_condvar_t *cv); +ZENOHC_API int8_t zp_condvar_signal(struct zp_condvar_t *cv); +ZENOHC_API int8_t zp_condvar_wait(struct zp_condvar_t *cv, struct zp_mutex_t *m); ZENOHC_API int8_t zp_mutex_free(struct zp_mutex_t *m); ZENOHC_API int8_t zp_mutex_init(struct zp_mutex_t *m); ZENOHC_API int8_t zp_mutex_lock(struct zp_mutex_t *m); ZENOHC_API int8_t zp_mutex_try_lock(struct zp_mutex_t *m); ZENOHC_API int8_t zp_mutex_unlock(struct zp_mutex_t *m); +ZENOHC_API +int8_t zp_task_init(struct zp_task_t *task, + const struct zp_task_attr_t *_attr, + void (*fun)(void *arg), + void *arg); +ZENOHC_API int8_t zp_task_join(struct zp_task_t *task); diff --git a/src/platform/synchronization.rs b/src/platform/synchronization.rs index 3c3c5ca5c..10ad28a87 100644 --- a/src/platform/synchronization.rs +++ b/src/platform/synchronization.rs @@ -1,18 +1,21 @@ -use std::sync::{Condvar, Mutex, MutexGuard}; +use std::{ + sync::{Condvar, Mutex, MutexGuard}, + thread::{self, JoinHandle}, +}; -use crate::{impl_guarded_transmute, GuardedTransmute}; +use libc::c_void; +use crate::{impl_guarded_transmute, GuardedTransmute}; pub struct ZPMutex<'a> { mutex: Mutex<()>, - lock: Option> + lock: Option>, } pub struct ZPMutexPtr { - data: Option>> + data: Option>>, } - /// Mutex /// #[repr(C)] @@ -24,6 +27,7 @@ impl_guarded_transmute!(ZPMutexPtr, zp_mutex_t); const EBUSY: i8 = -1; const EINVAL: i8 = -2; +const EAGAIN: i8 = -3; const EPOISON: i8 = -10; #[no_mangle] @@ -32,13 +36,10 @@ pub unsafe extern "C" fn zp_mutex_init(m: *mut zp_mutex_t) -> i8 { return EINVAL; } let t = ZPMutexPtr { - data: - Some(Box::new( - ZPMutex { - mutex: Mutex::new(()), - lock: None - } - )) + data: Some(Box::new(ZPMutex { + mutex: Mutex::new(()), + lock: None, + })), }; *m = t.transmute(); return 0; @@ -70,7 +71,7 @@ pub unsafe extern "C" fn zp_mutex_lock(m: *mut zp_mutex_t) -> i8 { Ok(new_lock) => { let old_lock = mut_data.lock.replace(std::mem::transmute(new_lock)); std::mem::forget(old_lock); - }, + } Err(_) => { return EPOISON; } @@ -112,7 +113,9 @@ pub unsafe extern "C" fn zp_mutex_try_lock(m: *mut zp_mutex_t) -> i8 { let new_lock = mut_data.mutex.try_lock(); let mut ret: i8 = 0; if new_lock.is_ok() { - let old_lock = mut_data.lock.replace(std::mem::transmute(new_lock.unwrap())); + let old_lock = mut_data + .lock + .replace(std::mem::transmute(new_lock.unwrap())); std::mem::forget(old_lock); } else { std::mem::drop(new_lock); @@ -122,12 +125,11 @@ pub unsafe extern "C" fn zp_mutex_try_lock(m: *mut zp_mutex_t) -> i8 { return ret; } - struct ZPCondvarPtr { - data: Option> + data: Option>, } -/// CondVar +/// Condvar /// #[repr(C)] #[derive(Clone, Copy)] @@ -136,70 +138,146 @@ pub struct zp_condvar_t(usize); impl_guarded_transmute!(zp_condvar_t, ZPCondvarPtr); impl_guarded_transmute!(ZPCondvarPtr, zp_condvar_t); - #[no_mangle] -pub unsafe extern "C" fn zp_condvar_init(c: *mut zp_condvar_t) -> i8 { - if c.is_null() { +pub unsafe extern "C" fn zp_condvar_init(cv: *mut zp_condvar_t) -> i8 { + if cv.is_null() { return EINVAL; } let t: ZPCondvarPtr = ZPCondvarPtr { - data: Some(Box::new(Condvar::new())) + data: Some(Box::new(Condvar::new())), }; - *c = t.transmute(); + *cv = t.transmute(); return 0; } #[no_mangle] -pub unsafe extern "C" fn zp_condvar_free(c: *mut zp_condvar_t) -> i8 { - if c.is_null() { +pub unsafe extern "C" fn zp_condvar_free(cv: *mut zp_condvar_t) -> i8 { + if cv.is_null() { return EINVAL; } - let mut t = (*c).transmute(); + let mut t = (*cv).transmute(); if t.data.is_none() { return EINVAL; } t.data.take(); - *c = t.transmute(); + *cv = t.transmute(); return 0; } #[no_mangle] -pub unsafe extern "C" fn zp_condvar_signal(c: *mut zp_condvar_t) -> i8 { - if c.is_null() { +pub unsafe extern "C" fn zp_condvar_signal(cv: *mut zp_condvar_t) -> i8 { + if cv.is_null() { return EINVAL; } - let t = (*c).transmute(); + let t = (*cv).transmute(); if t.data.is_none() { return EINVAL; } t.data.as_ref().unwrap().notify_one(); - *c = t.transmute(); + *cv = t.transmute(); return 0; } #[no_mangle] -pub unsafe extern "C" fn zp_condvar_wait(c: *mut zp_condvar_t, m: *mut zp_mutex_t) -> i8 { - if c.is_null() { +pub unsafe extern "C" fn zp_condvar_wait(cv: *mut zp_condvar_t, m: *mut zp_mutex_t) -> i8 { + if cv.is_null() { return EINVAL; } - let tc = (*c).transmute(); - if tc.data.is_none() { + let tcv = (*cv).transmute(); + if tcv.data.is_none() { return EINVAL; } if m.is_null() { return EINVAL; } let mut tm = (*m).transmute(); - if tm.data.is_none() || tm.data.as_ref().unwrap().lock.is_none() { + if tm.data.is_none() || tm.data.as_ref().unwrap().lock.is_none() { return EINVAL; } let mut_data = tm.data.as_mut().unwrap(); let lock = mut_data.lock.take().unwrap(); - match tc.data.as_ref().unwrap().wait(lock) { + match tcv.data.as_ref().unwrap().wait(lock) { Ok(new_lock) => mut_data.lock = Some(std::mem::transmute(new_lock)), - Err(_) => return EPOISON + Err(_) => return EPOISON, } - *c = tc.transmute(); + *cv = tcv.transmute(); *m = tm.transmute(); return 0; } + +struct ZPTask { + join_handle: JoinHandle<()>, +} + +struct ZPTaskPtr { + data: Option>, +} + +/// Task +/// +#[repr(C)] +#[derive(Clone, Copy)] +pub struct zp_task_t(usize); + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct zp_task_attr_t(usize); + +impl_guarded_transmute!(zp_task_t, ZPTaskPtr); +impl_guarded_transmute!(ZPTaskPtr, zp_task_t); + +struct FunArgPair { + fun: unsafe extern "C" fn(arg: *mut c_void), + arg: *mut c_void, +} + +impl FunArgPair { + unsafe fn call(self) { + (self.fun)(self.arg); + } +} + +unsafe impl Send for FunArgPair {} + +#[no_mangle] +pub unsafe extern "C" fn zp_task_init( + task: *mut zp_task_t, + _attr: *const zp_task_attr_t, + fun: unsafe extern "C" fn(arg: *mut c_void), + arg: *mut c_void, +) -> i8 { + if task.is_null() { + return EINVAL; + } + + let mut ttask = ZPTaskPtr { + data: None + }; + let fun_arg_pair = FunArgPair { fun, arg }; + + let mut ret = 0; + match thread::Builder::new().spawn(move || { fun_arg_pair.call()}) { + Ok(join_handle) => ttask.data = Some(Box::new(ZPTask { join_handle })), + Err(_) => ret = EAGAIN, + } + *task = ttask.transmute(); + return ret; +} + +#[no_mangle] +pub unsafe extern "C" fn zp_task_join(task: *mut zp_task_t) -> i8 { + if task.is_null() { + return EINVAL; + } + let mut ttask = (*task).transmute(); + if ttask.data.is_none() { + return EINVAL; + } + let data = ttask.data.take(); + let ret = match data.unwrap().join_handle.join() { + Ok(_) => 0, + Err(_) => EINVAL, + }; + *task = ttask.transmute(); + return ret; +} From 2a20b4684b8bf8666db4a62367f46c5d59966634 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 26 Feb 2024 09:58:32 +0100 Subject: [PATCH 3/7] remove p_thread header --- examples/z_ping.c | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/z_ping.c b/examples/z_ping.c index e0b16b32e..84606f728 100644 --- a/examples/z_ping.c +++ b/examples/z_ping.c @@ -1,5 +1,4 @@ #include -#include #include #include #include From b1a5f86422d9524bb7a0cbc23c16575e61255dfa Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 26 Feb 2024 10:16:39 +0100 Subject: [PATCH 4/7] clippy and format fix --- src/platform/mod.rs | 2 +- src/platform/synchronization.rs | 58 +++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 0922f3546..c9f87a9c7 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -13,4 +13,4 @@ // pub use synchronization::*; -mod synchronization; \ No newline at end of file +mod synchronization; diff --git a/src/platform/synchronization.rs b/src/platform/synchronization.rs index 10ad28a87..1b8af1ea9 100644 --- a/src/platform/synchronization.rs +++ b/src/platform/synchronization.rs @@ -31,6 +31,7 @@ const EAGAIN: i8 = -3; const EPOISON: i8 = -10; #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_mutex_init(m: *mut zp_mutex_t) -> i8 { if m.is_null() { return EINVAL; @@ -42,10 +43,11 @@ pub unsafe extern "C" fn zp_mutex_init(m: *mut zp_mutex_t) -> i8 { })), }; *m = t.transmute(); - return 0; + 0 } #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_mutex_free(m: *mut zp_mutex_t) -> i8 { if m.is_null() { return EINVAL; @@ -54,10 +56,11 @@ pub unsafe extern "C" fn zp_mutex_free(m: *mut zp_mutex_t) -> i8 { t.data.take(); *m = t.transmute(); - return 0; + 0 } #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_mutex_lock(m: *mut zp_mutex_t) -> i8 { if m.is_null() { return EINVAL; @@ -78,10 +81,11 @@ pub unsafe extern "C" fn zp_mutex_lock(m: *mut zp_mutex_t) -> i8 { } *m = t.transmute(); - return 0; + 0 } #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_mutex_unlock(m: *mut zp_mutex_t) -> i8 { if m.is_null() { return EINVAL; @@ -97,10 +101,11 @@ pub unsafe extern "C" fn zp_mutex_unlock(m: *mut zp_mutex_t) -> i8 { mut_data.lock.take(); } *m = t.transmute(); - return 0; + 0 } #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_mutex_try_lock(m: *mut zp_mutex_t) -> i8 { if m.is_null() { return EINVAL; @@ -110,19 +115,18 @@ pub unsafe extern "C" fn zp_mutex_try_lock(m: *mut zp_mutex_t) -> i8 { return EINVAL; } let mut_data = t.data.as_mut().unwrap(); - let new_lock = mut_data.mutex.try_lock(); let mut ret: i8 = 0; - if new_lock.is_ok() { - let old_lock = mut_data - .lock - .replace(std::mem::transmute(new_lock.unwrap())); - std::mem::forget(old_lock); - } else { - std::mem::drop(new_lock); - ret = EBUSY; + match mut_data.mutex.try_lock() { + Ok(new_lock) => { + let old_lock = mut_data.lock.replace(std::mem::transmute(new_lock)); + std::mem::forget(old_lock); + } + Err(_) => { + ret = EBUSY; + } } *m = t.transmute(); - return ret; + ret } struct ZPCondvarPtr { @@ -139,6 +143,7 @@ impl_guarded_transmute!(zp_condvar_t, ZPCondvarPtr); impl_guarded_transmute!(ZPCondvarPtr, zp_condvar_t); #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_condvar_init(cv: *mut zp_condvar_t) -> i8 { if cv.is_null() { return EINVAL; @@ -147,10 +152,11 @@ pub unsafe extern "C" fn zp_condvar_init(cv: *mut zp_condvar_t) -> i8 { data: Some(Box::new(Condvar::new())), }; *cv = t.transmute(); - return 0; + 0 } #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_condvar_free(cv: *mut zp_condvar_t) -> i8 { if cv.is_null() { return EINVAL; @@ -161,10 +167,11 @@ pub unsafe extern "C" fn zp_condvar_free(cv: *mut zp_condvar_t) -> i8 { } t.data.take(); *cv = t.transmute(); - return 0; + 0 } #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_condvar_signal(cv: *mut zp_condvar_t) -> i8 { if cv.is_null() { return EINVAL; @@ -175,10 +182,11 @@ pub unsafe extern "C" fn zp_condvar_signal(cv: *mut zp_condvar_t) -> i8 { } t.data.as_ref().unwrap().notify_one(); *cv = t.transmute(); - return 0; + 0 } #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_condvar_wait(cv: *mut zp_condvar_t, m: *mut zp_mutex_t) -> i8 { if cv.is_null() { return EINVAL; @@ -202,7 +210,7 @@ pub unsafe extern "C" fn zp_condvar_wait(cv: *mut zp_condvar_t, m: *mut zp_mutex } *cv = tcv.transmute(); *m = tm.transmute(); - return 0; + 0 } struct ZPTask { @@ -240,6 +248,7 @@ impl FunArgPair { unsafe impl Send for FunArgPair {} #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_task_init( task: *mut zp_task_t, _attr: *const zp_task_attr_t, @@ -250,21 +259,20 @@ pub unsafe extern "C" fn zp_task_init( return EINVAL; } - let mut ttask = ZPTaskPtr { - data: None - }; + let mut ttask = ZPTaskPtr { data: None }; let fun_arg_pair = FunArgPair { fun, arg }; let mut ret = 0; - match thread::Builder::new().spawn(move || { fun_arg_pair.call()}) { + match thread::Builder::new().spawn(move || fun_arg_pair.call()) { Ok(join_handle) => ttask.data = Some(Box::new(ZPTask { join_handle })), Err(_) => ret = EAGAIN, } - *task = ttask.transmute(); - return ret; + *task = ttask.transmute(); + ret } #[no_mangle] +#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn zp_task_join(task: *mut zp_task_t) -> i8 { if task.is_null() { return EINVAL; @@ -279,5 +287,5 @@ pub unsafe extern "C" fn zp_task_join(task: *mut zp_task_t) -> i8 { Err(_) => EINVAL, }; *task = ttask.transmute(); - return ret; + ret } From e7cbf516a6e8d2f268a2dc27f2791dc4ee88261f Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 28 Feb 2024 09:58:01 +0100 Subject: [PATCH 5/7] replace zp_ prefix with z_ --- examples/z_ping.c | 24 +++++----- include/zenoh_commons.h | 78 ++++++++++++++++----------------- src/platform/synchronization.rs | 70 ++++++++++++++--------------- 3 files changed, 86 insertions(+), 86 deletions(-) diff --git a/examples/z_ping.c b/examples/z_ping.c index 84606f728..ed5dff631 100644 --- a/examples/z_ping.c +++ b/examples/z_ping.c @@ -14,11 +14,11 @@ #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) -zp_condvar_t cond; -zp_mutex_t mutex; +z_condvar_t cond; +z_mutex_t mutex; -void callback(const z_sample_t* sample, void* context) { zp_condvar_signal(&cond); } -void drop(void* context) { zp_condvar_free(&cond); } +void callback(const z_sample_t* sample, void* context) { z_condvar_signal(&cond); } +void drop(void* context) { z_condvar_free(&cond); } struct args_t { unsigned int size; // -s @@ -42,8 +42,8 @@ int main(int argc, char** argv) { DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS); return 1; } - zp_mutex_init(&mutex); - zp_condvar_init(&cond); + z_mutex_init(&mutex); + z_condvar_init(&cond); z_owned_config_t config = args.config_path ? zc_config_from_file(args.config_path) : z_config_default(); z_owned_session_t session = z_open(z_move(config)); z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); @@ -55,7 +55,7 @@ int main(int argc, char** argv) { for (int i = 0; i < args.size; i++) { data[i] = i % 10; } - zp_mutex_lock(&mutex); + z_mutex_lock(&mutex); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); struct timespec wmup_start, wmup_stop, wmup_timeout; @@ -65,9 +65,9 @@ int main(int argc, char** argv) { clock_gettime(CLOCK_REALTIME, &wmup_timeout); wmup_timeout.tv_sec += PING_TIMEOUT_SEC; z_publisher_put(z_loan(pub), data, args.size, NULL); - int s = zp_condvar_wait(&cond, &mutex); + int s = z_condvar_wait(&cond, &mutex); if (s != 0) { - handle_error_en(s, "zp_condvar_wait"); + handle_error_en(s, "z_condvar_wait"); } clock_gettime(CLOCK_MONOTONIC, &wmup_stop); elapsed_us = @@ -81,9 +81,9 @@ int main(int argc, char** argv) { t_timeout.tv_sec += PING_TIMEOUT_SEC; clock_gettime(CLOCK_MONOTONIC, &t_start); z_publisher_put(z_loan(pub), data, args.size, NULL); - int s = zp_condvar_wait(&cond, &mutex); + int s = z_condvar_wait(&cond, &mutex); if (s != 0) { - handle_error_en(s, "zp_condvar_wait"); + handle_error_en(s, "z_condvar_wait"); } clock_gettime(CLOCK_MONOTONIC, &t_stop); results[i] = (1000000 * (t_stop.tv_sec - t_start.tv_sec) + (t_stop.tv_nsec - t_start.tv_nsec) / 1000); @@ -91,7 +91,7 @@ int main(int argc, char** argv) { for (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); } - zp_mutex_unlock(&mutex); + z_mutex_unlock(&mutex); free(results); free(data); z_drop(z_move(sub)); diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 2e0d04f06..bdefe4311 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -462,6 +462,20 @@ typedef struct z_owned_closure_zid_t { void (*call)(const struct z_id_t*, void*); void (*drop)(void*); } z_owned_closure_zid_t; +/** + * Condvar + * + */ +typedef struct z_condvar_t { + size_t _0; +} z_condvar_t; +/** + * Mutex + * + */ +typedef struct z_mutex_t { + size_t _0; +} z_mutex_t; /** * An owned zenoh configuration. * @@ -788,6 +802,16 @@ typedef struct z_owned_scouting_config_t { typedef struct z_subscriber_t { const struct z_owned_subscriber_t *_0; } z_subscriber_t; +/** + * Task + * + */ +typedef struct z_task_t { + size_t _0; +} z_task_t; +typedef struct z_task_attr_t { + size_t _0; +} z_task_attr_t; /** * The options for `zc_liveliness_declare_token` */ @@ -957,30 +981,6 @@ typedef struct ze_querying_subscriber_options_t { typedef struct ze_querying_subscriber_t { const struct ze_owned_querying_subscriber_t *_0; } ze_querying_subscriber_t; -/** - * Condvar - * - */ -typedef struct zp_condvar_t { - size_t _0; -} zp_condvar_t; -/** - * Mutex - * - */ -typedef struct zp_mutex_t { - size_t _0; -} zp_mutex_t; -/** - * Task - * - */ -typedef struct zp_task_t { - size_t _0; -} zp_task_t; -typedef struct zp_task_attr_t { - size_t _0; -} zp_task_attr_t; ZENOHC_API extern const unsigned int Z_ROUTER; ZENOHC_API extern const unsigned int Z_PEER; ZENOHC_API extern const unsigned int Z_CLIENT; @@ -1214,6 +1214,10 @@ ZENOHC_API void z_closure_zid_drop(struct z_owned_closure_zid_t *closure); * Constructs a null safe-to-drop value of 'z_owned_closure_zid_t' type */ ZENOHC_API struct z_owned_closure_zid_t z_closure_zid_null(void); +ZENOHC_API int8_t z_condvar_free(struct z_condvar_t *cv); +ZENOHC_API int8_t z_condvar_init(struct z_condvar_t *cv); +ZENOHC_API int8_t z_condvar_signal(struct z_condvar_t *cv); +ZENOHC_API int8_t z_condvar_wait(struct z_condvar_t *cv, struct z_mutex_t *m); /** * Returns ``true`` if `config` is valid. */ @@ -1631,6 +1635,11 @@ ZENOHC_API struct z_owned_str_t z_keyexpr_to_string(struct z_keyexpr_t keyexpr); */ ZENOHC_API struct z_keyexpr_t z_keyexpr_unchecked(const char *name); +ZENOHC_API int8_t z_mutex_free(struct z_mutex_t *m); +ZENOHC_API int8_t z_mutex_init(struct z_mutex_t *m); +ZENOHC_API int8_t z_mutex_lock(struct z_mutex_t *m); +ZENOHC_API int8_t z_mutex_try_lock(struct z_mutex_t *m); +ZENOHC_API int8_t z_mutex_unlock(struct z_mutex_t *m); /** * Opens a zenoh session. Should the session opening fail, `z_check` ing the returned value will return `false`. */ @@ -2028,6 +2037,12 @@ ZENOHC_API struct z_subscriber_options_t z_subscriber_options_default(void); * sub: The :c:type:`z_owned_pull_subscriber_t` to pull from. */ ZENOHC_API int8_t z_subscriber_pull(struct z_pull_subscriber_t sub); +ZENOHC_API +int8_t z_task_init(struct z_task_t *task, + const struct z_task_attr_t *_attr, + void (*fun)(void *arg), + void *arg); +ZENOHC_API int8_t z_task_join(struct z_task_t *task); /** * Returns ``true`` if `ts` is a valid timestamp */ @@ -2550,18 +2565,3 @@ int8_t ze_undeclare_publication_cache(struct ze_owned_publication_cache_t *pub_c */ ZENOHC_API int8_t ze_undeclare_querying_subscriber(struct ze_owned_querying_subscriber_t *sub); -ZENOHC_API int8_t zp_condvar_free(struct zp_condvar_t *cv); -ZENOHC_API int8_t zp_condvar_init(struct zp_condvar_t *cv); -ZENOHC_API int8_t zp_condvar_signal(struct zp_condvar_t *cv); -ZENOHC_API int8_t zp_condvar_wait(struct zp_condvar_t *cv, struct zp_mutex_t *m); -ZENOHC_API int8_t zp_mutex_free(struct zp_mutex_t *m); -ZENOHC_API int8_t zp_mutex_init(struct zp_mutex_t *m); -ZENOHC_API int8_t zp_mutex_lock(struct zp_mutex_t *m); -ZENOHC_API int8_t zp_mutex_try_lock(struct zp_mutex_t *m); -ZENOHC_API int8_t zp_mutex_unlock(struct zp_mutex_t *m); -ZENOHC_API -int8_t zp_task_init(struct zp_task_t *task, - const struct zp_task_attr_t *_attr, - void (*fun)(void *arg), - void *arg); -ZENOHC_API int8_t zp_task_join(struct zp_task_t *task); diff --git a/src/platform/synchronization.rs b/src/platform/synchronization.rs index 1b8af1ea9..1973e6d65 100644 --- a/src/platform/synchronization.rs +++ b/src/platform/synchronization.rs @@ -7,23 +7,23 @@ use libc::c_void; use crate::{impl_guarded_transmute, GuardedTransmute}; -pub struct ZPMutex<'a> { +pub struct ZMutex<'a> { mutex: Mutex<()>, lock: Option>, } -pub struct ZPMutexPtr { - data: Option>>, +pub struct ZMutexPtr { + data: Option>>, } /// Mutex /// #[repr(C)] #[derive(Clone, Copy)] -pub struct zp_mutex_t(usize); +pub struct z_mutex_t(usize); -impl_guarded_transmute!(zp_mutex_t, ZPMutexPtr); -impl_guarded_transmute!(ZPMutexPtr, zp_mutex_t); +impl_guarded_transmute!(z_mutex_t, ZMutexPtr); +impl_guarded_transmute!(ZMutexPtr, z_mutex_t); const EBUSY: i8 = -1; const EINVAL: i8 = -2; @@ -32,12 +32,12 @@ const EPOISON: i8 = -10; #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_mutex_init(m: *mut zp_mutex_t) -> i8 { +pub unsafe extern "C" fn z_mutex_init(m: *mut z_mutex_t) -> i8 { if m.is_null() { return EINVAL; } - let t = ZPMutexPtr { - data: Some(Box::new(ZPMutex { + let t = ZMutexPtr { + data: Some(Box::new(ZMutex { mutex: Mutex::new(()), lock: None, })), @@ -48,7 +48,7 @@ pub unsafe extern "C" fn zp_mutex_init(m: *mut zp_mutex_t) -> i8 { #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_mutex_free(m: *mut zp_mutex_t) -> i8 { +pub unsafe extern "C" fn z_mutex_free(m: *mut z_mutex_t) -> i8 { if m.is_null() { return EINVAL; } @@ -61,7 +61,7 @@ pub unsafe extern "C" fn zp_mutex_free(m: *mut zp_mutex_t) -> i8 { #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_mutex_lock(m: *mut zp_mutex_t) -> i8 { +pub unsafe extern "C" fn z_mutex_lock(m: *mut z_mutex_t) -> i8 { if m.is_null() { return EINVAL; } @@ -86,7 +86,7 @@ pub unsafe extern "C" fn zp_mutex_lock(m: *mut zp_mutex_t) -> i8 { #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_mutex_unlock(m: *mut zp_mutex_t) -> i8 { +pub unsafe extern "C" fn z_mutex_unlock(m: *mut z_mutex_t) -> i8 { if m.is_null() { return EINVAL; } @@ -106,7 +106,7 @@ pub unsafe extern "C" fn zp_mutex_unlock(m: *mut zp_mutex_t) -> i8 { #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_mutex_try_lock(m: *mut zp_mutex_t) -> i8 { +pub unsafe extern "C" fn z_mutex_try_lock(m: *mut z_mutex_t) -> i8 { if m.is_null() { return EINVAL; } @@ -129,7 +129,7 @@ pub unsafe extern "C" fn zp_mutex_try_lock(m: *mut zp_mutex_t) -> i8 { ret } -struct ZPCondvarPtr { +struct ZCondvarPtr { data: Option>, } @@ -137,18 +137,18 @@ struct ZPCondvarPtr { /// #[repr(C)] #[derive(Clone, Copy)] -pub struct zp_condvar_t(usize); +pub struct z_condvar_t(usize); -impl_guarded_transmute!(zp_condvar_t, ZPCondvarPtr); -impl_guarded_transmute!(ZPCondvarPtr, zp_condvar_t); +impl_guarded_transmute!(z_condvar_t, ZCondvarPtr); +impl_guarded_transmute!(ZCondvarPtr, z_condvar_t); #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_condvar_init(cv: *mut zp_condvar_t) -> i8 { +pub unsafe extern "C" fn z_condvar_init(cv: *mut z_condvar_t) -> i8 { if cv.is_null() { return EINVAL; } - let t: ZPCondvarPtr = ZPCondvarPtr { + let t: ZCondvarPtr = ZCondvarPtr { data: Some(Box::new(Condvar::new())), }; *cv = t.transmute(); @@ -157,7 +157,7 @@ pub unsafe extern "C" fn zp_condvar_init(cv: *mut zp_condvar_t) -> i8 { #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_condvar_free(cv: *mut zp_condvar_t) -> i8 { +pub unsafe extern "C" fn z_condvar_free(cv: *mut z_condvar_t) -> i8 { if cv.is_null() { return EINVAL; } @@ -172,7 +172,7 @@ pub unsafe extern "C" fn zp_condvar_free(cv: *mut zp_condvar_t) -> i8 { #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_condvar_signal(cv: *mut zp_condvar_t) -> i8 { +pub unsafe extern "C" fn z_condvar_signal(cv: *mut z_condvar_t) -> i8 { if cv.is_null() { return EINVAL; } @@ -187,7 +187,7 @@ pub unsafe extern "C" fn zp_condvar_signal(cv: *mut zp_condvar_t) -> i8 { #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_condvar_wait(cv: *mut zp_condvar_t, m: *mut zp_mutex_t) -> i8 { +pub unsafe extern "C" fn z_condvar_wait(cv: *mut z_condvar_t, m: *mut z_mutex_t) -> i8 { if cv.is_null() { return EINVAL; } @@ -213,26 +213,26 @@ pub unsafe extern "C" fn zp_condvar_wait(cv: *mut zp_condvar_t, m: *mut zp_mutex 0 } -struct ZPTask { +struct ZTask { join_handle: JoinHandle<()>, } -struct ZPTaskPtr { - data: Option>, +struct ZTaskPtr { + data: Option>, } /// Task /// #[repr(C)] #[derive(Clone, Copy)] -pub struct zp_task_t(usize); +pub struct z_task_t(usize); #[repr(C)] #[derive(Clone, Copy)] -pub struct zp_task_attr_t(usize); +pub struct z_task_attr_t(usize); -impl_guarded_transmute!(zp_task_t, ZPTaskPtr); -impl_guarded_transmute!(ZPTaskPtr, zp_task_t); +impl_guarded_transmute!(z_task_t, ZTaskPtr); +impl_guarded_transmute!(ZTaskPtr, z_task_t); struct FunArgPair { fun: unsafe extern "C" fn(arg: *mut c_void), @@ -249,9 +249,9 @@ unsafe impl Send for FunArgPair {} #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_task_init( - task: *mut zp_task_t, - _attr: *const zp_task_attr_t, +pub unsafe extern "C" fn z_task_init( + task: *mut z_task_t, + _attr: *const z_task_attr_t, fun: unsafe extern "C" fn(arg: *mut c_void), arg: *mut c_void, ) -> i8 { @@ -259,12 +259,12 @@ pub unsafe extern "C" fn zp_task_init( return EINVAL; } - let mut ttask = ZPTaskPtr { data: None }; + let mut ttask = ZTaskPtr { data: None }; let fun_arg_pair = FunArgPair { fun, arg }; let mut ret = 0; match thread::Builder::new().spawn(move || fun_arg_pair.call()) { - Ok(join_handle) => ttask.data = Some(Box::new(ZPTask { join_handle })), + Ok(join_handle) => ttask.data = Some(Box::new(ZTask { join_handle })), Err(_) => ret = EAGAIN, } *task = ttask.transmute(); @@ -273,7 +273,7 @@ pub unsafe extern "C" fn zp_task_init( #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub unsafe extern "C" fn zp_task_join(task: *mut zp_task_t) -> i8 { +pub unsafe extern "C" fn z_task_join(task: *mut z_task_t) -> i8 { if task.is_null() { return EINVAL; } From 19e862fa9f6791b59521ee8b69da311e17b0e8db Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 28 Feb 2024 12:05:33 +0100 Subject: [PATCH 6/7] set error values to correspond to the ones from GNU pthreads --- src/platform/synchronization.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/platform/synchronization.rs b/src/platform/synchronization.rs index 1973e6d65..73ae823c6 100644 --- a/src/platform/synchronization.rs +++ b/src/platform/synchronization.rs @@ -25,10 +25,11 @@ pub struct z_mutex_t(usize); impl_guarded_transmute!(z_mutex_t, ZMutexPtr); impl_guarded_transmute!(ZMutexPtr, z_mutex_t); -const EBUSY: i8 = -1; -const EINVAL: i8 = -2; -const EAGAIN: i8 = -3; -const EPOISON: i8 = -10; +// try to use the same error codes as in GNU pthreads +const EBUSY: i8 = 16; +const EINVAL: i8 = 22; +const EAGAIN: i8 = 11; +const EPOISON: i8 = 22; // same as EINVAL #[no_mangle] #[allow(clippy::missing_safety_doc)] From 33d7448c5dbe59086c337e3a0fb16326d003fb5f Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 28 Feb 2024 15:11:44 +0100 Subject: [PATCH 7/7] fix error codes sign to be negative --- src/platform/synchronization.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/platform/synchronization.rs b/src/platform/synchronization.rs index 73ae823c6..dbd9d94b8 100644 --- a/src/platform/synchronization.rs +++ b/src/platform/synchronization.rs @@ -25,11 +25,12 @@ pub struct z_mutex_t(usize); impl_guarded_transmute!(z_mutex_t, ZMutexPtr); impl_guarded_transmute!(ZMutexPtr, z_mutex_t); -// try to use the same error codes as in GNU pthreads -const EBUSY: i8 = 16; -const EINVAL: i8 = 22; -const EAGAIN: i8 = 11; -const EPOISON: i8 = 22; // same as EINVAL +// using the same error codes as in GNU pthreads, but with negative sign +// due to convention to return negative values on error +const EBUSY: i8 = -16; +const EINVAL: i8 = -22; +const EAGAIN: i8 = -11; +const EPOISON: i8 = -22; // same as EINVAL #[no_mangle] #[allow(clippy::missing_safety_doc)]