authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 00:40:04-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 00:40:04-04:00
logbf8e419d2b7853f5cb5aba4dcba45ae28a3840aa
treedca5260dd4a11773fec68dd5973201be6f61b725
parentabf90eaa674782e092e49bb23c4c7da0f581f604

linux uses pthreads when linking against libc


5 files changed, 10 insertions(+), 20 deletions(-)

src/all_types.hpp-1
...@@ -1486,7 +1486,6 @@ struct CodeGen {...@@ -1486,7 +1486,6 @@ struct CodeGen {
14861486
1487 ZigList<LinkLib *> link_libs_list;1487 ZigList<LinkLib *> link_libs_list;
1488 LinkLib *libc_link_lib;1488 LinkLib *libc_link_lib;
1489 LinkLib *pthread_link_lib;
14901489
1491 // add -framework [name] args to linker1490 // add -framework [name] args to linker
1492 ZigList<Buf *> darwin_frameworks;1491 ZigList<Buf *> darwin_frameworks;
src/analyze.cpp-8
...@@ -6049,15 +6049,10 @@ LinkLib *create_link_lib(Buf *name) {...@@ -6049,15 +6049,10 @@ LinkLib *create_link_lib(Buf *name) {
60496049
6050LinkLib *add_link_lib(CodeGen *g, Buf *name) {6050LinkLib *add_link_lib(CodeGen *g, Buf *name) {
6051 bool is_libc = buf_eql_str(name, "c");6051 bool is_libc = buf_eql_str(name, "c");
6052 bool is_pthread = buf_eql_str(name, "pthread");
60536052
6054 if (is_libc && g->libc_link_lib != nullptr)6053 if (is_libc && g->libc_link_lib != nullptr)
6055 return g->libc_link_lib;6054 return g->libc_link_lib;
60566055
6057 if (is_pthread && g->pthread_link_lib != nullptr) {
6058 return g->pthread_link_lib;
6059 }
6060
6061 for (size_t i = 0; i < g->link_libs_list.length; i += 1) {6056 for (size_t i = 0; i < g->link_libs_list.length; i += 1) {
6062 LinkLib *existing_lib = g->link_libs_list.at(i);6057 LinkLib *existing_lib = g->link_libs_list.at(i);
6063 if (buf_eql_buf(existing_lib->name, name)) {6058 if (buf_eql_buf(existing_lib->name, name)) {
...@@ -6071,9 +6066,6 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {...@@ -6071,9 +6066,6 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
6071 if (is_libc)6066 if (is_libc)
6072 g->libc_link_lib = link_lib;6067 g->libc_link_lib = link_lib;
60736068
6074 if (is_pthread)
6075 g->pthread_link_lib = link_lib;
6076
6077 return link_lib;6069 return link_lib;
6078}6070}
60796071
src/codegen.cpp-2
...@@ -145,7 +145,6 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out...@@ -145,7 +145,6 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
145 {145 {
146 g->libc_link_lib = create_link_lib(buf_create_from_str("c"));146 g->libc_link_lib = create_link_lib(buf_create_from_str("c"));
147 g->link_libs_list.append(g->libc_link_lib);147 g->link_libs_list.append(g->libc_link_lib);
148 g->pthread_link_lib = create_link_lib(buf_create_from_str("pthread"));
149 }148 }
150149
151 return g;150 return g;
...@@ -6374,7 +6373,6 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6374,7 +6373,6 @@ static void define_builtin_compile_vars(CodeGen *g) {
6374 buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt);6373 buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt);
6375 buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode));6374 buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode));
6376 buf_appendf(contents, "pub const link_libc = %s;\n", bool_to_str(g->libc_link_lib != nullptr));6375 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));
6378 buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing));6376 buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing));
63796377
6380 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");6378 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");
std/c/index.zig+5-5
...@@ -54,12 +54,12 @@ pub extern "c" fn realloc(&c_void, usize) ?&c_void;...@@ -54,12 +54,12 @@ pub extern "c" fn realloc(&c_void, usize) ?&c_void;
54pub extern "c" fn free(&c_void) void;54pub extern "c" fn free(&c_void) void;
55pub extern "c" fn posix_memalign(memptr: &&c_void, alignment: usize, size: usize) c_int;55pub extern "c" fn posix_memalign(memptr: &&c_void, alignment: usize, size: usize) c_int;
5656
57pub extern "c" fn pthread_create(noalias newthread: &pthread_t,57pub extern "pthread" fn pthread_create(noalias newthread: &pthread_t,
58 noalias attr: ?&const pthread_attr_t, start_routine: extern fn(?&c_void) ?&c_void,58 noalias attr: ?&const pthread_attr_t, start_routine: extern fn(?&c_void) ?&c_void,
59 noalias arg: ?&c_void) c_int;59 noalias arg: ?&c_void) c_int;
60pub extern "c" fn pthread_attr_init(attr: &pthread_attr_t) c_int;60pub extern "pthread" fn pthread_attr_init(attr: &pthread_attr_t) c_int;
61pub extern "c" fn pthread_attr_setstack(attr: &pthread_attr_t, stackaddr: &c_void, stacksize: usize) c_int;61pub extern "pthread" fn pthread_attr_setstack(attr: &pthread_attr_t, stackaddr: &c_void, stacksize: usize) c_int;
62pub extern "c" fn pthread_attr_destroy(attr: &pthread_attr_t) c_int;62pub extern "pthread" fn pthread_attr_destroy(attr: &pthread_attr_t) c_int;
63pub extern "c" fn pthread_join(thread: pthread_t, arg_return: ?&?&c_void) c_int;63pub extern "pthread" fn pthread_join(thread: pthread_t, arg_return: ?&?&c_void) c_int;
6464
65pub const pthread_t = &@OpaqueType();65pub const pthread_t = &@OpaqueType();
std/os/index.zig+5-4
...@@ -2352,11 +2352,12 @@ pub const Thread = struct {...@@ -2352,11 +2352,12 @@ pub const Thread = struct {
2352 stack: []u8,2352 stack: []u8,
2353 pthread_handle: pthread_t,2353 pthread_handle: pthread_t,
23542354
2355 const pthread_t = if (builtin.link_pthread) c.pthread_t else void;2355 pub const use_pthreads = is_posix and builtin.link_libc;
2356 const pid_t = if (!builtin.link_pthread) i32 else void;2356 const pthread_t = if (use_pthreads) c.pthread_t else void;
2357 const pid_t = if (!use_pthreads) i32 else void;
23572358
2358 pub fn wait(self: &const Thread) void {2359 pub fn wait(self: &const Thread) void {
2359 if (builtin.link_pthread) {2360 if (use_pthreads) {
2360 const err = c.pthread_join(self.pthread_handle, null);2361 const err = c.pthread_join(self.pthread_handle, null);
2361 switch (err) {2362 switch (err) {
2362 0 => {},2363 0 => {},
...@@ -2475,7 +2476,7 @@ pub fn spawnThread(stack: []align(os.page_size) u8, context: var, comptime start...@@ -2475,7 +2476,7 @@ pub fn spawnThread(stack: []align(os.page_size) u8, context: var, comptime start
2475 if (builtin.os == builtin.Os.windows) {2476 if (builtin.os == builtin.Os.windows) {
2476 // use windows API directly2477 // use windows API directly
2477 @compileError("TODO support spawnThread for Windows");2478 @compileError("TODO support spawnThread for Windows");
2478 } else if (builtin.link_pthread) {2479 } else if (Thread.use_pthreads) {
2479 // use pthreads2480 // use pthreads
2480 var attr: c.pthread_attr_t = undefined;2481 var attr: c.pthread_attr_t = undefined;
2481 if (c.pthread_attr_init(&attr) != 0) return SpawnThreadError.SystemResources;2482 if (c.pthread_attr_init(&attr) != 0) return SpawnThreadError.SystemResources;