Skip to content

Commit

Permalink
[DXFC-402] Implement adding multiple symbols and removing symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed May 22, 2023
1 parent fa70e60 commit 3c10ed7
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 83 deletions.
16 changes: 2 additions & 14 deletions include/dxfeed_graal_cpp_api/api/DXFeedSubscription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,12 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
debug("DXFeedSubscription(eventTypes = {})", namesToString(begin, end));
}

auto size = std::distance(begin, end);
auto list = handler_utils::EventClassList::create(begin, end);

if (size <= 0) {
if (!list) {
return;
}

auto list = handler_utils::EventClassList::create(size);

if (list->isEmpty()) {
return;
}

std::size_t i = 0;

for (auto it = begin; it != end; it++, i++) {
list->set(i, it->getId());
}

handler_ = createSubscriptionHandlerFromEventClassList(list);
}

Expand Down
60 changes: 54 additions & 6 deletions include/dxfeed_graal_cpp_api/internal/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

#pragma once

#define DXFCPP_DEBUG_ISOLATES
#ifndef DXFCPP_TRACE_ISOLATES
# define DXFCPP_TRACE_ISOLATES 0
#endif

#ifndef DXFCPP_TRACE_LISTS
# define DXFCPP_TRACE_LISTS 1
#endif

#ifdef __cpp_lib_bit_cast
# include <bit>
Expand Down Expand Up @@ -32,14 +38,21 @@ struct DXEndpointStateChangeListener {};

#if defined(NDEBUG) && !defined(DXFCPP_DEBUG)
constexpr bool isDebug = false;
constexpr bool isDebugIsolates = false;
constexpr bool traceIsolates = false;
constexpr bool traceLists = false;
#else
constexpr bool isDebug = true;

# ifdef DXFCPP_DEBUG_ISOLATES
constexpr bool isDebugIsolates = true;
# if DXFCPP_TRACE_ISOLATES == 1
constexpr bool traceIsolates = true;
# else
constexpr bool isDebugIsolates = false;
constexpr bool traceIsolates = false;
# endif

# if DXFCPP_TRACE_LISTS == 1
constexpr bool traceLists = true;
# else
constexpr bool traceLists = false;
# endif

#endif
Expand Down Expand Up @@ -154,10 +167,22 @@ inline std::string debugPrefixStr() {
return fmt::format("D {} [{}]", nowStr(), tid.str());
}

inline std::string tracePrefixStr() {
std::ostringstream tid{};

tid << std::this_thread::get_id();

return fmt::format("T {} [{}]", nowStr(), tid.str());
}

template <typename... Args> inline void debug(std::string_view format, Args &&...args) {
vprint(std::cerr, "{} {}\n", debugPrefixStr(), vformat(format, std::forward<Args>(args)...));
}

template <typename... Args> inline void trace(std::string_view format, Args &&...args) {
vprint(std::cerr, "{} ~~ {}\n", tracePrefixStr(), vformat(format, std::forward<Args>(args)...));
}

