@@ -78,6 +78,34 @@ spice::unload("hera/kernels/mk/hera_study_PO_EMA_2024.tm");
78
78
79
79
You can look for some inspirations in the [core tests][core tests link].
80
80
81
+ ## Multi-threaded usage
82
+
83
+ CSPICE itself contains massive amounts of shared mutable state and is thus not thread-safe - concurrent
84
+ calls to any SPICE functions will almost always lead to crashes. To prevent this, if you need
85
+ to call SPICE functions from multiple threads, this crate provides a thread-safe API with the `lock`
86
+ feature. When enabled, the API is exposed in the form of associated functions on a guard singleton
87
+ `SpiceLock`, which is `!Sync + Send`. You can then only share this singleton and thus the methods it
88
+ provides between threads using a `Mutex`, preventing concurrent API usage.
89
+
90
+ The lock exposes the [neat][neat link] versions of functions where available, and the [raw][raw link] versions for the rest.
91
+ For functions which have neither, you will have to use the unsafe (and unguarded) direct C bindings.
92
+ Just make sure you have the lock before calling them.
93
+
94
+ ```rust
95
+ use spice::SpiceLock;
96
+
97
+ // `try_acquire` will return `Err` if a lock already exists
98
+ let sl = SpiceLock::try_acquire().unwrap();
99
+
100
+ // SPICE functions are now associated functions of the lock with a `&self` arg
101
+ let mut kernel = sl.furnsh("hera/kernels/mk/hera_study_PO_EMA_2024.tm");
102
+
103
+ let et = sl.str2et("2027-MAR-23 16:00:00");
104
+ let (position, light_time) = sl.spkpos("DIMORPHOS", et, "J2000", "NONE", "SUN");
105
+
106
+ sl.unload("hera/kernels/mk/hera_study_PO_EMA_2024.tm");
107
+ ```
108
+
81
109
## In development
82
110
83
111
Developing an idiomatic interface for Spice in Rust takes time, and not all
@@ -163,6 +191,8 @@ Licensed under the [Apache License, Version 2.0][license link].
163
191
[cspice install link]: https://naif.jpl.nasa.gov/naif/toolkit_C.html
164
192
[cspice-sys link]: https://github.com/jacob-pro/cspice-rs/tree/master/cspice-sys
165
193
[config doc]: https://doc.rust-lang.org/cargo/reference/config.html
194
+ [raw link]: https://docs.rs/rust-spice/latest/spice/core/raw/index.html
195
+ [neat link]: https://docs.rs/rust-spice/latest/spice/core/neat/index.html
166
196
167
197
[s-rah url]: https://github.com/s-rah
168
198
[PR 2]: https://github.com/GregoireHENRY/rust-spice/pull/2
0 commit comments