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 {
14861486
14871487 ZigList<LinkLib *> link_libs_list;
14881488 LinkLib *libc_link_lib;
1489 LinkLib *pthread_link_lib;
14891490
14901491 // add -framework [name] args to linker
14911492 ZigList<Buf *> darwin_frameworks;
src/analyze.cpp+8
......@@ -6049,10 +6049,15 @@ LinkLib *create_link_lib(Buf *name) {
60496049
60506050LinkLib *add_link_lib(CodeGen *g, Buf *name) {
60516051 bool is_libc = buf_eql_str(name, "c");
6052 bool is_pthread = buf_eql_str(name, "pthread");
60526053
60536054 if (is_libc && g->libc_link_lib != nullptr)
60546055 return g->libc_link_lib;
60556056
6057 if (is_pthread && g->pthread_link_lib != nullptr) {
6058 return g->pthread_link_lib;
6059 }
6060
60566061 for (size_t i = 0; i < g->link_libs_list.length; i += 1) {
60576062 LinkLib *existing_lib = g->link_libs_list.at(i);
60586063 if (buf_eql_buf(existing_lib->name, name)) {
......@@ -6066,6 +6071,9 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
60666071 if (is_libc)
60676072 g->libc_link_lib = link_lib;
60686073
6074 if (is_pthread)
6075 g->pthread_link_lib = link_lib;
6076
60696077 return link_lib;
60706078}
60716079
src/codegen.cpp+2
......@@ -145,6 +145,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
145145 {
146146 g->libc_link_lib = create_link_lib(buf_create_from_str("c"));
147147 g->link_libs_list.append(g->libc_link_lib);
148 g->pthread_link_lib = create_link_lib(buf_create_from_str("pthread"));
148149 }
149150
150151 return g;
......@@ -6373,6 +6374,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
63736374 buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt);
63746375 buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode));
63756376 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));
63766378 buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing));
63776379
63786380 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 {
23522352 stack: []u8,
23532353 pthread_handle: pthread_t,
23542354
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;
23582357
23592358 pub fn wait(self: &const Thread) void {
2360 if (use_pthreads) {
2359 if (builtin.link_pthread) {
23612360 const err = c.pthread_join(self.pthread_handle, null);
23622361 switch (err) {
23632362 0 => {},
......@@ -2407,6 +2406,9 @@ pub const SpawnThreadError = error {
24072406 /// be copied.
24082407 SystemResources,
24092408
2409 /// pthreads requires at least 16384 bytes of stack space
2410 StackTooSmall,
2411
24102412 Unexpected,
24112413};
24122414
......@@ -2473,7 +2475,7 @@ pub fn spawnThread(stack: []u8, context: var, comptime startFn: var) SpawnThread
24732475 if (builtin.os == builtin.Os.windows) {
24742476 // use windows API directly
24752477 @compileError("TODO support spawnThread for Windows");
2476 } else if (Thread.use_pthreads) {
2478 } else if (builtin.link_pthread) {
24772479 // use pthreads
24782480 var attr: c.pthread_attr_t = undefined;
24792481 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
24812483
24822484 const stack_size = stack_end - @ptrToInt(stack.ptr);
24832485 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
24852487 }
24862488
24872489 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" {
5757 const thread1 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, {}, start1);
5858 const thread4 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, &shared_ctx, start2);
5959
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;
6262
6363 const thread2 = try std.os.spawnThread(stack1[0..], &shared_ctx, start2);
6464 const thread3 = try std.os.spawnThread(stack2[0..], &shared_ctx, start2);