Condy v1.8
C++ Asynchronous System Call Layer for Linux
Loading...
Searching...
No Matches
work_type.hpp
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <cassert>
8#include <cstddef>
9#include <cstdint>
10#include <type_traits>
11#include <utility>
12
13namespace condy {
14
15namespace detail {
16
17enum class WorkType : uint8_t {
18 Common,
19 Ignore,
20 Schedule,
21 Cancel,
22
23 // Add new work types above this line
24 WorkTypeMax,
25};
26static_assert(static_cast<uint8_t>(WorkType::WorkTypeMax) <= 8,
27 "WorkType must fit in 3 bits");
28
29inline uintptr_t encode_work_ptr(uintptr_t ptr, WorkType type) noexcept {
30 assert((ptr % 8) == 0);
31 return ptr | static_cast<uintptr_t>(type);
32}
33
34inline std::pair<void *, WorkType> decode_work(uintptr_t ptr) noexcept {
35 uintptr_t mask = (1 << 3) - 1;
36 WorkType type = static_cast<WorkType>(ptr & mask);
37 // NOLINTNEXTLINE(performance-no-int-to-ptr)
38 void *addr = reinterpret_cast<void *>(ptr & (~mask));
39 return std::make_pair(addr, type);
40}
41
42template <typename T>
43inline uintptr_t encode_work(T *ptr, WorkType type) noexcept {
44 static_assert(std::alignment_of_v<T> >= 8,
45 "Pointer must be at least 8-byte aligned");
46 return encode_work_ptr(reinterpret_cast<uintptr_t>(ptr), type);
47}
48
49inline uintptr_t encode_work(std::nullptr_t, WorkType type) noexcept {
50 return encode_work_ptr(0, type);
51}
52
53} // namespace detail
54
55} // namespace condy
The main namespace for the Condy library.
Definition condy.hpp:37