diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-04-29 00:07:32 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-04-29 00:07:32 -0400 |
| commit | a42542099392cf189b96bdd77ecd88feadfb6382 (patch) | |
| tree | 723f625b401ca2bfbe1c343d2f19e472a514ba65 /std | |
| parent | 998e25a01e8b3ada235aee4a9f785a7454de4b3f (diff) | |
| download | zig-a42542099392cf189b96bdd77ecd88feadfb6382.tar.gz zig-a42542099392cf189b96bdd77ecd88feadfb6382.zip | |
make pthreads threads work on darwin
darwin pthreads adds a restriction that the stack start and end
must be page aligned
Diffstat (limited to 'std')
| -rw-r--r-- | std/os/index.zig | 10 | ||||
| -rw-r--r-- | std/os/test.zig | 4 |
2 files changed, 9 insertions, 5 deletions
diff --git a/std/os/index.zig b/std/os/index.zig index fa1cc418a5..8681a018b9 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -2421,7 +2421,7 @@ pub fn spawnThreadAllocator(allocator: &mem.Allocator, context: var, comptime st // TODO compile-time call graph analysis to determine stack upper bound // https://github.com/zig-lang/zig/issues/157 const default_stack_size = 8 * 1024 * 1024; - const stack_bytes = try allocator.alloc(u8, default_stack_size); + const stack_bytes = try allocator.alignedAlloc(u8, os.page_size, default_stack_size); const thread = try spawnThread(stack_bytes, context, startFn); thread.allocator = allocator; return thread; @@ -2431,7 +2431,7 @@ pub fn spawnThreadAllocator(allocator: &mem.Allocator, context: var, comptime st /// fn startFn(@typeOf(context)) T /// where T is u8, noreturn, void, or !void /// caller must call wait on the returned thread -pub fn spawnThread(stack: []u8, context: var, comptime startFn: var) SpawnThreadError!&Thread { +pub fn spawnThread(stack: []align(os.page_size) u8, context: var, comptime startFn: var) SpawnThreadError!&Thread { const Context = @typeOf(context); comptime assert(@ArgType(@typeOf(startFn), 0) == Context); @@ -2481,8 +2481,12 @@ pub fn spawnThread(stack: []u8, context: var, comptime startFn: var) SpawnThread if (c.pthread_attr_init(&attr) != 0) return SpawnThreadError.SystemResources; defer assert(c.pthread_attr_destroy(&attr) == 0); + // align to page + stack_end -= stack_end % os.page_size; + const stack_size = stack_end - @ptrToInt(stack.ptr); - if (c.pthread_attr_setstack(&attr, @ptrCast(&c_void, stack.ptr), stack_size) != 0) { + const setstack_err = c.pthread_attr_setstack(&attr, @ptrCast(&c_void, stack.ptr), stack_size); + if (setstack_err != 0) { return SpawnThreadError.StackTooSmall; // pthreads requires at least 16384 bytes } diff --git a/std/os/test.zig b/std/os/test.zig index 87486bde4f..37e5bf4bb8 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -57,8 +57,8 @@ test "spawn threads" { const thread1 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, {}, start1); const thread4 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, &shared_ctx, start2); - var stack1: [20 * 1024]u8 = undefined; - var stack2: [20 * 1024]u8 = undefined; + var stack1: [20 * 1024]u8 align(os.page_size) = undefined; + var stack2: [20 * 1024]u8 align(os.page_size) = undefined; const thread2 = try std.os.spawnThread(stack1[0..], &shared_ctx, start2); const thread3 = try std.os.spawnThread(stack2[0..], &shared_ctx, start2); |
