holo-routing / yang-rs

Rust bindings for the libyang2 library.
MIT License
22 stars 10 forks source link

Is context concurrent safe? #5

Open Dennis-Zhang-SH opened 1 year ago

Dennis-Zhang-SH commented 1 year ago

@rwestphal Hi, I wonder is context concurrent safe? In this crate it's all raw pointers and actually should not be impl send and sync, but I don't know if the c library is concurrent safe? If so I think it's fine.

By the way, would you mind establishing a little community of this crate? Or should I keep bother you with issues?

rwestphal commented 1 year ago

@rwestphal Hi, I wonder is context concurrent safe? In this crate it's all raw pointers and actually should not be impl send and sync, but I don't know if the c library is concurrent safe? If so I think it's fine.

@Dennis-Zhang-SH The entire yang2-rs API should be thread-safe.

libyang's documentation assures thread-safety as long as certain rules are respected (single writer or multiple concurrent readers): https://netopeer.liberouter.org/doc/libyang/master/html/howto_threads.html

Since all yang2-rs functions that modify YANG contexts or data trees require mutable references, the borrow checker verifies at compile time if libyang's rules are being followed.

It shouldn't be possible, for example, to modify one data tree in one thread while reading the same data tree in another thread (or async task). The only way to do that would be by using a mutex or another synchronization primitive to ensure safe concurrent access.

Be mindful, however, that yang2-rs is only thread-safe as long as libyang withstands its promises.

By the way, would you mind establishing a little community of this crate? Or should I keep bother you with issues?

Please don't hesitate to open new issues whenever necessary, I do the same in the libyang repo ^^ I could open a Discord channel for this crate, but at this point that would be an overkill since there aren't many people using it.

Dennis-Zhang-SH commented 1 year ago

@rwestphal thanks! Just finished reading context part of codes, your construction of Set is pretty decent, I am writing same ffi project on golang, and I have to say golang has a weaker type system, I find it hard to translate your code into golang code, even with its generics.