scc  2022.4.0
SystemC components library
sc_thread_pool.cpp
1 /*******************************************************************************
2  * Copyright 2021 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #include <functional>
18 #include <ostream>
19 
20 #ifndef SC_INCLUDE_DYNAMIC_PROCESSES
21 #define SC_INCLUDE_DYNAMIC_PROCESSES
22 #endif
23 #include "sc_thread_pool.h"
24 
25 namespace scc {
26 
27 sc_thread_pool::sc_thread_pool()
28 : sc_core::sc_object(sc_core::sc_gen_unique_name("pool")) {}
29 
30 sc_thread_pool::~sc_thread_pool() = default;
31 
32 void sc_thread_pool::execute(std::function<void(void)> fct) {
33  sc_core::sc_spawn_options opts;
34  opts.set_stack_size(0x10000);
35  if((thread_avail == 0 || dispatch_queue.has_next()) && thread_active < max_concurrent_threads.get_value())
36  sc_core::sc_spawn(
37  [this]() {
38  thread_active++;
39  while(true) {
40  thread_avail++;
41  auto fct = dispatch_queue.get();
42  sc_assert(thread_avail > 0);
43  thread_avail--;
44  fct();
45  }
46  },
47  nullptr, &opts);
48  dispatch_queue.notify(std::move(fct));
49 }
50 
51 } /* namespace scc */
SCC SystemC utilities.