Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifying synchronization primitives #9136

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1e472d3
std.Thread: start on posix primitives
kprotty Aug 27, 2021
0c356a0
std.Thread: semaphore + cond var impl
kprotty Aug 30, 2021
5fe8c2d
std.Thread: Condition WIP
kprotty Aug 30, 2021
4703628
std.Thread: cleanup use of bitwise ops + noinline
kprotty Aug 31, 2021
afd5673
std.Thread: RwLock parking_lot impl
kprotty Sep 1, 2021
233277f
std.Thread: more citations for sync primitives
kprotty Sep 1, 2021
823ce33
std.Thread.Once + abstract out SpinWait
kprotty Sep 1, 2021
aa27f51
change *ResetEvent usage to Semaphore & Condition
kprotty Sep 1, 2021
cc3c287
replace std.once with std.Thread.Once
kprotty Sep 1, 2021
e8a4539
zig fmt
kprotty Sep 1, 2021
b39af73
std.Thread.Mutex: fix errors on windows + test case
kprotty Sep 1, 2021
bc4a3de
std.Thread.Condition: tests + fix compile for windows
kprotty Sep 1, 2021
09c4c64
std.Thread.Condition: fix compile for linux + add more tests
kprotty Sep 2, 2021
616b0eb
std.Thread: add Semaphore tests + fix the rest
kprotty Sep 3, 2021
06b9e7c
std.Thread: fix test cases & mutex usage for futex impls
kprotty Sep 3, 2021
8d8df37
std.Thread.Once: tests
kprotty Sep 5, 2021
30c2b76
Thread: fix builtin usage + misc compile errors
kprotty Oct 5, 2021
333db32
address more builtin changes
kprotty Oct 5, 2021
6ec54a3
fix pthread_once usage with libc on linux
kprotty Oct 6, 2021
40c5c47
detecting ulock_wait2 usage
kprotty Oct 13, 2021
4bf0c74
std.Thread: Remove Mutex.Held + fix tests
kprotty Nov 16, 2021
9e7a589
std.Thread.Once: generic function + add args support
kprotty Nov 16, 2021
31c0261
std.Thread: fix typos
kprotty Nov 16, 2021
f36a0d9
WaitGroup: typo
kprotty Nov 16, 2021
4b8ff40
src: remove StaticResetEvent
kprotty Nov 16, 2021
80ab25d
RwLock: fix naming + remove Held
kprotty Nov 16, 2021
dad05d0
std.Thread: add simple sync primitive impls for maintainers
kprotty Nov 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,12 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/target/wasm.zig"
"${CMAKE_SOURCE_DIR}/lib/std/target/x86.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread/AutoResetEvent.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread/Condition.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread/Once.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread/Futex.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread/Mutex.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread/ResetEvent.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread/StaticResetEvent.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread/RwLock.zig"
"${CMAKE_SOURCE_DIR}/lib/std/Thread/Semaphore.zig"
"${CMAKE_SOURCE_DIR}/lib/std/time.zig"
"${CMAKE_SOURCE_DIR}/lib/std/unicode.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig.zig"
Expand Down
60 changes: 20 additions & 40 deletions lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ const assert = std.debug.assert;
const target = builtin.target;
const Atomic = std.atomic.Atomic;

pub const AutoResetEvent = @import("Thread/AutoResetEvent.zig");
pub const Futex = @import("Thread/Futex.zig");
pub const ResetEvent = @import("Thread/ResetEvent.zig");
pub const StaticResetEvent = @import("Thread/StaticResetEvent.zig");
pub const Once = @import("Thread/Once.zig");
pub const RwLock = @import("Thread/RwLock.zig");
pub const Mutex = @import("Thread/Mutex.zig");
pub const Semaphore = @import("Thread/Semaphore.zig");
pub const Condition = @import("Thread/Condition.zig");
Expand Down Expand Up @@ -1024,20 +1023,13 @@ test "setName, getName" {
if (builtin.single_threaded) return error.SkipZigTest;

const Context = struct {
start_wait_event: ResetEvent = undefined,
test_done_event: ResetEvent = undefined,

done: std.atomic.Atomic(bool) = std.atomic.Atomic(bool).init(false),
start_wait_sema: Semaphore = .{},
test_done_sema: Semaphore = .{},
thread: Thread = undefined,

fn init(self: *@This()) !void {
try self.start_wait_event.init();
try self.test_done_event.init();
}

pub fn run(ctx: *@This()) !void {
// Wait for the main thread to have set the thread field in the context.
ctx.start_wait_event.wait();
// Wait for the main thread to post the thread field in the context.
ctx.start_wait_sema.wait(null) catch unreachable;

switch (target.os.tag) {
.windows => testThreadName(&ctx.thread) catch |err| switch (err) {
Expand All @@ -1048,21 +1040,17 @@ test "setName, getName" {
}

// Signal our test is done
ctx.test_done_event.set();

while (!ctx.done.load(.SeqCst)) {
std.time.sleep(5 * std.time.ns_per_ms);
}
ctx.test_done_sema.post(1);
}
};

var context = Context{};
try context.init();

var thread = try spawn(.{}, Context.run, .{&context});
defer thread.join();

context.thread = thread;
context.start_wait_event.set();
context.test_done_event.wait();
context.start_wait_sema.post(1);
context.test_done_sema.wait(null) catch unreachable;

switch (target.os.tag) {
.macos, .ios, .watchos, .tvos => {
Expand All @@ -1084,36 +1072,30 @@ test "setName, getName" {
try testThreadName(&thread);
},
}

context.done.store(true, .SeqCst);
thread.join();
}

test "std.Thread" {
// Doesn't use testing.refAllDecls() since that would pull in the compileError spinLoopHint.
_ = AutoResetEvent;
_ = Futex;
_ = ResetEvent;
_ = StaticResetEvent;
_ = Once;
_ = RwLock;
_ = Mutex;
_ = Semaphore;
_ = Condition;
}

fn testIncrementNotify(value: *usize, event: *ResetEvent) void {
fn testIncrementNotify(value: *usize, semaphore: *Semaphore) void {
value.* += 1;
event.set();
semaphore.post(1);
}

test "Thread.join" {
if (builtin.single_threaded) return error.SkipZigTest;

var value: usize = 0;
var event: ResetEvent = undefined;
try event.init();
defer event.deinit();
var semaphore = Semaphore{};

const thread = try Thread.spawn(.{}, testIncrementNotify, .{ &value, &event });
const thread = try Thread.spawn(.{}, testIncrementNotify, .{ &value, &semaphore });
thread.join();

try std.testing.expectEqual(value, 1);
Expand All @@ -1123,13 +1105,11 @@ test "Thread.detach" {
if (builtin.single_threaded) return error.SkipZigTest;

var value: usize = 0;
var event: ResetEvent = undefined;
try event.init();
defer event.deinit();
var semaphore = Semaphore{};

const thread = try Thread.spawn(.{}, testIncrementNotify, .{ &value, &event });
const thread = try Thread.spawn(.{}, testIncrementNotify, .{ &value, &semaphore });
thread.detach();

event.wait();
semaphore.wait(null) catch unreachable;
try std.testing.expectEqual(value, 1);
}
222 changes: 0 additions & 222 deletions lib/std/Thread/AutoResetEvent.zig

This file was deleted.