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
31namespace WTF {
32
33// This a simple single consumer, multiple producer Bag data structure.
34
35template<typename T>
36class LocklessBag final {
37 WTF_MAKE_FAST_ALLOCATED;
38 WTF_MAKE_NONCOPYABLE(LocklessBag);
39public:
40 struct Node {
41 WTF_MAKE_FAST_ALLOCATED;
42 public:
43 T data;
44 Node* next;
45 };
46
47 LocklessBag()
48 : m_head(nullptr)
49 {
50 }
51
52 enum PushResult { Empty, NonEmpty };
53 PushResult add(T&& element)
54 {
55 Node* newNode = new Node();
56 newNode->data = std::forward<T>(element);
57
58 Node* oldHead;
59 m_head.transaction([&] (Node*& head) {
60 oldHead = head;
61 newNode->next = head;
62 head = newNode;
63 return true;
64 });
65
66 return oldHead == nullptr ? Empty : NonEmpty;
67 }
68
69 // CONSUMER FUNCTIONS: Everything below here is only safe to call from the consumer thread.
70
71 // This function is actually safe to call from more than one thread, but ONLY if no thread can call consumeAll.
72 template<typename Functor>
73 void iterate(const Functor& func)
74 {
75 Node* node = m_head.load();
76 while (node) {
77 const T& data = node->data;
78 func(data);
79 node = node->next;
80 }
81 }
82
83 template<typename Functor>
84 void consumeAll(const Functor& func)
85 {
86 consumeAllWithNode([&] (T&& data, Node* node) {
87 func(WTFMove(data));
88 delete node;
89 });
90 }
91
92 template<typename Functor>
93 void consumeAllWithNode(const Functor& func)
94 {
95 Node* node = m_head.exchange(nullptr);
96 while (node) {
97 Node* oldNode = node;
98 node = node->next;
99 func(WTFMove(oldNode->data), oldNode);
100 }
101 }
102
103 ~LocklessBag()
104 {
105 consumeAll([] (T&&) { });
106 }
107
108private:
109 Atomic<Node*> m_head;
110};
111
112} // namespace WTF
113