mirror of
https://github.com/slint-ui/slint.git
synced 2025-09-30 22:01:13 +00:00
C++ Api to run a functor from a thread
This commit is contained in:
parent
7293129fe1
commit
aabd320e83
5 changed files with 111 additions and 14 deletions
|
@ -150,6 +150,8 @@ if(BUILD_TESTING)
|
|||
|
||||
FetchContent_MakeAvailable(Catch2)
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
macro(sixtyfps_test NAME)
|
||||
add_executable(test_${NAME} tests/${NAME}.cpp)
|
||||
target_link_libraries(test_${NAME} PRIVATE SixtyFPS Catch2::Catch2)
|
||||
|
@ -171,5 +173,6 @@ if(BUILD_TESTING)
|
|||
endmacro(sixtyfps_test)
|
||||
sixtyfps_test(datastructures)
|
||||
sixtyfps_test(interpreter)
|
||||
|
||||
sixtyfps_test(eventloop)
|
||||
target_link_libraries(test_eventloop PRIVATE Threads::Threads)
|
||||
endif()
|
||||
|
|
|
@ -683,6 +683,23 @@ inline void quit_event_loop()
|
|||
cbindgen_private::sixtyfps_quit_event_loop();
|
||||
}
|
||||
|
||||
/// Adds the specified functor to an internal queue, notifies the event loop to wake up.
|
||||
/// Once woken up, any queued up functors will be invoked.
|
||||
/// This function is thread-safe and can be called from any thread, including the one
|
||||
/// running the event loop. The provided functors will only be invoked from the thread
|
||||
/// that started the event loop.
|
||||
///
|
||||
/// You can use this to set properties or use any other SixtyFPS APIs from other threads,
|
||||
/// by collecting the code in a functor and queuing it up for invocation within the event loop.
|
||||
template<typename Functor>
|
||||
void invoke_from_event_loop(Functor f) {
|
||||
cbindgen_private::sixtyfps_post_event(
|
||||
[](void *data) { (*reinterpret_cast<Functor *>(data))(); },
|
||||
new Functor(std::move(f)),
|
||||
[](void *data) { delete reinterpret_cast<Functor *>(data); }
|
||||
);
|
||||
}
|
||||
|
||||
namespace private_api {
|
||||
|
||||
/// Registers a font by the specified path. The path must refer to an existing
|
||||
|
|
|
@ -9,6 +9,11 @@
|
|||
LICENSE END */
|
||||
/*! This scrates just expose the function used by the C++ integration */
|
||||
|
||||
use core::ffi::c_void;
|
||||
use sixtyfps_corelib::window::ffi::ComponentWindowOpaque;
|
||||
use sixtyfps_corelib::window::ComponentWindow;
|
||||
use sixtyfps_rendering_backend_default::backend;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[cold]
|
||||
pub fn use_modules() -> usize {
|
||||
|
@ -18,11 +23,6 @@ pub fn use_modules() -> usize {
|
|||
sixtyfps_corelib::use_modules()
|
||||
}
|
||||
|
||||
use sixtyfps_rendering_backend_default::backend;
|
||||
|
||||
use sixtyfps_corelib::window::ffi::ComponentWindowOpaque;
|
||||
use sixtyfps_corelib::window::ComponentWindow;
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sixtyfps_component_window_init(out: *mut ComponentWindowOpaque) {
|
||||
assert_eq!(
|
||||
|
@ -38,6 +38,33 @@ pub unsafe extern "C" fn sixtyfps_run_event_loop() {
|
|||
.run_event_loop(sixtyfps_corelib::backend::EventLoopQuitBehavior::QuitOnLastWindowClosed);
|
||||
}
|
||||
|
||||
/// Will execute the fiven functor in the main thread
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sixtyfps_post_event(
|
||||
event: extern "C" fn(user_data: *mut c_void),
|
||||
user_data: *mut c_void,
|
||||
drop_user_data: Option<extern "C" fn(*mut c_void)>,
|
||||
) {
|
||||
struct UserData {
|
||||
user_data: *mut c_void,
|
||||
drop_user_data: Option<extern "C" fn(*mut c_void)>,
|
||||
}
|
||||
impl Drop for UserData {
|
||||
fn drop(&mut self) {
|
||||
if let Some(x) = self.drop_user_data {
|
||||
x(self.user_data)
|
||||
}
|
||||
}
|
||||
}
|
||||
unsafe impl Send for UserData {}
|
||||
let ud = UserData { user_data, drop_user_data };
|
||||
|
||||
crate::backend().post_event(Box::new(move || {
|
||||
let ud = &ud;
|
||||
event(ud.user_data);
|
||||
}));
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sixtyfps_quit_event_loop() {
|
||||
crate::backend().quit_event_loop();
|
||||
|
|
|
@ -72,11 +72,3 @@ TEST_CASE("Property Tracker")
|
|||
REQUIRE(!tracker1.is_dirty());
|
||||
}
|
||||
|
||||
TEST_CASE("C++ Timers")
|
||||
{
|
||||
using namespace sixtyfps;
|
||||
|
||||
Timer testTimer(std::chrono::milliseconds(16), []() { sixtyfps::quit_event_loop(); });
|
||||
|
||||
sixtyfps::run_event_loop();
|
||||
}
|
||||
|
|
58
api/sixtyfps-cpp/tests/eventloop.cpp
Normal file
58
api/sixtyfps-cpp/tests/eventloop.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
/* LICENSE BEGIN
|
||||
This file is part of the SixtyFPS Project -- https://sixtyfps.io
|
||||
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
|
||||
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
|
||||
|
||||
SPDX-License-Identifier: GPL-3.0-only
|
||||
This file is also available under commercial licensing terms.
|
||||
Please contact info@sixtyfps.io for more information.
|
||||
LICENSE END */
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include "catch2/catch.hpp"
|
||||
|
||||
#include <sixtyfps.h>
|
||||
#include <thread>
|
||||
|
||||
TEST_CASE("C++ Timers")
|
||||
{
|
||||
using namespace sixtyfps;
|
||||
int called = 0;
|
||||
Timer testTimer(std::chrono::milliseconds(16), [&]() {
|
||||
sixtyfps::quit_event_loop();
|
||||
called += 10;
|
||||
});
|
||||
REQUIRE(called == 0);
|
||||
sixtyfps::run_event_loop();
|
||||
REQUIRE(called == 10);
|
||||
}
|
||||
|
||||
|
||||
SCENARIO("Quit from event")
|
||||
{
|
||||
int called = 0;
|
||||
sixtyfps::invoke_from_event_loop([&] {
|
||||
sixtyfps::quit_event_loop();
|
||||
called += 10;
|
||||
});
|
||||
REQUIRE(called == 0);
|
||||
sixtyfps::run_event_loop();
|
||||
REQUIRE(called == 10);
|
||||
}
|
||||
|
||||
|
||||
SCENARIO("Event from thread")
|
||||
{
|
||||
std::atomic<int> called = 0;
|
||||
auto t = std::thread([&] {
|
||||
called += 10;
|
||||
sixtyfps::invoke_from_event_loop([&] {
|
||||
called += 100;
|
||||
sixtyfps::quit_event_loop();
|
||||
});
|
||||
});
|
||||
|
||||
sixtyfps::run_event_loop();
|
||||
REQUIRE(called == 110);
|
||||
t.join();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue