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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//! generic thread pool with key-partitioned queues
//!
//! each worker has its own bounded ring buffer + mutex + condvar.
//! `submit(key, item, shutdown)` routes to `workers[key % N]` for per-key ordering.
//! blocks when the target queue is full (backpressure to caller).
//! items stored by value in pre-allocated ring buffer (zero alloc per submit).
const std = @import("std");
const Io = std.Io;
const Allocator = std.mem.Allocator;
pub fn ThreadPool(comptime T: type, comptime processFn: fn (*T) void) type {
return struct {
const Self = @This();
pub const Config = struct {
num_workers: u16 = 8,
queue_capacity: u16 = 4096,
stack_size: usize = 4 * 1024 * 1024,
};
const Worker = struct {
// ring buffer stored as a slice of T
queue: []T,
capacity: u16,
head: u16 = 0, // next slot to read
tail: u16 = 0, // next slot to write
count: u16 = 0,
mutex: Io.Mutex = Io.Mutex.init,
cond: Io.Condition = Io.Condition.init, // "not empty" — workers wait here
not_full: Io.Condition = Io.Condition.init, // "not full" — submitters wait here
alive: bool = true,
thread: ?std.Thread = null,
io: Io,
};
workers: []Worker,
allocator: Allocator,
io: Io,
pub fn init(allocator: Allocator, config: Config, io: Io) !Self {
const workers = try allocator.alloc(Worker, config.num_workers);
for (workers) |*w| {
w.* = .{
.queue = try allocator.alloc(T, config.queue_capacity),
.capacity = config.queue_capacity,
.io = io,
};
}
const self = Self{
.workers = workers,
.allocator = allocator,
.io = io,
};
// spawn worker threads
for (self.workers) |*w| {
w.thread = try std.Thread.spawn(
.{ .stack_size = config.stack_size },
workerLoop,
.{w},
);
}
return self;
}
/// submit an item for processing, routed by key.
/// blocks if the target worker's queue is full (backpressure).
/// returns false only if shutdown was requested while waiting.
pub fn submit(self: *Self, key: u64, item: T, stop: *std.atomic.Value(bool)) bool {
const idx = key % self.workers.len;
const w = &self.workers[idx];
w.mutex.lockUncancelable(w.io);
defer w.mutex.unlock(w.io);
while (w.count == w.capacity) {
if (stop.load(.acquire)) return false;
// poll: release mutex, sleep briefly, reacquire
// (Io.Condition has no timedWait — poll so stop check isn't starved)
w.mutex.unlock(w.io);
w.io.sleep(Io.Duration.fromMilliseconds(10), .awake) catch {};
w.mutex.lockUncancelable(w.io);
}
w.queue[w.tail] = item;
w.tail = @intCast((@as(u32, w.tail) + 1) % @as(u32, w.capacity));
w.count += 1;
w.cond.signal(w.io);
return true;
}
/// drain remaining items and join all worker threads.
pub fn shutdown(self: *Self) void {
// signal all workers to stop
for (self.workers) |*w| {
w.mutex.lockUncancelable(w.io);
w.alive = false;
w.cond.signal(w.io);
w.not_full.broadcast(w.io); // wake any blocked submitters
w.mutex.unlock(w.io);
}
// join all threads
for (self.workers) |*w| {
if (w.thread) |t| {
t.join();
w.thread = null;
}
}
}
/// free queue storage.
pub fn deinit(self: *Self) void {
for (self.workers) |*w| {
self.allocator.free(w.queue);
}
self.allocator.free(self.workers);
}
/// total pending items across all workers (diagnostic).
pub fn pendingCount(self: *Self) usize {
var total: usize = 0;
for (self.workers) |*w| {
w.mutex.lockUncancelable(w.io);
defer w.mutex.unlock(w.io);
total += w.count;
}
return total;
}
fn workerLoop(w: *Worker) void {
while (true) {
var item: T = undefined;
{
w.mutex.lockUncancelable(w.io);
defer w.mutex.unlock(w.io);
while (w.count == 0 and w.alive) {
w.cond.waitUncancelable(w.io, &w.mutex);
}
if (w.count == 0 and !w.alive) return;
item = w.queue[w.head];
w.head = @intCast((@as(u32, w.head) + 1) % @as(u32, w.capacity));
w.count -= 1;
w.not_full.signal(w.io); // wake one blocked submitter
}
processFn(&item);
}
}
};
}
// --- tests ---
const testing = std.testing;
test "basic submit and process" {
const Item = struct {
value: u32,
processed: *std.atomic.Value(u32),
};
const S = struct {
fn process(item: *Item) void {
_ = item.processed.fetchAdd(item.value, .monotonic);
}
};
var shutdown: std.atomic.Value(bool) = .{ .raw = false };
var counter: std.atomic.Value(u32) = .{ .raw = 0 };
var pool = try ThreadPool(Item, S.process).init(testing.allocator, .{
.num_workers = 2,
.queue_capacity = 64,
.stack_size = 1 * 1024 * 1024,
}, std.testing.io);
// submit items
for (0..10) |i| {
const ok = pool.submit(i, .{
.value = @intCast(i + 1),
.processed = &counter,
}, &shutdown);
try testing.expect(ok);
}
pool.shutdown();
defer pool.deinit();
// sum of 1..10 = 55
try testing.expectEqual(@as(u32, 55), counter.load(.acquire));
}
test "per-key ordering preserved" {
// items with the same key should be processed in FIFO order
const Item = struct {
seq: u32,
results: *std.ArrayListUnmanaged(u32),
mutex: *Io.Mutex,
allocator: Allocator,
io: Io,
};
const S = struct {
fn process(item: *Item) void {
item.mutex.lockUncancelable(item.io);
defer item.mutex.unlock(item.io);
item.results.append(item.allocator, item.seq) catch {};
}
};
var shutdown: std.atomic.Value(bool) = .{ .raw = false };
var results: std.ArrayListUnmanaged(u32) = .empty;
defer results.deinit(testing.allocator);
var mutex: Io.Mutex = Io.Mutex.init;
var pool = try ThreadPool(Item, S.process).init(testing.allocator, .{
.num_workers = 4,
.queue_capacity = 64,
.stack_size = 1 * 1024 * 1024,
}, std.testing.io);
// submit 20 items all with key=42 (same worker)
for (0..20) |i| {
const ok = pool.submit(42, .{
.seq = @intCast(i),
.results = &results,
.mutex = &mutex,
.allocator = testing.allocator,
.io = std.testing.io,
}, &shutdown);
try testing.expect(ok);
}
pool.shutdown();
defer pool.deinit();
try testing.expectEqual(@as(usize, 20), results.items.len);
for (results.items, 0..) |val, i| {
try testing.expectEqual(@as(u32, @intCast(i)), val);
}
}
test "submit blocks when queue full, succeeds after drain" {
const Item = struct {
counter: *std.atomic.Value(u32),
io: Io,
};
const S = struct {
fn process(item: *Item) void {
// slow worker — gives time for queue to fill
item.io.sleep(Io.Duration.fromMilliseconds(5), .awake) catch {};
_ = item.counter.fetchAdd(1, .monotonic);
}
};
var shutdown: std.atomic.Value(bool) = .{ .raw = false };
var counter: std.atomic.Value(u32) = .{ .raw = 0 };
var pool = try ThreadPool(Item, S.process).init(testing.allocator, .{
.num_workers = 1,
.queue_capacity = 4,
.stack_size = 1 * 1024 * 1024,
}, std.testing.io);
// submit more items than capacity — submit blocks until slots open
for (0..20) |_| {
const ok = pool.submit(0, .{ .counter = &counter, .io = std.testing.io }, &shutdown);
try testing.expect(ok);
}
pool.shutdown();
defer pool.deinit();
// all 20 should have been processed (none dropped)
try testing.expectEqual(@as(u32, 20), counter.load(.acquire));
}
test "submit returns false on shutdown" {
const Item = struct {
stop: *std.atomic.Value(bool),
io: Io,
};
const S = struct {
fn process(item: *Item) void {
// poll until shutdown — allows worker to exit promptly
while (!item.stop.load(.acquire)) {
item.io.sleep(Io.Duration.fromMilliseconds(5), .awake) catch {};
}
}
};
var shutdown: std.atomic.Value(bool) = .{ .raw = false };
var pool = try ThreadPool(Item, S.process).init(testing.allocator, .{
.num_workers = 1,
.queue_capacity = 2,
.stack_size = 1 * 1024 * 1024,
}, std.testing.io);
// fill: 1 processing + 2 queued = capacity reached
_ = pool.submit(0, .{ .stop = &shutdown, .io = std.testing.io }, &shutdown);
_ = pool.submit(0, .{ .stop = &shutdown, .io = std.testing.io }, &shutdown);
_ = pool.submit(0, .{ .stop = &shutdown, .io = std.testing.io }, &shutdown);
// signal shutdown — next submit should return false
shutdown.store(true, .release);
const ok = pool.submit(0, .{ .stop = &shutdown, .io = std.testing.io }, &shutdown);
try testing.expect(!ok);
pool.shutdown();
defer pool.deinit();
}
test "pendingCount reflects queued items" {
const Item = struct { x: u32, io: Io };
const S = struct {
fn process(item: *Item) void {
// slow worker so items accumulate
item.io.sleep(Io.Duration.fromMilliseconds(10), .awake) catch {};
}
};
var pool = try ThreadPool(Item, S.process).init(testing.allocator, .{
.num_workers = 1,
.queue_capacity = 64,
.stack_size = 1 * 1024 * 1024,
}, std.testing.io);
// initially empty
try testing.expectEqual(@as(usize, 0), pool.pendingCount());
pool.shutdown();
defer pool.deinit();
}
test "shutdown drains remaining items" {
const Item = struct {
counter: *std.atomic.Value(u32),
};
const S = struct {
fn process(item: *Item) void {
_ = item.counter.fetchAdd(1, .monotonic);
}
};
var shutdown: std.atomic.Value(bool) = .{ .raw = false };
var counter: std.atomic.Value(u32) = .{ .raw = 0 };
var pool = try ThreadPool(Item, S.process).init(testing.allocator, .{
.num_workers = 2,
.queue_capacity = 64,
.stack_size = 1 * 1024 * 1024,
}, std.testing.io);
for (0..30) |i| {
_ = pool.submit(i, .{ .counter = &counter }, &shutdown);
}
pool.shutdown();
defer pool.deinit();
// all 30 should have been processed (shutdown drains)
try testing.expectEqual(@as(u32, 30), counter.load(.acquire));
}