Add the lttng::eventfd class
[lttng-tools.git] / src / common / eventfd.cpp
1 /*
2 * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include "eventfd.hpp"
9
10 #include <common/exception.hpp>
11 #include <common/format.hpp>
12 #include <common/readwrite.hpp>
13
14 #include <sys/eventfd.h>
15
16 lttng::eventfd::eventfd(bool use_semaphore_semantics, std::uint64_t initial_value) :
17 file_descriptor([use_semaphore_semantics, initial_value]() {
18 int flags = ::EFD_CLOEXEC;
19
20 if (use_semaphore_semantics) {
21 flags |= ::EFD_SEMAPHORE;
22 }
23
24 const auto raw_fd = ::eventfd(initial_value, flags);
25 if (raw_fd < 0) {
26 LTTNG_THROW_POSIX("Failed to create eventfd", errno);
27 }
28
29 return raw_fd;
30 }())
31 {
32 }
33
34 void lttng::eventfd::increment(std::uint64_t value)
35 {
36 try {
37 write(&value, sizeof(value));
38 } catch (const std::exception& e) {
39 LTTNG_THROW_ERROR(fmt::format("Failed to increment eventfd: {}", e.what()));
40 }
41 }
42
43 std::uint64_t lttng::eventfd::decrement()
44 {
45 std::uint64_t value;
46
47 try {
48 read(&value, sizeof(value));
49 } catch (const std::exception& e) {
50 LTTNG_THROW_ERROR(fmt::format("Failed to decrement eventfd: {}", e.what()));
51 }
52
53 return value;
54 }
This page took 0.038667 seconds and 4 git commands to generate.