Skip to content

feat: support timeouts and deadlines - #1018

Open
pcolladosoto wants to merge 1 commit into
ZigEmbeddedGroup:mainfrom
pcolladosoto:main
Open

feat: support timeouts and deadlines#1018
pcolladosoto wants to merge 1 commit into
ZigEmbeddedGroup:mainfrom
pcolladosoto:main

Conversation

@pcolladosoto

Copy link
Copy Markdown
Contributor

This commit adds support for specifying deadlines for read and write operations over UARTs in one of two ways:

  1. By providing a deadline as an absolute time in the future as was done before. These deadlines can be 'refreshed' through the set_deadline methods.

  2. By providing a timeout with which a new deadline will be computed upon performing a read or write operation.

This allows for a precise handling of deadlines when leveraging the std.Io.{Reader,Writer} interface semantics.

Closes #1017

This commit adds support for specifying deadlines for read and write
operations over UARTs in one of two ways:

  1. By providing a deadline as an absolute time in the future as was
     done before. These deadlines can be 'refreshed' through the
     `set_deadline` methods.

  2. By providing a timeout with which a new deadline will be computed
     upon performing a read or write operation.

This allows for a precise handling of deadlines when leveraging the
std.Io.{Reader,Writer} interface semantics.
@pcolladosoto

Copy link
Copy Markdown
Contributor Author

Hi @Grazfather, @mattnite! Feel free to make any comments and/or changes: I'm by no means a Zig expert!

For instance, I believe the explicit dereference in the set_deadline methods might not be needed and I'd be fine with deleting it. Also, the naming can surely be improved (i.e. I'm not completely sold on the TimeFrontier type name) and some documentation can be added; I just deferred that a bit to get a feel for what might be needed to meet your requirements.

Thanks a ton for your time!

@tact1m4n3 tact1m4n3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid work! I like the direction of this. I only have a few style suggestions.

}
};

pub const TimeFrontier = union(enum) { timeout_us: u64, deadline: mdf.time.Deadline };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe format this on multiple lines?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add here a

pub const no_deadline: TimeFrontier = .{ .deadline = .no_deadline };

so you can do .no_deadline directly when you don't want any deadline.

pub const Writer = struct {
uart: UART,
deadline: mdf.time.Deadline,
timeFrontier: TimeFrontier,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: fields should be snake case

interface: std.Io.Writer,

pub fn set_deadline(self: *Writer, deadline: mdf.time.Deadline) void {
self.*.timeFrontier = TimeFrontier{.deadline = deadline};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing self.* here is not necessary. Field access dereferences the pointer automatically

Comment on lines +203 to +207
var deadline: mdf.time.Deadline = undefined;
switch (uart_writer.timeFrontier) {
.deadline => |d| deadline = d,
.timeout_us => |t| deadline = time.deadline_in_us(t)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: switch can be used as an expression so you could do

const deadline: mdf.time.Deadline = switch (uart_writer.time_frontier) {
    .deadline => |d| d,
    .timeout_us => |t| time.deadline_in_us(t),
};

/// };
pub fn init_logger(uart: UART) void {
uart_logger = uart.writer(.no_deadline, &.{});
uart_logger = uart.writer(.{.deadline = .no_deadline}, &.{});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can become uart.writer(.no_deadline). See the suggestion above

@tact1m4n3

Copy link
Copy Markdown
Collaborator

Also CI formatting seems to fail. You should run zig fmt on the file. Or you can just setup your editor to do that on save so you don't have to think about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorporating deadline into UART/writer interfaces in MicroZig

2 participants