Skip to content

Commit

Permalink
feat: add tsfn example
Browse files Browse the repository at this point in the history
  • Loading branch information
richerfu committed Jan 3, 2024
1 parent 547da85 commit a344159
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 1 deletion.
11 changes: 11 additions & 0 deletions example/tsfn/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[target.aarch64-unknown-linux-ohos]
ar = "$OHOS_NDK_HOME/native/llvm/bin/llvm-ar"
linker = "scripts/aarch64-unknown-linux-ohos-clang.sh"

[target.armv7-unknown-linux-ohos]
ar = "$OHOS_NDK_HOME/native/llvm/bin/llvm-ar"
linker = "scripts/armv7-unknown-linux-ohos-clang.sh"

[target.x86_64-unknown-linux-ohos]
ar = "$OHOS_NDK_HOME/native/llvm/bin/llvm-ar"
linker = "scripts/x86_64-unknown-linux-ohos-clang.sh"
4 changes: 4 additions & 0 deletions example/tsfn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
target/

Cargo.lock
16 changes: 16 additions & 0 deletions example/tsfn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "tsfn"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[dependencies]
napi-ohos = { version = "*" }
napi-derive-ohos = { version = "*" }

[build-dependencies]
napi-build-ohos = { version = "*" }
6 changes: 6 additions & 0 deletions example/tsfn/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

extern crate napi_build_ohos;

fn main() {
napi_build_ohos::setup();
}
6 changes: 6 additions & 0 deletions example/tsfn/scripts/aarch64-unknown-linux-ohos-clang++.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
exec $OHOS_NDK_HOME/native/llvm/bin/clang++ \
-target aarch64-linux-ohos \
--sysroot=$OHOS_NDK_HOME/native/sysroot \
-D__MUSL__ \
"$@"
6 changes: 6 additions & 0 deletions example/tsfn/scripts/aarch64-unknown-linux-ohos-clang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
exec $OHOS_NDK_HOME/native/llvm/bin/clang \
-target aarch64-linux-ohos \
--sysroot=$OHOS_NDK_HOME/native/sysroot \
-D__MUSL__ \
"$@"
10 changes: 10 additions & 0 deletions example/tsfn/scripts/armv7-unknown-linux-ohos-clang++.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
exec $OHOS_NDK_HOME/native/llvm/bin/clang++ \
-target arm-linux-ohos \
--sysroot=$OHOS_NDK_HOME/native/sysroot \
-D__MUSL__ \
-march=armv7-a \
-mfloat-abi=softfp \
-mtune=generic-armv7-a \
-mthumb \
"$@"
10 changes: 10 additions & 0 deletions example/tsfn/scripts/armv7-unknown-linux-ohos-clang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
exec $OHOS_NDK_HOME/native/llvm/bin/clang \
-target arm-linux-ohos \
--sysroot=$OHOS_NDK_HOME/native/sysroot \
-D__MUSL__ \
-march=armv7-a \
-mfloat-abi=softfp \
-mtune=generic-armv7-a \
-mthumb \
"$@"
6 changes: 6 additions & 0 deletions example/tsfn/scripts/x86_64-unknown-linux-ohos-clang++.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
exec $OHOS_NDK_HOME/native/llvm/bin/clang++ \
-target x86_64-linux-ohos \
--sysroot=$OHOS_NDK_HOME/native/sysroot \
-D__MUSL__ \
"$@"
7 changes: 7 additions & 0 deletions example/tsfn/scripts/x86_64-unknown-linux-ohos-clang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#!/bin/sh
exec $OHOS_NDK_HOME/native/llvm/bin/clang \
-target x86_64-linux-ohos \
--sysroot=$OHOS_NDK_HOME/native/sysroot \
-D__MUSL__ \
"$@"
50 changes: 50 additions & 0 deletions example/tsfn/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::thread;

use napi_derive_ohos::{js_function, module_exports};
use napi_ohos::bindgen_prelude::pre_init;
use napi_ohos::{
module_init,
threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunctionCallMode},
CallContext, JsFunction, JsNumber, JsObject, JsUndefined, Result,
};

#[js_function(1)]
pub fn test_threadsafe_function(ctx: CallContext) -> Result<JsUndefined> {
let func = ctx.get::<JsFunction>(0)?;

let tsfn =
ctx.env
.create_threadsafe_function(&func, 0, |ctx: ThreadSafeCallContext<Vec<u32>>| {
ctx.value
.iter()
.map(|v| ctx.env.create_uint32(*v))
.collect::<Result<Vec<JsNumber>>>()
})?;

let tsfn_cloned = tsfn.clone();

thread::spawn(move || {
let output: Vec<u32> = vec![0, 1, 2, 3];
// It's okay to call a threadsafe function multiple times.
tsfn.call(Ok(output), ThreadsafeFunctionCallMode::Blocking);
});

thread::spawn(move || {
let output: Vec<u32> = vec![3, 2, 1, 0];
// It's okay to call a threadsafe function multiple times.
tsfn_cloned.call(Ok(output), ThreadsafeFunctionCallMode::NonBlocking);
});

ctx.env.get_undefined()
}

#[module_exports]
pub fn register_js(mut exports: JsObject) -> Result<()> {
exports.create_named_method("testTsFn", test_threadsafe_function)?;
Ok(())
}

#[module_init]
fn init() {
pre_init();
}
Binary file added ohos_rs_example/entry/libs/arm64-v8a/libtsfn.so
Binary file not shown.
Binary file not shown.
Binary file added ohos_rs_example/entry/libs/x86_64/libtsfn.so
Binary file not shown.
5 changes: 5 additions & 0 deletions ohos_rs_example/entry/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ struct Index {
url: 'pages/External'
})
})
Button('TSFN').onClick(() => {
router.pushUrl({
url: 'pages/Tsfn'
})
})
}
.width('100%')
}
Expand Down
25 changes: 25 additions & 0 deletions ohos_rs_example/entry/src/main/ets/pages/Tsfn.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import tsfn from 'libtsfn.so';

@Entry
@Component
struct Tsfn {
@State message: string = '';

build() {
Row() {
Column({ space: 20 }) {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)

Button('Run TSFN').onClick(() => {
tsfn.testTsFn((args: number[]) => {
this.message = args.toString();
})
})
}
.width('100%')
}
.height('100%')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"pages/ArrayBuffer",
"pages/TokioRT",
"pages/Either",
"pages/External"
"pages/External",
"pages/Tsfn"
]
}

0 comments on commit a344159

Please sign in to comment.