authorgravatar for vincent@rischmann.frVincent Rischmann <vincent@rischmann.fr> 2021-02-01 21:16:39+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-02-01 15:16:39-05:00
log16905d96f70045fd9d630219c85a2eb508db38b4
treeb06e307af5953f5f2d58836f2813c5ec3c323bfc
parent11f6916f9bd9fbaf81a4bd60fd375eff8db78374
signature Signed by PGP key 4AEE18F83AFDEB23

Fixes for std.Thread.Condition (#7883)

* thread/condition: fix PthreadCondition compilation * thread/condition: add wait, signal and broadcast This is like std.Thread.Mutex which forwards calls to `impl`; avoids having to call `cond.impl` every time. * thread/condition: initialize the implementation

1 files changed, 14 insertions(+), 2 deletions(-)

lib/std/Thread/Condition.zig+14-2
...@@ -8,7 +8,7 @@...@@ -8,7 +8,7 @@
8//! to wake up. Spurious wakeups are possible.8//! to wake up. Spurious wakeups are possible.
9//! This API supports static initialization and does not require deinitialization.9//! This API supports static initialization and does not require deinitialization.
1010
11impl: Impl,11impl: Impl = .{},
1212
13const std = @import("../std.zig");13const std = @import("../std.zig");
14const Condition = @This();14const Condition = @This();
...@@ -17,6 +17,18 @@ const linux = std.os.linux;...@@ -17,6 +17,18 @@ const linux = std.os.linux;
17const Mutex = std.Thread.Mutex;17const Mutex = std.Thread.Mutex;
18const assert = std.debug.assert;18const assert = std.debug.assert;
1919
20pub fn wait(cond: *Condition, mutex: *Mutex) void {
21 cond.impl.wait(mutex);
22}
23
24pub fn signal(cond: *Condition) void {
25 cond.impl.signal();
26}
27
28pub fn broadcast(cond: *Condition) void {
29 cond.impl.broadcast();
30}
31
20const Impl = if (std.builtin.single_threaded)32const Impl = if (std.builtin.single_threaded)
21 SingleThreadedCondition33 SingleThreadedCondition
22else if (std.Target.current.os.tag == .windows)34else if (std.Target.current.os.tag == .windows)
...@@ -62,7 +74,7 @@ pub const PthreadCondition = struct {...@@ -62,7 +74,7 @@ pub const PthreadCondition = struct {
62 cond: std.c.pthread_cond_t = .{},74 cond: std.c.pthread_cond_t = .{},
6375
64 pub fn wait(cond: *PthreadCondition, mutex: *Mutex) void {76 pub fn wait(cond: *PthreadCondition, mutex: *Mutex) void {
65 const rc = std.c.pthread_cond_wait(&cond.cond, &mutex.mutex);77 const rc = std.c.pthread_cond_wait(&cond.cond, &mutex.impl.pthread_mutex);
66 assert(rc == 0);78 assert(rc == 0);
67 }79 }
6880