-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist/ | ||
target/ | ||
|
||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "*" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ \ | ||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ \ | ||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 \ | ||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 \ | ||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ \ | ||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ \ | ||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
"pages/ArrayBuffer", | ||
"pages/TokioRT", | ||
"pages/Either", | ||
"pages/External" | ||
"pages/External", | ||
"pages/Tsfn" | ||
] | ||
} |