Skip to content

Commit 3416448

Browse files
committed
return error for init
1 parent fe78c68 commit 3416448

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/ThreadPool.zig

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const ThreadPool = @This();
66

77
const std = @import("std");
8+
const Allocator = std.mem.Allocator;
89
const c = @cImport({
910
@cInclude("blst.h");
1011
});
@@ -34,7 +35,7 @@ pub const Opts = struct {
3435
n_workers: u16 = 1,
3536
};
3637

37-
allocator: std.mem.Allocator,
38+
allocator: Allocator,
3839
n_workers: usize,
3940
threads: [MAX_WORKERS - 1]std.Thread = undefined,
4041
work_ready: [MAX_WORKERS]std.Thread.ResetEvent = [_]std.Thread.ResetEvent{.{}} ** MAX_WORKERS,
@@ -50,16 +51,14 @@ dispatch_mutex: std.Thread.Mutex = .{},
5051

5152
/// Creates a thread pool with the specified number of workers.
5253
/// The caller owns the returned pool and must call `deinit` when done.
53-
pub fn init(allocator: std.mem.Allocator, opts: Opts) *ThreadPool {
54+
pub fn init(allocator: Allocator, opts: Opts) (Allocator.Error || std.Thread.SpawnError)!*ThreadPool {
5455
std.debug.assert(opts.n_workers >= 1 and opts.n_workers <= MAX_WORKERS);
55-
const pool = allocator.create(ThreadPool) catch
56-
@panic("ThreadPool: failed to allocate");
56+
const pool = try allocator.create(ThreadPool);
5757
pool.* = .{ .allocator = allocator, .n_workers = opts.n_workers };
5858
// Workers start from index 1; index 0 is reserved for the calling thread
5959
// which executes as worker 0 inside dispatch() to avoid wasting a core.
6060
for (1..pool.n_workers) |i| {
61-
pool.threads[i - 1] = std.Thread.spawn(.{}, workerLoop, .{ pool, i }) catch
62-
@panic("ThreadPool: failed to spawn worker");
61+
pool.threads[i - 1] = try std.Thread.spawn(.{}, workerLoop, .{ pool, i });
6362
}
6463
return pool;
6564
}
@@ -364,7 +363,7 @@ fn mergeAndVerify(pool: *ThreadPool, n_active: usize, gtsig: ?*const c.blst_fp12
364363
}
365364

366365
test "verifyMultipleAggregateSignatures multi-threaded" {
367-
const pool = ThreadPool.init(std.testing.allocator, .{ .n_workers = 4 });
366+
const pool = try ThreadPool.init(std.testing.allocator, .{ .n_workers = 4 });
368367
defer pool.deinit();
369368

370369
const ikm: [32]u8 = .{
@@ -418,7 +417,7 @@ test "verifyMultipleAggregateSignatures multi-threaded" {
418417
}
419418

420419
test "aggregateVerify multi-threaded" {
421-
const pool = ThreadPool.init(std.testing.allocator, .{ .n_workers = 4 });
420+
const pool = try ThreadPool.init(std.testing.allocator, .{ .n_workers = 4 });
422421
defer pool.deinit();
423422

424423
const AggregateSignature = blst.AggregateSignature;

0 commit comments

Comments
 (0)