aboutsummaryrefslogtreecommitdiff
path: root/lib/std/Thread/Semaphore.zig
blob: b5bde88d736a907267af9ee7921561109fba711d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! A semaphore is an unsigned integer that blocks the kernel thread if
//! the number would become negative.
//! This API supports static initialization and does not require deinitialization.

mutex: Mutex = .{},
cond: Condition = .{},
/// It is OK to initialize this field to any value.
permits: usize = 0,

const Semaphore = @This();
const std = @import("../std.zig");
const Mutex = std.Thread.Mutex;
const Condition = std.Thread.Condition;

pub fn wait(sem: *Semaphore) void {
    const held = sem.mutex.acquire();
    defer held.release();

    while (sem.permits == 0)
        sem.cond.wait(&sem.mutex);

    sem.permits -= 1;
    if (sem.permits > 0)
        sem.cond.signal();
}

pub fn post(sem: *Semaphore) void {
    const held = sem.mutex.acquire();
    defer held.release();

    sem.permits += 1;
    sem.cond.signal();
}