From bf8e419d2b7853f5cb5aba4dcba45ae28a3840aa Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 29 Apr 2018 00:40:04 -0400 Subject: [PATCH] linux uses pthreads when linking against libc --- src/all_types.hpp | 1 - src/analyze.cpp | 8 -------- src/codegen.cpp | 2 -- std/c/index.zig | 10 +++++----- std/os/index.zig | 9 +++++---- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/all_types.hpp b/src/all_types.hpp index f08b870b37..d1b2ad61d2 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1486,7 +1486,6 @@ struct CodeGen { ZigList link_libs_list; LinkLib *libc_link_lib; - LinkLib *pthread_link_lib; // add -framework [name] args to linker ZigList darwin_frameworks; diff --git a/src/analyze.cpp b/src/analyze.cpp index 8a9d236790..1ecfe32f4c 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -6049,15 +6049,10 @@ LinkLib *create_link_lib(Buf *name) { LinkLib *add_link_lib(CodeGen *g, Buf *name) { bool is_libc = buf_eql_str(name, "c"); - bool is_pthread = buf_eql_str(name, "pthread"); if (is_libc && g->libc_link_lib != nullptr) return g->libc_link_lib; - if (is_pthread && g->pthread_link_lib != nullptr) { - return g->pthread_link_lib; - } - for (size_t i = 0; i < g->link_libs_list.length; i += 1) { LinkLib *existing_lib = g->link_libs_list.at(i); if (buf_eql_buf(existing_lib->name, name)) { @@ -6071,9 +6066,6 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) { if (is_libc) g->libc_link_lib = link_lib; - if (is_pthread) - g->pthread_link_lib = link_lib; - return link_lib; } diff --git a/src/codegen.cpp b/src/codegen.cpp index 9f064d5f19..2d8c385f44 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -145,7 +145,6 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out { g->libc_link_lib = create_link_lib(buf_create_from_str("c")); g->link_libs_list.append(g->libc_link_lib); - g->pthread_link_lib = create_link_lib(buf_create_from_str("pthread")); } return g; @@ -6374,7 +6373,6 @@ static void define_builtin_compile_vars(CodeGen *g) { buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt); buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode)); buf_appendf(contents, "pub const link_libc = %s;\n", bool_to_str(g->libc_link_lib != nullptr)); - buf_appendf(contents, "pub const link_pthread = %s;\n", bool_to_str(g->pthread_link_lib != nullptr)); buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing)); buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n"); diff --git a/std/c/index.zig b/std/c/index.zig index 5ea7145cd3..34269d2aa2 100644 --- a/std/c/index.zig +++ b/std/c/index.zig @@ -54,12 +54,12 @@ pub extern "c" fn realloc(&c_void, usize) ?&c_void; pub extern "c" fn free(&c_void) void; pub extern "c" fn posix_memalign(memptr: &&c_void, alignment: usize, size: usize) c_int; -pub extern "c" fn pthread_create(noalias newthread: &pthread_t, +pub extern "pthread" fn pthread_create(noalias newthread: &pthread_t, noalias attr: ?&const pthread_attr_t, start_routine: extern fn(?&c_void) ?&c_void, noalias arg: ?&c_void) c_int; -pub extern "c" fn pthread_attr_init(attr: &pthread_attr_t) c_int; -pub extern "c" fn pthread_attr_setstack(attr: &pthread_attr_t, stackaddr: &c_void, stacksize: usize) c_int; -pub extern "c" fn pthread_attr_destroy(attr: &pthread_attr_t) c_int; -pub extern "c" fn pthread_join(thread: pthread_t, arg_return: ?&?&c_void) c_int; +pub extern "pthread" fn pthread_attr_init(attr: &pthread_attr_t) c_int; +pub extern "pthread" fn pthread_attr_setstack(attr: &pthread_attr_t, stackaddr: &c_void, stacksize: usize) c_int; +pub extern "pthread" fn pthread_attr_destroy(attr: &pthread_attr_t) c_int; +pub extern "pthread" fn pthread_join(thread: pthread_t, arg_return: ?&?&c_void) c_int; pub const pthread_t = &@OpaqueType(); diff --git a/std/os/index.zig b/std/os/index.zig index 8681a018b9..85e46a1bf9 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -2352,11 +2352,12 @@ pub const Thread = struct { stack: []u8, pthread_handle: pthread_t, - const pthread_t = if (builtin.link_pthread) c.pthread_t else void; - const pid_t = if (!builtin.link_pthread) i32 else void; + pub const use_pthreads = is_posix and builtin.link_libc; + const pthread_t = if (use_pthreads) c.pthread_t else void; + const pid_t = if (!use_pthreads) i32 else void; pub fn wait(self: &const Thread) void { - if (builtin.link_pthread) { + if (use_pthreads) { const err = c.pthread_join(self.pthread_handle, null); switch (err) { 0 => {}, @@ -2475,7 +2476,7 @@ pub fn spawnThread(stack: []align(os.page_size) u8, context: var, comptime start if (builtin.os == builtin.Os.windows) { // use windows API directly @compileError("TODO support spawnThread for Windows"); - } else if (builtin.link_pthread) { + } else if (Thread.use_pthreads) { // use pthreads var attr: c.pthread_attr_t = undefined; if (c.pthread_attr_init(&attr) != 0) return SpawnThreadError.SystemResources;