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