fairness  v1.0.0
A collection of advanced syncronization mechanisms.
Loading...
Searching...
No Matches
request_pool.hpp
Go to the documentation of this file.
1
13#ifndef BOOST_FAIRNESS_REQUEST_POOL_HPP
14#define BOOST_FAIRNESS_REQUEST_POOL_HPP
15
16#include <atomic>
18
20 #define PENDING 1
21 #define GRANTED 0
22
23 struct Thread;
24
25 struct alignas(BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE) Request{
26 Thread* watcher_{nullptr};
27 Thread* thread_{nullptr};
28 std::atomic<uint32_t> state_{PENDING};
29 //const size_t index_;
30 std::atomic_flag inUse_{};
31
32 Request() = default;
33 //Request(size_t idx) : index_(idx) {};
34
35 void reset(){
36 state_.store(PENDING, std::memory_order_relaxed);
37 watcher_ = nullptr;
38 thread_ = nullptr;
39 }
40 };
41
42 static_assert(sizeof(Request) == BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE, "Request size is not BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE");
43
44 //template <size_t... Is>
45 //auto make_requests(std::index_sequence<Is...>) {
46 // return std::array<Request, sizeof...(Is)>{Request(Is)...};
47 //}
48
49 template<size_t N>
51 public:
52
53 RequestPool() = default;
54
55 //RequestPool() : reqs_(make_requests(std::make_index_sequence<N>())) {}
56
58 for (uint32_t i = 0; i < N; ++i){
59 if (!reqs_[i].inUse_.test(std::memory_order_relaxed) && !reqs_[i].inUse_.test_and_set(std::memory_order_acquire))
60 return &reqs_[i];
61 spin_wait();
62 }
63 return nullptr;
64 }
65
66 void returnRequest(Request* const req){
67 req->reset();
68 req->inUse_.clear(std::memory_order_release);
69 //statuses_[req->index_].clear(std::memory_order_release);
70 }
71
72 private:
73 std::array<Request, N> reqs_{};
74 //alignas(BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE) std::array<std::atomic_flag, N> statuses_;
75 };
76
77}
78#endif // BOOST_FAIRNESS_REQUEST_POOL_HPP
Definition request_pool.hpp:50
void returnRequest(Request *const req)
Definition request_pool.hpp:66
Request * getRequest()
Definition request_pool.hpp:57
This file contains configurations about boost and 128bit cpu support. TODO.
#define BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE
Size to be aligned to avoid false sharing
Definition config.hpp:85
Definition coherent_priority_lock.hpp:25
void spin_wait(T &mem, K const expected) noexcept
Definition wait_ops.hpp:29
#define PENDING
Definition request_pool.hpp:20
Definition request_pool.hpp:25
void reset()
Definition request_pool.hpp:35
std::atomic_flag inUse_
Definition request_pool.hpp:30
Definition thread_pool.hpp:24