Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire committed Jun 12, 2024
1 parent 041e3a7 commit 22b7a9d
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 11 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ X.Y.Z Release notes (YYYY-MM-DD)
* Fixed a compilation issue seen in MSBuild 17.10.4 due to usage of `std::apply`.

### Enhancements
* None
* Add `realm::default_scheduler::set_default_factory(std::function<std::shared_ptr<realm::scheduler>()>&& factory_fn)` for generating a default scheduler.
Set your scheduler factory before instantiating a `realm::db_config`.
* Add `realm::default_scheduler::make_uv(uv_loop_t* loop);` which generates a scheduler powered by libuv with a user defined `uv_loop_t`.
* Add `realm::default_scheduler::make_default()` which generates a platform default scheduler if `realm::default_scheduler::set_default_factory` is not set.

### Compatibility
* Fileformat: Generates files with format v24. Reads and automatically upgrade from fileformat v10.
Expand Down
2 changes: 1 addition & 1 deletion include/cpprealm/internal/bridge/realm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ namespace realm::internal::bridge {
const std::shared_ptr<struct scheduler>& scheduler);
[[nodiscard]] std::string path() const;
[[nodiscard]] struct sync_config sync_config() const;
[[nodiscard]] std::shared_ptr<struct scheduler> scheduler();
[[nodiscard]] std::shared_ptr<struct scheduler> scheduler() const;
operator RealmConfig() const; //NOLINT(google-explicit-constructor)
void set_path(const std::string&);
void set_schema(const std::vector<object_schema>&);
Expand Down
1 change: 0 additions & 1 deletion include/cpprealm/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ namespace realm {
#define DECLARE_MANAGED_PROPERTY_NAME(cls, p) #p,
#define DECLARE_COND_UNMANAGED_TO_MANAGED(cls, p) if constexpr (std::is_same_v<decltype(ptr), decltype(&cls::p)>) { return &managed<cls>::p; }

#include <tuple>
#include <utility>

#include <cpprealm/internal/bridge/col_key.hpp>
Expand Down
2 changes: 1 addition & 1 deletion include/cpprealm/schedulers/default_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace realm::default_scheduler {
* `scheduler::make_default()` is called. This function is not thread-safe
* and must be called before any schedulers are created.
*/
void set_default_factory(std::shared_ptr<scheduler> (*factory)());
void set_default_factory(std::function<std::shared_ptr<scheduler>()>&& factory_fn);

/**
* Create a new instance of the scheduler type returned by the default
Expand Down
4 changes: 4 additions & 0 deletions src/cpprealm/internal/bridge/realm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ namespace realm::internal::bridge {
return get_config()->sync_config;
}

struct std::shared_ptr<scheduler> realm::config::scheduler() const {
return std::make_shared<realm_core_scheduler>(realm_core_scheduler(get_config()->scheduler));
}

struct std::shared_ptr<scheduler> realm::scheduler() const {
return std::make_shared<realm_core_scheduler>(realm_core_scheduler(m_realm->scheduler()));
}
Expand Down
1 change: 0 additions & 1 deletion src/cpprealm/internal/scheduler/realm_core_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace realm::internal {
bool realm_core_scheduler::is_same_as(const scheduler *other) const noexcept {
if (auto o = dynamic_cast<const realm_core_scheduler *>(other)) {
return o->s->is_same_as(this->s.get());
;
}
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/cpprealm/schedulers/default_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#endif

namespace realm::default_scheduler {
std::shared_ptr<scheduler> (*s_factory)() = make_platform_default;
std::function<std::shared_ptr<scheduler>()> s_factory = make_platform_default;

std::shared_ptr<scheduler> make_platform_default() {
#if REALM_PLATFORM_APPLE || REALM_ANDROID && !defined(REALM_AOSP_VENDOR)
Expand All @@ -20,8 +20,8 @@ namespace realm::default_scheduler {
#endif
}

void set_default_factory(std::shared_ptr<scheduler> (*factory)()) {
s_factory = std::move(factory);
void set_default_factory(std::function<std::shared_ptr<scheduler>()>&& factory_fn) {
s_factory = std::move(factory_fn);
}

/**
Expand Down
21 changes: 18 additions & 3 deletions tests/db/run_loop_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,20 @@ TEST_CASE("user defined uv_loop", "[scheduler]") {
v2.wait(lock2, [&] { return signal2; });
}

SECTION("main thread", "[run loops]") {
SECTION("main thread", "[scheduler]") {
realm::notification_token t1;
realm::notification_token t2;

bool signal = false;

auto loop = uv_loop_new();
auto scheduler = realm::default_scheduler::make_uv(loop);
auto obj = realm::AllTypesObject();
auto config = realm::db_config(path, realm::default_scheduler::make_uv(loop));
auto config = realm::db_config();
config.set_path(path);
config.set_scheduler(scheduler);
auto realm = realm::db(config);

auto realm = realm::db(std::move(config));
auto managed_obj = realm.write([&realm, &obj] {
return realm.add(std::move(obj));
});
Expand All @@ -168,6 +171,18 @@ TEST_CASE("user defined uv_loop", "[scheduler]") {
uv_loop_close(loop);
free(loop);
}

SECTION("set default factory", "[scheduler]") {
auto loop = uv_loop_new();
auto scheduler = realm::default_scheduler::make_uv(loop);

realm::default_scheduler::set_default_factory([&]() { return scheduler; });
auto default_scheduler = realm::default_scheduler::make_default();
CHECK(scheduler->is_same_as(default_scheduler.get()));

uv_loop_close(loop);
free(loop);
}
}

#endif

0 comments on commit 22b7a9d

Please sign in to comment.