-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
124 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
set(TargetName spsc_pub) | ||
|
||
add_executable(${TargetName} ${TargetName}.cpp) | ||
target_compile_features(${TargetName} | ||
PUBLIC cxx_std_20) | ||
set_target_properties(${TargetName} | ||
PROPERTIES | ||
CXX_STANDARD_REQUIRED ON | ||
CXX_EXTENSIONS OFF) | ||
target_compile_options(${TargetName} | ||
PUBLIC -Wall -Wextra -Wattributes -Wpedantic -Wstrict-aliasing -Wcast-align -g) | ||
target_link_libraries(${TargetName} | ||
PUBLIC fmt::fmt turboq::turboq) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <turboq/BoundedSPSCRawQueue.h> | ||
#include <turboq/utils.h> | ||
|
||
int main(int argc, char* argv[]) { | ||
try { | ||
char const* queueName = "turboq.spsc"; | ||
if (argc > 1) { | ||
queueName = argv[1]; | ||
} | ||
|
||
auto producer = turboq::BoundedSPSCRawQueue(queueName, {5 * 1024 * 1024}).createProducer(); | ||
|
||
for (std::string line; std::getline(std::cin, line);) { | ||
if (line.empty()) { | ||
continue; | ||
} | ||
|
||
auto result = producer.prepare(line.size()); | ||
if (result.empty()) { | ||
throw std::runtime_error("failed to prepare buffer to send"); | ||
} | ||
std::memcpy(result.data(), line.data(), line.size()); | ||
producer.commit(); | ||
} | ||
|
||
} catch (std::exception const& e) { | ||
fmt::print(stderr, "ERROR: {}\n", e.what()); | ||
return EXIT_FAILURE; | ||
} | ||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import pyturboq | ||
|
||
consumer = pyturboq.create_consumer('turboq.spsc') | ||
|
||
while True: | ||
str = consumer.dequeue() | ||
if len(str) > 0: | ||
print(str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,55 @@ | ||
// Copyright (c) Sergey Kovalevich <inndie@gmail.com> | ||
// SPDX-License-Identifier: AGPL-3.0 | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
#include <nanobind/nanobind.h> | ||
#include <nanobind/stl/string.h> | ||
#include <nanobind/stl/string_view.h> | ||
#include <nanobind/stl/vector.h> | ||
|
||
#include <turboq/BoundedMPSCRawQueue.h> | ||
#include <turboq/BoundedSPMCRawQueue.h> | ||
#include <turboq/BoundedSPSCRawQueue.h> | ||
|
||
NAMESPACE_BEGIN(NB_NAMESPACE) | ||
NAMESPACE_BEGIN(detail) | ||
|
||
template <class T, std::size_t Extent> | ||
struct type_caster<std::span<T, Extent>> : list_caster<std::span<T, Extent>, T> {}; | ||
|
||
NAMESPACE_END(detail) | ||
NAMESPACE_END(NB_NAMESPACE) | ||
|
||
using namespace nanobind::literals; | ||
|
||
NB_MODULE(pyturboq, m) { | ||
m.def( | ||
"sample", | ||
[](int a, int b = 1) { | ||
return a + b; | ||
}, | ||
"a"_a, "b"_a = 1, "sample text"); | ||
#if 0 | ||
nanobind::class_<turboq::MappedRegion>(m, "MappedRegion"); | ||
#endif | ||
|
||
nanobind::class_<turboq::BoundedSPSCRawQueue::Producer>(m, "BoundedSPSCRawQueueProducer") | ||
.def("enqueue", [](turboq::BoundedSPSCRawQueue::Producer& self, std::string_view s) -> bool { | ||
auto result = self.prepare(s.size()); | ||
if (result.empty()) { | ||
return false; | ||
} | ||
std::memcpy(result.data(), s.data(), s.size()); | ||
self.commit(); | ||
return true; | ||
}); | ||
|
||
nanobind::class_<turboq::BoundedSPSCRawQueue::Consumer>(m, "BoundedSPSCRawQueueConsumer") | ||
.def("dequeue", [](turboq::BoundedSPSCRawQueue::Consumer& self) -> std::string { | ||
auto result = self.fetch(); | ||
if (!result.empty()) { | ||
std::string data(std::bit_cast<char const*>(result.data()), result.size()); | ||
self.consume(); | ||
return data; | ||
} | ||
return std::string(); | ||
}); | ||
|
||
m.def("create_consumer", [](std::string_view name) -> turboq::BoundedSPSCRawQueue::Consumer { | ||
return turboq::BoundedSPSCRawQueue(name).createConsumer(); | ||
}); | ||
} |