20 #ifndef MIR_THREAD_SAFE_LIST_H_
21 #define MIR_THREAD_SAFE_LIST_H_
47 template<
class Element>
51 void add(Element
const& element);
52 void remove(Element
const& element);
53 unsigned int remove_all(Element
const& element);
55 void for_each(std::function<
void(Element
const& element)>
const& f);
63 std::atomic<ListItem*> next{
nullptr};
65 ~ListItem() {
delete next.load(); }
69 template<
class Element>
71 std::function<
void(Element
const& element)>
const& f)
73 ListItem* current_item = &head;
80 if (
auto const copy_of_element = current_item->element) f(copy_of_element);
82 current_item = current_item->next;
86 template<
class Element>
89 ListItem* current_item = &head;
97 if (current_item->element)
continue;
102 if (!current_item->element)
104 current_item->element = element;
108 while (current_item->next && (current_item = current_item->next));
111 auto new_item =
new ListItem;
112 new_item->element = element;
114 for (ListItem* expected{
nullptr};
115 !current_item->next.compare_exchange_weak(expected, new_item);
118 if (expected) current_item = expected;
122 template<
class Element>
125 ListItem* current_item = &head;
131 if (current_item->element != element)
continue;
136 if (current_item->element == element)
138 current_item->element = Element{};
142 while ((current_item = current_item->next));
145 template<
class Element>
148 ListItem* current_item = &head;
155 if (current_item->element != element)
continue;
160 if (current_item->element == element)
162 current_item->element = Element{};
166 while ((current_item = current_item->next));
171 template<
class Element>
174 ListItem* current_item = &head;
179 current_item->element = Element{};
181 while ((current_item = current_item->next));
All things Mir.
Definition: atomic_callback.h:25
Definition: recursive_read_write_mutex.h:56
unsigned int remove_all(Element const &element)
Definition: thread_safe_list.h:146
void remove(Element const &element)
Definition: thread_safe_list.h:123
void for_each(std::function< void(Element const &element)> const &f)
Definition: thread_safe_list.h:70
void add(Element const &element)
Definition: thread_safe_list.h:87
a recursive read-write mutex.
Definition: recursive_read_write_mutex.h:31
void clear()
Definition: thread_safe_list.h:172
Definition: thread_safe_list.h:48
Definition: recursive_read_write_mutex.h:66