Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add evented write support #3907

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 31 additions & 17 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,26 @@ const Module = struct {
/// Tries to write to stderr, unbuffered, and ignores any error returned.
/// Does not append a newline.
var stderr_file: File = undefined;
var stderr_file_out_stream: File.OutStream = undefined;
var stderr_file_out_stream: File.BlockingOutStream = undefined;

var stderr_stream: ?*io.OutStream(File.WriteError) = null;
var stderr_stream: ?*io.BlockingOutStream(File.WriteError) = null;
var stderr_mutex = std.Mutex.init();


pub fn warn(comptime fmt: []const u8, args: var) void {
const held = stderr_mutex.acquire();
defer held.release();
const stderr = getStderrStream();
stderr.print(fmt, args) catch return;
}

pub fn getStderrStream() *io.OutStream(File.WriteError) {

pub fn getStderrStream() *io.BlockingOutStream(File.WriteError) {
if (stderr_stream) |st| {
return st;
} else {
stderr_file = io.getStdErr();
stderr_file_out_stream = stderr_file.outStream();
stderr_file_out_stream = stderr_file.blockingOutStream();
const st = &stderr_file_out_stream.stream;
stderr_stream = st;
return st;
Expand Down Expand Up @@ -91,16 +93,20 @@ fn wantTtyColor() bool {
/// TODO multithreaded awareness
pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
const stderr = getStderrStream();
return dumpCurrentStackTraceToStream(stderr, start_addr);
}

pub fn dumpCurrentStackTraceToStream(stream: var, start_addr: ?usize) void {
if (builtin.strip_debug_info) {
stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
stream.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
return;
}
const debug_info = getSelfDebugInfo() catch |err| {
stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
stream.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
return;
};
writeCurrentStackTrace(stderr, debug_info, wantTtyColor(), start_addr) catch |err| {
stderr.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return;
writeCurrentStackTrace(stream, debug_info, wantTtyColor(), start_addr) catch |err| {
stream.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return;
return;
};
}
Expand All @@ -110,24 +116,28 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
/// TODO multithreaded awareness
pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
const stderr = getStderrStream();
return dumpStackTraceFromBaseToStream(stderr, bp, ip);
}

pub fn dumpStackTraceFromBaseToStream(stream: var, bp: usize, ip: usize) void {
if (builtin.strip_debug_info) {
stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
stream.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
return;
}
const debug_info = getSelfDebugInfo() catch |err| {
stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
stream.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
return;
};
const tty_color = wantTtyColor();
printSourceAtAddress(debug_info, stderr, ip, tty_color) catch return;
printSourceAtAddress(debug_info, stream, ip, tty_color) catch return;
const first_return_address = @intToPtr(*const usize, bp + @sizeOf(usize)).*;
printSourceAtAddress(debug_info, stderr, first_return_address - 1, tty_color) catch return;
printSourceAtAddress(debug_info, stream, first_return_address - 1, tty_color) catch return;
var it = StackIterator{
.first_addr = null,
.fp = bp,
};
while (it.next()) |return_address| {
printSourceAtAddress(debug_info, stderr, return_address - 1, tty_color) catch return;
printSourceAtAddress(debug_info, stream, return_address - 1, tty_color) catch return;
}
}

Expand Down Expand Up @@ -183,16 +193,20 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace
/// TODO multithreaded awareness
pub fn dumpStackTrace(stack_trace: builtin.StackTrace) void {
const stderr = getStderrStream();
return dumpStackTraceToStream(stderr, stack_trace);
}

pub fn dumpStackTraceToStream(stream: var, stack_trace: builtin.StackTrace) void {
if (builtin.strip_debug_info) {
stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
stream.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
return;
}
const debug_info = getSelfDebugInfo() catch |err| {
stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
stream.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
return;
};
writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, wantTtyColor()) catch |err| {
stderr.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return;
writeStackTrace(stack_trace, stream, getDebugInfoAllocator(), debug_info, wantTtyColor()) catch |err| {
stream.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return;
return;
};
}
Expand Down
6 changes: 5 additions & 1 deletion lib/std/event/loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const Loop = struct {
handle: anyframe,
overlapped: Overlapped,


pub const overlapped_init = switch (builtin.os) {
.windows => windows.OVERLAPPED{
.Internal = 0,
Expand Down Expand Up @@ -92,6 +93,7 @@ pub const Loop = struct {
.evented => &global_instance_state,
};
pub const instance: ?*Loop = if (@hasDecl(root, "event_loop")) root.event_loop else default_instance;
initialized: bool = false,

/// TODO copy elision / named return values so that the threads referencing *Loop
/// have the correct pointer value.
Expand Down Expand Up @@ -158,6 +160,8 @@ pub const Loop = struct {

try self.initOsData(extra_thread_count);
errdefer self.deinitOsData();

self.initialized = true;
}

pub fn deinit(self: *Loop) void {
Expand Down Expand Up @@ -237,7 +241,7 @@ pub const Loop = struct {
var extra_thread_index: usize = 0;
errdefer {
// writing 8 bytes to an eventfd cannot fail
os.write(self.os_data.final_eventfd, &wakeup_bytes) catch unreachable;
os.writeMode(self.os_data.final_eventfd, &wakeup_bytes, .blocking) catch unreachable;
while (extra_thread_index != 0) {
extra_thread_index -= 1;
self.extra_threads[extra_thread_index].wait();
Expand Down