<commit_before>// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2020 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// The MIT license requires this copyright notice to be included in all copies
// and substantial portions of the software.
const std = @import("std");
const ThreadPool = @This();

lock: std.Thread.Mutex = .{},
is_running: bool = true,
allocator: *std.mem.Allocator,
workers: []Worker,
run_queue: RunQueue = .{},
idle_queue: IdleQueue = .{},

const IdleQueue = std.SinglyLinkedList(std.Thread.ResetEvent);
const RunQueue = std.SinglyLinkedList(Runnable);
const Runnable = struct {
    runFn: fn (*Runnable) void,
};

const Worker = struct {
    pool: *ThreadPool,
    thread: *std.Thread,
    /// The node is for this worker only and must have an already initialized event
    /// when the thread is spawned.
    idle_node: IdleQueue.Node,

    fn run(worker: *Worker) void {
        while (true) {
            const held = worker.pool.lock.acquire();

            if (worker.pool.run_queue.popFirst()) |run_node| {
                held.release();
                (run_node.data.runFn)(&run_node.data);
                continue;
            }

            if (worker.pool.is_running) {
                worker.idle_node.data.reset();

                worker.pool.idle_queue.prepend(&worker.idle_node);
                held.release();

                worker.idle_node.data.wait();
                continue;
            }

            held.release();
            return;
        }
    }
};

pub fn init(self: *ThreadPool, allocator: *std.mem.Allocator) !void {<commit_msg>std.Thread: fixup ThreadPool.zig<commit_after>// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2020 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// The MIT license requires this copyright notice to be included in all copies
// and substantial portions of the software.
const std = @import("std");
const ThreadPool = @This();

lock: std.Thread.Mutex = .{},
is_running: bool = true,
allocator: *std.mem.Allocator,
workers: []Worker,
run_queue: RunQueue = .{},
idle_queue: IdleQueue = .{},

const IdleQueue = std.SinglyLinkedList(std.Thread.ResetEvent);
const RunQueue = std.SinglyLinkedList(Runnable);
const Runnable = struct {
    runFn: fn (*Runnable) void,
};

const Worker = struct {
    pool: *ThreadPool,
    thread: std.Thread,
    /// The node is for this worker only and must have an already initialized event
    /// when the thread is spawned.
    idle_node: IdleQueue.Node,

    fn run(worker: *Worker) void {
        while (true) {
            const held = worker.pool.lock.acquire();

            if (worker.pool.run_queue.popFirst()) |run_node| {
                held.release();
                (run_node.data.runFn)(&run_node.data);
                continue;
            }

            if (worker.pool.is_running) {
                worker.idle_node.data.reset();

                worker.pool.idle_queue.prepend(&worker.idle_node);
                held.release();

                worker.idle_node.data.wait();
                continue;
            }

            held.release();
            return;
        }
    }
};

pub fn init(self: *ThreadPool, allocator: *std.mem.Allocator) !void {
{'fraction_non_alphanumeric': 0.12575941676792224, 'fraction_numerical': 0.004860267314702308, 'mean_word_length': 2.3095477386934675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7615640', 'n_tokens_mistral': 1005, 'n_tokens_neox': 970, 'n_words': 258}