From 16905d96f70045fd9d630219c85a2eb508db38b4 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Mon, 1 Feb 2021 21:16:39 +0100 Subject: [PATCH] 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 --- lib/std/Thread/Condition.zig | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/std/Thread/Condition.zig b/lib/std/Thread/Condition.zig index 9a8fafbb7c..a14b57f6b4 100644 --- a/lib/std/Thread/Condition.zig +++ b/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); }