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 <cstdint>
9#include <utility>
10
11namespace condy {
12
13enum class WorkType : uint8_t {
14 Common,
15 Ignore,
16 Schedule,
17 Cancel,
18
19 // Add new work types above this line
20 WorkTypeMax,
21};
22static_assert(static_cast<uint8_t>(WorkType::WorkTypeMax) <= 8,
23 "WorkType must fit in 3 bits");
24
25inline std::pair<void *, WorkType> decode_work(void *ptr) noexcept {
26 intptr_t mask = (1 << 3) - 1;
27 intptr_t addr = reinterpret_cast<intptr_t>(ptr);
28 WorkType type = static_cast<WorkType>(addr & mask);
29 // NOLINTNEXTLINE(performance-no-int-to-ptr)
30 void *actual_ptr = reinterpret_cast<void *>(addr & (~mask));
31 return std::make_pair(actual_ptr, type);
32}
33
34inline void *encode_work(void *ptr, WorkType type) noexcept {
35 intptr_t addr = reinterpret_cast<intptr_t>(ptr);
36 // Ensure align of 8
37 assert(addr % 8 == 0);
38 addr |= static_cast<intptr_t>(type);
39 // NOLINTNEXTLINE(performance-no-int-to-ptr)
40 return reinterpret_cast<void *>(addr);
41}
42
43} // namespace condy
The main namespace for the Condy library.
Definition condy.hpp:30