Grok 20.3.2
TFSingleton.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 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#include <stdexcept>
21#include <unordered_map>
22#include <mutex>
23#include <vector>
24#include <cassert>
25
26#include "grk_taskflow.h"
27
33{
34public:
40 static void create(size_t numThreads)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 numThreads = numThreads ? numThreads : std::thread::hardware_concurrency();
44 if(numThreads_ == numThreads)
45 return;
46 numThreads_ = numThreads;
47 instance_ = std::make_unique<tf::Executor>(numThreads_);
48 }
49
54 static tf::Executor& get(void)
55 {
56 std::lock_guard<std::mutex> lock(mutex_);
57 if(!instance_)
58 {
59 // Initialize with default thread count if instance is null
60 numThreads_ = std::thread::hardware_concurrency();
61 ;
62 instance_ = std::make_unique<tf::Executor>(numThreads_);
63 }
64 assert(instance_); // Should always be valid now
65 return *instance_;
66 }
67
73 static size_t num_threads()
74 {
75 std::lock_guard<std::mutex> lock(mutex_);
76 return numThreads_;
77 }
78
82 static void destroy()
83 {
84 std::lock_guard<std::mutex> lock(mutex_);
85 instance_.reset();
86 }
87
94 static uint32_t workerId(void)
95 {
96 auto id = get().this_worker_id();
97 return (id >= 0) ? (uint32_t)id : 0u;
98 }
99
100private:
101 // Deleted copy constructor and assignment operator
102 TFSingleton(const TFSingleton&) = delete;
107 TFSingleton() = default;
108
112 static std::unique_ptr<tf::Executor> instance_;
113
117 static std::mutex mutex_;
118
122 static size_t numThreads_;
123};
static size_t num_threads()
Gets total number of threads (including driver thread).
Definition TFSingleton.h:73
static std::unique_ptr< tf::Executor > instance_
Taskflow Executor instance.
Definition TFSingleton.h:112
TFSingleton(const TFSingleton &)=delete
static std::mutex mutex_
std::mutex to control access to instance_
Definition TFSingleton.h:117
static void create(size_t numThreads)
Creates singleton instance.
Definition TFSingleton.h:40
static uint32_t workerId(void)
Gets worker id for current worker.
Definition TFSingleton.h:94
TFSingleton()=default
Constructs an TFSingleton.
TFSingleton & operator=(const TFSingleton &)=delete
static void destroy()
Destroys TFSingleton.
Definition TFSingleton.h:82
static size_t numThreads_
total number of threads
Definition TFSingleton.h:122
static tf::Executor & get(void)
Gets current instance of the Singleton (creates with full hardware concurrency if null).
Definition TFSingleton.h:54