summaryrefslogtreecommitdiff
path: root/rust/src/worker_thread.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/src/worker_thread.rs')
-rw-r--r--rust/src/worker_thread.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/rust/src/worker_thread.rs b/rust/src/worker_thread.rs
new file mode 100644
index 00000000..349456e5
--- /dev/null
+++ b/rust/src/worker_thread.rs
@@ -0,0 +1,71 @@
+use crate::string::BnStrCompatible;
+use binaryninjacore_sys::*;
+use std::ffi::{c_char, c_void};
+
+pub struct WorkerThreadActionExecutor {
+ func: Box<dyn Fn()>,
+}
+
+impl WorkerThreadActionExecutor {
+ unsafe extern "C" fn cb_execute(ctx: *mut c_void) {
+ let f: Box<Self> = Box::from_raw(ctx as *mut Self);
+ f.execute();
+ }
+
+ pub fn execute(&self) {
+ (self.func)();
+ }
+}
+
+pub fn execute_on_worker_thread<F: Fn() + 'static, S: BnStrCompatible>(name: S, f: F) {
+ let boxed_executor = Box::new(WorkerThreadActionExecutor { func: Box::new(f) });
+ let raw_executor = Box::into_raw(boxed_executor);
+ let name = name.into_bytes_with_nul();
+ unsafe {
+ BNWorkerEnqueueNamed(
+ raw_executor as *mut c_void,
+ Some(WorkerThreadActionExecutor::cb_execute),
+ name.as_ref().as_ptr() as *const c_char,
+ )
+ }
+}
+
+pub fn execute_on_worker_thread_priority<F: Fn() + 'static, S: BnStrCompatible>(name: S, f: F) {
+ let boxed_executor = Box::new(WorkerThreadActionExecutor { func: Box::new(f) });
+ let raw_executor = Box::into_raw(boxed_executor);
+ let name = name.into_bytes_with_nul();
+ unsafe {
+ BNWorkerPriorityEnqueueNamed(
+ raw_executor as *mut c_void,
+ Some(WorkerThreadActionExecutor::cb_execute),
+ name.as_ref().as_ptr() as *const c_char,
+ )
+ }
+}
+
+pub fn execute_on_worker_thread_interactive<F: Fn() + 'static, S: BnStrCompatible>(name: S, f: F) {
+ let boxed_executor = Box::new(WorkerThreadActionExecutor { func: Box::new(f) });
+ let raw_executor = Box::into_raw(boxed_executor);
+ let name = name.into_bytes_with_nul();
+ unsafe {
+ BNWorkerInteractiveEnqueueNamed(
+ raw_executor as *mut c_void,
+ Some(WorkerThreadActionExecutor::cb_execute),
+ name.as_ref().as_ptr() as *const c_char,
+ )
+ }
+}
+
+/// Returns the number of worker threads that are currently running.
+/// By default, this is the number of cores on the system minus one
+///
+/// To set the worker thread count use [`set_worker_thread_count`].
+pub fn worker_thread_count() -> usize {
+ unsafe { BNGetWorkerThreadCount() }
+}
+
+/// Sets the number of worker threads that are currently running.
+/// By default, this is the number of cores on the system minus one.
+pub fn set_worker_thread_count(count: usize) {
+ unsafe { BNSetWorkerThreadCount(count) }
+}