authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-28 23:47:39-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-28 23:47:39-04:00
log998e25a01e8b3ada235aee4a9f785a7454de4b3f
tree1b79bfe9853f513f921f396724be9c7787dc6463
parenta344cb03bc3c48f3c7fec32dc19c1bcad0910941

pthread support working


5 files changed, 21 insertions(+), 8 deletions(-)

src/all_types.hpp+1
...@@ -1486,6 +1486,7 @@ struct CodeGen {...@@ -1486,6 +1486,7 @@ 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;
14891490
1490 // add -framework [name] args to linker1491 // add -framework [name] args to linker
1491 ZigList<Buf *> darwin_frameworks;1492 ZigList<Buf *> darwin_frameworks;
src/analyze.cpp+8
...@@ -6049,10 +6049,15 @@ LinkLib *create_link_lib(Buf *name) {...@@ -6049,10 +6049,15 @@ 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");
60526053
6053 if (is_libc && g->libc_link_lib != nullptr)6054 if (is_libc && g->libc_link_lib != nullptr)
6054 return g->libc_link_lib;6055 return g->libc_link_lib;
60556056
6057 if (is_pthread && g->pthread_link_lib != nullptr) {
6058 return g->pthread_link_lib;
6059 }
6060
6056 for (size_t i = 0; i < g->link_libs_list.length; i += 1) {6061 for (size_t i = 0; i < g->link_libs_list.length; i += 1) {
6057 LinkLib *existing_lib = g->link_libs_list.at(i);6062 LinkLib *existing_lib = g->link_libs_list.at(i);
6058 if (buf_eql_buf(existing_lib->name, name)) {6063 if (buf_eql_buf(existing_lib->name, name)) {
...@@ -6066,6 +6071,9 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {...@@ -6066,6 +6071,9 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
6066 if (is_libc)6071 if (is_libc)
6067 g->libc_link_lib = link_lib;6072 g->libc_link_lib = link_lib;
60686073
6074 if (is_pthread)
6075 g->pthread_link_lib = link_lib;
6076
6069 return link_lib;6077 return link_lib;
6070}6078}
60716079
src/codegen.cpp+2
...@@ -145,6 +145,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out...@@ -145,6 +145,7 @@ 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"));
148 }149 }
149150
150 return g;151 return g;
...@@ -6373,6 +6374,7 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6373,6 +6374,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6373 buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt);6374 buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt);
6374 buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode));6375 buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode));
6375 buf_appendf(contents, "pub const link_libc = %s;\n", bool_to_str(g->libc_link_lib != nullptr));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 buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing));6378 buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing));
63776379
6378 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");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,12 +2352,11 @@ pub const Thread = struct {
2352 stack: []u8,2352 stack: []u8,
2353 pthread_handle: pthread_t,2353 pthread_handle: pthread_t,
23542354
2355 pub const use_pthreads = is_posix and builtin.link_libc;2355 const pthread_t = if (builtin.link_pthread) c.pthread_t else void;
2356 const pthread_t = if (use_pthreads) c.pthread_t else void;2356 const pid_t = if (!builtin.link_pthread) i32 else void;
2357 const pid_t = if (!use_pthreads) i32 else void;
23582357
2359 pub fn wait(self: &const Thread) void {2358 pub fn wait(self: &const Thread) void {
2360 if (use_pthreads) {2359 if (builtin.link_pthread) {
2361 const err = c.pthread_join(self.pthread_handle, null);2360 const err = c.pthread_join(self.pthread_handle, null);
2362 switch (err) {2361 switch (err) {
2363 0 => {},2362 0 => {},
...@@ -2407,6 +2406,9 @@ pub const SpawnThreadError = error {...@@ -2407,6 +2406,9 @@ pub const SpawnThreadError = error {
2407 /// be copied.2406 /// be copied.
2408 SystemResources,2407 SystemResources,
24092408
2409 /// pthreads requires at least 16384 bytes of stack space
2410 StackTooSmall,
2411
2410 Unexpected,2412 Unexpected,
2411};2413};
24122414
...@@ -2473,7 +2475,7 @@ pub fn spawnThread(stack: []u8, context: var, comptime startFn: var) SpawnThread...@@ -2473,7 +2475,7 @@ pub fn spawnThread(stack: []u8, context: var, comptime startFn: var) SpawnThread
2473 if (builtin.os == builtin.Os.windows) {2475 if (builtin.os == builtin.Os.windows) {
2474 // use windows API directly2476 // use windows API directly
2475 @compileError("TODO support spawnThread for Windows");2477 @compileError("TODO support spawnThread for Windows");
2476 } else if (Thread.use_pthreads) {2478 } else if (builtin.link_pthread) {
2477 // use pthreads2479 // use pthreads
2478 var attr: c.pthread_attr_t = undefined;2480 var attr: c.pthread_attr_t = undefined;
2479 if (c.pthread_attr_init(&attr) != 0) return SpawnThreadError.SystemResources;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,7 +2483,7 @@ pub fn spawnThread(stack: []u8, context: var, comptime startFn: var) SpawnThread
24812483
2482 const stack_size = stack_end - @ptrToInt(stack.ptr);2484 const stack_size = stack_end - @ptrToInt(stack.ptr);
2483 if (c.pthread_attr_setstack(&attr, @ptrCast(&c_void, stack.ptr), stack_size) != 0) {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 }
24862488
2487 const err = c.pthread_create(&thread_ptr.pthread_handle, &attr, MainFuncs.posixThreadMain, @intToPtr(&c_void, arg));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,8 +57,8 @@ test "spawn threads" {
57 const thread1 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, {}, start1);57 const thread1 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, {}, start1);
58 const thread4 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, &shared_ctx, start2);58 const thread4 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, &shared_ctx, start2);
5959
60 var stack1: [1024]u8 = undefined;60 var stack1: [20 * 1024]u8 = undefined;
61 var stack2: [1024]u8 = undefined;61 var stack2: [20 * 1024]u8 = undefined;
6262
63 const thread2 = try std.os.spawnThread(stack1[0..], &shared_ctx, start2);63 const thread2 = try std.os.spawnThread(stack1[0..], &shared_ctx, start2);
64 const thread3 = try std.os.spawnThread(stack2[0..], &shared_ctx, start2);64 const thread3 = try std.os.spawnThread(stack2[0..], &shared_ctx, start2);