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

timeout_wheel: Use enums instead of booleans #1

Merged
merged 1 commit into from Aug 26, 2020
Merged
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
68 changes: 40 additions & 28 deletions timeout_wheel.zig
Expand Up @@ -17,7 +17,19 @@ fn fls(n: var) usize {
/// wheel_num - The number of wheels. wheel_bit * wheel_num = the number of
/// value bits used by all the wheels. Any timeout value
/// larger than this will cycle through again.
fn TimeoutWheel(comptime timeout_t: type, wheel_bit: comptime_int, wheel_num: comptime_int, allow_intervals: bool, allow_relative_access: bool) type {
fn TimeoutWheel(
comptime timeout_t: type,
wheel_bit: comptime_int,
wheel_num: comptime_int,
intervals: enum {
NoIntervals,
AllowIntervals,
},
relative_access: enum {
NoRelative,
AllowRelative,
},
) type {
const abstime_t = timeout_t;
const reltime_t = timeout_t;

Expand Down Expand Up @@ -52,7 +64,7 @@ fn TimeoutWheel(comptime timeout_t: type, wheel_bit: comptime_int, wheel_num: co
.expires = 0,
.pending = null,
.interval = init: {
if (allow_intervals) {
if (intervals == .AllowIntervals) {
if (init_interval) |int| {
assert(int > 0);
break :init int;
Expand All @@ -63,7 +75,7 @@ fn TimeoutWheel(comptime timeout_t: type, wheel_bit: comptime_int, wheel_num: co
assert(init_interval == null);
}
},
.timeouts = if (allow_relative_access) null,
.timeouts = if (relative_access == .AllowRelative) null,
};
}

Expand All @@ -75,19 +87,19 @@ fn TimeoutWheel(comptime timeout_t: type, wheel_bit: comptime_int, wheel_num: co

/// Timeout interval if periodic
/// rather than using an optional type we internally use 0 to indicate no interval
interval: if (allow_intervals) reltime_t else void,
interval: if (intervals == .AllowIntervals) reltime_t else void,

/// timeouts collection if member of
timeouts: if (allow_relative_access) ?*TimeoutWheelType else void,
timeouts: if (relative_access == .AllowRelative) ?*TimeoutWheelType else void,
fn setTimeouts(self: *Timeout, T: ?*TimeoutWheelType) void {
if (allow_relative_access) {
if (relative_access == .AllowRelative) {
self.timeouts = T;
}
}

/// true if on timing wheel, false otherwise
pub fn isPending(self: *Timeout) (if (allow_relative_access) bool else void) {
if (allow_relative_access) {
pub fn isPending(self: *Timeout) (if (relative_access == .AllowRelative) bool else void) {
if (relative_access == .AllowRelative) {
if (self.pending) |p| {
return p != &self.timeouts.?.expiredList;
}
Expand All @@ -96,8 +108,8 @@ fn TimeoutWheel(comptime timeout_t: type, wheel_bit: comptime_int, wheel_num: co
}

/// true if on expired queue, false otherwise
pub fn isExpired(self: *Timeout) (if (allow_relative_access) bool else void) {
if (allow_relative_access) {
pub fn isExpired(self: *Timeout) (if (relative_access == .AllowRelative) bool else void) {
if (relative_access == .AllowRelative) {
if (self.pending) |p| {
return p == &self.timeouts.?.expiredList;
}
Expand All @@ -107,7 +119,7 @@ fn TimeoutWheel(comptime timeout_t: type, wheel_bit: comptime_int, wheel_num: co

/// remove timeout from any timing wheel (okay if not member of any)
pub fn remove(self: *Timeout) void {
if (allow_relative_access) {
if (relative_access == .AllowRelative) {
self.timeouts.?.remove(self);
}
}
Expand Down Expand Up @@ -382,7 +394,7 @@ fn TimeoutWheel(comptime timeout_t: type, wheel_bit: comptime_int, wheel_num: co
to.pending = null;
to.setTimeouts(null);

if (allow_intervals and to.interval != 0) {
if (intervals == .AllowIntervals and to.interval != 0) {
to.expires += to.interval;

if (to.expires <= self.curtime) {
Expand All @@ -402,7 +414,7 @@ fn TimeoutWheel(comptime timeout_t: type, wheel_bit: comptime_int, wheel_num: co
};
}

const DefaultTimeoutWheel = TimeoutWheel(u64, 6, 4, true, true);
const DefaultTimeoutWheel = TimeoutWheel(u64, 6, 4, .AllowIntervals, .AllowRelative);

test "timeout_wheel" {
assert(DefaultTimeoutWheel.timeout_wheel(1) == 0);
Expand All @@ -417,23 +429,23 @@ test "basic test" {
inline for ([]type{
DefaultTimeoutWheel,
// test with all flag combinations
TimeoutWheel(u64, 6, 4, false, true),
TimeoutWheel(u64, 6, 4, true, false),
TimeoutWheel(u64, 6, 4, false, false),
TimeoutWheel(u64, 6, 4, .NoIntervals, .AllowRelative),
TimeoutWheel(u64, 6, 4, .AllowIntervals, .NoRelative),
TimeoutWheel(u64, 6, 4, .NoIntervals, .NoRelative),
// test with some different bit sizes
TimeoutWheel(u128, 11, 4, true, true),
TimeoutWheel(u64, 10, 4, true, true),
TimeoutWheel(u64, 9, 4, true, true),
TimeoutWheel(u64, 8, 4, true, true),
TimeoutWheel(u64, 7, 4, true, true),
TimeoutWheel(u64, 5, 4, true, true),
TimeoutWheel(u64, 4, 4, true, true),
TimeoutWheel(u64, 3, 4, true, true),
TimeoutWheel(u128, 11, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u64, 10, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u64, 9, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u64, 8, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u64, 7, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u64, 5, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u64, 4, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u64, 3, 4, .AllowIntervals, .AllowRelative),
// different timeout sizes
TimeoutWheel(u128, 3, 4, true, true),
TimeoutWheel(u32, 3, 4, true, true),
TimeoutWheel(u16, 3, 4, true, true),
TimeoutWheel(u8, 2, 2, true, true),
TimeoutWheel(u128, 3, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u32, 3, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u16, 3, 4, .AllowIntervals, .AllowRelative),
TimeoutWheel(u8, 2, 2, .AllowIntervals, .AllowRelative),
}) |TimeoutWheelType| {
var mywheel = TimeoutWheelType.init();
assert(mywheel.pending() == false);
Expand Down