Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions deps/node/src/lwnode/lwnode-public.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class Runtime::Internal {
return runner_.Run(*instance_);
}

void Stop() {
if (instance_ == nullptr) {
return;
}

runner_.Stop();
}

void Free() {
if (is_initialized && instance_) {
DisposeNode(instance_);
Expand Down Expand Up @@ -105,6 +113,10 @@ int Runtime::Start(int argc, char** argv, std::promise<void>&& promise) {
return result;
}

void Runtime::Stop() {
internal_->runner_.Stop();
}

std::shared_ptr<Port> Runtime::GetPort() {
return internal_->runner_.GetPort();
}
Expand Down
8 changes: 8 additions & 0 deletions deps/node/src/node_main_lw_runner-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ class LWNodeMainRunner {
return exit_code;
}

void Stop() {
CHECK_NOT_NULL(environment_);
if (environment_->is_stopping()) {
return;
}
environment_->ExitEnv();
}

std::shared_ptr<Port> GetPort() {
CHECK_NOT_NULL(environment_);
return environment_->GetPort();
Expand Down
6 changes: 6 additions & 0 deletions include/lwnode/lwnode-public.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class LWNODE_EXPORT Runtime {
**/
int Start(int argc, char** argv, std::promise<void>&& promise);

/**
* Stop the runtime. You can use this function to stop the runtime from another
* thread.
Comment on lines +92 to +93
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stop the runtime. This function can be used to stop the runtime from another thread safely.

nit: I'd like to suggest to avoid you in API documentation. It's better to use a more neutral and objective phrasing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I will fix next commit.

**/
void Stop();

std::shared_ptr<Port> GetPort();

private:
Expand Down
19 changes: 13 additions & 6 deletions test/embedding/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ cmake_minimum_required(VERSION 3.21.3)

project(embedtest)

include_directories("../../include/lwnode")
link_libraries(lwnode pthread)
add_compile_options(-std=c++17)
set(TARGET_OS "linux")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "armv7l")
set(TARGET_OS "tizen/arm")
Comment thread
daeyeon marked this conversation as resolved.
endif()

set(BUILD_TYPE "Release")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-O2 -g)
link_directories("../../out/linux/Debug/lib")
else()
link_directories("../../out/linux/Release/lib")
set(BUILD_TYPE "Debug")
endif()

include_directories("../../include/lwnode")
find_library(LWNODE_LIB lwnode HINT "../../out/${TARGET_OS}/${BUILD_TYPE}/lib")
link_libraries(pthread ${LWNODE_LIB} -Wl,-rpath='./')

add_compile_options(-std=c++17)
Comment thread
daeyeon marked this conversation as resolved.

add_executable(embedtest.x embedtest.cc)
add_executable(example.x example.cc)
add_executable(send-message-sync.x send-message-sync.cc)
add_executable(runtime-stop.x runtime-stop.cc)
58 changes: 58 additions & 0 deletions test/embedding/packaging/lwnode-embedding.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (c) 2025-present Samsung Electronics Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Name: lwnode-embedding
Summary: -
Version: 1.0.0
Release: 1
Group: Development
License: Apache-2.0 and MIT and BSD-2-Clause and BSD-3-Clause and BOEHM-GC and ICU and LGPL-2.1+ and Zlib
Source: %{name}-%{version}.tar.gz

BuildRequires: cmake
BuildRequires: make
BuildRequires: pkgconfig(dlog)


# Initialize the variables
%define _output_path out/tizen/arm/Release

%description
lwnode embedding test

%prep
%setup -q

%build

cmake -S test/embedding -B%{_output_path} -DCMAKE_BUILD_TYPE=Release
make -C %{_output_path} -j$(nproc)


%install

%clean
rm -fr ./*.list
rm -fr ./*.manifest

%post
/sbin/ldconfig

%postun
/sbin/ldconfig

%files
%defattr(-,tizenglobalapp,root,-)

%license LICENSE LICENSE.Apache-2.0 LICENSE.NodeJS LICENSE.MIT LICENSE.BSD-2-Clause LICENSE.BSD-3-Clause LICENSE.BOEHM-GC LICENSE.ICU LICENSE.LGPL-2.1+ LICENSE.Zlib
39 changes: 39 additions & 0 deletions test/embedding/runtime-stop.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <filesystem>
#include <future>
#include <iostream>
#include <thread>
#include <chrono>

#include <lwnode-public.h>
#include <message-port.h>

#define COUNT_OF(array) (sizeof(array) / sizeof((array)[0]))

int main(int argc, char* argv[]) {
auto runtime = std::make_shared<lwnode::Runtime>();

std::promise<void> promise;
std::future<void> init_future = promise.get_future();
const char* script = "test/embedding/test-20-runtime-stop.js";
std::string path = (std::filesystem::current_path() / script).string();
char* args[] = {const_cast<char*>(""), const_cast<char*>(path.c_str())};

std::thread worker = std::thread(
[&](std::promise<void>&& promise) mutable {
// FIXME: Fix Runtime::Init() call to ensure environment initialization
// before running the loop, Runtime::Run(). This workaround passes a
// promise directly to know when that is.
runtime->Start(COUNT_OF(args), args, std::move(promise));
},
std::move(promise));

init_future.wait();

std::cout << "[C]sleep app" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
runtime->Stop();
std::cout << "[C]terminate app" << std::endl;

worker.join();
return 0;
}
19 changes: 19 additions & 0 deletions test/embedding/test-20-runtime-stop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const lwnode = process.lwnode;
const http = require('http');

if (process.lwnode.hasSystemInfo('tizen')) {
console.log('[JS] start gmain-loop')
require("./gmain-loop").init();
}

console.log('[JS] start app')

console.log('[JS] create server')
const server = http.createServer();
server.listen(1234);

lwnode.ref();

setInterval(() => {
console.log('[JS]loop');
}, 1000);