Skip to content

Commit 772be1d

Browse files
committed
Sync primitives timeout refactor and high prio task yield
1 parent 82c7d8d commit 772be1d

File tree

4 files changed

+258
-200
lines changed

4 files changed

+258
-200
lines changed

examples/espressif/esp/src/lwip/exports.zig

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,13 @@ export fn sys_mbox_set_invalid(ptr: *c.sys_mbox_t) void {
107107

108108
export fn sys_mbox_post(ptr: *c.sys_mbox_t, element: MailboxElement) void {
109109
const mailbox: *Mailbox = @ptrCast(@alignCast(ptr.*));
110-
mailbox.put_one(element, null) catch unreachable;
110+
mailbox.put_one(element, .never) catch unreachable;
111111
}
112112

113113
export fn sys_mbox_trypost(ptr: *c.sys_mbox_t, element: MailboxElement) c.err_t {
114114
const mailbox: *Mailbox = @ptrCast(@alignCast(ptr.*));
115-
if (mailbox.put_one_non_blocking(element)) {
116-
return c.ERR_OK;
117-
} else {
118-
return c.ERR_MEM;
119-
}
115+
mailbox.put_one(element, .non_blocking) catch return c.ERR_MEM;
116+
return c.ERR_OK;
120117
}
121118

122119
comptime {
@@ -126,7 +123,7 @@ comptime {
126123
export fn sys_arch_mbox_fetch(ptr: *c.sys_mbox_t, element_ptr: *MailboxElement, timeout: u32) u32 {
127124
const mailbox: *Mailbox = @ptrCast(@alignCast(ptr.*));
128125
const now = esp.time.get_time_since_boot();
129-
element_ptr.* = mailbox.get_one(if (timeout != 0) .from_ms(timeout) else null) catch {
126+
element_ptr.* = mailbox.get_one(if (timeout != 0) .{ .after = .from_ms(timeout) } else .never) catch {
130127
return c.SYS_ARCH_TIMEOUT;
131128
};
132129
// returns waiting time in ms
@@ -135,12 +132,9 @@ export fn sys_arch_mbox_fetch(ptr: *c.sys_mbox_t, element_ptr: *MailboxElement,
135132

136133
export fn sys_arch_mbox_tryfetch(ptr: *c.sys_mbox_t, element_ptr: *MailboxElement) u32 {
137134
const mailbox: *Mailbox = @ptrCast(@alignCast(ptr.*));
138-
if (mailbox.get_one_non_blocking()) |element| {
139-
element_ptr.* = element;
140-
return 0;
141-
} else {
142-
return c.SYS_MBOX_EMPTY;
143-
}
135+
const element = mailbox.get_one(.non_blocking) catch return c.SYS_MBOX_EMPTY;
136+
element_ptr.* = element;
137+
return 0;
144138
}
145139

146140
export fn sys_sem_new(ptr: *c.sys_sem_t, count: u8) c.err_t {
@@ -166,7 +160,7 @@ export fn sys_sem_signal(ptr: *c.sys_sem_t) void {
166160
export fn sys_arch_sem_wait(ptr: *c.sys_sem_t, timeout: u32) u32 {
167161
const sem: *rtos.Semaphore = @ptrCast(@alignCast(ptr.*));
168162
const now = esp.time.get_time_since_boot();
169-
sem.take_with_timeout(if (timeout != 0) .from_ms(timeout) else null) catch {
163+
sem.take_with_timeout(if (timeout != 0) .{ .after = .from_ms(timeout) } else .never) catch {
170164
return c.SYS_ARCH_TIMEOUT;
171165
};
172166
// returns waiting time in ms

port/espressif/esp/src/hal/radio/osi.zig

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,12 @@ pub fn semphr_take(ptr: ?*anyopaque, tick: u32) callconv(.c) i32 {
321321
log.debug("semphr_take {?} {}", .{ ptr, tick });
322322

323323
const sem: *rtos.Semaphore = @ptrCast(@alignCast(ptr));
324-
const maybe_timeout: ?rtos.Duration = if (tick == c.OSI_FUNCS_TIME_BLOCKING)
325-
.from_ticks(tick)
326-
else
327-
null;
328-
sem.take_with_timeout(maybe_timeout) catch {
324+
const timeout: rtos.Timeout = switch (tick) {
325+
0 => .non_blocking,
326+
c.OSI_FUNCS_TIME_BLOCKING => .never,
327+
else => |ticks| .{ .after = .from_ticks(ticks) },
328+
};
329+
sem.take_with_timeout(timeout) catch {
329330
log.debug(">>>> return from semaphore take with timeout: {*}", .{sem});
330331
return 1;
331332
};
@@ -370,10 +371,11 @@ const RecursiveMutex = struct {
370371
if (@intFromEnum(current_task.priority) > @intFromEnum(owning_task.priority)) {
371372
mutex.prev_priority = owning_task.priority;
372373
owning_task.priority = current_task.priority;
373-
rtos.make_ready(owning_task);
374+
var _hptw = false;
375+
rtos.make_ready(owning_task, &_hptw);
374376
}
375377

376-
mutex.wait_queue.wait(current_task, null);
378+
mutex.wait_queue.wait(null);
377379
}
378380

379381
assert(mutex.value == 0);
@@ -397,8 +399,13 @@ const RecursiveMutex = struct {
397399
owning_task.priority = prev_priority;
398400
mutex.prev_priority = null;
399401
}
402+
400403
mutex.owning_task = null;
401-
mutex.wait_queue.wake_one();
404+
405+
var hptw = false;
406+
mutex.wait_queue.wake_one(&hptw);
407+
if (hptw) rtos.yield_from_cs(.reschedule);
408+
402409
return true;
403410
} else {
404411
return false;
@@ -507,17 +514,12 @@ pub fn queue_send(ptr: ?*anyopaque, item_ptr: ?*anyopaque, block_time_tick: u32)
507514
const queue: *QueueWrapper = @ptrCast(@alignCast(ptr));
508515
const item: [*]const u8 = @ptrCast(@alignCast(item_ptr));
509516

510-
const size = switch (block_time_tick) {
511-
0 => queue.inner.put_non_blocking(item[0..queue.item_len]),
512-
else => queue.inner.put(
513-
item[0..queue.item_len],
514-
1,
515-
if (block_time_tick != c.OSI_FUNCS_TIME_BLOCKING)
516-
.from_ticks(block_time_tick)
517-
else
518-
null,
519-
),
517+
const timeout: rtos.Timeout = switch (block_time_tick) {
518+
0 => .non_blocking,
519+
c.OSI_FUNCS_TIME_BLOCKING => .never,
520+
else => |ticks| .{ .after = .from_ticks(ticks) },
520521
};
522+
const size = queue.inner.put(item[0..queue.item_len], 1, timeout);
521523
if (size == 0) return -1;
522524
return 1;
523525
}
@@ -527,9 +529,11 @@ pub fn queue_send_from_isr(ptr: ?*anyopaque, item_ptr: ?*anyopaque, _hptw: ?*any
527529

528530
const queue: *QueueWrapper = @ptrCast(@alignCast(ptr));
529531
const item: [*]const u8 = @ptrCast(@alignCast(item_ptr));
530-
const n = @divExact(queue.inner.put_non_blocking(item[0..queue.item_len]), queue.item_len);
531532

532-
@as(*u32, @ptrCast(@alignCast(_hptw))).* = @intFromBool(rtos.is_a_higher_priority_task_ready());
533+
var hptw = false;
534+
const n = @divExact(queue.inner.put_from_isr(item[0..queue.item_len], &hptw), queue.item_len);
535+
536+
@as(*u32, @ptrCast(@alignCast(_hptw))).* |= @intFromBool(hptw);
533537

534538
return @intCast(n);
535539
}
@@ -548,17 +552,12 @@ pub fn queue_recv(ptr: ?*anyopaque, item_ptr: ?*anyopaque, block_time_tick: u32)
548552
const queue: *QueueWrapper = @ptrCast(@alignCast(ptr));
549553
const item: [*]u8 = @ptrCast(@alignCast(item_ptr));
550554

551-
const size = switch (block_time_tick) {
552-
0 => queue.inner.get_non_blocking(item[0..queue.item_len]),
553-
else => queue.inner.get(
554-
item[0..queue.item_len],
555-
queue.item_len,
556-
if (block_time_tick != c.OSI_FUNCS_TIME_BLOCKING)
557-
.from_ticks(block_time_tick)
558-
else
559-
null,
560-
),
555+
const timeout: rtos.Timeout = switch (block_time_tick) {
556+
0 => .non_blocking,
557+
c.OSI_FUNCS_TIME_BLOCKING => .never,
558+
else => |ticks| .{ .after = .from_ticks(ticks) },
561559
};
560+
const size = queue.inner.get(item[0..queue.item_len], 1, timeout);
562561
if (size == 0) return -1;
563562
return 1;
564563
}

port/espressif/esp/src/hal/radio/timer.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,16 @@ fn task_fn() void {
150150
callback(arg);
151151
}
152152

153-
const sleep_duration: ?rtos.Duration = blk: {
153+
const timeout: rtos.Timeout = blk: {
154154
mutex.lock();
155155
defer mutex.unlock();
156156
break :blk if (find_next_wake_absolute()) |next_wake_absolute|
157-
.from_us(@truncate(next_wake_absolute.diff(now).to_us()))
157+
.{ .after = .from_us(@truncate(next_wake_absolute.diff(now).to_us())) }
158158
else
159-
null;
159+
.never;
160160
};
161161

162-
reload_semaphore.take_with_timeout(sleep_duration) catch {};
162+
reload_semaphore.take_with_timeout(timeout) catch {};
163163
}
164164
}
165165

0 commit comments

Comments
 (0)