namespace handler_utils {

template <typename T> struct JavaObjectHandler {
Expand Down Expand Up @@ -185,7 +210,28 @@ template <typename T> struct JavaObjectHandler {
};

struct EventClassList {
static std::unique_ptr<EventClassList> create(std::size_t size) noexcept;
template <typename EventTypeIt>
static std::unique_ptr<EventClassList> create(EventTypeIt begin, EventTypeIt end) noexcept {
auto size = std::distance(begin, end);

if (size <= 0) {
return {};
}

auto list = handler_utils::EventClassList::create(size);

if (list->isEmpty()) {
return {};
}

std::size_t i = 0;

for (auto it = begin; it != end; it++, i++) {
list->set(i, it->getId());
}

return list;
}

void set(std::size_t index, std::uint32_t id) noexcept;

Expand All @@ -198,6 +244,8 @@ struct EventClassList {
~EventClassList() noexcept;

private:
static std::unique_ptr<EventClassList> create(std::size_t size) noexcept;

EventClassList() noexcept;

struct Impl;
Expand Down
96 changes: 48 additions & 48 deletions include/dxfeed_graal_cpp_api/internal/Isolate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ class Isolate final {

this->idx = idx++;

if constexpr (isDebugIsolates) {
debug("IsolateThread{{{}, isMain = {}, tid = {}, idx = {}}}()", bit_cast<std::size_t>(handle), isMain,
if constexpr (traceIsolates) {
trace("IsolateThread{{{}, isMain = {}, tid = {}, idx = {}}}()", bit_cast<std::size_t>(handle), isMain,
tid, idx);
}
}

CEntryPointErrors detach() noexcept {
if constexpr (isDebugIsolates) {
debug("{}::detach()", toString());
if constexpr (traceIsolates) {
trace("{}::detach()", toString());
}

// OK if nothing is attached.
if (!handle) {
if constexpr (isDebugIsolates) {
debug("\tNot attached");
if constexpr (traceIsolates) {
trace("\tNot attached");
}

return CEntryPointErrors::NO_ERROR;
Expand All @@ -59,8 +59,8 @@ class Isolate final {
auto result = CEntryPointErrors::valueOf(graal_detach_thread(handle));

if (result == CEntryPointErrors::NO_ERROR) {
if constexpr (isDebugIsolates) {
debug("\tDetached");
if constexpr (traceIsolates) {
trace("\tDetached");
}

handle = nullptr;
Expand All @@ -70,13 +70,13 @@ class Isolate final {
}

CEntryPointErrors detachAllThreadsAndTearDownIsolate() noexcept {
if constexpr (isDebugIsolates) {
debug("{}::detachAllThreadsAndTearDownIsolate()", toString());
if constexpr (traceIsolates) {
trace("{}::detachAllThreadsAndTearDownIsolate()", toString());
}

if (!handle) {
if constexpr (isDebugIsolates) {
debug("\tNot attached");
if constexpr (traceIsolates) {
trace("\tNot attached");
}

return CEntryPointErrors::NO_ERROR;
Expand All @@ -85,8 +85,8 @@ class Isolate final {
auto result = CEntryPointErrors::valueOf(graal_detach_all_threads_and_tear_down_isolate(handle));

if (result == CEntryPointErrors::NO_ERROR) {
if constexpr (isDebugIsolates) {
debug("\tAll threads have been detached. The isolate has been teared down.");
if constexpr (traceIsolates) {
trace("\tAll threads have been detached. The isolate has been teared down.");
}

handle = nullptr;
Expand All @@ -96,13 +96,13 @@ class Isolate final {
}

~IsolateThread() noexcept {
if constexpr (isDebugIsolates) {
debug("~{}()", toString());
if constexpr (traceIsolates) {
trace("~{}()", toString());
}

if (isMain) {
if constexpr (isDebugIsolates) {
debug("\tThis is the main thread");
if constexpr (traceIsolates) {
trace("\tThis is the main thread");
}

return;
Expand All @@ -128,15 +128,15 @@ class Isolate final {
currentIsolateThread_.handle = mainIsolateThreadHandle;
currentIsolateThread_.isMain = true;

if constexpr (isDebugIsolates) {
debug("Isolate{{{}, main = {}, current = {}}}()", bit_cast<std::size_t>(handle),
if constexpr (traceIsolates) {
trace("Isolate{{{}, main = {}, current = {}}}()", bit_cast<std::size_t>(handle),
mainIsolateThread_.toString(), currentIsolateThread_.toString());
}
}

static std::shared_ptr<Isolate> create() noexcept {
if constexpr (isDebugIsolates) {
debug("Isolate::create()");
if constexpr (traceIsolates) {
trace("Isolate::create()");
}

GraalIsolateHandle graalIsolateHandle{};
Expand All @@ -147,38 +147,38 @@ class Isolate final {

auto result = std::shared_ptr<Isolate>{new Isolate{graalIsolateHandle, graalIsolateThreadHandle}};

if constexpr (isDebugIsolates) {
debug("Isolate::create() -> *{}", result->toString());
if constexpr (traceIsolates) {
trace("Isolate::create() -> *{}", result->toString());
}

return result;
}

if constexpr (isDebugIsolates) {
debug("\t-> nullptr");
if constexpr (traceIsolates) {
trace("\t-> nullptr");
}

return nullptr;
}

CEntryPointErrors attach() noexcept {
if constexpr (isDebugIsolates) {
debug("{}::attach()", toString());
if constexpr (traceIsolates) {
trace("{}::attach()", toString());
}

// We will not re-attach.
if (!currentIsolateThread_.handle) {
if constexpr (isDebugIsolates) {
debug("\tNeeds to be attached.");
if constexpr (traceIsolates) {
trace("\tNeeds to be attached.");
}

GraalIsolateThreadHandle newIsolateThreadHandle{};

if (auto result = CEntryPointErrors::valueOf(graal_attach_thread(handle_, &newIsolateThreadHandle));
result != CEntryPointErrors::NO_ERROR) {

if constexpr (isDebugIsolates) {
debug("\t-> {}", result.getDescription());
if constexpr (traceIsolates) {
trace("\t-> {}", result.getDescription());
}

return result;
Expand All @@ -187,21 +187,21 @@ class Isolate final {
currentIsolateThread_.handle = newIsolateThreadHandle;
currentIsolateThread_.isMain = mainIsolateThread_.handle == newIsolateThreadHandle;

if constexpr (isDebugIsolates) {
debug("\tAttached: {}", currentIsolateThread_.toString());
if constexpr (traceIsolates) {
trace("\tAttached: {}", currentIsolateThread_.toString());
}
} else {
if constexpr (isDebugIsolates) {
debug("\tCached: {}", currentIsolateThread_.toString());
if constexpr (traceIsolates) {
trace("\tCached: {}", currentIsolateThread_.toString());
}
}

return CEntryPointErrors::NO_ERROR;
}

GraalIsolateThreadHandle get() noexcept {
if constexpr (isDebugIsolates) {
debug("{}::get()", toString());
if constexpr (traceIsolates) {
trace("{}::get()", toString());
}

return graal_get_current_thread(handle_);
Expand All @@ -213,23 +213,23 @@ class Isolate final {
Isolate &operator=(const Isolate &) = delete;

static std::shared_ptr<Isolate> getInstance() noexcept {
if constexpr (isDebugIsolates) {
debug("Isolate::getInstance()");
if constexpr (traceIsolates) {
trace("Isolate::getInstance()");
}

static std::shared_ptr<Isolate> instance = create();

if constexpr (isDebugIsolates) {
debug("Isolate::getInstance() -> *{}", instance->toString());
if constexpr (traceIsolates) {
trace("Isolate::getInstance() -> *{}", instance->toString());
}

return instance;
}

template <typename F>
auto runIsolated(F &&f) -> std::variant<CEntryPointErrors, std::invoke_result_t<F &&, GraalIsolateThreadHandle>> {
if constexpr (isDebugIsolates) {
debug("{}::runIsolated({})", toString(), bit_cast<std::size_t>(&f));
if constexpr (traceIsolates) {
trace("{}::runIsolated({})", toString(), bit_cast<std::size_t>(&f));
}

// Perhaps the code is already running within the GraalVM thread (for example, we are in a listener)
Expand All @@ -238,8 +238,8 @@ class Isolate final {
}

if (auto result = attach(); result != CEntryPointErrors::NO_ERROR) {
if constexpr (isDebugIsolates) {
debug("\t-> {}", result.getDescription());
if constexpr (traceIsolates) {
trace("\t-> {}", result.getDescription());
}

return result;
Expand All @@ -266,8 +266,8 @@ class Isolate final {
}

~Isolate() {
if constexpr (isDebugIsolates) {
debug("~Isolate()");
if constexpr (traceIsolates) {
trace("~Isolate()");
}
}

Expand Down
Loading

0 comments on commit 3c10ed7

Please sign in to comment.