Commit 16905d96f7

Vincent Rischmann <vincent@rischmann.fr>
2021-02-01 21:16:39
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 parent 11f6916
Changed files (1)
lib
std
lib/std/Thread/Condition.zig
@@ -8,7 +8,7 @@
 //! to wake up. Spurious wakeups are possible.
 //! This API supports static initialization and does not require deinitialization.
 
-impl: Impl,
+impl: Impl = .{},
 
 const std = @import("../std.zig");
 const Condition = @This();
@@ -17,6 +17,18 @@ const linux = std.os.linux;
 const Mutex = std.Thread.Mutex;
 const assert = std.debug.assert;
 
+pub fn wait(cond: *Condition, mutex: *Mutex) void {
+    cond.impl.wait(mutex);
+}
+
+pub fn signal(cond: *Condition) void {
+    cond.impl.signal();
+}
+
+pub fn broadcast(cond: *Condition) void {
+    cond.impl.broadcast();
+}
+
 const Impl = if (std.builtin.single_threaded)
     SingleThreadedCondition
 else if (std.Target.current.os.tag == .windows)
@@ -62,7 +74,7 @@ pub const PthreadCondition = struct {
     cond: std.c.pthread_cond_t = .{},
 
     pub fn wait(cond: *PthreadCondition, mutex: *Mutex) void {
-        const rc = std.c.pthread_cond_wait(&cond.cond, &mutex.mutex);
+        const rc = std.c.pthread_cond_wait(&cond.cond, &mutex.impl.pthread_mutex);
         assert(rc == 0);
     }