| ... | ... | @@ -389,6 +389,7 @@ const AsyncClosure = struct { |
| 389 | 389 | select_condition: ?*ResetEvent, |
| 390 | 390 | context_alignment: std.mem.Alignment, |
| 391 | 391 | result_offset: usize, |
| 392 | alloc_len: usize, |
| 392 | 393 | |
| 393 | 394 | const done_reset_event: *ResetEvent = @ptrFromInt(@alignOf(ResetEvent)); |
| 394 | 395 | |
| ... | ... | @@ -425,18 +426,59 @@ const AsyncClosure = struct { |
| 425 | 426 | |
| 426 | 427 | fn contextPointer(ac: *AsyncClosure) [*]u8 { |
| 427 | 428 | const base: [*]u8 = @ptrCast(ac); |
| 428 | | return base + ac.context_alignment.forward(@sizeOf(AsyncClosure)); |
| 429 | const context_offset = ac.context_alignment.forward(@intFromPtr(ac) + @sizeOf(AsyncClosure)) - @intFromPtr(ac); |
| 430 | return base + context_offset; |
| 431 | } |
| 432 | |
| 433 | fn init( |
| 434 | gpa: Allocator, |
| 435 | mode: enum { async, concurrent }, |
| 436 | result_len: usize, |
| 437 | result_alignment: std.mem.Alignment, |
| 438 | context: []const u8, |
| 439 | context_alignment: std.mem.Alignment, |
| 440 | func: *const fn (context: *const anyopaque, result: *anyopaque) void, |
| 441 | ) Allocator.Error!*AsyncClosure { |
| 442 | const max_context_misalignment = context_alignment.toByteUnits() -| @alignOf(AsyncClosure); |
| 443 | const worst_case_context_offset = context_alignment.forward(@sizeOf(AsyncClosure) + max_context_misalignment); |
| 444 | const worst_case_result_offset = result_alignment.forward(worst_case_context_offset + context.len); |
| 445 | const alloc_len = worst_case_result_offset + result_len; |
| 446 | |
| 447 | const ac: *AsyncClosure = @ptrCast(@alignCast(try gpa.alignedAlloc(u8, .of(AsyncClosure), alloc_len))); |
| 448 | errdefer comptime unreachable; |
| 449 | |
| 450 | const actual_context_addr = context_alignment.forward(@intFromPtr(ac) + @sizeOf(AsyncClosure)); |
| 451 | const actual_result_addr = result_alignment.forward(actual_context_addr + context.len); |
| 452 | const actual_result_offset = actual_result_addr - @intFromPtr(ac); |
| 453 | ac.* = .{ |
| 454 | .closure = .{ |
| 455 | .cancel_tid = .none, |
| 456 | .start = start, |
| 457 | .is_concurrent = switch (mode) { |
| 458 | .async => false, |
| 459 | .concurrent => true, |
| 460 | }, |
| 461 | }, |
| 462 | .func = func, |
| 463 | .context_alignment = context_alignment, |
| 464 | .result_offset = actual_result_offset, |
| 465 | .alloc_len = alloc_len, |
| 466 | .reset_event = .unset, |
| 467 | .select_condition = null, |
| 468 | }; |
| 469 | @memcpy(ac.contextPointer()[0..context.len], context); |
| 470 | return ac; |
| 429 | 471 | } |
| 430 | 472 | |
| 431 | | fn waitAndFree(ac: *AsyncClosure, gpa: Allocator, result: []u8) void { |
| 473 | fn waitAndDeinit(ac: *AsyncClosure, gpa: Allocator, result: []u8) void { |
| 432 | 474 | ac.reset_event.waitUncancelable(); |
| 433 | 475 | @memcpy(result, ac.resultPointer()[0..result.len]); |
| 434 | | free(ac, gpa, result.len); |
| 476 | ac.deinit(gpa); |
| 435 | 477 | } |
| 436 | 478 | |
| 437 | | fn free(ac: *AsyncClosure, gpa: Allocator, result_len: usize) void { |
| 479 | fn deinit(ac: *AsyncClosure, gpa: Allocator) void { |
| 438 | 480 | const base: [*]align(@alignOf(AsyncClosure)) u8 = @ptrCast(ac); |
| 439 | | gpa.free(base[0 .. ac.result_offset + result_len]); |
| 481 | gpa.free(base[0..ac.alloc_len]); |
| 440 | 482 | } |
| 441 | 483 | }; |
| 442 | 484 | |
| ... | ... | @@ -452,6 +494,7 @@ fn async( |
| 452 | 494 | start(context.ptr, result.ptr); |
| 453 | 495 | return null; |
| 454 | 496 | } |
| 497 | |
| 455 | 498 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| 456 | 499 | const cpu_count = t.cpu_count catch { |
| 457 | 500 | return concurrent(userdata, result.len, result_alignment, context, context_alignment, start) catch { |
| ... | ... | @@ -459,37 +502,20 @@ fn async( |
| 459 | 502 | return null; |
| 460 | 503 | }; |
| 461 | 504 | }; |
| 505 | |
| 462 | 506 | const gpa = t.allocator; |
| 463 | | const context_offset = context_alignment.forward(@sizeOf(AsyncClosure)); |
| 464 | | const result_offset = result_alignment.forward(context_offset + context.len); |
| 465 | | const n = result_offset + result.len; |
| 466 | | const ac: *AsyncClosure = @ptrCast(@alignCast(gpa.alignedAlloc(u8, .of(AsyncClosure), n) catch { |
| 507 | const ac = AsyncClosure.init(gpa, .async, result.len, result_alignment, context, context_alignment, start) catch { |
| 467 | 508 | start(context.ptr, result.ptr); |
| 468 | 509 | return null; |
| 469 | | })); |
| 470 | | |
| 471 | | ac.* = .{ |
| 472 | | .closure = .{ |
| 473 | | .cancel_tid = .none, |
| 474 | | .start = AsyncClosure.start, |
| 475 | | .is_concurrent = false, |
| 476 | | }, |
| 477 | | .func = start, |
| 478 | | .context_alignment = context_alignment, |
| 479 | | .result_offset = result_offset, |
| 480 | | .reset_event = .unset, |
| 481 | | .select_condition = null, |
| 482 | 510 | }; |
| 483 | 511 | |
| 484 | | @memcpy(ac.contextPointer()[0..context.len], context); |
| 485 | | |
| 486 | 512 | t.mutex.lock(); |
| 487 | 513 | |
| 488 | 514 | const thread_capacity = cpu_count - 1 + t.concurrent_count; |
| 489 | 515 | |
| 490 | 516 | t.threads.ensureTotalCapacityPrecise(gpa, thread_capacity) catch { |
| 491 | 517 | t.mutex.unlock(); |
| 492 | | ac.free(gpa, result.len); |
| 518 | ac.deinit(gpa); |
| 493 | 519 | start(context.ptr, result.ptr); |
| 494 | 520 | return null; |
| 495 | 521 | }; |
| ... | ... | @@ -501,7 +527,7 @@ fn async( |
| 501 | 527 | if (t.threads.items.len == 0) { |
| 502 | 528 | assert(t.run_queue.popFirst() == &ac.closure.node); |
| 503 | 529 | t.mutex.unlock(); |
| 504 | | ac.free(gpa, result.len); |
| 530 | ac.deinit(gpa); |
| 505 | 531 | start(context.ptr, result.ptr); |
| 506 | 532 | return null; |
| 507 | 533 | } |
| ... | ... | @@ -530,27 +556,11 @@ fn concurrent( |
| 530 | 556 | |
| 531 | 557 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| 532 | 558 | const cpu_count = t.cpu_count catch 1; |
| 559 | |
| 533 | 560 | const gpa = t.allocator; |
| 534 | | const context_offset = context_alignment.forward(@sizeOf(AsyncClosure)); |
| 535 | | const result_offset = result_alignment.forward(context_offset + context.len); |
| 536 | | const n = result_offset + result_len; |
| 537 | | const ac_bytes = gpa.alignedAlloc(u8, .of(AsyncClosure), n) catch |
| 561 | const ac = AsyncClosure.init(gpa, .concurrent, result_len, result_alignment, context, context_alignment, start) catch { |
| 538 | 562 | return error.ConcurrencyUnavailable; |
| 539 | | const ac: *AsyncClosure = @ptrCast(@alignCast(ac_bytes)); |
| 540 | | |
| 541 | | ac.* = .{ |
| 542 | | .closure = .{ |
| 543 | | .cancel_tid = .none, |
| 544 | | .start = AsyncClosure.start, |
| 545 | | .is_concurrent = true, |
| 546 | | }, |
| 547 | | .func = start, |
| 548 | | .context_alignment = context_alignment, |
| 549 | | .result_offset = result_offset, |
| 550 | | .reset_event = .unset, |
| 551 | | .select_condition = null, |
| 552 | 563 | }; |
| 553 | | @memcpy(ac.contextPointer()[0..context.len], context); |
| 554 | 564 | |
| 555 | 565 | t.mutex.lock(); |
| 556 | 566 | |
| ... | ... | @@ -559,7 +569,7 @@ fn concurrent( |
| 559 | 569 | |
| 560 | 570 | t.threads.ensureTotalCapacity(gpa, thread_capacity) catch { |
| 561 | 571 | t.mutex.unlock(); |
| 562 | | ac.free(gpa, result_len); |
| 572 | ac.deinit(gpa); |
| 563 | 573 | return error.ConcurrencyUnavailable; |
| 564 | 574 | }; |
| 565 | 575 | |
| ... | ... | @@ -569,7 +579,7 @@ fn concurrent( |
| 569 | 579 | const thread = std.Thread.spawn(.{ .stack_size = t.stack_size }, worker, .{t}) catch { |
| 570 | 580 | assert(t.run_queue.popFirst() == &ac.closure.node); |
| 571 | 581 | t.mutex.unlock(); |
| 572 | | ac.free(gpa, result_len); |
| 582 | ac.deinit(gpa); |
| 573 | 583 | return error.ConcurrencyUnavailable; |
| 574 | 584 | }; |
| 575 | 585 | t.threads.appendAssumeCapacity(thread); |
| ... | ... | @@ -588,7 +598,7 @@ const GroupClosure = struct { |
| 588 | 598 | node: std.SinglyLinkedList.Node, |
| 589 | 599 | func: *const fn (*Io.Group, context: *anyopaque) void, |
| 590 | 600 | context_alignment: std.mem.Alignment, |
| 591 | | context_len: usize, |
| 601 | alloc_len: usize, |
| 592 | 602 | |
| 593 | 603 | fn start(closure: *Closure) void { |
| 594 | 604 | const gc: *GroupClosure = @alignCast(@fieldParentPtr("closure", closure)); |
| ... | ... | @@ -616,22 +626,48 @@ const GroupClosure = struct { |
| 616 | 626 | if (prev_state == (sync_one_pending | sync_is_waiting)) reset_event.set(); |
| 617 | 627 | } |
| 618 | 628 | |
| 619 | | fn free(gc: *GroupClosure, gpa: Allocator) void { |
| 620 | | const base: [*]align(@alignOf(GroupClosure)) u8 = @ptrCast(gc); |
| 621 | | gpa.free(base[0..contextEnd(gc.context_alignment, gc.context_len)]); |
| 622 | | } |
| 623 | | |
| 624 | | fn contextOffset(context_alignment: std.mem.Alignment) usize { |
| 625 | | return context_alignment.forward(@sizeOf(GroupClosure)); |
| 626 | | } |
| 627 | | |
| 628 | | fn contextEnd(context_alignment: std.mem.Alignment, context_len: usize) usize { |
| 629 | | return contextOffset(context_alignment) + context_len; |
| 630 | | } |
| 631 | | |
| 632 | 629 | fn contextPointer(gc: *GroupClosure) [*]u8 { |
| 633 | 630 | const base: [*]u8 = @ptrCast(gc); |
| 634 | | return base + contextOffset(gc.context_alignment); |
| 631 | const context_offset = gc.context_alignment.forward(@intFromPtr(gc) + @sizeOf(GroupClosure)) - @intFromPtr(gc); |
| 632 | return base + context_offset; |
| 633 | } |
| 634 | |
| 635 | /// Does not initialize the `node` field. |
| 636 | fn init( |
| 637 | gpa: Allocator, |
| 638 | t: *Threaded, |
| 639 | group: *Io.Group, |
| 640 | context: []const u8, |
| 641 | context_alignment: std.mem.Alignment, |
| 642 | func: *const fn (*Io.Group, context: *const anyopaque) void, |
| 643 | ) Allocator.Error!*GroupClosure { |
| 644 | const max_context_misalignment = context_alignment.toByteUnits() -| @alignOf(GroupClosure); |
| 645 | const worst_case_context_offset = context_alignment.forward(@sizeOf(GroupClosure) + max_context_misalignment); |
| 646 | const alloc_len = worst_case_context_offset + context.len; |
| 647 | |
| 648 | const gc: *GroupClosure = @ptrCast(@alignCast(try gpa.alignedAlloc(u8, .of(GroupClosure), alloc_len))); |
| 649 | errdefer comptime unreachable; |
| 650 | |
| 651 | gc.* = .{ |
| 652 | .closure = .{ |
| 653 | .cancel_tid = .none, |
| 654 | .start = start, |
| 655 | .is_concurrent = false, |
| 656 | }, |
| 657 | .t = t, |
| 658 | .group = group, |
| 659 | .node = undefined, |
| 660 | .func = func, |
| 661 | .context_alignment = context_alignment, |
| 662 | .alloc_len = alloc_len, |
| 663 | }; |
| 664 | @memcpy(gc.contextPointer()[0..context.len], context); |
| 665 | return gc; |
| 666 | } |
| 667 | |
| 668 | fn deinit(gc: *GroupClosure, gpa: Allocator) void { |
| 669 | const base: [*]align(@alignOf(GroupClosure)) u8 = @ptrCast(gc); |
| 670 | gpa.free(base[0..gc.alloc_len]); |
| 635 | 671 | } |
| 636 | 672 | |
| 637 | 673 | const sync_is_waiting: usize = 1 << 0; |
| ... | ... | @@ -646,27 +682,14 @@ fn groupAsync( |
| 646 | 682 | start: *const fn (*Io.Group, context: *const anyopaque) void, |
| 647 | 683 | ) void { |
| 648 | 684 | if (builtin.single_threaded) return start(group, context.ptr); |
| 685 | |
| 649 | 686 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| 650 | 687 | const cpu_count = t.cpu_count catch 1; |
| 688 | |
| 651 | 689 | const gpa = t.allocator; |
| 652 | | const n = GroupClosure.contextEnd(context_alignment, context.len); |
| 653 | | const gc: *GroupClosure = @ptrCast(@alignCast(gpa.alignedAlloc(u8, .of(GroupClosure), n) catch { |
| 690 | const gc = GroupClosure.init(gpa, t, group, context, context_alignment, start) catch { |
| 654 | 691 | return start(group, context.ptr); |
| 655 | | })); |
| 656 | | gc.* = .{ |
| 657 | | .closure = .{ |
| 658 | | .cancel_tid = .none, |
| 659 | | .start = GroupClosure.start, |
| 660 | | .is_concurrent = false, |
| 661 | | }, |
| 662 | | .t = t, |
| 663 | | .group = group, |
| 664 | | .node = undefined, |
| 665 | | .func = start, |
| 666 | | .context_alignment = context_alignment, |
| 667 | | .context_len = context.len, |
| 668 | 692 | }; |
| 669 | | @memcpy(gc.contextPointer()[0..context.len], context); |
| 670 | 693 | |
| 671 | 694 | t.mutex.lock(); |
| 672 | 695 | |
| ... | ... | @@ -678,7 +701,7 @@ fn groupAsync( |
| 678 | 701 | |
| 679 | 702 | t.threads.ensureTotalCapacityPrecise(gpa, thread_capacity) catch { |
| 680 | 703 | t.mutex.unlock(); |
| 681 | | gc.free(gpa); |
| 704 | gc.deinit(gpa); |
| 682 | 705 | return start(group, context.ptr); |
| 683 | 706 | }; |
| 684 | 707 | |
| ... | ... | @@ -688,7 +711,7 @@ fn groupAsync( |
| 688 | 711 | const thread = std.Thread.spawn(.{ .stack_size = t.stack_size }, worker, .{t}) catch { |
| 689 | 712 | assert(t.run_queue.popFirst() == &gc.closure.node); |
| 690 | 713 | t.mutex.unlock(); |
| 691 | | gc.free(gpa); |
| 714 | gc.deinit(gpa); |
| 692 | 715 | return start(group, context.ptr); |
| 693 | 716 | }; |
| 694 | 717 | t.threads.appendAssumeCapacity(thread); |
| ... | ... | @@ -730,7 +753,7 @@ fn groupWait(userdata: ?*anyopaque, group: *Io.Group, token: *anyopaque) void { |
| 730 | 753 | while (true) { |
| 731 | 754 | const gc: *GroupClosure = @fieldParentPtr("node", node); |
| 732 | 755 | const node_next = node.next; |
| 733 | | gc.free(gpa); |
| 756 | gc.deinit(gpa); |
| 734 | 757 | node = node_next orelse break; |
| 735 | 758 | } |
| 736 | 759 | } |
| ... | ... | @@ -761,7 +784,7 @@ fn groupCancel(userdata: ?*anyopaque, group: *Io.Group, token: *anyopaque) void |
| 761 | 784 | while (true) { |
| 762 | 785 | const gc: *GroupClosure = @fieldParentPtr("node", node); |
| 763 | 786 | const node_next = node.next; |
| 764 | | gc.free(gpa); |
| 787 | gc.deinit(gpa); |
| 765 | 788 | node = node_next orelse break; |
| 766 | 789 | } |
| 767 | 790 | } |
| ... | ... | @@ -776,7 +799,7 @@ fn await( |
| 776 | 799 | _ = result_alignment; |
| 777 | 800 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| 778 | 801 | const closure: *AsyncClosure = @ptrCast(@alignCast(any_future)); |
| 779 | | closure.waitAndFree(t.allocator, result); |
| 802 | closure.waitAndDeinit(t.allocator, result); |
| 780 | 803 | } |
| 781 | 804 | |
| 782 | 805 | fn cancel( |
| ... | ... | @@ -789,7 +812,7 @@ fn cancel( |
| 789 | 812 | const t: *Threaded = @ptrCast(@alignCast(userdata)); |
| 790 | 813 | const ac: *AsyncClosure = @ptrCast(@alignCast(any_future)); |
| 791 | 814 | ac.closure.requestCancel(); |
| 792 | | ac.waitAndFree(t.allocator, result); |
| 815 | ac.waitAndDeinit(t.allocator, result); |
| 793 | 816 | } |
| 794 | 817 | |
| 795 | 818 | fn cancelRequested(userdata: ?*anyopaque) bool { |
| ... | ... | @@ -2864,7 +2887,8 @@ fn nowWindows(userdata: ?*anyopaque, clock: Io.Clock) Io.Clock.Error!Io.Timestam |
| 2864 | 2887 | .real => { |
| 2865 | 2888 | // RtlGetSystemTimePrecise() has a granularity of 100 nanoseconds |
| 2866 | 2889 | // and uses the NTFS/Windows epoch, which is 1601-01-01. |
| 2867 | | return .{ .nanoseconds = @as(i96, windows.ntdll.RtlGetSystemTimePrecise()) * 100 }; |
| 2890 | const epoch_ns = std.time.epoch.windows * std.time.ns_per_s; |
| 2891 | return .{ .nanoseconds = @as(i96, windows.ntdll.RtlGetSystemTimePrecise()) * 100 + epoch_ns }; |
| 2868 | 2892 | }, |
| 2869 | 2893 | .awake, .boot => { |
| 2870 | 2894 | // QPC on windows doesn't fail on >= XP/2000 and includes time suspended. |