| ... | ... | @@ -739,32 +739,24 @@ const PosixThreadImpl = struct { |
| 739 | 739 | }; |
| 740 | 740 | |
| 741 | 741 | const WasiThreadImpl = struct { |
| 742 | | comptime { |
| 743 | | // Sets the stack pointer, which is needed after creating a new thread |
| 744 | | // to ensure the stack of the main thread isn't being poluted. |
| 745 | | asm ( |
| 746 | | \\ .text |
| 747 | | \\ .export_name	__set_stack_pointer, __set_stack_pointer |
| 748 | | \\ .globaltype __stack_pointer, i32 |
| 749 | | \\ .hidden wasi_thread_start |
| 750 | | \\ .globl wasi_thread_start |
| 751 | | \\ .type __set_stack_pointer, @function |
| 752 | | \\ |
| 753 | | \\ __set_stack_pointer: |
| 754 | | \\	 .functype	__set_stack_pointer (i32) -> () |
| 755 | | \\ local.get 0 # The raw pointer which replaces the stack pointer |
| 756 | | \\ global.set __stack_pointer |
| 757 | | \\ end_function |
| 758 | | ); |
| 759 | | } |
| 760 | 742 | thread: *WasiThread, |
| 761 | 743 | |
| 762 | 744 | pub const ThreadHandle = i32; |
| 763 | 745 | threadlocal var tls_thread_id: Id = 0; |
| 764 | 746 | |
| 765 | 747 | const WasiThread = struct { |
| 748 | /// Thread ID |
| 766 | 749 | tid: Atomic(i32) = Atomic(i32).init(0), |
| 750 | /// Contains all memory which was allocated to bootstrap this thread, including: |
| 751 | /// - Guard page |
| 752 | /// - Stack |
| 753 | /// - TLS segment |
| 754 | /// - `Instance` |
| 755 | /// All memory is freed upon call to `join` |
| 767 | 756 | memory: []u8, |
| 757 | /// The allocator used to allocate the thread's memory, |
| 758 | /// which is also used during `join` to ensure clean-up. |
| 759 | allocator: std.mem.Allocator, |
| 768 | 760 | }; |
| 769 | 761 | |
| 770 | 762 | /// A meta-data structure used to bootstrap a thread |
| ... | ... | @@ -790,7 +782,7 @@ const WasiThreadImpl = struct { |
| 790 | 782 | } |
| 791 | 783 | |
| 792 | 784 | fn getHandle(self: Impl) ThreadHandle { |
| 793 | | return self.thread.tid; |
| 785 | return self.thread.tid.load(.SeqCst); |
| 794 | 786 | } |
| 795 | 787 | |
| 796 | 788 | fn detach(self: Impl) void { |
| ... | ... | @@ -813,7 +805,6 @@ const WasiThreadImpl = struct { |
| 813 | 805 | } |
| 814 | 806 | }; |
| 815 | 807 | |
| 816 | | var guard_offset: usize = undefined; |
| 817 | 808 | var stack_offset: usize = undefined; |
| 818 | 809 | var tls_offset: usize = undefined; |
| 819 | 810 | var wrapper_offset: usize = undefined; |
| ... | ... | @@ -824,8 +815,11 @@ const WasiThreadImpl = struct { |
| 824 | 815 | // - The TLS segment |
| 825 | 816 | // - `Instance` - containing information about how to call the user's function. |
| 826 | 817 | const map_bytes = blk: { |
| 818 | // start with atleast a single page, which is used as a guard to prevent |
| 819 | // other threads clobbering our new thread. |
| 820 | // Unfortunately, WebAssembly has no notion of read-only segments, so this |
| 821 | // is only a temporary measure until the entire page is "run over". |
| 827 | 822 | var bytes: usize = std.wasm.page_size; |
| 828 | | guard_offset = bytes; |
| 829 | 823 | |
| 830 | 824 | bytes = std.mem.alignForward(usize, bytes, 16); // align stack to 16 bytes |
| 831 | 825 | stack_offset = bytes; |
| ... | ... | @@ -855,7 +849,7 @@ const WasiThreadImpl = struct { |
| 855 | 849 | |
| 856 | 850 | const instance = @ptrCast(*Instance, @alignCast(@alignOf(Instance), &allocated_memory[instance_offset])); |
| 857 | 851 | instance.* = .{ |
| 858 | | .thread = .{ .memory = allocated_memory }, |
| 852 | .thread = .{ .memory = allocated_memory, .allocator = config.allocator.? }, |
| 859 | 853 | .base = @ptrToInt(allocated_memory.ptr), |
| 860 | 854 | .tls_base = tls_offset, |
| 861 | 855 | .stack_pointer = stack_offset, |
| ... | ... | @@ -923,6 +917,16 @@ const WasiThreadImpl = struct { |
| 923 | 917 | : [ret] "=r" (-> u32), |
| 924 | 918 | ); |
| 925 | 919 | } |
| 920 | |
| 921 | /// Allows for setting the stack pointer in the WebAssembly module. |
| 922 | inline fn __set_stack_pointer(addr: [*]u8) void { |
| 923 | asm volatile ( |
| 924 | \\ local.get %[ptr] |
| 925 | \\ global.set __stack_pointer |
| 926 | : |
| 927 | : [ptr] "r" (addr), |
| 928 | ); |
| 929 | } |
| 926 | 930 | }; |
| 927 | 931 | |
| 928 | 932 | const LinuxThreadImpl = struct { |