Condy v1.6.0
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
15enum class WorkType : uint8_t {
16 Common,
17 Ignore,
18 Schedule,
19 Cancel,
20
21 // Add new work types above this line
22 WorkTypeMax,
23};
24static_assert(static_cast<uint8_t>(WorkType::WorkTypeMax) <= 8,
25 "WorkType must fit in 3 bits");
26
27namespace detail {
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
34} // namespace detail
35
36inline std::pair<void *, WorkType> decode_work(uintptr_t ptr) noexcept {
37 uintptr_t mask = (1 << 3) - 1;
38 WorkType type = static_cast<WorkType>(ptr & mask);
39 // NOLINTNEXTLINE(performance-no-int-to-ptr)
40 void *addr = reinterpret_cast<void *>(ptr & (~mask));
41 return std::make_pair(addr, type);
42}
43
44template <typename T>
45inline uintptr_t encode_work(T *ptr, WorkType type) noexcept {
46 static_assert(std::alignment_of_v<T> >= 8,
47 "Pointer must be at least 8-byte aligned");
48 return detail::encode_work_ptr(reinterpret_cast<uintptr_t>(ptr), type);
49}
50
51inline uintptr_t encode_work(std::nullptr_t, WorkType type) noexcept {
52 return detail::encode_work_ptr(0, type);
53}
54
55} // namespace condy
The main namespace for the Condy library.
Definition condy.hpp:30