const Task = struct fn create(gpa: Allocator, group: Group, context: []const u8, context_alignment: Alignment, func: *const fn(context: *const anyopaque) void) Allocator.Error!*TaskTask.runnable.node is undefined in the created Task.
fn destroy(task: *Task, gpa: Allocator) voidfn contextPointer(task: *Task) [*]u8fn start(r: *Runnable, thread: *Thread, t: *Threaded) void485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575 const Task = struct {
runnable: Runnable,
group: *Io.Group,
func: *const fn (context: *const anyopaque) void,
context_alignment: Alignment,
alloc_len: usize,
/// `Task.runnable.node` is `undefined` in the created `Task`.
fn create(
gpa: Allocator,
group: Group,
context: []const u8,
context_alignment: Alignment,
func: *const fn (context: *const anyopaque) void,
) Allocator.Error!*Task {
const max_context_misalignment = context_alignment.toByteUnits() -| @alignOf(Task);
const worst_case_context_offset = context_alignment.forward(@sizeOf(Task) + max_context_misalignment);
const alloc_len = worst_case_context_offset + context.len;
const task: *Task = @ptrCast(@alignCast(try gpa.alignedAlloc(u8, .of(Task), alloc_len)));
errdefer comptime unreachable;
task.* = .{
.runnable = .{
.node = undefined,
.startFn = &start,
},
.group = group.ptr,
.func = func,
.context_alignment = context_alignment,
.alloc_len = alloc_len,
};
@memcpy(task.contextPointer()[0..context.len], context);
return task;
}
fn destroy(task: *Task, gpa: Allocator) void {
const base: [*]align(@alignOf(Task)) u8 = @ptrCast(task);
gpa.free(base[0..task.alloc_len]);
}
fn contextPointer(task: *Task) [*]u8 {
const base: [*]u8 = @ptrCast(task);
const offset = task.context_alignment.forward(@intFromPtr(base) + @sizeOf(Task)) - @intFromPtr(base);
return base + offset;
}
fn start(r: *Runnable, thread: *Thread, t: *Threaded) void {
const task: *Task = @fieldParentPtr("runnable", r);
const group: Group = .{ .ptr = task.group };
// This would be a simple store, but it's upgraded to an RMW so we can use `.acquire` to
// enforce the ordering between this and the `group.status().load` below. Paired with
// the `.release` rmw on `Thread.status` in `cancelThreads`, this creates a StoreLoad
// barrier which guarantees that when a group is canceled, either we see the cancelation
// in the group status, or the canceler sees our thread status so can directly notify us
// of the cancelation.
_ = thread.status.swap(.{
.cancelation = .none,
.awaitable = .fromGroup(group.ptr),
}, .acquire);
if (group.status().load(.monotonic).canceled) {
thread.status.store(.{
.cancelation = .canceling,
.awaitable = .fromGroup(group.ptr),
}, .monotonic);
}
task.func(task.contextPointer());
thread.status.store(.{ .cancelation = .none, .awaitable = .null }, .monotonic);
const old_status = group.status().fetchSub(.{
.num_running = 1,
.have_awaiter = false,
.canceled = false,
}, .acq_rel); // acquire `group.awaiter()`, release task results
assert(old_status.num_running > 0);
if (old_status.have_awaiter and old_status.num_running == 1) {
const to_signal = group.awaiter().*;
// `awaiter` should only be modified by us. For another thread to see `num_running`
// drop to 0 after this point would indicate that another task started up, meaning
// `async`/`cancel` was racing with awaited group completion.
group.awaiter().* = undefined;
_ = to_signal.fetchAdd(1, .release); // release results
Thread.futexWake(&to_signal.raw, 1);
}
// Task completed. Self-destruct sequence initiated.
task.destroy(t.allocator);
}
}