Grok 15.1.0
ThreadPool.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2025 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#ifndef _MSC_VER
21#pragma GCC diagnostic push
22#pragma GCC diagnostic ignored "-Wsign-conversion"
23#pragma GCC diagnostic ignored "-Wconversion"
24#ifdef __clang__
25#pragma GCC diagnostic ignored "-Wimplicit-int-float-conversion"
26#endif
27#endif
28#include <taskflow/taskflow.hpp>
29#ifndef _MSC_VER
30#pragma GCC diagnostic pop
31#endif
32
38{
39public:
45 static void create(uint32_t numThreads)
46 {
47 std::lock_guard<std::mutex> lock(mutex_);
48 numThreads = numThreads ? numThreads : std::thread::hardware_concurrency() + 1;
49 if(numThreads_ == numThreads)
50 return;
51 numThreads_ = numThreads;
52 if(numThreads_ == 1)
53 {
54 instance_.reset();
55 return;
56 }
57 instance_ = std::make_unique<tf::Executor>(numThreads_ - 1);
58 }
59
64 static tf::Executor& get(void)
65 {
66 std::lock_guard<std::mutex> lock(mutex_);
67 if(!instance_)
68 create(0);
69 return *instance_;
70 }
71
77 static size_t num_threads()
78 {
79 return numThreads_;
80 }
81
85 static void destroy()
86 {
87 std::lock_guard<std::mutex> lock(mutex_);
88 instance_.reset();
89 }
90
95 static uint32_t workerId(void)
96 {
97 return numThreads_ > 1 ? (uint32_t)get().this_worker_id() : 0;
98 }
99
100private:
101 // Deleted copy constructor and assignment operator
102 ExecSingleton(const ExecSingleton&) = delete;
107 ExecSingleton() = default;
108
112 static std::unique_ptr<tf::Executor> instance_;
113
117 static std::mutex mutex_;
118
123 static size_t numThreads_;
124};
static void destroy()
Destroys ExecSingleton.
Definition ThreadPool.hpp:85
static void create(uint32_t numThreads)
Creates singleton instance.
Definition ThreadPool.hpp:45
static std::unique_ptr< tf::Executor > instance_
Taskflow Executor instance.
Definition ThreadPool.hpp:112
ExecSingleton(const ExecSingleton &)=delete
static size_t num_threads()
Gets total number of threads.
Definition ThreadPool.hpp:77
static tf::Executor & get(void)
Gets current instance of the Singleton (creates with full hardware concurrency if null)
Definition ThreadPool.hpp:64
ExecSingleton & operator=(const ExecSingleton &)=delete
ExecSingleton()=default
Constructs an ExecSingleton.
static std::mutex mutex_
std::mutex to control access to instance_
Definition ThreadPool.hpp:117
static uint32_t workerId(void)
Gets worker id for current worker.
Definition ThreadPool.hpp:95
static size_t numThreads_
total number of threads
Definition ThreadPool.hpp:123