Skip to content

Commit

Permalink
RCPP-41 Update vcpkg
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire committed Mar 13, 2024
1 parent 1ad46a7 commit ab627b9
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 109 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ else()
endif()
endif()

if(MSVC AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
if(MSVC AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY AND NOT DEFINED BUILD_SHARED_LIBS)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

Expand Down
4 changes: 4 additions & 0 deletions ports/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ vcpkg_execute_required_process(

set(CPPREALM_CMAKE_OPTIONS -DREALM_CPP_NO_TESTS=ON -DREALM_CORE_SUBMODULE_BUILD=OFF)

if("use-libuv" IN_LIST FEATURES)
list(APPEND CPPREALM_CMAKE_OPTIONS -DCPPREALM_USE_UV=1)
endif()

if (ANDROID OR WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
list(APPEND CPPREALM_CMAKE_OPTIONS -DREALM_USE_SYSTEM_OPENSSL=ON)
endif()
Expand Down
8 changes: 3 additions & 5 deletions ports/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"description": "Realm is a mobile database that runs directly inside phones, tablets or wearables.",
"homepage": "https://github.com/realm/realm-cpp",
"license": "Apache-2.0",
"features": {
"use-libuv": { "description": "Use LibUV for scheduler.", "dependencies": ["libuv"]}
},
"dependencies": [
{
"name": "vcpkg-cmake",
Expand All @@ -13,11 +16,6 @@
"name": "vcpkg-cmake-config",
"host": true
},
{
"name": "libuv",
"version>=": "1.43.0",
"platform": "!osx"
},
{
"name": "curl",
"version>=": "8.4.0",
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ if (APPLE)
endif()
if (DEFINED CPPREALM_USE_UV)
list(APPEND HEADERS cpprealm/schedulers/uv_scheduler.hpp)
list(APPEND SOURCES cpprealm/schedulers/uv_scheduler.cpp)
endif()

include(GNUInstallDirs)
Expand Down
97 changes: 0 additions & 97 deletions src/cpprealm/schedulers/uv_scheduler.cpp

This file was deleted.

92 changes: 91 additions & 1 deletion src/cpprealm/schedulers/uv_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,97 @@
#include <uv.h>

namespace realm {
std::shared_ptr<scheduler> make_uv_scheduler(uv_loop_t* run_loop);

class invocation_queue {
public:
void push(std::function<void()>&&);
void invoke_all();

private:
std::mutex m_mutex;
std::vector<std::function<void()>> m_functions;
};

void invocation_queue::push(std::function<void()>&& fn)
{
std::lock_guard lock(m_mutex);
m_functions.push_back(std::move(fn));
}

void invocation_queue::invoke_all()
{
std::vector<std::function<void()>> functions;
{
std::lock_guard lock(m_mutex);
functions.swap(m_functions);
}
for (auto&& fn : functions) {
fn();
}
}

class uv_scheduler final : public realm::scheduler {
public:
uv_scheduler(uv_loop_t* loop)
: m_handle(std::make_unique<uv_async_t>()) {
int err = uv_async_init(loop, m_handle.get(), [](uv_async_t *handle) {
if (!handle->data) {
return;
}
auto &data = *static_cast<struct data *>(handle->data);
if (data.close_requested) {
uv_close(reinterpret_cast<uv_handle_t *>(handle), [](uv_handle_t *handle) {
delete reinterpret_cast<struct data *>(handle->data);
delete reinterpret_cast<uv_async_t *>(handle);
});
} else {
data.queue.invoke_all();
}
});
if (err < 0) {
throw std::runtime_error("uv_async_init failed");
}
m_handle->data = new data;
}

~uv_scheduler() {
if (m_handle && m_handle->data) {
static_cast<struct data *>(m_handle->data)->close_requested = true;
uv_async_send(m_handle.get());
// Don't delete anything here as we need to delete it from within the event loop instead
m_handle.release();
}
}

bool is_on_thread() const noexcept override {
return m_id == std::this_thread::get_id();
}
bool is_same_as(const scheduler *other) const noexcept override {
auto o = dynamic_cast<const uv_scheduler *>(other);
return (o && (o->m_id == m_id));
}
bool can_invoke() const noexcept override {
return true;
}

void invoke(std::function<void()> &&fn) override {
auto &data = *static_cast<struct data *>(m_handle->data);
data.queue.push(std::move(fn));
uv_async_send(m_handle.get());
}

private:
struct data {
invocation_queue queue;
std::atomic<bool> close_requested = {false};
};
std::unique_ptr<uv_async_t> m_handle;
std::thread::id m_id = std::this_thread::get_id();
};

std::shared_ptr<scheduler> make_uv_scheduler(uv_loop_t* run_loop) {
return std::make_shared<uv_scheduler>(run_loop);
}
}; // namespace realm
#endif // __has_include(<uv.h>) && defined(CPPREALM_USE_UV)

Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ if(DEFINED CPPREALM_USE_UV)
if (DEFINED USES_CONAN)
find_package(libuv REQUIRED)
target_compile_definitions(cpprealm_db_tests PUBLIC CPPREALM_USE_UV=1)
elseif(DEFINED VCPKG_OVERLAY_PORTS)
find_package(libuv CONFIG REQUIRED)
target_compile_definitions(cpprealm_db_tests PUBLIC CPPREALM_USE_UV=1)
target_link_libraries(cpprealm_db_tests PUBLIC libuv::uv_a)
else()
include(FetchContent)
set(liUV_Git_TAG "v1.48.0")
Expand Down
3 changes: 0 additions & 3 deletions tests/db/run_loop_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

#if __APPLE__
#include <cpprealm/schedulers/apple_scheduler.hpp>
#endif

#if __APPLE__

void run_until(CFRunLoopRef loop, std::function<bool()> predicate)
{
Expand Down
7 changes: 6 additions & 1 deletion tests/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"dependencies": [
"cpprealm"
{
"name": "cpprealm",
"features": [
"use-libuv"
]
}
]
}
6 changes: 6 additions & 0 deletions tools/cmake/Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ if(NOT APPLE AND NOT EMSCRIPTEN AND NOT WINDOWS_STORE AND NOT ANDROID)
find_package(CURL)
endif()

if (@CPPREALM_USE_UV@)
set(CPPREALM_USE_UV 1)
find_package(libuv REQUIRED)
find_dependency(libuv)
endif()

find_package(Realm CONFIG REQUIRED)

check_required_components(@PROJECT_NAME@)

0 comments on commit ab627b9

Please sign in to comment.