-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmented_queue.zig
More file actions
294 lines (257 loc) · 12.6 KB
/
segmented_queue.zig
File metadata and controls
294 lines (257 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const std = @import("std");
const Allocator = std.mem.Allocator;
const DVyukovMPMCQueue = @import("dvyukov-mpmc").DVyukovMPMCQueue;
const ebr = @import("ebr");
const Backoff = @import("backoff").Backoff;
pub fn SegmentedQueue(comptime T: type, comptime segment_capacity: usize) type {
const InnerQueue = DVyukovMPMCQueue(T, segment_capacity);
return struct {
const Self = @This();
/// Re-export EBR types for convenience
pub const Collector = ebr.Collector;
pub const Guard = ebr.Guard;
pub const ThreadHandle = ebr.ThreadHandle;
const Segment = struct {
queue: *InnerQueue,
allocator: Allocator,
next: std.atomic.Value(?*Segment),
};
head_segment: std.atomic.Value(*Segment) align(std.atomic.cache_line),
tail_segment: std.atomic.Value(*Segment) align(std.atomic.cache_line),
allocator: Allocator,
collector: *Collector,
/// Initialize a new SegmentedQueue with the given allocator and EBR collector.
///
/// The collector must outlive the queue. Threads using this queue must
/// register with the collector via `collector.registerThread()` before
/// calling enqueue/dequeue operations.
pub fn init(allocator: Allocator, collector: *Collector) !Self {
const first_seg = try allocator.create(Segment);
errdefer allocator.destroy(first_seg);
const queue = try allocator.create(InnerQueue);
errdefer allocator.destroy(queue);
queue.* = try InnerQueue.init(allocator);
first_seg.queue = queue;
first_seg.allocator = allocator;
first_seg.next = std.atomic.Value(?*Segment).init(null);
return .{
.head_segment = std.atomic.Value(*Segment).init(first_seg),
.tail_segment = std.atomic.Value(*Segment).init(first_seg),
.allocator = allocator,
.collector = collector,
};
}
pub fn deinit(self: *Self) void {
var seg: ?*Segment = self.head_segment.load(.acquire);
while (seg) |current| {
const next = current.next.load(.acquire);
current.queue.deinit();
current.allocator.destroy(current.queue);
current.allocator.destroy(current);
seg = next;
}
}
/// Core enqueue logic extracted to eliminate duplication.
/// Marked inline to ensure zero overhead - the function call is eliminated
/// at compile time, producing the same machine code as manual duplication.
inline fn tryEnqueueItem(self: *Self, item: T) !void {
while (true) {
const tail_seg = self.tail_segment.load(.acquire);
tail_seg.queue.enqueue(item) catch |err| switch (err) {
error.QueueFull => {
const next = tail_seg.next.load(.acquire);
if (next) |n| {
_ = self.tail_segment.cmpxchgWeak(
tail_seg,
n,
.release,
.acquire,
);
continue;
}
// Allocate segment and queue. Queue MUST be initialized before CAS
// to avoid race where another thread sees the segment before queue is ready.
const new_seg = try self.allocator.create(Segment);
errdefer self.allocator.destroy(new_seg);
const queue = try self.allocator.create(InnerQueue);
errdefer self.allocator.destroy(queue);
queue.* = try InnerQueue.init(self.allocator);
errdefer queue.deinit();
new_seg.queue = queue;
new_seg.allocator = self.allocator;
new_seg.next = std.atomic.Value(?*Segment).init(null);
// Try to install the new segment via CAS
if (tail_seg.next.cmpxchgStrong(
null,
new_seg,
.release,
.acquire,
) != null) {
// CAS failed - another thread won. Clean up (errdefer handles this).
queue.deinit();
self.allocator.destroy(queue);
self.allocator.destroy(new_seg);
continue;
}
_ = self.tail_segment.cmpxchgWeak(
tail_seg,
new_seg,
.release,
.acquire,
);
continue;
},
else => |e| return e,
};
return;
}
}
/// Primary enqueue API. Requires an active EBR guard in the current thread.
/// The caller must create an EBR guard before calling this method.
pub fn enqueue(self: *Self, item: T) !void {
return self.tryEnqueueItem(item);
}
/// Convenience method that creates and manages an EBR guard automatically.
/// For better performance with multiple operations, use enqueue() with
/// external guard management or enqueueMany() for batches.
pub fn enqueueWithAutoGuard(self: *Self, item: T) !void {
const guard = self.collector.pin();
defer guard.unpin();
return self.enqueue(item);
}
/// Primary dequeue API. Requires an active EBR guard.
/// The caller must create an EBR guard before calling this method.
/// Uses adaptive exponential backoff (Crossbeam-style) for intelligent retry.
pub fn dequeue(self: *Self) ?T {
while (true) {
const head_seg = self.head_segment.load(.acquire);
// Try to dequeue from current segment
if (head_seg.queue.dequeue()) |item| {
return item;
}
// Segment appears empty. Check for next segment BEFORE spinning.
// If next exists, advance immediately rather than wasting cycles on empty segment.
const next = head_seg.next.load(.acquire);
if (next) |n| {
// Next segment exists, try to advance to it immediately
if (self.head_segment.cmpxchgWeak(
head_seg,
n,
.release,
.acquire,
) == null) {
// Successfully advanced, retire old segment via EBR
self.collector.deferReclaim(@ptrCast(head_seg), destroySegment);
}
// Loop continues to try dequeue from new head segment
continue;
}
// No next segment exists. Segment might be contended (producers still enqueueing).
// Use short adaptive backoff with reduced spin limit (4 instead of default 6).
// This gives producers a chance to finish without excessive spinning.
var backoff = Backoff.init(.{ .spin_limit = 4, .yield_limit = 6 });
while (!backoff.isCompleted()) {
if (head_seg.queue.dequeue()) |item| {
return item;
}
backoff.snooze();
}
// After backoff, recheck for next segment before giving up.
// A producer might have created and linked a new segment during our backoff.
// This prevents a race where we return null while items exist in a newly-linked segment.
const next_after_backoff = head_seg.next.load(.acquire);
if (next_after_backoff == null) {
// Still no next segment after backoff. Queue is truly empty.
return null;
}
// Next segment appeared during backoff. Loop back to advance and retry.
continue;
}
}
/// Convenience method that creates and manages an EBR guard automatically.
/// For better performance with multiple operations, use dequeue() with
/// external guard management or dequeueMany() for batches.
pub fn dequeueWithAutoGuard(self: *Self) ?T {
const guard = self.collector.pin();
defer guard.unpin();
return self.dequeue();
}
/// Batch enqueue operation. Enqueues all items from the slice.
/// Uses a single EBR guard for all operations, amortizing guard overhead.
/// More efficient than calling enqueue() repeatedly for multiple items.
pub fn enqueueMany(self: *Self, items: []const T) !void {
const guard = self.collector.pin();
defer guard.unpin();
for (items) |item| {
try self.tryEnqueueItem(item);
}
}
/// Batch dequeue operation. Attempts to fill the provided buffer.
/// Uses a single EBR guard for all operations, amortizing guard overhead.
/// More efficient than calling dequeue() repeatedly for multiple items.
/// Returns the number of items actually dequeued (may be less than buffer size if queue empties).
pub fn dequeueMany(self: *Self, buffer: []T) usize {
const guard = self.collector.pin();
defer guard.unpin();
var count: usize = 0;
for (buffer) |*slot| {
item_loop: while (true) {
const head_seg = self.head_segment.load(.acquire);
// Try to dequeue from current segment
if (head_seg.queue.dequeue()) |item| {
slot.* = item;
count += 1;
break :item_loop;
}
// Segment appears empty. Check for next segment BEFORE spinning.
// If next exists, advance immediately rather than wasting cycles.
const next = head_seg.next.load(.acquire);
if (next) |n| {
// Next segment exists, try to advance to it immediately
if (self.head_segment.cmpxchgWeak(
head_seg,
n,
.release,
.acquire,
) == null) {
// Successfully advanced, retire old segment via EBR
self.collector.deferReclaim(@ptrCast(head_seg), destroySegment);
}
// Loop continues to try dequeue from new head segment
continue;
}
// No next segment exists. Segment might be contended (producers still enqueueing).
// Use short adaptive backoff with reduced spin limit.
var backoff = Backoff.init(.{ .spin_limit = 4, .yield_limit = 6 });
while (!backoff.isCompleted()) {
if (head_seg.queue.dequeue()) |item| {
slot.* = item;
count += 1;
break :item_loop;
}
backoff.snooze();
}
// After backoff, recheck for next segment before giving up.
// A producer might have created and linked a new segment during our backoff.
const next_after_backoff = head_seg.next.load(.acquire);
if (next_after_backoff == null) {
// Still no next segment after backoff. Queue is empty.
return count;
}
// Next segment appeared during backoff. Loop back to advance and retry.
continue;
}
}
return count;
}
fn destroySegment(ptr: *anyopaque) void {
const seg: *Segment = @ptrCast(@alignCast(ptr));
seg.queue.deinit();
seg.allocator.destroy(seg.queue);
seg.allocator.destroy(seg);
}
};
}
test {
std.testing.refAllDecls(@This());
}