1 | /* |
2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/Atomics.h> |
29 | #include <wtf/Noncopyable.h> |
30 | |
31 | namespace WTF { |
32 | |
33 | // This a simple single consumer, multiple producer Bag data structure. |
34 | |
35 | template<typename T> |
36 | class LocklessBag { |
37 | WTF_MAKE_NONCOPYABLE(LocklessBag); |
38 | public: |
39 | struct Node { |
40 | WTF_MAKE_FAST_ALLOCATED; |
41 | public: |
42 | T data; |
43 | Node* next; |
44 | }; |
45 | |
46 | LocklessBag() |
47 | : m_head(nullptr) |
48 | { |
49 | } |
50 | |
51 | enum PushResult { Empty, NonEmpty }; |
52 | PushResult add(T&& element) |
53 | { |
54 | Node* newNode = new Node(); |
55 | newNode->data = std::forward<T>(element); |
56 | |
57 | Node* oldHead; |
58 | m_head.transaction([&] (Node*& head) { |
59 | oldHead = head; |
60 | newNode->next = head; |
61 | head = newNode; |
62 | return true; |
63 | }); |
64 | |
65 | return oldHead == nullptr ? Empty : NonEmpty; |
66 | } |
67 | |
68 | // CONSUMER FUNCTIONS: Everything below here is only safe to call from the consumer thread. |
69 | |
70 | // This function is actually safe to call from more than one thread, but ONLY if no thread can call consumeAll. |
71 | template<typename Functor> |
72 | void iterate(const Functor& func) |
73 | { |
74 | Node* node = m_head.load(); |
75 | while (node) { |
76 | const T& data = node->data; |
77 | func(data); |
78 | node = node->next; |
79 | } |
80 | } |
81 | |
82 | template<typename Functor> |
83 | void consumeAll(const Functor& func) |
84 | { |
85 | consumeAllWithNode([&] (T&& data, Node* node) { |
86 | func(WTFMove(data)); |
87 | delete node; |
88 | }); |
89 | } |
90 | |
91 | template<typename Functor> |
92 | void consumeAllWithNode(const Functor& func) |
93 | { |
94 | Node* node = m_head.exchange(nullptr); |
95 | while (node) { |
96 | Node* oldNode = node; |
97 | node = node->next; |
98 | func(WTFMove(oldNode->data), oldNode); |
99 | } |
100 | } |
101 | |
102 | ~LocklessBag() |
103 | { |
104 | consumeAll([] (T&&) { }); |
105 | } |
106 | |
107 | private: |
108 | Atomic<Node*> m_head; |
109 | }; |
110 | |
111 | } // namespace WTF |
112 | |