| author | |
| committer | |
| log | 998e25a01e8b3ada235aee4a9f785a7454de4b3f |
| tree | 1b79bfe9853f513f921f396724be9c7787dc6463 |
| parent | a344cb03bc3c48f3c7fec32dc19c1bcad0910941 |
5 files changed, 21 insertions(+), 8 deletions(-)
src/all_types.hpp+1| ... | ... | @@ -1486,6 +1486,7 @@ struct CodeGen { |
| 1486 | 1486 | |
| 1487 | 1487 | ZigList<LinkLib *> link_libs_list; |
| 1488 | 1488 | LinkLib *libc_link_lib; |
| 1489 | LinkLib *pthread_link_lib; | |
| 1489 | 1490 | |
| 1490 | 1491 | // add -framework [name] args to linker |
| 1491 | 1492 | ZigList<Buf *> darwin_frameworks; |
src/analyze.cpp+8| ... | ... | @@ -6049,10 +6049,15 @@ LinkLib *create_link_lib(Buf *name) { |
| 6049 | 6049 | |
| 6050 | 6050 | LinkLib *add_link_lib(CodeGen *g, Buf *name) { |
| 6051 | 6051 | bool is_libc = buf_eql_str(name, "c"); |
| 6052 | bool is_pthread = buf_eql_str(name, "pthread"); | |
| 6052 | 6053 | |
| 6053 | 6054 | if (is_libc && g->libc_link_lib != nullptr) |
| 6054 | 6055 | return g->libc_link_lib; |
| 6055 | 6056 | |
| 6057 | if (is_pthread && g->pthread_link_lib != nullptr) { | |
| 6058 | return g->pthread_link_lib; | |
| 6059 | } | |
| 6060 | ||
| 6056 | 6061 | for (size_t i = 0; i < g->link_libs_list.length; i += 1) { |
| 6057 | 6062 | LinkLib *existing_lib = g->link_libs_list.at(i); |
| 6058 | 6063 | if (buf_eql_buf(existing_lib->name, name)) { |
| ... | ... | @@ -6066,6 +6071,9 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) { |
| 6066 | 6071 | if (is_libc) |
| 6067 | 6072 | g->libc_link_lib = link_lib; |
| 6068 | 6073 | |
| 6074 | if (is_pthread) | |
| 6075 | g->pthread_link_lib = link_lib; | |
| 6076 | ||
| 6069 | 6077 | return link_lib; |
| 6070 | 6078 | } |
| 6071 | 6079 |
src/codegen.cpp+2| ... | ... | @@ -145,6 +145,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out |
| 145 | 145 | { |
| 146 | 146 | g->libc_link_lib = create_link_lib(buf_create_from_str("c")); |
| 147 | 147 | g->link_libs_list.append(g->libc_link_lib); |
| 148 | g->pthread_link_lib = create_link_lib(buf_create_from_str("pthread")); | |
| 148 | 149 | } |
| 149 | 150 | |
| 150 | 151 | return g; |
| ... | ... | @@ -6373,6 +6374,7 @@ static void define_builtin_compile_vars(CodeGen *g) { |
| 6373 | 6374 | buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt); |
| 6374 | 6375 | buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode)); |
| 6375 | 6376 | buf_appendf(contents, "pub const link_libc = %s;\n", bool_to_str(g->libc_link_lib != nullptr)); |
| 6377 | buf_appendf(contents, "pub const link_pthread = %s;\n", bool_to_str(g->pthread_link_lib != nullptr)); | |
| 6376 | 6378 | buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing)); |
| 6377 | 6379 | |
| 6378 | 6380 | buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n"); |
std/os/index.zig+8-6| ... | ... | @@ -2352,12 +2352,11 @@ pub const Thread = struct { |
| 2352 | 2352 | stack: []u8, |
| 2353 | 2353 | pthread_handle: pthread_t, |
| 2354 | 2354 | |
| 2355 | pub const use_pthreads = is_posix and builtin.link_libc; | |
| 2356 | const pthread_t = if (use_pthreads) c.pthread_t else void; | |
| 2357 | const pid_t = if (!use_pthreads) i32 else void; | |
| 2355 | const pthread_t = if (builtin.link_pthread) c.pthread_t else void; | |
| 2356 | const pid_t = if (!builtin.link_pthread) i32 else void; | |
| 2358 | 2357 | |
| 2359 | 2358 | pub fn wait(self: &const Thread) void { |
| 2360 | if (use_pthreads) { | |
| 2359 | if (builtin.link_pthread) { | |
| 2361 | 2360 | const err = c.pthread_join(self.pthread_handle, null); |
| 2362 | 2361 | switch (err) { |
| 2363 | 2362 | 0 => {}, |
| ... | ... | @@ -2407,6 +2406,9 @@ pub const SpawnThreadError = error { |
| 2407 | 2406 | /// be copied. |
| 2408 | 2407 | SystemResources, |
| 2409 | 2408 | |
| 2409 | /// pthreads requires at least 16384 bytes of stack space | |
| 2410 | StackTooSmall, | |
| 2411 | ||
| 2410 | 2412 | Unexpected, |
| 2411 | 2413 | }; |
| 2412 | 2414 | |
| ... | ... | @@ -2473,7 +2475,7 @@ pub fn spawnThread(stack: []u8, context: var, comptime startFn: var) SpawnThread |
| 2473 | 2475 | if (builtin.os == builtin.Os.windows) { |
| 2474 | 2476 | // use windows API directly |
| 2475 | 2477 | @compileError("TODO support spawnThread for Windows"); |
| 2476 | } else if (Thread.use_pthreads) { | |
| 2478 | } else if (builtin.link_pthread) { | |
| 2477 | 2479 | // use pthreads |
| 2478 | 2480 | var attr: c.pthread_attr_t = undefined; |
| 2479 | 2481 | if (c.pthread_attr_init(&attr) != 0) return SpawnThreadError.SystemResources; |
| ... | ... | @@ -2481,7 +2483,7 @@ pub fn spawnThread(stack: []u8, context: var, comptime startFn: var) SpawnThread |
| 2481 | 2483 | |
| 2482 | 2484 | const stack_size = stack_end - @ptrToInt(stack.ptr); |
| 2483 | 2485 | if (c.pthread_attr_setstack(&attr, @ptrCast(&c_void, stack.ptr), stack_size) != 0) { |
| 2484 | return SpawnThreadError.SystemResources; | |
| 2486 | return SpawnThreadError.StackTooSmall; // pthreads requires at least 16384 bytes | |
| 2485 | 2487 | } |
| 2486 | 2488 | |
| 2487 | 2489 | const err = c.pthread_create(&thread_ptr.pthread_handle, &attr, MainFuncs.posixThreadMain, @intToPtr(&c_void, arg)); |
std/os/test.zig+2-2| ... | ... | @@ -57,8 +57,8 @@ test "spawn threads" { |
| 57 | 57 | const thread1 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, {}, start1); |
| 58 | 58 | const thread4 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, &shared_ctx, start2); |
| 59 | 59 | |
| 60 | var stack1: [1024]u8 = undefined; | |
| 61 | var stack2: [1024]u8 = undefined; | |
| 60 | var stack1: [20 * 1024]u8 = undefined; | |
| 61 | var stack2: [20 * 1024]u8 = undefined; | |
| 62 | 62 | |
| 63 | 63 | const thread2 = try std.os.spawnThread(stack1[0..], &shared_ctx, start2); |
| 64 | 64 | const thread3 = try std.os.spawnThread(stack2[0..], &shared_ctx, start2); |