authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-21 15:20:36-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-21 15:20:36-05:00
log6aecc268fd55d19ef096b320f78343c69c1ed09c
treee50c161bbdbe54b9b91faa770d2019d71e4d54a0
parentdff7ca6784d2cf8e46fcfffb5e5e2eb537b71b4d
signaturelock-open Commit is signed but in an unrecognized format.

remove the allocator from std.event.Loop

closes #3539

1 files changed, 13 insertions(+), 12 deletions(-)

lib/std/event/loop.zig+13-12
......@@ -12,15 +12,18 @@ const maxInt = std.math.maxInt;
1212const Thread = std.Thread;
1313
1414pub const Loop = struct {
15 allocator: *mem.Allocator,
1615 next_tick_queue: std.atomic.Queue(anyframe),
1716 os_data: OsData,
1817 final_resume_node: ResumeNode,
1918 pending_event_count: usize,
2019 extra_threads: []*Thread,
2120
22 // pre-allocated eventfds. all permanently active.
23 // this is how we send promises to be resumed on other threads.
21 /// For resources that have the same lifetime as the `Loop`.
22 /// This is only used by `Loop` for the thread pool and associated resources.
23 arena: std.heap.ArenaAllocator,
24
25 /// Pre-allocated eventfds. All permanently active.
26 /// This is how `Loop` sends promises to be resumed on other threads.
2427 available_eventfd_resume_nodes: std.atomic.Stack(ResumeNode.EventFd),
2528 eventfd_resume_nodes: []std.atomic.Stack(ResumeNode.EventFd).Node,
2629
......@@ -127,11 +130,9 @@ pub const Loop = struct {
127130 /// Thread count is the total thread count. The thread pool size will be
128131 /// max(thread_count - 1, 0)
129132 pub fn initThreadPool(self: *Loop, thread_count: usize) !void {
130 // TODO: https://github.com/ziglang/zig/issues/3539
131 const allocator = std.heap.page_allocator;
132133 self.* = Loop{
134 .arena = std.heap.ArenaAllocator.init(std.heap.page_allocator),
133135 .pending_event_count = 1,
134 .allocator = allocator,
135136 .os_data = undefined,
136137 .next_tick_queue = std.atomic.Queue(anyframe).init(),
137138 .extra_threads = undefined,
......@@ -143,17 +144,17 @@ pub const Loop = struct {
143144 .overlapped = ResumeNode.overlapped_init,
144145 },
145146 };
147 errdefer self.arena.deinit();
148
146149 // We need at least one of these in case the fs thread wants to use onNextTick
147150 const extra_thread_count = thread_count - 1;
148151 const resume_node_count = std.math.max(extra_thread_count, 1);
149 self.eventfd_resume_nodes = try self.allocator.alloc(
152 self.eventfd_resume_nodes = try self.arena.allocator.alloc(
150153 std.atomic.Stack(ResumeNode.EventFd).Node,
151154 resume_node_count,
152155 );
153 errdefer self.allocator.free(self.eventfd_resume_nodes);
154156
155 self.extra_threads = try self.allocator.alloc(*Thread, extra_thread_count);
156 errdefer self.allocator.free(self.extra_threads);
157 self.extra_threads = try self.arena.allocator.alloc(*Thread, extra_thread_count);
157158
158159 try self.initOsData(extra_thread_count);
159160 errdefer self.deinitOsData();
......@@ -161,7 +162,8 @@ pub const Loop = struct {
161162
162163 pub fn deinit(self: *Loop) void {
163164 self.deinitOsData();
164 self.allocator.free(self.extra_threads);
165 self.arena.deinit();
166 self.* = undefined;
165167 }
166168
167169 const InitOsDataError = os.EpollCreateError || mem.Allocator.Error || os.EventFdError ||
......@@ -407,7 +409,6 @@ pub const Loop = struct {
407409 noasync os.close(self.os_data.final_eventfd);
408410 while (self.available_eventfd_resume_nodes.pop()) |node| noasync os.close(node.data.eventfd);
409411 noasync os.close(self.os_data.epollfd);
410 self.allocator.free(self.eventfd_resume_nodes);
411412 },
412413 .macosx, .freebsd, .netbsd, .dragonfly => {
413414 noasync os.close(self.os_data.kqfd);