SystemC Library API Reference Manual
Reference documentation for the Simics SystemC Library.
 
Loading...
Searching...
No Matches
thread_pool.h
Go to the documentation of this file.
1// -*- mode: C++; c-file-style: "virtutech-c++" -*-
2
3/*
4 © 2019 Intel Corporation
5
6 This software and the related documents are Intel copyrighted materials, and
7 your use of them is governed by the express license under which they were
8 provided to you ("License"). Unless the License provides otherwise, you may
9 not use, modify, copy, publish, distribute, disclose or transmit this software
10 or the related documents without Intel's prior written permission.
11
12 This software and the related documents are provided as is, with no express or
13 implied warranties, other than those that are expressly stated in the License.
14*/
15
16#ifndef SIMICS_SYSTEMC_THREAD_POOL_H
17#define SIMICS_SYSTEMC_THREAD_POOL_H
18
21
22#include <systemc>
23
24#include <queue>
25#include <unordered_map>
26
27namespace simics {
28namespace systemc {
29
30class ThreadInterface;
31
34 public:
36 virtual void run(ThreadInterface *call) = 0;
37 virtual void block(ThreadInterface *call) = 0;
38 virtual void finish(ThreadInterface *call) = 0;
39 virtual void exception(ThreadInterface *call) = 0;
41};
42
45 public:
51 };
52 virtual ~ThreadInterface() {}
54};
55
57class Thread : public ThreadInterface {
58 public:
60 void spawn();
62
63 protected:
64 virtual const char *thread_name();
65
66 private:
67 void thread_function();
68
70 sc_core::sc_process_handle process_handle_;
71 sc_core::sc_thread_process *thread_process_;
72 sc_core::sc_event wait_for_work_;
73 bool finished_;
74};
75
77template<class TThread = Thread>
79 public:
80 // ThreadInterface
82 if (idle_.empty()) {
84 allocateThread();
85 }
86
87 ThreadInterface *c = idle_.front();
88 idle_.pop();
89 active_[c] = cb;
90 return c->call(this);
91 }
92
93 private:
94 void allocateThread() {
95 TThread *t = new TThread;
96 t->spawn();
97 idle_.push(t);
98 }
99 // ThreadCallbackInterface
100 virtual void run(ThreadInterface *call) {
101 active_[call]->run(call);
102 }
103 virtual void block(ThreadInterface *call) {
104 active_[call]->block(call);
105 }
106 virtual void finish(ThreadInterface *call) {
107 auto it = active_.find(call);
108 ASSERT(it != active_.end());
109 it->second->finish(call);
110 // A micro benchmark (10M MMIO read/write) shows the erase
111 // from the map affects performance (>7%)
112 it->second = nullptr;
113 idle_.push(call);
114 }
115 virtual void exception(ThreadInterface *call) {
116 if (active_[call])
117 active_[call]->exception(call);
118 }
119 virtual iface::SimulationInterface *simulation(ThreadInterface *call) {
120 return active_[call]->simulation(call);
121 }
122
123 std::queue<ThreadInterface *> idle_;
124 std::unordered_map<ThreadInterface *, ThreadCallbackInterface *> active_;
125};
126
127} // namespace systemc
128} // namespace simics
129
130#endif
Definition: kernel_state_modifier.h:30
Definition: thread_pool.h:33
virtual iface::SimulationInterface * simulation(ThreadInterface *call)=0
virtual void exception(ThreadInterface *call)=0
virtual ~ThreadCallbackInterface()
Definition: thread_pool.h:35
virtual void block(ThreadInterface *call)=0
virtual void run(ThreadInterface *call)=0
virtual void finish(ThreadInterface *call)=0
Definition: thread_pool.h:44
virtual CallReturn call(ThreadCallbackInterface *cb)=0
virtual ~ThreadInterface()
Definition: thread_pool.h:52
CallReturn
Definition: thread_pool.h:46
@ CALL_RETURN_TERMINATED
Definition: thread_pool.h:49
@ CALL_RETURN_WAITING
Definition: thread_pool.h:48
@ CALL_RETURN_ERROR_IN_USE
Definition: thread_pool.h:50
@ CALL_RETURN_FINISHED
Definition: thread_pool.h:47
Definition: thread_pool.h:78
virtual CallReturn call(ThreadCallbackInterface *cb)
Definition: thread_pool.h:81
Definition: thread_pool.h:57
virtual CallReturn call(ThreadCallbackInterface *cb)
virtual const char * thread_name()
Interface to the SystemC simulation.
Definition: simulation_interface.h:27
Definition: pci_bus_interface.h:24