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
This commit is contained in:
Vincent Rischmann 2021-02-01 21:16:39 +01:00 committed by GitHub
parent 11f6916f9b
commit 16905d96f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}