Run clang-format
This commit is contained in:
@@ -1,49 +1,42 @@
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
unsigned const max_hazard_pointers=100;
|
||||
struct hazard_pointer
|
||||
{
|
||||
std::atomic<std::thread::id> id;
|
||||
std::atomic<void*> pointer;
|
||||
unsigned const max_hazard_pointers = 100;
|
||||
struct hazard_pointer {
|
||||
std::atomic<std::thread::id> id;
|
||||
std::atomic<void*> pointer;
|
||||
};
|
||||
hazard_pointer hazard_pointers[max_hazard_pointers];
|
||||
class hp_owner
|
||||
{
|
||||
hazard_pointer* hp;
|
||||
class hp_owner {
|
||||
hazard_pointer* hp;
|
||||
|
||||
public:
|
||||
hp_owner(hp_owner const&)=delete;
|
||||
hp_owner operator=(hp_owner const&)=delete;
|
||||
hp_owner():
|
||||
hp(nullptr)
|
||||
{
|
||||
for(unsigned i=0;i<max_hazard_pointers;++i)
|
||||
{
|
||||
std::thread::id old_id;
|
||||
if(hazard_pointers[i].id.compare_exchange_strong(
|
||||
old_id,std::this_thread::get_id()))
|
||||
{
|
||||
hp=&hazard_pointers[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!hp)
|
||||
{
|
||||
throw std::runtime_error("No hazard pointers available");
|
||||
}
|
||||
hp_owner(hp_owner const&) = delete;
|
||||
hp_owner operator=(hp_owner const&) = delete;
|
||||
hp_owner() : hp(nullptr)
|
||||
{
|
||||
for (unsigned i = 0; i < max_hazard_pointers; ++i) {
|
||||
std::thread::id old_id;
|
||||
if (hazard_pointers[i].id.compare_exchange_strong(
|
||||
old_id, std::this_thread::get_id())) {
|
||||
hp = &hazard_pointers[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::atomic<void*>& get_pointer()
|
||||
{
|
||||
return hp->pointer;
|
||||
}
|
||||
~hp_owner()
|
||||
{
|
||||
hp->pointer.store(nullptr);
|
||||
hp->id.store(std::thread::id());
|
||||
if (!hp) {
|
||||
throw std::runtime_error("No hazard pointers available");
|
||||
}
|
||||
}
|
||||
std::atomic<void*>& get_pointer() { return hp->pointer; }
|
||||
~hp_owner()
|
||||
{
|
||||
hp->pointer.store(nullptr);
|
||||
hp->id.store(std::thread::id());
|
||||
}
|
||||
};
|
||||
std::atomic<void*>& get_hazard_pointer_for_current_thread()
|
||||
std::atomic<void*>&
|
||||
get_hazard_pointer_for_current_thread()
|
||||
{
|
||||
thread_local static hp_owner hazard;
|
||||
return hazard.get_pointer();
|
||||
thread_local static hp_owner hazard;
|
||||
return hazard.get_pointer();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user