Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 3c5f479

Browse files
authored
Merge pull request #1119 from karczex/mpsc_queue_docs
Mpsc queue docs
2 parents 8e5eec1 + 4f2acb8 commit 3c5f479

File tree

4 files changed

+263
-66
lines changed

4 files changed

+263
-66
lines changed

examples/CMakeLists.txt

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,11 @@ include_directories(${LIBPMEMOBJ_INCLUDE_DIRS} .)
4545
link_directories(${LIBPMEMOBJ_LIBRARY_DIRS})
4646

4747
# Add developer checks
48-
add_cppstyle(examples-common ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)
49-
add_check_whitespace(examples-common ${CMAKE_CURRENT_SOURCE_DIR}/*.*)
50-
51-
add_cppstyle(examples-array ${CMAKE_CURRENT_SOURCE_DIR}/array/*.*pp)
52-
add_check_whitespace(examples-array ${CMAKE_CURRENT_SOURCE_DIR}/array/*.*)
53-
54-
add_cppstyle(examples-map_cli ${CMAKE_CURRENT_SOURCE_DIR}/map_cli/*.*pp)
55-
add_check_whitespace(examples-map_cli ${CMAKE_CURRENT_SOURCE_DIR}/map_cli/*.*)
56-
57-
add_cppstyle(examples-panaconda ${CMAKE_CURRENT_SOURCE_DIR}/panaconda/*.*pp)
58-
add_check_whitespace(examples-panaconda ${CMAKE_CURRENT_SOURCE_DIR}/panaconda/*.*)
59-
60-
add_cppstyle(examples-pman ${CMAKE_CURRENT_SOURCE_DIR}/pman/*.*pp)
61-
add_check_whitespace(examples-pman ${CMAKE_CURRENT_SOURCE_DIR}/pman/*.*)
62-
63-
add_cppstyle(examples-pmpong ${CMAKE_CURRENT_SOURCE_DIR}/pmpong/*.*pp)
64-
add_check_whitespace(examples-pmpong ${CMAKE_CURRENT_SOURCE_DIR}/pmpong/*.*)
65-
66-
add_cppstyle(examples-queue ${CMAKE_CURRENT_SOURCE_DIR}/queue/*.*pp)
67-
add_check_whitespace(examples-queue ${CMAKE_CURRENT_SOURCE_DIR}/queue/*.*)
68-
69-
add_cppstyle(examples-simplekv ${CMAKE_CURRENT_SOURCE_DIR}/simplekv/*.*pp)
70-
add_check_whitespace(examples-simplekv ${CMAKE_CURRENT_SOURCE_DIR}/simplekv/*.*)
71-
72-
add_cppstyle(examples-simplekv_rebuild ${CMAKE_CURRENT_SOURCE_DIR}/simplekv_rebuild/*.*pp)
73-
add_check_whitespace(examples-simplekv_rebuild ${CMAKE_CURRENT_SOURCE_DIR}/simplekv_rebuild/*.*)
74-
75-
add_cppstyle(examples-slab ${CMAKE_CURRENT_SOURCE_DIR}/slab/*.*pp)
76-
add_check_whitespace(examples-slab ${CMAKE_CURRENT_SOURCE_DIR}/slab/*.*)
77-
78-
add_cppstyle(examples-string ${CMAKE_CURRENT_SOURCE_DIR}/string/*.*pp)
79-
add_check_whitespace(examples-string ${CMAKE_CURRENT_SOURCE_DIR}/string/*.*)
80-
81-
add_cppstyle(examples-v ${CMAKE_CURRENT_SOURCE_DIR}/v/*.*pp)
82-
add_check_whitespace(examples-v ${CMAKE_CURRENT_SOURCE_DIR}/v/*.*)
48+
add_cppstyle(examples ${CMAKE_CURRENT_SOURCE_DIR}/*/*.*pp)
49+
add_check_whitespace(examples ${CMAKE_CURRENT_SOURCE_DIR}/*/*.*)
8350

84-
add_cppstyle(examples-segment_vector ${CMAKE_CURRENT_SOURCE_DIR}/segment_vector/*.*pp)
85-
add_check_whitespace(examples-segment_vector ${CMAKE_CURRENT_SOURCE_DIR}/segment_vector/*.*)
86-
87-
add_cppstyle(examples-concurrent_hash_map ${CMAKE_CURRENT_SOURCE_DIR}/concurrent_hash_map/*.*pp)
88-
add_check_whitespace(examples-concurrent_hash_map ${CMAKE_CURRENT_SOURCE_DIR}/concurrent_hash_map/*.*)
89-
90-
add_cppstyle(examples-defrag ${CMAKE_CURRENT_SOURCE_DIR}/defrag/*.*pp)
91-
add_check_whitespace(examples-defrag ${CMAKE_CURRENT_SOURCE_DIR}/defrag/*.*)
92-
93-
add_cppstyle(examples-pool ${CMAKE_CURRENT_SOURCE_DIR}/pool/*.*pp)
94-
add_check_whitespace(examples-pool ${CMAKE_CURRENT_SOURCE_DIR}/pool/*.*)
95-
96-
add_cppstyle(examples-mutex ${CMAKE_CURRENT_SOURCE_DIR}/mutex/*.*pp)
97-
add_check_whitespace(examples-mutex ${CMAKE_CURRENT_SOURCE_DIR}/mutex/*.*)
98-
99-
add_cppstyle(examples-make_persistent ${CMAKE_CURRENT_SOURCE_DIR}/make_persistent/*.*pp)
100-
add_check_whitespace(examples-make_persistent ${CMAKE_CURRENT_SOURCE_DIR}/make_persistent/*.*)
101-
102-
add_cppstyle(examples-persistent ${CMAKE_CURRENT_SOURCE_DIR}/persistent/*.*pp)
103-
add_check_whitespace(examples-persistent ${CMAKE_CURRENT_SOURCE_DIR}/persistent/*.*)
104-
105-
add_cppstyle(examples-transaction ${CMAKE_CURRENT_SOURCE_DIR}/transaction/*.*pp)
106-
add_check_whitespace(examples-transaction ${CMAKE_CURRENT_SOURCE_DIR}/transaction/*.*)
107-
108-
add_cppstyle(examples-inline_string ${CMAKE_CURRENT_SOURCE_DIR}/inline_string/*.*pp)
109-
add_check_whitespace(examples-inline_string ${CMAKE_CURRENT_SOURCE_DIR}/inline_string/*.*)
110-
111-
add_cppstyle(examples-radix_tree ${CMAKE_CURRENT_SOURCE_DIR}/radix_tree/*.*pp)
112-
add_check_whitespace(examples-radix_tree ${CMAKE_CURRENT_SOURCE_DIR}/radix_tree/*.*)
51+
add_cppstyle(examples-common ${CMAKE_CURRENT_SOURCE_DIR}/*.*pp)
52+
add_check_whitespace(examples-common ${CMAKE_CURRENT_SOURCE_DIR}/*.*)
11353

11454
# Find required packages for examples
11555
if(PKG_CONFIG_FOUND)
@@ -233,3 +173,5 @@ add_example(make_persistent make_persistent/make_persistent.cpp)
233173
add_example(persistent persistent/persistent.cpp)
234174

235175
add_example(transaction transaction/transaction.cpp)
176+
177+
add_example(mpsc_queue mpsc_queue/mpsc_queue.cpp)

examples/mpsc_queue/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright 2021, Intel Corporation
3+
4+
cmake_minimum_required(VERSION 3.3)
5+
project(mpsc_queue CXX)
6+
7+
set(CXX_STANDARD_REQUIRED ON)
8+
set(CMAKE_CXX_STANDARD 11)
9+
10+
find_package(PkgConfig QUIET)
11+
if(PKG_CONFIG_FOUND)
12+
pkg_check_modules(LIBPMEMOBJ++ REQUIRED libpmemobj++)
13+
else()
14+
find_package(LIBPMEMOBJ++ REQUIRED)
15+
endif()
16+
17+
link_directories(${LIBPMEMOBJ++_LIBRARY_DIRS})
18+
19+
add_executable(mpsc_queue.cpp)
20+
target_include_directories(mpsc_queue PUBLIC ${LIBPMEMOBJ++_INCLUDE_DIRS} . ..)
21+
target_link_libraries(mpsc_queue ${LIBPMEMOBJ++_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

examples/mpsc_queue/mpsc_queue.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/* Copyright 2021, Intel Corporation */
3+
4+
/**
5+
* mpsc_queue.cpp -- example which shows how to use
6+
* pmem::obj::experimental::mpsc_queue
7+
*/
8+
9+
#include <libpmemobj++/experimental/mpsc_queue.hpp>
10+
#include <libpmemobj++/make_persistent.hpp>
11+
#include <libpmemobj++/persistent_ptr.hpp>
12+
#include <libpmemobj++/transaction.hpp>
13+
14+
#include <iostream>
15+
#include <string>
16+
17+
void
18+
show_usage(char *argv[])
19+
{
20+
std::cerr << "usage: " << argv[0] << " file-name" << std::endl;
21+
}
22+
23+
//! [mpsc_queue_single_threaded_example]
24+
25+
struct root {
26+
pmem::obj::persistent_ptr<
27+
pmem::obj::experimental::mpsc_queue::pmem_log_type>
28+
log;
29+
};
30+
31+
void
32+
single_threaded(pmem::obj::pool<root> pop)
33+
{
34+
35+
std::vector<std::string> values_to_produce = {"xxx", "aaaaaaa", "bbbbb",
36+
"cccc", "ddddddddddd"};
37+
pmem::obj::persistent_ptr<root> proot = pop.root();
38+
39+
/* Create mpsc_queue, which uses pmem_log_type object to store
40+
* data. */
41+
auto queue = pmem::obj::experimental::mpsc_queue(*proot->log, 1);
42+
43+
/* Consume data, which was stored in the queue in the previous run of
44+
* the application. */
45+
//! [try_consume_batch]
46+
queue.try_consume_batch(
47+
[&](pmem::obj::experimental::mpsc_queue::batch_type rd_acc) {
48+
for (pmem::obj::string_view str : rd_acc) {
49+
std::cout << std::string(str.data(), str.size())
50+
<< std::endl;
51+
}
52+
});
53+
//! [try_consume_batch]
54+
/* Produce and consume data. */
55+
//! [register_worker]
56+
pmem::obj::experimental::mpsc_queue::worker worker =
57+
queue.register_worker();
58+
//! [register_worker]
59+
for (std::string &value : values_to_produce) {
60+
//! [try_produce]
61+
/* Produce data. */
62+
worker.try_produce(value);
63+
//! [try_produce]
64+
/* Consume produced data. */
65+
queue.try_consume_batch(
66+
[&](pmem::obj::experimental::mpsc_queue::batch_type
67+
rd_acc) {
68+
for (pmem::obj::string_view str : rd_acc) {
69+
std::cout << std::string(str.data(),
70+
str.size())
71+
<< std::endl;
72+
}
73+
});
74+
}
75+
//! [try_produce_string_view]
76+
/* Produce data to be consumed in next run of the application. */
77+
worker.try_produce("Left for next run");
78+
//! [try_produce_string_view]
79+
}
80+
81+
//! [mpsc_queue_single_threaded_example]
82+
83+
int
84+
main(int argc, char *argv[])
85+
{
86+
if (argc < 2) {
87+
show_usage(argv);
88+
return 1;
89+
}
90+
91+
const char *path = argv[1];
92+
93+
static constexpr size_t QUEUE_SIZE = 1000;
94+
95+
pmem::obj::pool<root> pop;
96+
try {
97+
pop = pmem::obj::pool<root>::open(path, "mpsc_queue");
98+
if (pop.root()->log == nullptr) {
99+
pmem::obj::transaction::run(pop, [&] {
100+
pop.root()->log = pmem::obj::make_persistent<
101+
pmem::obj::experimental::mpsc_queue::
102+
pmem_log_type>(QUEUE_SIZE);
103+
});
104+
}
105+
single_threaded(pop);
106+
107+
} catch (pmem::pool_error &e) {
108+
std::cerr << e.what() << std::endl;
109+
std::cerr
110+
<< "To create pool run: pmempool create obj --layout=mpsc_queue -s 100M path_to_pool"
111+
<< std::endl;
112+
} catch (std::exception &e) {
113+
std::cerr << e.what() << std::endl;
114+
}
115+
116+
try {
117+
pop.close();
118+
} catch (const std::logic_error &e) {
119+
std::cerr << e.what() << std::endl;
120+
}
121+
return 0;
122+
}

0 commit comments

Comments
 (0)