Grok 12.0.1
ThreadPool.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2024 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
34{
35 public:
36 // Deleted copy constructor and assignment operator
37 ExecSingleton(const ExecSingleton&) = delete;
39
40 // Get instance of the Singleton with a specific number of threads
41 static tf::Executor& instance(uint32_t numthreads)
42 {
43 std::lock_guard<std::mutex> lock(mutex_);
44 instance_ = std::make_unique<tf::Executor>(numthreads ? numthreads
45 : std::thread::hardware_concurrency());
46
47 return *instance_;
48 }
49
50 // Get current instance of the Singleton (create with hardware concurrency if null)
51 static tf::Executor& get(void)
52 {
53 std::lock_guard<std::mutex> lock(mutex_);
54 if(!instance_)
55 return instance(0);
56 return *instance_;
57 }
58
59 // Destroy the Singleton instance
60 static void destroy()
61 {
62 std::lock_guard<std::mutex> lock(mutex_);
63 instance_.reset();
64 }
65
66 static uint32_t threadId(void)
67 {
68 return get().num_workers() > 1 ? (uint32_t)get().this_worker_id() : 0;
69 }
70
71 private:
72 ExecSingleton() = default;
73
74 static std::unique_ptr<tf::Executor> instance_;
75 static std::mutex mutex_;
76};
Definition ThreadPool.hpp:34
static void destroy()
Definition ThreadPool.hpp:60
static std::unique_ptr< tf::Executor > instance_
Definition ThreadPool.hpp:74
ExecSingleton(const ExecSingleton &)=delete
static uint32_t threadId(void)
Definition ThreadPool.hpp:66
static tf::Executor & instance(uint32_t numthreads)
Definition ThreadPool.hpp:41
static tf::Executor & get(void)
Definition ThreadPool.hpp:51
ExecSingleton & operator=(const ExecSingleton &)=delete
ExecSingleton()=default
static std::mutex mutex_
Definition ThreadPool.hpp:75