Commit 76c729c967
2022-11-26 23:58:36
1 parent
82b8689Changed files (1)
lib
std
Thread
lib/std/Thread/Condition.zig
@@ -604,82 +604,3 @@ test "Condition - broadcasting - wake all threads" {
}
}
}
-
-test "Condition - signal wakes one" {
- // This test requires spawning threads
- if (builtin.single_threaded) {
- return error.SkipZigTest;
- }
-
- if (builtin.os.tag == .windows) {
- // https://github.com/ziglang/zig/issues/13660
- return error.SkipZigTest;
- }
-
- var num_runs: usize = 1;
- const num_threads = 3;
- const timeoutDelay = 10 * std.time.ns_per_ms;
-
- while (num_runs > 0) : (num_runs -= 1) {
-
- // Start multiple runner threads, wait for them to start and send the signal
- // then. Expect that one thread wake up and all other times out.
- //
- // Test depends on delay in timedWait! If too small all threads can timeout
- // before any one gets wake up.
-
- const Runner = struct {
- mutex: Mutex = .{},
- cond: Condition = .{},
- completed: Condition = .{},
- count: usize = 0,
- threads: [num_threads]std.Thread = undefined,
- wakeups: usize = 0,
- timeouts: usize = 0,
-
- fn run(self: *@This()) void {
- self.mutex.lock();
- defer self.mutex.unlock();
-
- // The last started thread tells the main test thread it's completed.
- self.count += 1;
- if (self.count == num_threads) {
- self.completed.signal();
- }
-
- self.cond.timedWait(&self.mutex, timeoutDelay) catch {
- self.timeouts += 1;
- return;
- };
- self.wakeups += 1;
- }
- };
-
- // Start threads
- var runner = Runner{};
- for (runner.threads) |*t| {
- t.* = try std.Thread.spawn(.{}, Runner.run, .{&runner});
- }
-
- {
- runner.mutex.lock();
- defer runner.mutex.unlock();
-
- // Wait for all the threads to spawn.
- // timedWait() to detect any potential deadlocks.
- while (runner.count != num_threads) {
- try runner.completed.timedWait(&runner.mutex, 1 * std.time.ns_per_s);
- }
- // Signal one thread, the others should get timeout.
- runner.cond.signal();
- }
-
- for (runner.threads) |t| {
- t.join();
- }
-
- // Expect that only one got singal
- try std.testing.expectEqual(runner.wakeups, 1);
- try std.testing.expectEqual(runner.timeouts, num_threads - 1);
- }
-}